Skip to content

Commit 9eec604

Browse files
authored
chore(test): add tests to network utils and validations (#2228)
* chore(test): add tests to network utils and validations * chore(linter): no idea why this wans't caught
1 parent 7f68707 commit 9eec604

File tree

10 files changed

+1291
-54
lines changed

10 files changed

+1291
-54
lines changed

api/pkg/utils/netutils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (n *netUtils) ListValidNetworkInterfaces() ([]string, error) {
2727

2828
names := []string{}
2929
for _, i := range ifs {
30-
names = append(names, i.Name)
30+
names = append(names, i.Name())
3131
}
3232
return names, nil
3333
}

pkg-new/config/cidr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cli
1+
package config
22

33
import (
44
"fmt"

pkg-new/config/cidr_test.go

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
package config
2+
3+
import (
4+
"net"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestValidateCIDR(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
cidr string
15+
wantErr bool
16+
}{
17+
{
18+
name: "valid CIDR /16",
19+
cidr: "10.0.0.0/16",
20+
wantErr: false,
21+
},
22+
{
23+
name: "valid CIDR /8",
24+
cidr: "10.0.0.0/8",
25+
wantErr: false,
26+
},
27+
{
28+
name: "invalid CIDR - small subnet /24",
29+
cidr: "192.168.1.0/24",
30+
wantErr: true,
31+
},
32+
{
33+
name: "invalid CIDR - non private subnet",
34+
cidr: "4.0.0.0/8",
35+
wantErr: true,
36+
},
37+
{
38+
name: "invalid CIDR - empty string",
39+
cidr: "",
40+
wantErr: true,
41+
},
42+
{
43+
name: "invalid CIDR - not a CIDR",
44+
cidr: "not-a-cidr",
45+
wantErr: true,
46+
},
47+
{
48+
name: "invalid CIDR - missing prefix",
49+
cidr: "10.0.0.0",
50+
wantErr: true,
51+
},
52+
{
53+
name: "invalid CIDR - invalid IP",
54+
cidr: "999.999.999.999/16",
55+
wantErr: true,
56+
},
57+
{
58+
name: "invalid CIDR - negative prefix",
59+
cidr: "10.0.0.0/-1",
60+
wantErr: true,
61+
},
62+
{
63+
name: "invalid CIDR - prefix too large",
64+
cidr: "10.0.0.0/33",
65+
wantErr: true,
66+
},
67+
}
68+
69+
for _, tt := range tests {
70+
t.Run(tt.name, func(t *testing.T) {
71+
err := ValidateCIDR(tt.cidr)
72+
if tt.wantErr {
73+
assert.Error(t, err)
74+
} else {
75+
assert.NoError(t, err)
76+
}
77+
})
78+
}
79+
}
80+
81+
func TestSplitCIDR(t *testing.T) {
82+
tests := []struct {
83+
name string
84+
cidr string
85+
wantPodCIDR string
86+
wantServiceCIDR string
87+
wantErr bool
88+
}{
89+
{
90+
name: "valid CIDR /16",
91+
cidr: "10.0.0.0/16",
92+
wantPodCIDR: "10.0.0.0/17",
93+
wantServiceCIDR: "10.0.128.0/17",
94+
wantErr: false,
95+
},
96+
{
97+
name: "valid CIDR /8",
98+
cidr: "10.0.0.0/8",
99+
wantPodCIDR: "10.0.0.0/9",
100+
wantServiceCIDR: "10.128.0.0/9",
101+
wantErr: false,
102+
},
103+
{
104+
name: "invalid CIDR - empty string",
105+
cidr: "",
106+
wantPodCIDR: "",
107+
wantServiceCIDR: "",
108+
wantErr: true,
109+
},
110+
{
111+
name: "invalid CIDR - not a CIDR",
112+
cidr: "not-a-cidr",
113+
wantPodCIDR: "",
114+
wantServiceCIDR: "",
115+
wantErr: true,
116+
},
117+
{
118+
name: "invalid CIDR - cannot split /32",
119+
cidr: "10.0.0.1/32",
120+
wantPodCIDR: "",
121+
wantServiceCIDR: "",
122+
wantErr: true,
123+
},
124+
}
125+
126+
for _, tt := range tests {
127+
t.Run(tt.name, func(t *testing.T) {
128+
gotPodCIDR, gotServiceCIDR, err := SplitCIDR(tt.cidr)
129+
if tt.wantErr {
130+
require.Error(t, err)
131+
return
132+
}
133+
require.NoError(t, err)
134+
assert.Equal(t, tt.wantPodCIDR, gotPodCIDR)
135+
assert.Equal(t, tt.wantServiceCIDR, gotServiceCIDR)
136+
})
137+
}
138+
}
139+
140+
func TestCleanCIDR(t *testing.T) {
141+
tests := []struct {
142+
name string
143+
input string
144+
want string
145+
wantErr bool
146+
}{
147+
{
148+
name: "clean CIDR with host bits set",
149+
input: "192.168.1.5/24",
150+
want: "192.168.1.0/24",
151+
wantErr: false,
152+
},
153+
{
154+
name: "clean CIDR with host bits set /16",
155+
input: "10.0.50.100/16",
156+
want: "10.0.0.0/16",
157+
wantErr: false,
158+
},
159+
{
160+
name: "already clean CIDR",
161+
input: "192.168.0.0/24",
162+
want: "192.168.0.0/24",
163+
wantErr: false,
164+
},
165+
{
166+
name: "clean CIDR /8",
167+
input: "10.50.100.200/8",
168+
want: "10.0.0.0/8",
169+
wantErr: false,
170+
},
171+
{
172+
name: "clean CIDR /32",
173+
input: "192.168.1.5/32",
174+
want: "192.168.1.5/32",
175+
wantErr: false,
176+
},
177+
}
178+
179+
for _, tt := range tests {
180+
t.Run(tt.name, func(t *testing.T) {
181+
// Parse the input to create an IPNet
182+
_, ipnet, err := net.ParseCIDR(tt.input)
183+
require.NoError(t, err, "Failed to parse test input CIDR %q", tt.input)
184+
185+
got, err := cleanCIDR(ipnet)
186+
if tt.wantErr {
187+
require.Error(t, err)
188+
return
189+
}
190+
require.NoError(t, err)
191+
assert.Equal(t, tt.want, got)
192+
})
193+
}
194+
}

pkg-new/config/network_interface.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
package cli
1+
package config
22

33
import (
44
"fmt"
55
"net"
66

7+
"github.com/replicatedhq/embedded-cluster/pkg/netutils"
78
apimachinerynet "k8s.io/apimachinery/pkg/util/net"
89
)
910

11+
// Dependency injection variables for testing
12+
var (
13+
chooseHostInterface = apimachinerynet.ChooseHostInterface
14+
networkInterfaceProvider = netutils.DefaultNetworkInterfaceProvider
15+
)
16+
1017
var (
1118
ErrNoAutoInterface = fmt.Errorf("no auto interface found")
1219
ErrBestInterfaceWas6 = fmt.Errorf("best interface was IPv6")
@@ -15,7 +22,7 @@ var (
1522

1623
// DetermineBestNetworkInterface attempts to determine the best network interface to use for the cluster.
1724
func DetermineBestNetworkInterface() (string, error) {
18-
iface, err := apimachinerynet.ChooseHostInterface()
25+
iface, err := chooseHostInterface()
1926

2027
if err != nil || iface == nil {
2128
return "", ErrNoAutoInterface
@@ -34,15 +41,15 @@ func DetermineBestNetworkInterface() (string, error) {
3441
}
3542

3643
func findInterfaceNameByIP(ip net.IP) (string, error) {
37-
interfaces, err := net.Interfaces()
44+
interfaces, err := networkInterfaceProvider.Interfaces()
3845
if err != nil {
3946
return "", fmt.Errorf("failed to list interfaces: %v", err)
4047
}
4148

4249
for _, iface := range interfaces {
4350
addrs, err := iface.Addrs()
4451
if err != nil {
45-
return "", fmt.Errorf("failed to get addresses for interface %s: %v", iface.Name, err)
52+
return "", fmt.Errorf("failed to get addresses for interface %s: %v", iface.Name(), err)
4653
}
4754

4855
for _, addr := range addrs {
@@ -55,7 +62,7 @@ func findInterfaceNameByIP(ip net.IP) (string, error) {
5562
}
5663

5764
if ifaceIP != nil && ifaceIP.Equal(ip) {
58-
return iface.Name, nil
65+
return iface.Name(), nil
5966
}
6067
}
6168
}

0 commit comments

Comments
 (0)