Skip to content

Commit 8444e5a

Browse files
test: scan command
Add test suite for the retrieveBackendsFromHCL function
1 parent e9d00d1 commit 8444e5a

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

pkg/cmd/scan.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ func retrieveBackendsFromHCL(workdir string) ([]config.SupplierConfig, error) {
387387
if err != nil {
388388
return nil, err
389389
}
390-
var supplierConfigs []config.SupplierConfig
390+
supplierConfigs := make([]config.SupplierConfig, 0)
391391

392392
for _, match := range matches {
393393
body, err := hcl.ParseTerraformFromHCL(match)

pkg/cmd/scan_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"testing"
55

66
"github.com/snyk/driftctl/pkg"
7+
"github.com/snyk/driftctl/pkg/iac/config"
8+
"github.com/snyk/driftctl/pkg/iac/terraform/state"
9+
"github.com/snyk/driftctl/pkg/iac/terraform/state/backend"
710
"github.com/snyk/driftctl/test"
811
"github.com/spf13/cobra"
912
"github.com/stretchr/testify/assert"
@@ -156,3 +159,37 @@ func Test_Options(t *testing.T) {
156159
})
157160
}
158161
}
162+
163+
func Test_RetrieveBackendsFromHCL(t *testing.T) {
164+
cases := []struct {
165+
name string
166+
dir string
167+
expected []config.SupplierConfig
168+
wantErr error
169+
}{
170+
{
171+
name: "should parse s3 backend and ignore invalid file",
172+
dir: "testdata/backend/s3",
173+
expected: []config.SupplierConfig{
174+
{
175+
Key: state.TerraformStateReaderSupplier,
176+
Backend: backend.BackendKeyS3,
177+
Path: "terraform-state-prod/network/terraform.tfstate",
178+
},
179+
},
180+
},
181+
{
182+
name: "should not find any match and return empty slice",
183+
dir: "testdata/backend",
184+
expected: []config.SupplierConfig{},
185+
},
186+
}
187+
188+
for _, tt := range cases {
189+
t.Run(tt.name, func(t *testing.T) {
190+
configs, err := retrieveBackendsFromHCL(tt.dir)
191+
assert.Equal(t, tt.wantErr, err)
192+
assert.Equal(t, tt.expected, configs)
193+
})
194+
}
195+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
invalid {}

pkg/cmd/testdata/backend/s3/s3.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
terraform {
2+
backend "s3" {
3+
bucket = "terraform-state-prod"
4+
key = "network/terraform.tfstate"
5+
region = "us-east-1"
6+
}
7+
}

0 commit comments

Comments
 (0)