This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
vlan_assignments_test.go
264 lines (234 loc) · 6.82 KB
/
vlan_assignments_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
package packngo
import (
"encoding/json"
"fmt"
"path"
"strconv"
"strings"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
const (
testAssignmentId = "1f6ca206-da6b-43f6-aa6a-2a0f777d9f16"
testPortId = "a8841b6d-1047-46c2-bd21-51c021f7c844"
testVnId = "91c05d6d-6e6c-4692-9172-da63acdbe30a"
)
func mockAssignedPortBody(assignmentid, portid, vnid string) string {
return fmt.Sprintf(`{
"id":"%s",
"created_at":"2021-05-28T16:02:33Z",
"updated_at":"2021-05-28T16:02:33Z",
"native":false,
"virtual_network":
{"href":"/virtual-networks/%s"},
"port":{"href":"/ports/%s"},
"vlan":1234,
"state":"assigned"
}`, assignmentid, vnid, portid)
}
func TestVLANAssignmentServiceOp_Get(t *testing.T) {
type fields struct {
client requestDoer
}
type args struct {
portID string
assignmentID string
opts *GetOptions
}
tests := []struct {
name string
fields fields
args args
want *VLANAssignment
wantErr bool
}{
{
name: "Simple",
fields: fields{
client: &MockClient{
fnDoRequest: func(method, pathURL string, body, v interface{}) (*Response, error) {
raw := mockAssignedPortBody(testAssignmentId, testPortId, testVnId)
// baseURL is not needed here
expectedPath := path.Join(portBasePath, testPortId, portVLANAssignmentsPath, testAssignmentId)
if expectedPath != pathURL {
return nil, fmt.Errorf("wrong url")
}
if err := json.NewDecoder(strings.NewReader(raw)).Decode(v); err != nil {
return nil, err
}
return mockResponse(200, raw, nil), nil
},
},
},
args: args{portID: testPortId, assignmentID: testAssignmentId},
// TODO: This is testing the code residing inside (json.Decoder).Decode(), which is already exhaustively tested.
// What needs testing is the code residing inside VLANAssignmentServiceOp.Get().
want: &VLANAssignment{
ID: testAssignmentId,
CreatedAt: Timestamp{Time: func() time.Time { t, _ := time.Parse(time.RFC3339, "2021-05-28T16:02:33Z"); return t }()},
UpdatedAt: Timestamp{Time: func() time.Time { t, _ := time.Parse(time.RFC3339, "2021-05-28T16:02:33Z"); return t }()},
VirtualNetwork: &VirtualNetwork{Href: "/virtual-networks/" + testVnId},
Port: &Port{Href: &Href{Href: "/ports/" + testPortId}},
VLAN: 1234,
State: VLANAssignmentAssigned,
},
wantErr: false,
},
{
name: "Error",
fields: fields{
client: &MockClient{
fnDoRequest: func(method, path string, body, v interface{}) (*Response, error) {
return nil, errBoom
},
},
},
args: args{},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &VLANAssignmentServiceOp{
client: tt.fields.client,
}
got, _, err := s.Get(tt.args.portID, tt.args.assignmentID, tt.args.opts)
if (err != nil) != tt.wantErr {
t.Errorf("VLANAssignmentServiceOp.Get() error = %v, wantErr %v", err, tt.wantErr)
return
}
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("VLANAssignmentServiceOp.Get() mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestAccVLANAssignmentServiceOp(t *testing.T) {
skipUnlessAcceptanceTestsAllowed(t)
t.Parallel()
c, projectID, teardown := setupWithProject(t)
defer teardown()
hn := randString8()
metro := testMetro()
cr := DeviceCreateRequest{
Hostname: hn,
Metro: metro,
Plan: testPlan(),
OS: testOS,
ProjectID: projectID,
BillingCycle: "hourly",
}
d, _, err := c.Devices.Create(&cr)
if err != nil {
t.Fatal(err)
}
defer deleteDevice(t, c, d.ID, false)
dID := d.ID
d = waitDeviceActive(t, c, dID)
bond0, err := d.GetPortByName("bond0")
if err != nil {
t.Fatal(err)
}
if len(bond0.AttachedVirtualNetworks) != 0 {
t.Fatal("No vlans should be attached to a eth1 in the beginning of this test")
}
vlans := new([2]*VirtualNetwork)
for i, vxlan := range []int{1222, 1234} {
vncr := VirtualNetworkCreateRequest{
ProjectID: projectID,
Metro: metro,
VXLAN: vxlan,
}
vlans[i], _, err = c.ProjectVirtualNetworks.Create(&vncr)
if err != nil {
t.Fatal(err)
}
defer deleteProjectVirtualNetwork(t, c, vlans[i].ID)
}
// test unassignment and assignment states with both supported formats, VLAN ID and VXLAN
vabcr := &VLANAssignmentBatchCreateRequest{
VLANAssignments: []VLANAssignmentCreateRequest{{
VLAN: vlans[0].ID,
State: VLANAssignmentUnassigned,
}, {
VLAN: strconv.Itoa(vlans[1].VXLAN),
State: VLANAssignmentAssigned,
Native: func() *bool { b := false; return &b }(),
}},
}
b, _, err := c.VLANAssignments.CreateBatch(bond0.ID, vabcr, nil)
if err != nil {
t.Fatal(err)
}
// We must unassign the port when done testing because we can not delete the VLAN when in use
defer func() {
_, _, err := c.Ports.Unassign(bond0.ID, strconv.Itoa(vlans[1].VXLAN))
if err != nil {
t.Log(err)
}
}()
if b.Quantity != 2 {
t.Fatal("Exactly two attachments should be batched")
}
if path.Base(b.Port.Href.Href) != bond0.ID {
t.Fatal("mismatch in the UUID of the assigned Port")
}
// verify batch can be fetched
b2 := waitVLANAssignmentBatch(t, c, bond0.ID, b.ID)
if b2.CreatedAt != b.CreatedAt {
t.Fatal("Reloaded VLANAssignment batch create time should match original response")
}
// verify port vlan assignments can be listed
allAs, _, err := c.VLANAssignments.List(bond0.ID, nil)
if err != nil {
t.Fatal(err)
}
// We do not expect "unassigned" vlan assignments to persist
if len(allAs) != 1 {
t.Fatal("unexpected or missing VLANAssignments in List results")
}
// verify single assignment can be fetched
a2, _, err := c.VLANAssignments.Get(bond0.ID, allAs[0].ID, nil)
if err != nil {
t.Fatal(err)
}
if a2.CreatedAt != allAs[0].CreatedAt {
t.Fatal("Reloaded VLANAssignment create time should match original response")
}
// verify port vlan batch assignments can be listed
allBs, _, err := c.VLANAssignments.ListBatch(bond0.ID, nil)
if err != nil {
t.Fatal(err)
}
unseen := map[string]bool{
b2.ID: true,
}
for _, b := range allBs {
delete(unseen, b.ID)
}
if len(unseen) != 0 {
t.Fatal("unexpected or missing Batch Assignments in ListBatch results")
}
}
func waitVLANAssignmentBatch(t *testing.T, c *Client, portID, id string) *VLANAssignmentBatch {
// 15 minutes = 180 * 5sec-retry
for i := 0; i < 180; i++ {
<-time.After(5 * time.Second)
b, _, err := c.VLANAssignments.GetBatch(portID, id, nil)
if err != nil {
t.Fatal(err)
return nil
}
if b.State == VLANAssignmentBatchCompleted {
return b
}
if b.State == VLANAssignmentBatchFailed {
t.Fatalf("vlan assignment batch %s provisioning failed: %s", id, strings.Join(b.ErrorMessages, "; "))
return nil
}
}
t.Fatal(fmt.Errorf("vlan assignment batch %s is still not complete after timeout", id))
return nil
}