-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip_slice_test.go
146 lines (111 loc) · 3.72 KB
/
ip_slice_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
package dynflags_test
import (
"net"
"testing"
"github.com/containeroo/dynflags"
"github.com/stretchr/testify/assert"
)
func TestIPSlicesValue(t *testing.T) {
t.Parallel()
t.Run("Parse valid IP", func(t *testing.T) {
t.Parallel()
ipSlicesValue := dynflags.IPSlicesValue{Bound: &[]net.IP{}}
parsed, err := ipSlicesValue.Parse("192.168.0.1")
assert.NoError(t, err)
assert.Equal(t, net.ParseIP("192.168.0.1"), parsed)
})
t.Run("Parse invalid IP", func(t *testing.T) {
t.Parallel()
ipSlicesValue := dynflags.IPSlicesValue{Bound: &[]net.IP{}}
parsed, err := ipSlicesValue.Parse("invalid-ip")
assert.Error(t, err)
assert.Nil(t, parsed)
})
t.Run("Set valid IP", func(t *testing.T) {
t.Parallel()
bound := []net.IP{net.ParseIP("192.168.0.1")}
ipSlicesValue := dynflags.IPSlicesValue{Bound: &bound}
err := ipSlicesValue.Set(net.ParseIP("10.0.0.1"))
assert.NoError(t, err)
assert.Equal(t, []net.IP{net.ParseIP("192.168.0.1"), net.ParseIP("10.0.0.1")}, bound)
})
t.Run("Set invalid type", func(t *testing.T) {
t.Parallel()
bound := []net.IP{}
ipSlicesValue := dynflags.IPSlicesValue{Bound: &bound}
err := ipSlicesValue.Set("invalid-ip-type")
assert.Error(t, err)
assert.EqualError(t, err, "invalid value type: expected net.IP")
})
}
func TestGroupConfigIPSlices(t *testing.T) {
t.Parallel()
t.Run("Define IP slices flag", func(t *testing.T) {
t.Parallel()
group := &dynflags.ConfigGroup{Flags: make(map[string]*dynflags.Flag)}
defaultValue := []net.IP{net.ParseIP("192.168.0.1"), net.ParseIP("10.0.0.1")}
group.IPSlices("ipSliceFlag", defaultValue, "An IP slices flag")
assert.Contains(t, group.Flags, "ipSliceFlag")
assert.Equal(t, "An IP slices flag", group.Flags["ipSliceFlag"].Usage)
assert.Equal(t, "192.168.0.1,10.0.0.1", group.Flags["ipSliceFlag"].Default)
})
}
func TestGetIPSlices(t *testing.T) {
t.Parallel()
t.Run("Retrieve []net.IP value", func(t *testing.T) {
t.Parallel()
ip1 := net.ParseIP("192.168.1.1")
ip2 := net.ParseIP("10.0.0.1")
parsedGroup := &dynflags.ParsedGroup{
Name: "testGroup",
Values: map[string]interface{}{"flag1": []net.IP{ip1, ip2}},
}
result, err := parsedGroup.GetIPSlices("flag1")
assert.NoError(t, err)
assert.Equal(t, []net.IP{ip1, ip2}, result)
})
t.Run("Retrieve single net.IP value as []net.IP", func(t *testing.T) {
t.Parallel()
ip := net.ParseIP("127.0.0.1")
parsedGroup := &dynflags.ParsedGroup{
Name: "testGroup",
Values: map[string]interface{}{"flag1": ip},
}
result, err := parsedGroup.GetIPSlices("flag1")
assert.NoError(t, err)
assert.Equal(t, []net.IP{ip}, result)
})
t.Run("Flag not found", func(t *testing.T) {
t.Parallel()
parsedGroup := &dynflags.ParsedGroup{
Name: "testGroup",
Values: map[string]interface{}{},
}
result, err := parsedGroup.GetIPSlices("nonExistentFlag")
assert.Error(t, err)
assert.Nil(t, result)
assert.EqualError(t, err, "flag 'nonExistentFlag' not found in group 'testGroup'")
})
t.Run("Flag value is invalid type", func(t *testing.T) {
t.Parallel()
parsedGroup := &dynflags.ParsedGroup{
Name: "testGroup",
Values: map[string]interface{}{"flag1": "invalid"},
}
result, err := parsedGroup.GetIPSlices("flag1")
assert.Error(t, err)
assert.Nil(t, result)
assert.EqualError(t, err, "flag 'flag1' is not a []net.IP")
})
}
func TestIPSlicesGetBound(t *testing.T) {
t.Run("IPSlicesValue - GetBound", func(t *testing.T) {
var slices *[]net.IP
val := []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("10.0.0.1")}
slices = &val
ipSlicesValue := dynflags.IPSlicesValue{Bound: slices}
assert.Equal(t, val, ipSlicesValue.GetBound())
ipSlicesValue = dynflags.IPSlicesValue{Bound: nil}
assert.Nil(t, ipSlicesValue.GetBound())
})
}