-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathmigration_test.go
316 lines (286 loc) · 10.4 KB
/
migration_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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright 2019 HAProxy Technologies
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package clustermode_test
import (
"encoding/json"
"fmt"
"os"
"path"
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/renameio"
"github.com/haproxytech/dataplaneapi/configuration"
"github.com/haproxytech/dataplaneapi/storagetype"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)
var (
fixtureDir = "fixture"
expectedDir = "expected"
)
func initConfiguration(dapiCfgPath, dapiStorageDir string) *configuration.Configuration {
testCfg := &configuration.Configuration{
HAProxy: configuration.HAProxyConfiguration{
DataplaneStorageDir: dapiStorageDir,
DataplaneConfig: dapiCfgPath,
},
}
return testCfg
}
func TestConfiguration_LoadDataplaneStorageConfig(t *testing.T) {
tests := []struct {
name string
fixtureDapiCfg string
fixtureCluster string
expectedDapiCfg string
expectedCluster string
}{
{
name: "from empty cluster.json, 1 cluster user",
fixtureDapiCfg: "dataplaneapi-1.yaml",
fixtureCluster: "empty.json",
expectedDapiCfg: "dataplaneapi-1.yaml",
expectedCluster: "cluster-1.json",
},
{
// Migration already done, we start with cluster-2.json that is the same as expected/cluster-1.json
// Same results as "empty cluster.json" should be.
name: "from non empty cluster.json",
fixtureDapiCfg: "dataplaneapi-1.yaml", // same as empty cluster.son
fixtureCluster: "cluster-1_2.json",
expectedDapiCfg: "dataplaneapi-1.yaml", // same as empty cluster.son
expectedCluster: "cluster-1.json", // same as empty cluster.son
},
{
name: "from empty cluster.json, 1 cluster user, 1 single mode user",
fixtureDapiCfg: "dataplaneapi-2.yaml",
fixtureCluster: "empty.json",
expectedDapiCfg: "dataplaneapi-2.yaml",
expectedCluster: "cluster-1.json",
},
{
name: "from non empty cluster.json, 1 cluster user, 1 single mode user",
fixtureDapiCfg: "dataplaneapi-2.yaml",
fixtureCluster: "cluster-1_2.json",
expectedDapiCfg: "dataplaneapi-2.yaml",
expectedCluster: "cluster-1.json",
},
{
name: "from non empty cluster.json, adding 1 cluster user",
fixtureDapiCfg: "dataplaneapi-3.yaml",
fixtureCluster: "cluster-1_2.json",
expectedDapiCfg: "dataplaneapi-2.yaml",
expectedCluster: "cluster-3.json",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
dapiCfgPathSrc := path.Join(fixtureDir, tc.fixtureDapiCfg)
dapiStoragePathSrc := path.Join(fixtureDir, tc.fixtureCluster)
dir := t.TempDir()
dapiCfgPath := path.Join(dir, "dataplaneapi.yaml")
dapiStoragePath := path.Join(dir, "cluster.json")
// Copy dapi config file to tmp dir
bytesRead, err := os.ReadFile(dapiCfgPathSrc)
require.NoError(t, err)
err = renameio.WriteFile(dapiCfgPath, bytesRead, 0o644)
require.NoError(t, err)
bytesRead, err = os.ReadFile(dapiStoragePathSrc)
require.NoError(t, err)
err = renameio.WriteFile(dapiStoragePath, bytesRead, 0o644)
require.NoError(t, err)
// Load dapi config
cfg := initConfiguration(dapiCfgPath, dir)
cfg.Load()
// override storage Dir
cfg.HAProxy.DataplaneStorageDir = dir
// Load and migrate
cfg.LoadDataplaneStorageConfig()
cfg.Save()
// Check migrated cluster.json
// Check dataplaneapi.yaml (removed cluster...)dapiCfgMigrated, err := os.ReadFile(dapiCfgPath)
var areEqual bool
dapiCfg := getDapiCfg(t, dapiCfgPath)
dapiExectedCfg := getDapiCfg(t, path.Join(expectedDir, tc.expectedDapiCfg))
areEqual = reflect.DeepEqual(dapiExectedCfg, dapiCfg)
require.True(t, areEqual, "migrated dataplaneapi.yaml is not equal to expected")
dapiStorageMigrated := getClusterMode(t, dapiStoragePath)
dapiExpectedStorage := getClusterMode(t, path.Join(expectedDir, tc.expectedCluster))
areEqual = reflect.DeepEqual(dapiExpectedStorage, dapiStorageMigrated)
require.True(t, areEqual, fmt.Sprintf("test: %s migrated cluster.json is not equal to expected", tc.name))
})
}
}
func getClusterMode(t *testing.T, dapiStoragePath string) *storagetype.ClusterModeData {
t.Helper()
dapiStorageJ, err := os.ReadFile(dapiStoragePath)
require.NoError(t, err)
var cmd storagetype.ClusterModeData
json.Unmarshal(dapiStorageJ, &cmd)
return &cmd
}
func getDapiCfg(t *testing.T, dapiCfgPath string) *configuration.Configuration {
t.Helper()
dapiCfgJ, err := os.ReadFile(dapiCfgPath)
require.NoError(t, err)
var cfg configuration.Configuration
yaml.Unmarshal(dapiCfgJ, &cfg)
return &cfg
}
func TestConfiguration_LoadDataplaneStorageConfig_SD_Consul(t *testing.T) {
tests := []struct {
name string
fixtureDapiCfg string
fixtureConsul string
expectedDapiCfg string
expectedConsul string
}{
{
name: "from empty consul.json",
fixtureDapiCfg: "dataplaneapi-sd-consul-1.yaml",
fixtureConsul: "empty.json",
expectedDapiCfg: "dataplaneapi-1.yaml",
expectedConsul: "consul-1.json",
},
{
name: "from non empty consul.json",
fixtureDapiCfg: "dataplaneapi-sd-consul-1.yaml",
fixtureConsul: "consul-1_2.json",
expectedDapiCfg: "dataplaneapi-1.yaml",
expectedConsul: "consul-1_2.json",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
dapiCfgPathSrc := path.Join(fixtureDir, tc.fixtureDapiCfg)
dapiStorageConsulPathSrc := path.Join(fixtureDir, tc.fixtureConsul)
dir := t.TempDir()
dapiCfgPath := path.Join(dir, "dataplaneapi.yaml")
dapiStoragePath := path.Join(dir, "service_discovery", "consul.json")
os.MkdirAll(path.Join(dir, "service_discovery"), 0o755)
// Copy dapi config file to tmp dir
bytesRead, err := os.ReadFile(dapiCfgPathSrc)
require.NoError(t, err)
err = renameio.WriteFile(dapiCfgPath, bytesRead, 0o644)
require.NoError(t, err)
bytesRead, err = os.ReadFile(dapiStorageConsulPathSrc)
require.NoError(t, err)
err = renameio.WriteFile(dapiStoragePath, bytesRead, 0o644)
require.NoError(t, err)
// Load dapi config
cfg := initConfiguration(dapiCfgPath, dir)
cfg.Load()
// override storage Dir
cfg.HAProxy.DataplaneStorageDir = dir
// Load and migrate
cfg.LoadDataplaneStorageConfig()
cfg.Save()
// Check migrated consul.json
// Check dataplaneapi.yaml (removed cluster...)dapiCfgMigrated, err := os.ReadFile(dapiCfgPath)
var areEqual bool
dapiCfg := getDapiCfg(t, dapiCfgPath)
dapiExectedCfg := getDapiCfg(t, path.Join(expectedDir, tc.expectedDapiCfg))
areEqual = reflect.DeepEqual(dapiExectedCfg, dapiCfg)
require.True(t, areEqual, "migrated dataplaneapi.yaml is not equal to expected")
dapiStorageMigrated := getConsul(t, dapiStoragePath)
dapiExpectedStorage := getConsul(t, path.Join(expectedDir, tc.expectedConsul))
areEqual = reflect.DeepEqual(dapiExpectedStorage, dapiStorageMigrated)
diff := cmp.Diff(dapiExpectedStorage, dapiStorageMigrated)
fmt.Println(diff)
require.True(t, areEqual, fmt.Sprintf("test: %s migrated consul.json is not equal to expected", tc.name))
})
}
}
func TestConfiguration_LoadDataplaneStorageConfig_SD_AWS(t *testing.T) {
tests := []struct {
name string
fixtureDapiCfg string
fixtureAWS string
expectedDapiCfg string
expectedAWS string
}{
{
name: "from empty aws.json",
fixtureDapiCfg: "dataplaneapi-sd-aws-1.yaml",
fixtureAWS: "empty.json",
expectedDapiCfg: "dataplaneapi-1.yaml",
expectedAWS: "aws-1.json",
},
{
name: "from non empty aws.json",
fixtureDapiCfg: "dataplaneapi-sd-aws-1.yaml",
fixtureAWS: "aws-1_2.json",
expectedDapiCfg: "dataplaneapi-1.yaml",
expectedAWS: "aws-2.json",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
dapiCfgPathSrc := path.Join(fixtureDir, tc.fixtureDapiCfg)
dapiStorageConsulPathSrc := path.Join(fixtureDir, tc.fixtureAWS)
dir := t.TempDir()
dapiCfgPath := path.Join(dir, "dataplaneapi.yaml")
dapiStoragePath := path.Join(dir, "service_discovery", "aws.json")
os.MkdirAll(path.Join(dir, "service_discovery"), 0o755)
// Copy dapi config file to tmp dir
bytesRead, err := os.ReadFile(dapiCfgPathSrc)
require.NoError(t, err)
err = renameio.WriteFile(dapiCfgPath, bytesRead, 0o644)
require.NoError(t, err)
bytesRead, err = os.ReadFile(dapiStorageConsulPathSrc)
require.NoError(t, err)
err = renameio.WriteFile(dapiStoragePath, bytesRead, 0o644)
require.NoError(t, err)
// Load dapi config
cfg := initConfiguration(dapiCfgPath, dir)
cfg.Load()
// override storage Dir
cfg.HAProxy.DataplaneStorageDir = dir
// Load and migrate
cfg.LoadDataplaneStorageConfig()
cfg.Save()
// Check aws.json
// Check dataplaneapi.yaml (removed cluster...)dapiCfgMigrated, err := os.ReadFile(dapiCfgPath)
var areEqual bool
dapiCfg := getDapiCfg(t, dapiCfgPath)
dapiExectedCfg := getDapiCfg(t, path.Join(expectedDir, tc.expectedDapiCfg))
areEqual = reflect.DeepEqual(dapiExectedCfg, dapiCfg)
require.True(t, areEqual, "migrated dataplaneapi.yaml is not equal to expected")
dapiStorageMigrated := getAWS(t, dapiStoragePath)
dapiExpectedStorage := getAWS(t, path.Join(expectedDir, tc.expectedAWS))
areEqual = reflect.DeepEqual(dapiExpectedStorage, dapiStorageMigrated)
diff := cmp.Diff(dapiExpectedStorage, dapiStorageMigrated)
fmt.Println(diff)
require.True(t, areEqual, fmt.Sprintf("test: %s migrated aws.json is not equal to expected", tc.name))
})
}
}
func getConsul(t *testing.T, dapiStoragePath string) *storagetype.ConsulData {
t.Helper()
dapiStorageJ, err := os.ReadFile(dapiStoragePath)
require.NoError(t, err)
var data storagetype.ConsulData
json.Unmarshal(dapiStorageJ, &data)
return &data
}
func getAWS(t *testing.T, dapiStoragePath string) *storagetype.AWSRegionData {
t.Helper()
dapiStorageJ, err := os.ReadFile(dapiStoragePath)
require.NoError(t, err)
var data storagetype.AWSRegionData
json.Unmarshal(dapiStorageJ, &data)
return &data
}