2022-06-24 08:12:32 +00:00
|
|
|
package hcl
|
|
|
|
|
|
|
|
import (
|
2022-07-12 07:07:48 +00:00
|
|
|
"path"
|
2022-06-24 08:12:32 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/snyk/driftctl/pkg/iac/config"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBackend_SupplierConfig(t *testing.T) {
|
|
|
|
cases := []struct {
|
2022-07-12 07:07:48 +00:00
|
|
|
name string
|
|
|
|
filename string
|
|
|
|
want *config.SupplierConfig
|
|
|
|
wantErr string
|
2022-06-24 08:12:32 +00:00
|
|
|
}{
|
|
|
|
{
|
2022-07-12 07:07:48 +00:00
|
|
|
name: "test with no backend block",
|
|
|
|
filename: "testdata/no_backend_block.tf",
|
|
|
|
want: nil,
|
2022-06-24 08:12:32 +00:00
|
|
|
},
|
|
|
|
{
|
2022-07-12 07:07:48 +00:00
|
|
|
name: "test with local backend block",
|
|
|
|
filename: "testdata/local_backend_block.tf",
|
2022-06-24 08:12:32 +00:00
|
|
|
want: &config.SupplierConfig{
|
|
|
|
Key: "tfstate",
|
|
|
|
Path: "terraform-state-prod/network/terraform.tfstate",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-07-12 07:07:48 +00:00
|
|
|
name: "test with S3 backend block",
|
|
|
|
filename: "testdata/s3_backend_block.tf",
|
2022-06-24 08:12:32 +00:00
|
|
|
want: &config.SupplierConfig{
|
|
|
|
Key: "tfstate",
|
|
|
|
Backend: "s3",
|
|
|
|
Path: "terraform-state-prod/network/terraform.tfstate",
|
|
|
|
},
|
|
|
|
},
|
2022-07-11 13:47:28 +00:00
|
|
|
{
|
2022-07-12 07:07:48 +00:00
|
|
|
name: "test with S3 backend block with non-default workspace",
|
|
|
|
filename: "testdata/s3_backend_workspace/s3_backend_block.tf",
|
2022-07-11 13:47:28 +00:00
|
|
|
want: &config.SupplierConfig{
|
|
|
|
Key: "tfstate",
|
|
|
|
Backend: "s3",
|
2022-07-12 10:14:55 +00:00
|
|
|
Path: "terraform-state-prod/env:/bar/network/terraform.tfstate",
|
2022-07-11 13:47:28 +00:00
|
|
|
},
|
|
|
|
},
|
2022-06-24 08:12:32 +00:00
|
|
|
{
|
2022-07-12 07:07:48 +00:00
|
|
|
name: "test with GCS backend block",
|
|
|
|
filename: "testdata/gcs_backend_block.tf",
|
2022-06-24 08:12:32 +00:00
|
|
|
want: &config.SupplierConfig{
|
|
|
|
Key: "tfstate",
|
|
|
|
Backend: "gs",
|
2022-07-11 13:47:28 +00:00
|
|
|
Path: "tf-state-prod/terraform/state/default.tfstate",
|
2022-06-24 08:12:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-07-12 07:07:48 +00:00
|
|
|
name: "test with Azure backend block",
|
|
|
|
filename: "testdata/azurerm_backend_block.tf",
|
2022-06-24 08:12:32 +00:00
|
|
|
want: &config.SupplierConfig{
|
|
|
|
Key: "tfstate",
|
|
|
|
Backend: "azurerm",
|
|
|
|
Path: "states/prod.terraform.tfstate",
|
|
|
|
},
|
|
|
|
},
|
2022-07-12 10:21:26 +00:00
|
|
|
{
|
|
|
|
name: "test with Azure backend block with non-default workspace",
|
|
|
|
filename: "testdata/azurerm_backend_workspace/azurerm_backend_block.tf",
|
|
|
|
want: &config.SupplierConfig{
|
|
|
|
Key: "tfstate",
|
|
|
|
Backend: "azurerm",
|
|
|
|
Path: "states/prod.terraform.tfstateenv:bar",
|
|
|
|
},
|
|
|
|
},
|
2022-07-12 07:07:48 +00:00
|
|
|
{
|
|
|
|
name: "test with unknown backend",
|
|
|
|
filename: "testdata/unknown_backend_block.tf",
|
|
|
|
want: nil,
|
|
|
|
},
|
2022-06-24 08:12:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range cases {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-07-12 07:07:48 +00:00
|
|
|
hcl, err := ParseTerraformFromHCL(tt.filename)
|
2022-06-24 08:12:32 +00:00
|
|
|
if tt.wantErr == "" {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
assert.EqualError(t, err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-12 07:07:48 +00:00
|
|
|
ws := GetCurrentWorkspaceName(path.Dir(tt.filename))
|
2022-07-13 07:55:31 +00:00
|
|
|
if hcl.Backend == nil || hcl.Backend.SupplierConfig(ws) == nil {
|
2022-06-24 08:12:32 +00:00
|
|
|
assert.Nil(t, tt.want)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-12 07:07:48 +00:00
|
|
|
assert.Equal(t, *tt.want, *hcl.Backend.SupplierConfig(ws))
|
2022-06-24 08:12:32 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|