-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvlan_test.go
179 lines (152 loc) · 3.78 KB
/
vlan_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
package lochness_test
import (
"errors"
"testing"
"github.com/mistifyio/lochness"
"github.com/mistifyio/lochness/internal/tests/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
func TestVLAN(t *testing.T) {
suite.Run(t, new(VLANSuite))
}
type VLANSuite struct {
common.Suite
}
func (s *VLANSuite) TestNewVLAN() {
vlan := s.Context.NewVLAN()
s.Equal(1, vlan.Tag)
}
func (s *VLANSuite) TestVLAN() {
vlan := s.NewVLAN()
tests := []struct {
description string
tag int
expectedErr bool
}{
{"invalid tag", -1, true},
{"nonexistant tag", 1000, true},
{"real tag", vlan.Tag, false},
}
for _, test := range tests {
msg := s.Messager(test.description)
v, err := s.Context.VLAN(test.tag)
if test.expectedErr {
s.Error(err, msg("lookup should fail"))
s.Nil(v, msg("failure shouldn't return a vlan"))
} else {
s.NoError(err, msg("lookup should succeed"))
s.True(assert.ObjectsAreEqual(vlan, v), msg("success should return correct data"))
}
}
}
func (s *VLANSuite) TestRefresh() {
vlan := s.NewVLAN()
vlanCopy := &lochness.VLAN{}
*vlanCopy = *vlan
_ = vlan.Save()
s.NoError(vlanCopy.Refresh(), "refresh existing should succeed")
s.True(assert.ObjectsAreEqual(vlan, vlanCopy), "refresh should pull new data")
NewVLAN := s.Context.NewVLAN()
NewVLAN.Tag = 50
s.Error(NewVLAN.Refresh(), "unsaved vlan refresh should fail")
}
func (s *VLANSuite) TestValidate() {
tests := []struct {
description string
tag int
expectedErr bool
}{
{"negative tag", -1, true},
{"zero tag", 0, true},
{"min tag", 1, false},
{"max tag", 4095, false},
{"above max", 4096, true},
}
for _, test := range tests {
msg := s.Messager(test.description)
v := &lochness.VLAN{Tag: test.tag}
err := v.Validate()
if test.expectedErr {
s.Error(err, msg("should be invalid"))
} else {
s.NoError(err, msg("should be valid"))
}
}
}
func (s *VLANSuite) TestSave() {
goodVLAN := s.Context.NewVLAN()
clobberVLAN := *goodVLAN
tests := []struct {
description string
vlan *lochness.VLAN
expectedErr bool
}{
{"invalid vlan", &lochness.VLAN{}, true},
{"valid vlan", goodVLAN, false},
{"existing vlan", goodVLAN, false},
{"existing vlan clobber", &clobberVLAN, true},
}
for _, test := range tests {
msg := s.Messager(test.description)
err := test.vlan.Save()
if test.expectedErr {
s.Error(err, msg("should be invalid"))
} else {
s.NoError(err, msg("should be valid"))
}
}
}
func (s *VLANSuite) TestDestroy() {
vlan := s.NewVLAN()
vlangroup := s.NewVLANGroup()
_ = vlangroup.AddVLAN(vlan)
invalidV := s.Context.NewVLAN()
invalidV.Tag = 0
tests := []struct {
description string
v *lochness.VLAN
expectChange bool
}{
{"invalid vlan", invalidV, false},
{"existing vlan", vlan, false},
{"nonexistant vlan", s.Context.NewVLAN(), true},
}
for _, test := range tests {
msg := s.Messager(test.description)
err := test.v.Destroy()
s.NoError(err, msg("should not error"))
if !test.expectChange {
continue
}
_ = vlangroup.Refresh()
s.Len(vlangroup.VLANs(), 0, msg("should remove vlan link"))
}
}
func (s *VLANSuite) TestForEachVLAN() {
vlan := s.NewVLAN()
vlan2 := s.NewVLAN()
expectedFound := map[int]bool{
vlan.Tag: true,
vlan2.Tag: true,
}
resultFound := make(map[int]bool)
err := s.Context.ForEachVLAN(func(v *lochness.VLAN) error {
resultFound[v.Tag] = true
return nil
})
s.NoError(err)
s.True(assert.ObjectsAreEqual(expectedFound, resultFound))
returnErr := errors.New("an error")
err = s.Context.ForEachVLAN(func(v *lochness.VLAN) error {
return returnErr
})
s.Error(err)
s.Equal(returnErr, err)
}
func (s *VLANSuite) TestVLANGroups() {
vlanGroup := s.NewVLANGroup()
vlan := s.NewVLAN()
_ = vlanGroup.AddVLAN(vlan)
s.Len(vlan.VLANGroups(), 1)
}