Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

pkg/terraform: improvements #1027

Merged
merged 7 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions pkg/terraform/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,28 +516,27 @@ func (ex *Executor) logPath(id int) string {
func (ex *Executor) checkVersion() error {
vOutput, err := ex.executeSync("--version")
if err != nil {
return fmt.Errorf("checking Terraform version: %w", err)
return fmt.Errorf("executing 'terraform --version': %w", err)
}

format := "Terraform v%s\n"
var vStr string
n, err := fmt.Sscanf(string(vOutput), "Terraform v%s\n", &vStr)
n, err := fmt.Sscanf(string(vOutput), format, &vStr)
if err != nil {
return fmt.Errorf("checking Terraform version: %w", err)
return fmt.Errorf("output %q does not match format %q: %w", string(vOutput), format, err)
}

if n != 1 {
return fmt.Errorf("error parsing Terraform version")
return fmt.Errorf("version not found in 'terraform --version' output")
}

v, err := version.NewVersion(vStr)
if err != nil {
return fmt.Errorf("checking Terraform version: %w", err)
return fmt.Errorf("parsing Terraform version %q: %w", vStr, err)
}

constraints, err := version.NewConstraint(requiredVersion)
if err != nil {
return fmt.Errorf("checking Terraform version: %w", err)
}
// requiredVersion is const, so we test it in unit tests.
constraints, _ := version.NewConstraint(requiredVersion)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can log it in debug mode? So we don't lose track of it altogether.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? It is covered by unit tests, running exactly the same code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's best to panic in such case, if this error should be found during development itself.


if !constraints.Check(v) {
return fmt.Errorf("version '%s' of Terraform not supported. Needed %s", v, constraints)
Expand Down
75 changes: 75 additions & 0 deletions pkg/terraform/executor_e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2020 The Lokomotive Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//+build e2e
invidian marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should e2e tests sit in test dir? Mixing unit tests and e2e tests?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, those are package-scoped tests, so I think it's fine if they sit in this directory. They are marked as e2e, as they require external dependency to pass (terraform binary).


package terraform_test

import (
"io/ioutil"
"os"
"strings"
"testing"

"github.com/kinvolk/lokomotive/pkg/terraform"
)

func executor(t *testing.T) *terraform.Executor {
tmpDir, err := ioutil.TempDir("", "lokoctl-tests-")
if err != nil {
t.Fatalf("Creating tmp dir should succeed, got: %v", err)
}

t.Cleanup(func() {
if err := os.RemoveAll(tmpDir); err != nil {
t.Logf("Removing directory %q: %v", tmpDir, err)
}
})

conf := terraform.Config{
Verbose: false,
WorkingDir: tmpDir,
}

ex, err := terraform.NewExecutor(conf)
if err != nil {
t.Fatalf("Creating new executor should succeed, got: %v", err)
}

return ex
}

func TestExecuteCheckErrors(t *testing.T) {
ex := executor(t)

if err := ex.Apply(); err == nil {
t.Fatalf("Applying on empty directory should fail")
}
}

func TestOutputIncludeKeyInError(t *testing.T) {
ex := executor(t)

k := "foo"
o := ""

err := ex.Output(k, &o)
if err == nil {
t.Fatalf("Output should fail on non existing installation")
}

if !strings.Contains(err.Error(), k) {
t.Fatalf("Error message should contain key, got: %v", err)
}
}
27 changes: 27 additions & 0 deletions pkg/terraform/executor_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2020 The Lokomotive Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package terraform
invidian marked this conversation as resolved.
Show resolved Hide resolved

import (
"testing"

"github.com/hashicorp/go-version"
)

func TestRequiredVersion(t *testing.T) {
if _, err := version.NewConstraint(requiredVersion); err != nil {
t.Fatalf("requiredVersion const must be valid version constraint, got: %v", err)
}
}
113 changes: 79 additions & 34 deletions pkg/terraform/executor_test.go
Original file line number Diff line number Diff line change
@@ -1,55 +1,100 @@
//+build e2e
// Copyright 2020 The Lokomotive Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package terraform
surajssd marked this conversation as resolved.
Show resolved Hide resolved
package terraform_test
invidian marked this conversation as resolved.
Show resolved Hide resolved

import (
"fmt"
"io/ioutil"
"os"
"strings"
"path/filepath"
"testing"

"github.com/kinvolk/lokomotive/pkg/terraform"
)

func executor(t *testing.T) *Executor {
tmpDir, err := ioutil.TempDir("", "lokoctl-tests-")
if err != nil {
t.Fatalf("Creating tmp dir should succeed, got: %v", err)
//nolint:funlen
func TestVersionConstraint(t *testing.T) {
cases := map[string]struct {
output string
expectError bool
}{
"valid": {
output: "Terraform v0.12.10",
},
"outdated": {
output: "Terraform v0.11.0",
expectError: true,
},
"unsupported": {
output: "Terraform v0.13.5",
expectError: true,
},
"with extra test": {
output: `Terraform v0.12.11

Your version of Terraform is out of date! The latest version
knrt10 marked this conversation as resolved.
Show resolved Hide resolved
is 0.13.3. You can update by downloading from https://www.terraform.io/downloads.html`,
},
}

defer os.RemoveAll(tmpDir)
for n, c := range cases {
c := c

conf := Config{
Verbose: false,
WorkingDir: tmpDir,
}
t.Run(n, func(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "lokoctl-tests-")
if err != nil {
t.Fatalf("Creating tmp dir should succeed, got: %v", err)
}

ex, err := NewExecutor(conf)
if err != nil {
t.Fatalf("Creating new executor should succeed, got: %v", err)
}
t.Cleanup(func() {
if err := os.RemoveAll(tmpDir); err != nil {
t.Logf("Removing directory %q: %v", tmpDir, err)
}
})

return ex
}
v := []byte(fmt.Sprintf(`#!/bin/sh
cat <<EOF
%s
EOF
`, c.output))

func TestExecuteCheckErrors(t *testing.T) {
ex := executor(t)
path := filepath.Join(tmpDir, "terraform")

if err := ex.Apply(); err == nil {
t.Fatalf("Applying on empty directory should fail")
}
}
// #nosec G306 // File must be executable to pretend it's a Terraform binary.
if err := ioutil.WriteFile(path, v, 0o700); err != nil {
t.Fatalf("Writing file %q: %v", path, err)
}

func TestOutputIncludeKeyInError(t *testing.T) {
ex := executor(t)
if err := os.Setenv("PATH", fmt.Sprintf("%s:%s", tmpDir, os.Getenv("PATH"))); err != nil {
t.Fatalf("Overriding PATH variable for testing: %v", err)
}

k := "foo"
o := ""
conf := terraform.Config{
Verbose: false,
WorkingDir: tmpDir,
}

err := ex.Output(k, &o)
if err == nil {
t.Fatalf("Output should fail on non existing installation")
}
_, err = terraform.NewExecutor(conf)

if !c.expectError && err != nil {
t.Fatalf("Creating new executor should succeed, got: %v", err)
}

if !strings.Contains(err.Error(), k) {
t.Fatalf("Error message should contain key, got: %v", err)
if c.expectError && err == nil {
t.Fatalf("Creating new executor should fail")
}
})
}
}