forked from cloudscale-ch/cloudscale-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subnets_test.go
162 lines (148 loc) · 4 KB
/
subnets_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
package cloudscale
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestSubnets_Get(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v1/subnets/cfde831a-4e87-4a75-960f-89b0148aa2cc", func(w http.ResponseWriter, r *http.Request) {
testHTTPMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"uuid": "cfde831a-4e87-4a75-960f-89b0148aa2cc"}`)
})
subnet, err := client.Subnets.Get(ctx, "cfde831a-4e87-4a75-960f-89b0148aa2cc")
if err != nil {
t.Errorf("Subnets.Get returned error: %v", err)
}
expected := &Subnet{UUID: "cfde831a-4e87-4a75-960f-89b0148aa2cc"}
if !reflect.DeepEqual(subnet, expected) {
t.Errorf("Subnets.Get\n got=%#v\nwant=%#v", subnet, expected)
}
}
func TestSubnets_List(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v1/subnets", func(w http.ResponseWriter, r *http.Request) {
testHTTPMethod(t, r, http.MethodGet)
fmt.Fprint(w, `[{"uuid": "47cec963-fcd2-482f-bdb6-24461b2d47b1"}]`)
})
subnets, err := client.Subnets.List(ctx)
if err != nil {
t.Errorf("Subnets.List returned error: %v", err)
}
expected := []Subnet{{UUID: "47cec963-fcd2-482f-bdb6-24461b2d47b1"}}
if !reflect.DeepEqual(subnets, expected) {
t.Errorf("Subnets.List\n got=%#v\nwant=%#v", subnets, expected)
}
}
func TestMarshalingOfDNSServersInSubnetUpdateRequest(t *testing.T) {
testCases := []struct {
name string
request SubnetUpdateRequest
expected string // This tests the value sent to the API, this is not the expected value returned on the subnet.
}{
{
name: "one dns server",
request: SubnetUpdateRequest{
DNSServers: &[]string{"8.8.8.8"},
},
expected: "{\"dns_servers\":[\"8.8.8.8\"]}",
},
{
name: "two dns servers",
request: SubnetUpdateRequest{
DNSServers: &[]string{"8.8.8.8", "8.8.4.4"},
},
expected: "{\"dns_servers\":[\"8.8.8.8\",\"8.8.4.4\"]}",
},
{
name: "no dns servers",
request: SubnetUpdateRequest{
DNSServers: &[]string{},
},
expected: "{\"dns_servers\":[]}",
},
{
name: "defaults",
request: SubnetUpdateRequest{
DNSServers: &UseCloudscaleDefaults,
},
expected: "{\"dns_servers\":null}",
},
{
name: "gateway",
request: SubnetUpdateRequest{
GatewayAddress: "192.168.1.1",
},
expected: "{\"gateway_address\":\"192.168.1.1\"}",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
b, err := json.Marshal(tc.request)
if err != nil {
t.Errorf("Error marshaling JSON: %v", err)
}
if actualOutput := string(b); actualOutput != tc.expected {
t.Errorf("Unexpected JSON output:\nExpected: %s\nActual: %s", tc.expected, actualOutput)
}
})
}
}
func TestMarshalingOfDNSServersInSubnetSubnetCreateRequest(t *testing.T) {
testCases := []struct {
name string
request SubnetCreateRequest
expected string // This tests the value sent to the API, this is not the expected value returned on the subnet.
}{
{
name: "one dns server",
request: SubnetCreateRequest{
DNSServers: &[]string{"8.8.8.8"},
},
expected: "{\"dns_servers\":[\"8.8.8.8\"]}",
},
{
name: "two dns servers",
request: SubnetCreateRequest{
DNSServers: &[]string{"8.8.8.8", "8.8.4.4"},
},
expected: "{\"dns_servers\":[\"8.8.8.8\",\"8.8.4.4\"]}",
},
{
name: "no dns servers",
request: SubnetCreateRequest{
DNSServers: &[]string{},
},
expected: "{\"dns_servers\":[]}",
},
{
name: "defaults",
request: SubnetCreateRequest{
DNSServers: &UseCloudscaleDefaults,
},
expected: "{\"dns_servers\":null}",
},
{
name: "gateway",
request: SubnetCreateRequest{
GatewayAddress: "192.168.1.1",
},
expected: "{\"gateway_address\":\"192.168.1.1\"}",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
b, err := json.Marshal(tc.request)
if err != nil {
t.Errorf("Error marshaling JSON: %v", err)
}
if actualOutput := string(b); actualOutput != tc.expected {
t.Errorf("Unexpected JSON output:\nExpected: %s\nActual: %s", tc.expected, actualOutput)
}
})
}
}