-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply_changes_test.go
200 lines (195 loc) · 5.6 KB
/
apply_changes_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package iac
import (
"encoding/json"
"github.com/google/go-cmp/cmp"
"github.com/nullstone-io/iac/workspace"
moduleConfig "github.com/nullstone-io/module/config"
"github.com/stretchr/testify/require"
"gopkg.in/nullstone-io/go-api-client.v0/types"
"path/filepath"
"testing"
)
func TestApplyChanges(t *testing.T) {
input1 := types.WorkspaceConfig{
Source: "nullstone/aws-fargate-service",
SourceVersion: "0.1.0",
Providers: []string{"aws"},
Variables: types.Variables{
"cpu": {
Variable: moduleConfig.Variable{Type: "number"},
Value: 256,
},
"memory": {
Variable: moduleConfig.Variable{Type: "number"},
Value: 512,
},
},
Connections: types.Connections{},
EnvVariables: types.EnvVariables{
"KEY1": types.EnvVariable{Value: "value1"},
},
Capabilities: types.CapabilityConfigs{
types.CapabilityConfig{
Id: 3,
Source: "nullstone/fake-cap",
SourceVersion: "0.1.2",
Variables: types.Variables{
"fake-var": types.Variable{
Variable: moduleConfig.Variable{Type: "number"},
Value: 10,
},
},
Connections: types.Connections{},
},
},
}
app1 := types.Block{Type: string(types.BlockTypeApplication), Name: "app1"}
previewEnv1 := types.Environment{Type: types.EnvTypePreview, Name: "f-123-something"}
tests := map[string]struct {
input types.WorkspaceConfig
block types.Block
env types.Environment
testFixturesDir string
want types.WorkspaceConfig
}{
"no config, no previews, should apply no changes": {
input: input1,
block: app1,
env: previewEnv1,
testFixturesDir: "scenario1",
want: mustClone(t, input1),
},
"no config, has previews, should apply overrides": {
input: input1,
block: app1,
env: previewEnv1,
testFixturesDir: "scenario2",
want: types.WorkspaceConfig{
Source: "nullstone/aws-fargate-service",
SourceVersion: "0.1.0",
Providers: []string{"aws"},
Variables: types.Variables{
"cpu": {
// changed by overrides
Variable: moduleConfig.Variable{Type: "number"},
Value: 512,
},
"memory": {
Variable: moduleConfig.Variable{Type: "number"},
Value: float64(512),
},
},
Connections: types.Connections{},
EnvVariables: types.EnvVariables{
"KEY1": types.EnvVariable{Value: "value1"},
"KEY3": types.EnvVariable{Value: "value3"},
},
Capabilities: types.CapabilityConfigs{
types.CapabilityConfig{
// changed by overrides
Id: 3,
Source: "nullstone/fake-cap",
SourceVersion: "0.1.2",
Variables: types.Variables{
"fake-var": types.Variable{
Variable: moduleConfig.Variable{Type: "number"},
Value: 20,
},
},
Connections: types.Connections{},
},
},
},
},
"config+previews, apply config, then overrides": {
input: input1,
block: app1,
env: previewEnv1,
testFixturesDir: "scenario3",
want: types.WorkspaceConfig{
Source: "nullstone/aws-fargate-service",
SourceVersion: "0.1.0",
Providers: []string{"aws"},
Variables: types.Variables{
"cpu": {
// changed by overrides
Variable: moduleConfig.Variable{Type: "number"},
Value: 512,
},
"memory": {
// changed by config.yml
Variable: moduleConfig.Variable{Type: "number"},
Value: 2048,
},
},
Connections: types.Connections{},
EnvVariables: types.EnvVariables{
"KEY2": types.EnvVariable{Value: "value2"},
"KEY3": types.EnvVariable{Value: "value3"},
},
Capabilities: types.CapabilityConfigs{
// cleared by config.yml
},
},
},
"config, no previews, apply config": {
input: input1,
block: app1,
env: previewEnv1,
testFixturesDir: "scenario4",
want: types.WorkspaceConfig{
Source: "nullstone/aws-fargate-service",
SourceVersion: "0.1.0",
Providers: []string{"aws"},
Variables: types.Variables{
"cpu": {
Variable: moduleConfig.Variable{Type: "number"},
Value: 1024,
},
"memory": {
Variable: moduleConfig.Variable{Type: "number"},
Value: float64(512),
},
},
Connections: types.Connections{},
EnvVariables: types.EnvVariables{
"KEY2": types.EnvVariable{Value: "value2"},
},
Capabilities: types.CapabilityConfigs{
types.CapabilityConfig{
// preserved in config.yml
Id: 3,
Source: "nullstone/fake-cap",
SourceVersion: "0.1.2",
Variables: types.Variables{
"fake-var": types.Variable{
Variable: moduleConfig.Variable{Type: "number"},
Value: 10,
},
},
Connections: types.Connections{},
},
},
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
pmr, err := ParseConfigDir("", "TestApplyChanges", filepath.Join("test-fixtures", test.testFixturesDir))
require.NoError(t, err, "cannot test fixture dir")
got := mustClone(t, test.input)
err = ApplyChangesTo(*pmr, test.block, test.env, workspace.ConfigUpdater{Config: &got})
require.NoError(t, err, "unexpected error")
if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("-want, +got:\n%s", diff)
}
})
}
}
func mustClone[T any](t *testing.T, wc T) T {
raw, err := json.Marshal(wc)
require.NoError(t, err, "marshaling clone")
var result T
require.NoError(t, json.Unmarshal(raw, &result), "unmarshaling clone")
return result
}