-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathupdate_test.go
343 lines (292 loc) · 9.4 KB
/
update_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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package main
import (
"context"
"reflect"
"testing"
"time"
"github.com/zerotier/go-ztcentral"
"github.com/zerotier/go-ztcentral/pkg/spec"
)
func boolPtr(b bool) *bool {
return &b
}
func stringPtr(s string) *string {
return &s
}
// this is slightly different than the converters.go version; the result coming
// back from json parsing will always be `float64`, instead of `int`, which we
// pass normally through the type system in converters. See
// pkg/zerotier/converters.go for more information.
func toUintList(i interface{}) []uint {
ray := []uint{}
for _, x := range i.([]interface{}) {
ray = append(ray, uint(x.(float64))) // here
}
return ray
}
func toUintDoubleList(i interface{}) [][]uint {
ray := [][]uint{}
for _, x := range i.([]interface{}) {
items := []uint{}
for _, y := range x.([]interface{}) {
items = append(items, uint(y.(float64)))
}
ray = append(ray, items)
}
return ray
}
func modifyMember(ctx context.Context, networkID string, memberID string, updateFunc func(*spec.Member)) error {
c, err := ztcentral.NewClient(controllerToken)
if err != nil {
return err
}
member, err := c.GetMember(ctx, networkID, memberID)
if err != nil {
return err
}
updateFunc(member)
if _, err := c.UpdateMember(ctx, networkID, memberID, member); err != nil {
return err
}
return nil
}
func TestMemberUpdate(t *testing.T) {
// see TestNetworkUpdate for the flow of this test.
tf := getTFTest(t)
tf.Apply("testdata/plans/basic-member.tf")
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
t.Cleanup(cancel)
// for each network, perform a set of transformations with the client.
for _, resource := range a(tf.State()["resources"]) {
m := h(resource)
attrs := h(h(a(m["instances"])[0])["attributes"])
switch m["type"] {
case "zerotier_member":
switch m["name"] {
case "alice":
err := modifyMember(ctx, attrs["network_id"].(string), attrs["member_id"].(string), func(member *spec.Member) {
member.Description = stringPtr("This is a new description")
member.Hidden = boolPtr(false)
member.Config.ActiveBridge = boolPtr(false)
member.Config.NoAutoAssignIps = boolPtr(false)
member.Config.IpAssignments = &[]string{"10.0.0.2"}
member.Config.Capabilities = &[]int{0, 1, 2}
member.Config.Tags = &[][]interface{}{{0, 1}}
})
if err != nil {
t.Fatal(err)
}
default:
t.Fatalf("invalid member %q encountered", m["name"])
}
}
}
tf.Refresh()
// for each network, validate the transformations were applied.
for _, resource := range a(tf.State()["resources"]) {
m := h(resource)
attrs := h(h(a(m["instances"])[0])["attributes"])
switch m["type"] {
case "zerotier_member":
switch m["name"] {
case "alice":
if attrs["description"].(string) != "This is a new description" {
t.Fatal("description was not set")
}
isBool(t, attrs["hidden"], false, "hidden")
isBool(t, attrs["allow_ethernet_bridging"], false, "allow_ethernet_bridging")
isBool(t, attrs["no_auto_assign_ips"], false, "no_auto_assign_ips")
if a(attrs["ip_assignments"])[0].(string) != "10.0.0.2" {
t.Fatal("ip_assignments was improperly set")
}
if !reflect.DeepEqual(toUintList(a(attrs["capabilities"])), []uint{0, 1, 2}) {
t.Fatalf("capabilities was improperly set: is %#v", a(attrs["capabilities"]))
}
if !reflect.DeepEqual(toUintDoubleList(a(attrs["tags"])), [][]uint{{0, 1}}) {
t.Fatalf("tags was improperly set: is %#v", a(attrs["tags"]))
}
default:
t.Fatalf("invalid member %q encountered", m["name"])
}
}
}
// reset the settings with terraform. Did it work?
tf.Apply("testdata/plans/basic-member.tf")
// for each network, validate the transformations were applied.
for _, resource := range a(tf.State()["resources"]) {
m := h(resource)
attrs := h(h(a(m["instances"])[0])["attributes"])
switch m["type"] {
case "zerotier_member":
switch m["name"] {
case "alice":
if attrs["description"].(string) != "Hello, world" {
t.Fatalf("description was not set; was %q", attrs["description"])
}
isBool(t, attrs["hidden"], true, "hidden")
isBool(t, attrs["allow_ethernet_bridging"], true, "allow_ethernet_bridging")
isBool(t, attrs["no_auto_assign_ips"], true, "no_auto_assign_ips")
if !reflect.DeepEqual(toUintList(a(attrs["capabilities"])), []uint{1, 2, 3}) {
t.Fatalf("capabilities was improperly set: is %#v", a(attrs["capabilities"]))
}
if a(attrs["ip_assignments"])[0].(string) != "10.0.0.1" {
t.Fatal("ip_assignments was improperly set")
}
if !reflect.DeepEqual(toUintDoubleList(a(attrs["tags"])), [][]uint{{1000, 100}}) {
t.Fatalf("tags was improperly set: is %#v", a(attrs["tags"]))
}
default:
t.Fatalf("invalid member %q encountered", m["name"])
}
}
}
}
func modifyNetwork(ctx context.Context, id string, updateFunc func(*spec.Network)) error {
c, err := ztcentral.NewClient(controllerToken)
if err != nil {
return err
}
net, err := c.GetNetwork(ctx, id)
if err != nil {
return err
}
updateFunc(net)
if _, err := c.UpdateNetwork(ctx, *net.Id, net); err != nil {
return err
}
return nil
}
func TestNetworkUpdate(t *testing.T) { // nolint:gocyclo
// this test uses the same plan as BasicNetwork, but then inverts some values
// back to defaults in each one to ensure that terraform picks them up on
// refresh.
tf := getTFTest(t)
tf.Apply("testdata/plans/basic-network.tf")
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
t.Cleanup(cancel)
// for each network, perform a set of transformations with the client.
for _, resource := range a(tf.State()["resources"]) {
m := h(resource)
attrs := h(h(a(m["instances"])[0])["attributes"])
switch m["type"] {
case "zerotier_network":
switch m["name"] {
// XXX please, before you modify any of this, read the comments in provision_test.go.
case "multicast_limit":
// not updateable
case "description":
err := modifyNetwork(ctx, attrs["id"].(string), func(net *spec.Network) {
net.Description = nil
})
if err != nil {
t.Fatal(err)
}
case "assign_off":
err := modifyNetwork(ctx, attrs["id"].(string), func(net *spec.Network) {
net.Config.V4AssignMode = &spec.IPV4AssignMode{Zt: boolPtr(false)}
net.Config.V6AssignMode = &spec.IPV6AssignMode{Zt: boolPtr(false), N6plane: boolPtr(true), Rfc4193: boolPtr(true)}
})
if err != nil {
t.Fatal(err)
}
case "private":
err := modifyNetwork(ctx, attrs["id"].(string), func(net *spec.Network) {
net.Config.Private = boolPtr(false)
})
if err != nil {
t.Fatal(err)
}
case "no_broadcast":
err := modifyNetwork(ctx, attrs["id"].(string), func(net *spec.Network) {
net.Config.EnableBroadcast = boolPtr(true)
})
if err != nil {
t.Fatal(err)
}
case "flow_rules":
c, err := ztcentral.NewClient(controllerToken)
if err != nil {
t.Fatal(err)
}
rules, err := c.UpdateNetworkRules(ctx, attrs["id"].(string), "accept;")
if err != nil {
t.Fatal(err)
}
if rules != "accept;" {
t.Fatal("rules were not alterered on set")
}
case "dns_settings":
err := modifyNetwork(ctx, attrs["id"].(string), func(net *spec.Network) {
domain := "testing.home"
net.Config.Dns = &spec.DNS{
Domain: &domain,
Servers: &[]string{"1.1.1.1", "8.8.8.8"},
}
})
if err != nil {
t.Fatal(err)
}
case "alice", "bobs_garage":
// this is a collection of defaults; not sure testing this is really worth the effort.
default:
t.Fatalf("Unexpected network %q in plan", m["name"])
}
}
}
tf.Refresh()
// for each network, validate the transformations were applied.
for _, resource := range a(tf.State()["resources"]) {
m := h(resource)
attrs := h(h(a(m["instances"])[0])["attributes"])
switch m["type"] {
case "zerotier_network":
switch m["name"] {
case "multicast_limit":
// not updateable
case "description":
if attrs["description"].(string) != "My description is changed!" {
t.Fatal("description was not updated")
}
case "flow_rules":
if attrs["flow_rules"].(string) != "accept;" {
t.Fatal("flow_rules were not updated")
}
case "assign_off":
isBool(t, h(a(attrs["assign_ipv4"])[0])["zerotier"], false, "assign_ipv4/zerotier")
table := map[string]bool{
"zerotier": false,
"sixplane": true,
"rfc4193": true,
}
for name, val := range table {
isBool(t, h(a(attrs["assign_ipv6"])[0])[name], val, "assign_ipv6/"+name)
}
case "private":
isBool(t, attrs["private"], false, "private")
case "no_broadcast":
isBool(t, attrs["enable_broadcast"], true, "private")
case "dns_settings":
if attrs["dns"] == nil {
t.Fatal("dns settings were missing")
}
domain := h(a(attrs["dns"])[0])["domain"].(string)
if domain != "testing.home" {
t.Fatalf("dns domain was not updated, was %q", domain)
}
if len(a(h(a(attrs["dns"])[0])["servers"])) != 2 {
t.Fatal("dns servers were not updated")
}
if a(h(a(attrs["dns"])[0])["servers"])[0] != "1.1.1.1" {
t.Fatal("dns servers were incorrect")
}
if a(h(a(attrs["dns"])[0])["servers"])[1] != "8.8.8.8" {
t.Fatal("dns servers were incorrect")
}
case "alice", "bobs_garage":
// this is a collection of defaults; not sure testing this is really worth the effort.
default:
t.Fatalf("Unexpected network %q in plan", m["name"])
}
}
}
}