Skip to content

Commit

Permalink
test: scan command
Browse files Browse the repository at this point in the history
Add test suite for the retrieveBackendsFromHCL function
  • Loading branch information
sundowndev-snyk committed Jul 4, 2022
1 parent e9d00d1 commit 8444e5a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func retrieveBackendsFromHCL(workdir string) ([]config.SupplierConfig, error) {
if err != nil {
return nil, err
}
var supplierConfigs []config.SupplierConfig
supplierConfigs := make([]config.SupplierConfig, 0)

for _, match := range matches {
body, err := hcl.ParseTerraformFromHCL(match)
Expand Down
37 changes: 37 additions & 0 deletions pkg/cmd/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"testing"

"github.com/snyk/driftctl/pkg"
"github.com/snyk/driftctl/pkg/iac/config"
"github.com/snyk/driftctl/pkg/iac/terraform/state"
"github.com/snyk/driftctl/pkg/iac/terraform/state/backend"
"github.com/snyk/driftctl/test"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -156,3 +159,37 @@ func Test_Options(t *testing.T) {
})
}
}

func Test_RetrieveBackendsFromHCL(t *testing.T) {
cases := []struct {
name string
dir string
expected []config.SupplierConfig
wantErr error
}{
{
name: "should parse s3 backend and ignore invalid file",
dir: "testdata/backend/s3",
expected: []config.SupplierConfig{
{
Key: state.TerraformStateReaderSupplier,
Backend: backend.BackendKeyS3,
Path: "terraform-state-prod/network/terraform.tfstate",
},
},
},
{
name: "should not find any match and return empty slice",
dir: "testdata/backend",
expected: []config.SupplierConfig{},
},
}

for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
configs, err := retrieveBackendsFromHCL(tt.dir)
assert.Equal(t, tt.wantErr, err)
assert.Equal(t, tt.expected, configs)
})
}
}
1 change: 1 addition & 0 deletions pkg/cmd/testdata/backend/s3/invalid.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invalid {}
7 changes: 7 additions & 0 deletions pkg/cmd/testdata/backend/s3/s3.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
terraform {
backend "s3" {
bucket = "terraform-state-prod"
key = "network/terraform.tfstate"
region = "us-east-1"
}
}

0 comments on commit 8444e5a

Please sign in to comment.