forked from zerotier/terraform-provider-zerotier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovision_test.go
272 lines (227 loc) · 6.97 KB
/
provision_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
package main
import (
"strings"
"testing"
"github.com/erikh/tftest"
)
// these are deliberately named to keep the code small. they do not add
// anything else.
func h(m interface{}) map[string]interface{} {
return m.(map[string]interface{})
}
func a(m interface{}) []interface{} {
return m.([]interface{})
}
func s(m interface{}) string {
return m.(string)
}
func isBool(t *testing.T, i interface{}, val bool, name string) {
b, ok := i.(bool)
if ok && b != val {
t.Fatalf("%q was not set to %v", name, val)
} else if !ok {
b2, ok := i.(*bool)
if ok && (b2 == nil || *b2 != val) {
t.Fatalf("%q was not set to %v", name, val)
} else if !ok {
t.Fatalf("%q was not set properly", name)
}
}
}
func isNum(t *testing.T, i interface{}, val float64, name string) {
if f, ok := i.(float64); !ok || f != val {
t.Fatalf("%q was not set to %v", name, val)
}
}
func isNonZeroNum(t *testing.T, i interface{}, name string) {
if f, ok := i.(float64); !ok || f == 0 {
t.Fatalf("%q was not set", name)
}
}
type identity struct {
name string
pubkey string
privkey string
}
func extractIdentities(tf *tftest.Harness) map[string]identity {
resources := a(tf.State()["resources"])
ids := map[string]identity{}
for _, resource := range resources {
res := h(resource)
name := s(res["name"])
ids[name] = identity{
name: name,
pubkey: s(h(h(a(res["instances"])[0])["attributes"])["public_key"]),
privkey: s(h(h(a(res["instances"])[0])["attributes"])["private_key"]),
}
}
return ids
}
func TestIdentity(t *testing.T) {
tf := getTFTest(t)
tf.Apply("testdata/plans/basic-identity.tf")
ids := extractIdentities(tf)
if len(ids) != 2 {
t.Fatalf("invalid count of identities created")
}
for _, name := range []string{"alice", "bob"} {
if _, ok := ids[name]; !ok {
t.Fatalf("%q was not present in state", name)
}
if ids[name].pubkey == "" {
t.Fatalf("%q: pubkey empty", name)
}
if ids[name].privkey == "" {
t.Fatalf("%q: pubkey empty", name)
}
}
}
func TestToken(t *testing.T) {
tf := getTFTest(t)
tf.Apply("testdata/plans/token.tf")
var sawHello bool // ah this does suck
for _, resource := range a(tf.State()["resources"]) {
m := h(resource)
attrs := h(h(a(m["instances"])[0])["attributes"])
switch m["type"] {
case "zerotier_token":
if len(attrs["name"].(string)) == 0 || len(attrs["token"].(string)) == 0 {
t.Fatal("name or token was not set")
}
if attrs["name"].(string) == "hello-world" {
sawHello = true
}
}
}
if !sawHello {
t.Fatal("never saw the hello-world token")
}
}
func TestBasicMembers(t *testing.T) {
tf := getTFTest(t)
tf.Apply("testdata/plans/basic-member.tf")
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"] {
// see TestBasicNetworkSetup for examples on how this loop works.
case "alice":
if attrs["description"].(string) != "Hello, world" {
t.Fatal("description was not set")
}
isBool(t, attrs["hidden"], true, "hidden")
isBool(t, attrs["sso_exempt"], true, "sso_exempt")
isBool(t, attrs["allow_ethernet_bridging"], true, "allow_ethernet_bridging")
isBool(t, attrs["no_auto_assign_ips"], true, "no_auto_assign_ips")
if a(attrs["ip_assignments"])[0].(string) != "10.0.0.1" {
t.Fatal("ip_assignments was improperly set")
}
default:
t.Fatalf("Unexpected network member %q in plan", m["name"])
}
}
}
}
func TestBasicNetworkSetup(t *testing.T) {
tf := getTFTest(t)
tf.Apply("testdata/plans/basic-network.tf")
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"] {
// FIXME: missing/failing support for these test cases in the Client API
//
// What should happen when the API is updated so that these items can
// be modified, is that these tests can be uncommented and that they
// will automagically pass, because all the plumbing is already done
// for you, presuming nothing moves, etc.
//
// PLEASE NOTE that the case statements /themselves/ must be left
// available so they are exhausted in before the default statement, which
// will fail the test for unknown networks. This is a safeguard to keep
// extraneous stuff from landing in the test plan.
case "multicast_limit":
// i, ok := attrs["multicast_limit"].(float64)
// if !ok {
// t.Fatal("multicast_limit was not set")
// }
//
// if i != 50 {
// t.Fatalf("multicast_limit was improperly set: %f", i)
// }
case "description":
s, ok := attrs["description"].(string)
if !ok {
t.Fatal("description was not set")
}
if s != "My description is changed!" {
t.Fatalf("description was improperly set")
}
case "flow_rules":
if attrs["flow_rules"].(string) != "drop;" {
t.Fatal("flow_rules were not altered", attrs["flow_rules"])
}
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"], true, "private")
case "no_broadcast":
isBool(t, attrs["enable_broadcast"], false, "enable_broadcast")
case "dns_settings":
if h(a(attrs["dns"])[0])["domain"].(string) != "leisure.town" {
t.Fatal("domain was incorrect")
}
if a(h(a(attrs["dns"])[0])["servers"])[0].(string) != "1.2.3.4" {
t.Fatal("nameserver #1 was incorrect")
}
if a(h(a(attrs["dns"])[0])["servers"])[1].(string) != "5.6.7.8" {
t.Fatal("nameserver #2 was incorrect")
}
case "alice", "bobs_garage":
for _, name := range []string{"creation_time"} {
isNonZeroNum(t, attrs[name], name)
}
isBool(t, attrs["enable_broadcast"], true, "enable_broadcast")
isBool(t, attrs["private"], false, "private")
m, ok := attrs["assign_ipv4"]
if !ok {
t.Fatal("assign_ipv4 key was missing")
}
isBool(t, h(a(m)[0])["zerotier"], true, "assign_ipv4/zerotier")
m, ok = attrs["assign_ipv6"]
if !ok {
t.Fatal("assign_ipv6 key was missing")
}
table := map[string]bool{
"zerotier": false,
"sixplane": false,
"rfc4193": false,
}
for name, val := range table {
isBool(t, h(a(m)[0])[name], val, "assign_ipv6/"+name)
}
if !strings.HasSuffix(strings.TrimSpace(attrs["flow_rules"].(string)), "accept;") {
t.Fatal("flow_rules were not accept by default:", attrs["flow_rules"])
}
// FIXME needs patch to ztcentral
// if f, ok := attrs["last_modified"].(float64); !ok || f == 0 {
// t.Fatal("last modified (on zerotier) for alice network was 0")
// }
default:
t.Fatalf("Unexpected network %q in plan", m["name"])
}
}
}
}