-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathupdate_test.go
221 lines (188 loc) · 5.89 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
package main
import (
"context"
"testing"
"time"
"github.com/zerotier/go-ztcentral"
)
func boolPtr(b bool) *bool {
return &b
}
func modifyMember(ctx context.Context, networkID string, memberID string, updateFunc func(*ztcentral.Member)) error {
c := ztcentral.NewClient(controllerToken)
member, err := c.GetMember(ctx, networkID, memberID)
if err != nil {
return err
}
updateFunc(member)
if _, err := c.UpdateMember(ctx, 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 *ztcentral.Member) {
member.Description = "This is a new description"
member.Hidden = false
member.Config.ActiveBridge = false
member.Config.NoAutoAssignIPs = false
member.Config.IPAssignments = []string{"10.0.0.2"}
})
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")
}
default:
t.Fatalf("invalid member %q encountered", m["name"])
}
}
}
}
func modifyNetwork(ctx context.Context, id string, updateFunc func(*ztcentral.Network)) error {
c := ztcentral.NewClient(controllerToken)
net, err := c.GetNetwork(ctx, id)
if err != nil {
return err
}
updateFunc(net)
if _, err := c.UpdateNetwork(ctx, net); err != nil {
return err
}
return nil
}
func TestNetworkUpdate(t *testing.T) {
// 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 "mtu":
// not updateable
case "multicast_limit":
// not updateable
case "description":
// not updateable
case "assign_off":
err := modifyNetwork(ctx, attrs["id"].(string), func(net *ztcentral.Network) {
net.Config.IPV4AssignMode = &ztcentral.IPV4AssignMode{ZeroTier: boolPtr(true)}
net.Config.IPV6AssignMode = &ztcentral.IPV6AssignMode{ZeroTier: boolPtr(true), ZT6Plane: boolPtr(false), RFC4193: boolPtr(false)}
})
if err != nil {
t.Fatal(err)
}
case "private":
err := modifyNetwork(ctx, attrs["id"].(string), func(net *ztcentral.Network) {
net.Config.Private = boolPtr(false)
})
if err != nil {
t.Fatal(err)
}
case "no_broadcast":
err := modifyNetwork(ctx, attrs["id"].(string), func(net *ztcentral.Network) {
net.Config.EnableBroadcast = boolPtr(true)
})
if err != nil {
t.Fatal(err)
}
case "flow_rules":
c := ztcentral.NewClient(controllerToken)
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 "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"] {
// XXX please, before you modify any of this, read the comments in provision_test.go.
case "mtu":
// not updateable
case "multicast_limit":
// not updateable
case "description":
// not updateable
case "flow_rules":
if attrs["flow_rules"].(string) != "accept;" {
t.Fatal("flow_rules were not updated")
}
case "assign_off":
isBool(t, h(attrs["assign_ipv4"])["zerotier"], true, "assign_ipv4/zerotier")
table := map[string]bool{
"zerotier": true,
"sixplane": false,
"rfc4193": false,
}
for name, val := range table {
isBool(t, h(attrs["assign_ipv6"])[name], val, "assign_ipv6/"+name)
}
case "private":
isBool(t, attrs["private"], false, "private")
case "no_broadcast":
isBool(t, attrs["enable_broadcast"], true, "private")
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"])
}
}
}
}