|
1 | 1 | package terraform
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
4 | 5 | "testing"
|
5 | 6 |
|
| 7 | + tfjson "github.com/hashicorp/terraform-json" |
6 | 8 | "github.com/stretchr/testify/require"
|
7 | 9 |
|
8 | 10 | "github.com/coder/coder/v2/provisionersdk/proto"
|
@@ -41,3 +43,133 @@ From standing in the English rain`))
|
41 | 43 | }
|
42 | 44 | require.Equal(t, expected, logr.logs)
|
43 | 45 | }
|
| 46 | + |
| 47 | +func TestOnlyDataResources(t *testing.T) { |
| 48 | + t.Parallel() |
| 49 | + |
| 50 | + tests := []struct { |
| 51 | + name string |
| 52 | + stateMod *tfjson.StateModule |
| 53 | + expected *tfjson.StateModule |
| 54 | + }{ |
| 55 | + { |
| 56 | + name: "empty state module", |
| 57 | + stateMod: &tfjson.StateModule{}, |
| 58 | + expected: &tfjson.StateModule{}, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "only data resources", |
| 62 | + stateMod: &tfjson.StateModule{ |
| 63 | + Resources: []*tfjson.StateResource{ |
| 64 | + {Name: "cat", Type: "coder_parameter", Mode: "data", Address: "cat-address"}, |
| 65 | + {Name: "cow", Type: "foobaz", Mode: "data", Address: "cow-address"}, |
| 66 | + }, |
| 67 | + ChildModules: []*tfjson.StateModule{ |
| 68 | + { |
| 69 | + Resources: []*tfjson.StateResource{ |
| 70 | + {Name: "child-cat", Type: "coder_parameter", Mode: "data", Address: "child-cat-address"}, |
| 71 | + {Name: "child-dog", Type: "foobar", Mode: "data", Address: "child-dog-address"}, |
| 72 | + }, |
| 73 | + Address: "child-module-1", |
| 74 | + }, |
| 75 | + }, |
| 76 | + Address: "fake-module", |
| 77 | + }, |
| 78 | + expected: &tfjson.StateModule{ |
| 79 | + Resources: []*tfjson.StateResource{ |
| 80 | + {Name: "cat", Type: "coder_parameter", Mode: "data", Address: "cat-address"}, |
| 81 | + {Name: "cow", Type: "foobaz", Mode: "data", Address: "cow-address"}, |
| 82 | + }, |
| 83 | + ChildModules: []*tfjson.StateModule{ |
| 84 | + { |
| 85 | + Resources: []*tfjson.StateResource{ |
| 86 | + {Name: "child-cat", Type: "coder_parameter", Mode: "data", Address: "child-cat-address"}, |
| 87 | + {Name: "child-dog", Type: "foobar", Mode: "data", Address: "child-dog-address"}, |
| 88 | + }, |
| 89 | + Address: "child-module-1", |
| 90 | + }, |
| 91 | + }, |
| 92 | + Address: "fake-module", |
| 93 | + }, |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "only non-data resources", |
| 97 | + stateMod: &tfjson.StateModule{ |
| 98 | + Resources: []*tfjson.StateResource{ |
| 99 | + {Name: "cat", Type: "coder_parameter", Mode: "foobar", Address: "cat-address"}, |
| 100 | + {Name: "cow", Type: "foobaz", Mode: "foo", Address: "cow-address"}, |
| 101 | + }, |
| 102 | + ChildModules: []*tfjson.StateModule{ |
| 103 | + { |
| 104 | + Resources: []*tfjson.StateResource{ |
| 105 | + {Name: "child-cat", Type: "coder_parameter", Mode: "foobar", Address: "child-cat-address"}, |
| 106 | + {Name: "child-dog", Type: "foobar", Mode: "foobaz", Address: "child-dog-address"}, |
| 107 | + }, |
| 108 | + Address: "child-module-1", |
| 109 | + }, |
| 110 | + }, |
| 111 | + Address: "fake-module", |
| 112 | + }, |
| 113 | + expected: &tfjson.StateModule{ |
| 114 | + Address: "fake-module", |
| 115 | + ChildModules: []*tfjson.StateModule{ |
| 116 | + {Address: "child-module-1"}, |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + { |
| 121 | + name: "mixed resources", |
| 122 | + stateMod: &tfjson.StateModule{ |
| 123 | + Resources: []*tfjson.StateResource{ |
| 124 | + {Name: "cat", Type: "coder_parameter", Mode: "data", Address: "cat-address"}, |
| 125 | + {Name: "dog", Type: "foobar", Mode: "magic", Address: "dog-address"}, |
| 126 | + {Name: "cow", Type: "foobaz", Mode: "data", Address: "cow-address"}, |
| 127 | + }, |
| 128 | + ChildModules: []*tfjson.StateModule{ |
| 129 | + { |
| 130 | + Resources: []*tfjson.StateResource{ |
| 131 | + {Name: "child-cat", Type: "coder_parameter", Mode: "data", Address: "child-cat-address"}, |
| 132 | + {Name: "child-dog", Type: "foobar", Mode: "data", Address: "child-dog-address"}, |
| 133 | + {Name: "child-cow", Type: "foobaz", Mode: "magic", Address: "child-cow-address"}, |
| 134 | + }, |
| 135 | + Address: "child-module-1", |
| 136 | + }, |
| 137 | + }, |
| 138 | + Address: "fake-module", |
| 139 | + }, |
| 140 | + expected: &tfjson.StateModule{ |
| 141 | + Resources: []*tfjson.StateResource{ |
| 142 | + {Name: "cat", Type: "coder_parameter", Mode: "data", Address: "cat-address"}, |
| 143 | + {Name: "cow", Type: "foobaz", Mode: "data", Address: "cow-address"}, |
| 144 | + }, |
| 145 | + ChildModules: []*tfjson.StateModule{ |
| 146 | + { |
| 147 | + Resources: []*tfjson.StateResource{ |
| 148 | + {Name: "child-cat", Type: "coder_parameter", Mode: "data", Address: "child-cat-address"}, |
| 149 | + {Name: "child-dog", Type: "foobar", Mode: "data", Address: "child-dog-address"}, |
| 150 | + }, |
| 151 | + Address: "child-module-1", |
| 152 | + }, |
| 153 | + }, |
| 154 | + Address: "fake-module", |
| 155 | + }, |
| 156 | + }, |
| 157 | + } |
| 158 | + |
| 159 | + for _, tt := range tests { |
| 160 | + tt := tt |
| 161 | + |
| 162 | + t.Run(tt.name, func(t *testing.T) { |
| 163 | + t.Parallel() |
| 164 | + |
| 165 | + filtered := onlyDataResources(*tt.stateMod) |
| 166 | + |
| 167 | + expected, err := json.Marshal(tt.expected) |
| 168 | + require.NoError(t, err) |
| 169 | + got, err := json.Marshal(filtered) |
| 170 | + require.NoError(t, err) |
| 171 | + |
| 172 | + require.Equal(t, string(expected), string(got)) |
| 173 | + }) |
| 174 | + } |
| 175 | +} |
0 commit comments