-
Notifications
You must be signed in to change notification settings - Fork 10
/
ipset_test.go
197 lines (159 loc) · 3.79 KB
/
ipset_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
package ipset
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"
)
func Test_Check(t *testing.T) {
t.Run("ipset path is ready", func(t *testing.T) {
ipsetPath = "I'm ready"
defer func() { ipsetPath = "" }()
assert.Nil(t, Check())
})
t.Run("ipset path is not exist", func(t *testing.T) {
setupLookPath("error")
defer teardownLookPath()
assert.Equal(t, ErrNotFound, Check())
})
t.Run("ipset version error", func(t *testing.T) {
setupLookPath()
defer teardownLookPath()
setupCmd(flag)
defer teardownCmd()
assert.NotNil(t, Check())
})
t.Run("supported version", func(t *testing.T) {
setupLookPath()
defer teardownLookPath()
setupCmd()
defer teardownCmd()
assert.Nil(t, Check())
})
t.Run("non supported version", func(t *testing.T) {
setupLookPath("non-supported")
defer teardownLookPath()
setupCmd()
defer teardownCmd()
assert.Equal(t, ErrVersionNotSupported, Check())
})
}
func Test_GetMajorVersion(t *testing.T) {
t.Parallel()
tt := []struct {
out []byte
v int
}{
{[]byte("no version"), 0},
{[]byte("ipset v5e.rsion"), 0},
{[]byte("ipset v6.29, protocol version: 6"), 6},
{[]byte("ipset v7.1, protocol version: 7"), 7},
{[]byte("ipset v10.0, protocol version: 10"), 10},
{[]byte("Warning: Kernel support protocol versions 6-6 while userspace supports protocol versions 6-7\nipset v7.1, protocol version: 7"), 7},
}
for _, tc := range tt {
assert.Equal(t, tc.v, getMajorVersion(tc.out))
}
}
func Test_New(t *testing.T) {
t.Run("success", func(t *testing.T) {
setupCmd()
defer teardownCmd()
s, err := New("test", HashIp)
assert.Nil(t, err)
assert.NotNil(t, s)
})
t.Run("error", func(t *testing.T) {
setupCmd(flag)
defer teardownCmd()
_, err := New("test", HashIp)
require.Error(t, err)
assert.Equal(t,
fmt.Sprintf("ipset: can't %s %s %s: fake error", _create, "test", HashIp),
err.Error())
})
}
func Test_Flush(t *testing.T) {
t.Run("all", func(t *testing.T) {
t.Run("success", func(t *testing.T) {
setupCmd()
defer teardownCmd()
assert.Nil(t, Flush())
})
t.Run("error", func(t *testing.T) {
setupCmd(flag)
defer teardownCmd()
err := Flush()
require.Error(t, err)
assert.Equal(t,
"ipset: can't flush all set: fake error",
err.Error())
})
})
t.Run("multi", func(t *testing.T) {
t.Run("success", func(t *testing.T) {
setupCmd()
defer teardownCmd()
assert.Nil(t, Flush("a", "b"))
})
t.Run("error", func(t *testing.T) {
setupCmd(flag)
defer teardownCmd()
err := Flush("a", "b")
require.Error(t, err)
assert.Equal(t,
"ipset: can't flush set a: fake error",
err.Error())
})
})
}
func Test_Destroy(t *testing.T) {
t.Run("all", func(t *testing.T) {
t.Run("success", func(t *testing.T) {
setupCmd()
defer teardownCmd()
assert.Nil(t, Destroy())
})
t.Run("error", func(t *testing.T) {
setupCmd(flag)
defer teardownCmd()
err := Destroy()
require.Error(t, err)
assert.Equal(t,
"ipset: can't destroy all set: fake error",
err.Error())
})
})
t.Run("multi", func(t *testing.T) {
t.Run("success", func(t *testing.T) {
setupCmd()
defer teardownCmd()
assert.Nil(t, Destroy("a", "b"))
})
t.Run("error", func(t *testing.T) {
setupCmd(flag)
defer teardownCmd()
err := Destroy("a", "b")
require.Error(t, err)
assert.Equal(t,
"ipset: can't destroy set a: fake error",
err.Error())
})
})
}
func Test_Swap(t *testing.T) {
t.Run("success", func(t *testing.T) {
setupCmd()
defer teardownCmd()
assert.Nil(t, Swap("a", "b"))
})
t.Run("error", func(t *testing.T) {
setupCmd(flag)
defer teardownCmd()
err := Swap("a", "b")
require.Error(t, err)
assert.Equal(t,
"ipset: can't swap from a to b: fake error",
err.Error())
})
}