Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several improvements on acceptance tests #67

Merged
merged 1 commit into from
Jan 6, 2021
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
2 changes: 1 addition & 1 deletion pkg/resource/aws/aws_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestAcc_AwsInstance_WithBlockDevices(t *testing.T) {
var mutatedInstanceId string
acceptance.Run(t, acceptance.AccTestCase{
Path: "./testdata/acc/aws_instance",
Args: []string{"scan"}, // TODO add filter to limit scan scope to aws_instances
Args: []string{"scan", "--filter", "Type=='aws_instance'"},
Checks: []acceptance.AccCheck{
{
Check: func(result *acceptance.ScanResult, stdout string, err error) {
Expand Down
37 changes: 37 additions & 0 deletions pkg/resource/aws/testdata/acc/aws_instance/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 53 additions & 24 deletions test/acceptance/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"path"
"testing"

"github.com/cloudskiff/driftctl/pkg/analyser"
Expand Down Expand Up @@ -85,24 +86,27 @@ func (c *AccTestCase) getResult(t *testing.T) *ScanResult {
return NewScanResult(t, analysis)
}

func terraformApply() error {
// Disable terraform version checks
// @link https://www.terraform.io/docs/commands/index.html#upgrade-and-security-bulletin-checks
checkpoint := os.Getenv("CHECKPOINT_DISABLE")
os.Setenv("CHECKPOINT_DISABLE", "true")
defer os.Setenv("CHECKPOINT_DISABLE", checkpoint)

logrus.Debug("Running terraform init ...")
cmd := exec.Command("terraform", "init", "-upgrade")
out, err := cmd.CombinedOutput()
if err != nil {
return errors.Wrap(err, string(out))
func (c *AccTestCase) terraformInit() error {
_, err := os.Stat(path.Join(c.Path, ".terraform"))
if os.IsNotExist(err) {
logrus.Debug("Running terraform init ...")
cmd := exec.Command("terraform", "init", "-input=false")
cmd.Dir = c.Path
out, err := cmd.CombinedOutput()
if err != nil {
return errors.Wrap(err, string(out))
}
logrus.Debug("Terraform init done")
}
logrus.Debug("Terraform init done")

return nil
}

func (c *AccTestCase) terraformApply() error {
logrus.Debug("Running terraform apply ...")
cmd = exec.Command("terraform", "apply", "-auto-approve")
out, err = cmd.CombinedOutput()
cmd := exec.Command("terraform", "apply", "-auto-approve")
cmd.Dir = c.Path
out, err := cmd.CombinedOutput()
if err != nil {
return errors.Wrap(err, string(out))
}
Expand All @@ -111,10 +115,17 @@ func terraformApply() error {
return nil
}

func terraformDestroy() {
func (c *AccTestCase) terraformDestroy() error {
logrus.Debug("Running terraform destroy ...")
_ = exec.Command("terraform", "destroy", "-auto-approve").Run()
cmd := exec.Command("terraform", "destroy", "-auto-approve")
cmd.Dir = c.Path
out, err := cmd.CombinedOutput()
if err != nil {
return errors.Wrap(err, string(out))
}
logrus.Debug("Terraform destroy done")

return nil
}

func runDriftCtlCmd(driftctlCmd *cmd.DriftctlCmd) (*cobra.Command, string, error) {
Expand Down Expand Up @@ -147,18 +158,33 @@ func Run(t *testing.T, c AccTestCase) {
t.Fatal(err)
}

err := os.Chdir(c.Path)
if err != nil {
t.Fatal(err)
}
if c.OnStart != nil {
c.OnStart()
}
err = terraformApply()

// Disable terraform version checks
// @link https://www.terraform.io/docs/commands/index.html#upgrade-and-security-bulletin-checks
checkpoint := os.Getenv("CHECKPOINT_DISABLE")
os.Setenv("CHECKPOINT_DISABLE", "true")

// Execute terraform init if .terraform folder is not found in test folder
err := c.terraformInit()
if err != nil {
t.Fatal(err)
}

err = c.terraformApply()
if err != nil {
t.Fatal(err)
}
defer terraformDestroy()

defer func() {
err := c.terraformDestroy()
wbeuil marked this conversation as resolved.
Show resolved Hide resolved
os.Setenv("CHECKPOINT_DISABLE", checkpoint)
if err != nil {
t.Fatal(err)
}
}()

logger.Init(logger.GetConfig())

Expand All @@ -170,7 +196,10 @@ func Run(t *testing.T, c AccTestCase) {
}
if c.Args != nil {
c.Args = append([]string{""}, c.Args...)
c.Args = append(c.Args, "--output", fmt.Sprintf("json://%s", c.getResultFilePath()))
c.Args = append(c.Args,
"--from", fmt.Sprintf("tfstate://%s", path.Join(c.Path, "terraform.tfstate")),
"--output", fmt.Sprintf("json://%s", c.getResultFilePath()),
)
}
os.Args = c.Args

Expand Down