-
Notifications
You must be signed in to change notification settings - Fork 3
/
fwgroup_test.go
142 lines (121 loc) · 3.52 KB
/
fwgroup_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
package lochness_test
import (
"encoding/json"
"net"
"testing"
"github.com/mistifyio/lochness"
"github.com/mistifyio/lochness/internal/tests/common"
"github.com/pborman/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
func TestFWGroup(t *testing.T) {
suite.Run(t, new(FWGroupSuite))
}
type FWGroupSuite struct {
common.Suite
}
func (s *FWGroupSuite) TestNewFWGroup() {
fw := s.Context.NewFWGroup()
s.NotEmpty(uuid.Parse(fw.ID))
}
func (s *FWGroupSuite) TestFWGroup() {
fwgroup := s.NewFWGroup()
tests := []struct {
description string
id string
expectedErr bool
}{
{"missing id", "", true},
{"invalid id", "asdf", true},
{"nonexistant id", uuid.New(), true},
{"real id", fwgroup.ID, false},
}
for _, test := range tests {
msg := s.Messager(test.description)
f, err := s.Context.FWGroup(test.id)
if test.expectedErr {
s.Error(err, msg("lookup should fail"))
s.Nil(f, msg("failure shouldn't return a fwgroup"))
} else {
s.NoError(err, msg("lookup should succeed"))
// For some reason, asser.ObjectsAreEqual fails here
s.Equal(fwgroup.ID, f.ID, msg("success should pull correct id"))
s.Len(f.Rules, len(fwgroup.Rules), msg("success should pull the rules"))
}
}
}
func (s *FWGroupSuite) TestRefresh() {
fwgroup := s.NewFWGroup()
fwgroupCopy := &lochness.FWGroup{}
*fwgroupCopy = *fwgroup
fwgroup.Rules = lochness.FWRules{&lochness.FWRule{}}
_ = fwgroup.Save()
s.NoError(fwgroupCopy.Refresh(), "refresh existing should succeed")
// For some reason, asser.ObjectsAreEqual fails here
s.Equal(fwgroup.ID, fwgroupCopy.ID, "should pull correct id")
s.Len(fwgroupCopy.Rules, len(fwgroup.Rules), "should pull the rules")
newFWGroup := s.Context.NewFWGroup()
s.Error(newFWGroup.Refresh(), "unsaved fwgroup refresh should fail")
}
func (s *FWGroupSuite) TestValidate() {
tests := []struct {
description string
ID string
expectedErr bool
}{
{"missing ID", "", true},
{"non uuid ID", "asdf", true},
{"uuid ID", uuid.New(), false},
}
for _, test := range tests {
msg := s.Messager(test.description)
fg := &lochness.FWGroup{ID: test.ID}
err := fg.Validate()
if test.expectedErr {
s.Error(err, msg("should be invalid"))
} else {
s.NoError(err, msg("should be valid"))
}
}
}
func (s *FWGroupSuite) TestSave() {
goodFWGroup := s.Context.NewFWGroup()
clobberFWGroup := *goodFWGroup
tests := []struct {
description string
fwgroup *lochness.FWGroup
expectedErr bool
}{
{"valid fwgroup", goodFWGroup, false},
{"existing fwgroup", goodFWGroup, false},
{"existing fwgroup clobber changes", &clobberFWGroup, true},
}
for _, test := range tests {
msg := s.Messager(test.description)
err := test.fwgroup.Save()
if test.expectedErr {
s.Error(err, msg("should be invalid"))
} else {
s.NoError(err, msg("should be valid"))
}
}
}
func (s *FWGroupSuite) TestJSON() {
fwgroup := s.Context.NewFWGroup()
_, n, _ := net.ParseCIDR("192.168.100.1/16")
fwrule := &lochness.FWRule{
Source: n,
PortStart: 1000,
PortEnd: 2000,
}
fwgroup.Rules = lochness.FWRules{fwrule}
fwgroupBytes, err := json.Marshal(fwgroup)
s.NoError(err)
fwgroupFromJSON := &lochness.FWGroup{}
s.NoError(json.Unmarshal(fwgroupBytes, fwgroupFromJSON))
// For some reason, asser.ObjectsAreEqual fails here
s.Equal(fwgroup.ID, fwgroupFromJSON.ID, "should pull correct id")
s.Len(fwgroupFromJSON.Rules, len(fwgroup.Rules), "should pull the rules")
s.True(assert.ObjectsAreEqual(fwrule, fwgroupFromJSON.Rules[0]), "rules should be equal")
}