|
1 | 1 | package hcl
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "path" |
4 | 5 | "testing"
|
5 | 6 |
|
6 | 7 | "github.com/snyk/driftctl/pkg/iac/config"
|
7 | 8 | "github.com/stretchr/testify/assert"
|
8 | 9 | )
|
9 | 10 |
|
| 11 | +func TestHCL_getCurrentWorkspaceName(t *testing.T) { |
| 12 | + cases := []struct { |
| 13 | + name string |
| 14 | + dir string |
| 15 | + want string |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "test with non-default workspace", |
| 19 | + dir: "testdata/foo_workspace", |
| 20 | + want: "foo", |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "test with non-existing directory", |
| 24 | + dir: "testdata/noenvfile", |
| 25 | + want: "default", |
| 26 | + }, |
| 27 | + } |
| 28 | + |
| 29 | + for _, tt := range cases { |
| 30 | + t.Run(tt.name, func(t *testing.T) { |
| 31 | + workspace := GetCurrentWorkspaceName(tt.dir) |
| 32 | + assert.Equal(t, tt.want, workspace) |
| 33 | + }) |
| 34 | + } |
| 35 | +} |
| 36 | + |
10 | 37 | func TestBackend_SupplierConfig(t *testing.T) {
|
11 | 38 | cases := []struct {
|
12 |
| - name string |
13 |
| - dir string |
14 |
| - want *config.SupplierConfig |
15 |
| - wantErr string |
| 39 | + name string |
| 40 | + filename string |
| 41 | + want *config.SupplierConfig |
| 42 | + wantErr string |
16 | 43 | }{
|
17 | 44 | {
|
18 |
| - name: "test with no backend block", |
19 |
| - dir: "testdata/no_backend_block.tf", |
20 |
| - want: nil, |
21 |
| - wantErr: "testdata/no_backend_block.tf:1,11-11: Missing backend block; A backend block is required.", |
| 45 | + name: "test with no backend block", |
| 46 | + filename: "testdata/no_backend_block.tf", |
| 47 | + want: nil, |
| 48 | + wantErr: "testdata/no_backend_block.tf:1,11-11: Missing backend block; A backend block is required.", |
22 | 49 | },
|
23 | 50 | {
|
24 |
| - name: "test with local backend block", |
25 |
| - dir: "testdata/local_backend_block.tf", |
| 51 | + name: "test with local backend block", |
| 52 | + filename: "testdata/local_backend_block.tf", |
26 | 53 | want: &config.SupplierConfig{
|
27 | 54 | Key: "tfstate",
|
28 | 55 | Path: "terraform-state-prod/network/terraform.tfstate",
|
29 | 56 | },
|
30 | 57 | },
|
31 | 58 | {
|
32 |
| - name: "test with S3 backend block", |
33 |
| - dir: "testdata/s3_backend_block.tf", |
| 59 | + name: "test with S3 backend block", |
| 60 | + filename: "testdata/s3_backend_block.tf", |
34 | 61 | want: &config.SupplierConfig{
|
35 | 62 | Key: "tfstate",
|
36 | 63 | Backend: "s3",
|
37 | 64 | Path: "terraform-state-prod/network/terraform.tfstate",
|
38 | 65 | },
|
39 | 66 | },
|
40 | 67 | {
|
41 |
| - name: "test with GCS backend block", |
42 |
| - dir: "testdata/gcs_backend_block.tf", |
| 68 | + name: "test with S3 backend block with non-default workspace", |
| 69 | + filename: "testdata/s3_backend_workspace/s3_backend_block.tf", |
| 70 | + want: &config.SupplierConfig{ |
| 71 | + Key: "tfstate", |
| 72 | + Backend: "s3", |
| 73 | + Path: "terraform-state-prod/env:/bar/network/terraform.tfstate", |
| 74 | + }, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "test with GCS backend block", |
| 78 | + filename: "testdata/gcs_backend_block.tf", |
43 | 79 | want: &config.SupplierConfig{
|
44 | 80 | Key: "tfstate",
|
45 | 81 | Backend: "gs",
|
46 |
| - Path: "tf-state-prod/terraform/state.tfstate", |
| 82 | + Path: "tf-state-prod/terraform/state/default.tfstate", |
47 | 83 | },
|
48 | 84 | },
|
49 | 85 | {
|
50 |
| - name: "test with Azure backend block", |
51 |
| - dir: "testdata/azurerm_backend_block.tf", |
| 86 | + name: "test with Azure backend block", |
| 87 | + filename: "testdata/azurerm_backend_block.tf", |
52 | 88 | want: &config.SupplierConfig{
|
53 | 89 | Key: "tfstate",
|
54 | 90 | Backend: "azurerm",
|
55 | 91 | Path: "states/prod.terraform.tfstate",
|
56 | 92 | },
|
57 | 93 | },
|
| 94 | + { |
| 95 | + name: "test with Azure backend block with non-default workspace", |
| 96 | + filename: "testdata/azurerm_backend_workspace/azurerm_backend_block.tf", |
| 97 | + want: &config.SupplierConfig{ |
| 98 | + Key: "tfstate", |
| 99 | + Backend: "azurerm", |
| 100 | + Path: "states/prod.terraform.tfstateenv:bar", |
| 101 | + }, |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "test with unknown backend", |
| 105 | + filename: "testdata/unknown_backend_block.tf", |
| 106 | + want: nil, |
| 107 | + }, |
58 | 108 | }
|
59 | 109 |
|
60 | 110 | for _, tt := range cases {
|
61 | 111 | t.Run(tt.name, func(t *testing.T) {
|
62 |
| - hcl, err := ParseTerraformFromHCL(tt.dir) |
| 112 | + hcl, err := ParseTerraformFromHCL(tt.filename) |
63 | 113 | if tt.wantErr == "" {
|
64 | 114 | assert.NoError(t, err)
|
65 | 115 | } else {
|
66 | 116 | assert.EqualError(t, err, tt.wantErr)
|
67 | 117 | return
|
68 | 118 | }
|
69 | 119 |
|
70 |
| - if hcl.Backend.SupplierConfig() == nil { |
| 120 | + ws := GetCurrentWorkspaceName(path.Dir(tt.filename)) |
| 121 | + if hcl.Backend.SupplierConfig(ws) == nil { |
71 | 122 | assert.Nil(t, tt.want)
|
72 | 123 | return
|
73 | 124 | }
|
74 | 125 |
|
75 |
| - assert.Equal(t, *tt.want, *hcl.Backend.SupplierConfig()) |
| 126 | + assert.Equal(t, *tt.want, *hcl.Backend.SupplierConfig(ws)) |
76 | 127 | })
|
77 | 128 | }
|
78 | 129 | }
|
0 commit comments