Skip to content

Commit 5b8e468

Browse files
cmd/microcloud: Add unit tests for detectCollisions
Signed-off-by: Gabriel Mougard <gabriel.mougard@canonical.com>
1 parent daaaecb commit 5b8e468

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

cmd/microcloud/main_init_test.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package main
22

33
import (
4+
"net"
45
"testing"
56

67
lxdAPI "github.com/canonical/lxd/shared/api"
8+
"github.com/stretchr/testify/assert"
79

810
"github.com/canonical/microcloud/microcloud/multicast"
911
"github.com/canonical/microcloud/microcloud/service"
@@ -226,3 +228,117 @@ func TestValidateSystemsMultiSystem(t *testing.T) {
226228
t.Fatalf("sys4 with conflicting management IP and ipv6.ovn.ranges passed validation")
227229
}
228230
}
231+
232+
func newNetwork(ifaceName, ipStr string) *Network {
233+
ip, subnet, _ := net.ParseCIDR(ipStr)
234+
return &Network{
235+
Interface: net.Interface{Name: ifaceName},
236+
IP: ip,
237+
Subnet: subnet,
238+
}
239+
}
240+
241+
func TestValidateSystemsNetworkCollision(t *testing.T) {
242+
tests := []struct {
243+
name string
244+
systems map[string]InitSystem
245+
expectedWarnings []string
246+
}{
247+
{
248+
name: "no collisions",
249+
systems: map[string]InitSystem{
250+
"system1": {
251+
MicroCephPublicNetwork: newNetwork("eth0", "10.0.1.0/24"),
252+
MicroCephInternalNetwork: newNetwork("eth1", "10.0.2.0/24"),
253+
MicroCloudInternalNetwork: newNetwork("eth2", "10.0.3.0/24"),
254+
OVNGeneveNetwork: newNetwork("eth3", "10.0.4.0/24"),
255+
},
256+
"system2": {
257+
MicroCephPublicNetwork: newNetwork("eth0", "10.1.1.0/24"),
258+
MicroCephInternalNetwork: newNetwork("eth1", "10.1.2.0/24"),
259+
MicroCloudInternalNetwork: newNetwork("eth2", "10.1.3.0/24"),
260+
OVNGeneveNetwork: newNetwork("eth3", "10.1.4.0/24"),
261+
},
262+
},
263+
expectedWarnings: nil,
264+
},
265+
{
266+
name: "single system interface collision",
267+
systems: map[string]InitSystem{
268+
"system1": {
269+
MicroCephInternalNetwork: newNetwork("eth0", "10.0.1.0/24"),
270+
OVNGeneveNetwork: newNetwork("eth0", "10.0.2.0/24"), // Same interface
271+
MicroCloudInternalNetwork: newNetwork("eth1", "10.0.3.0/24"),
272+
},
273+
},
274+
expectedWarnings: []string{
275+
"- Ceph cluster network, OVN underlay sharing network interface \"eth0\"",
276+
},
277+
},
278+
{
279+
name: "single system with multiple interface collisions",
280+
systems: map[string]InitSystem{
281+
"system1": {
282+
MicroCephPublicNetwork: newNetwork("eth0", "10.0.1.0/24"),
283+
MicroCephInternalNetwork: newNetwork("eth0", "10.0.2.0/24"),
284+
OVNGeneveNetwork: newNetwork("eth0", "10.0.3.0/24"),
285+
MicroCloudInternalNetwork: newNetwork("eth0", "10.0.4.0/24"),
286+
},
287+
},
288+
expectedWarnings: []string{
289+
"- Ceph cluster network, Ceph public network, MicroCloud internal network, OVN underlay sharing network interface \"eth0\"",
290+
},
291+
},
292+
{
293+
name: "cross-system subnet collision",
294+
systems: map[string]InitSystem{
295+
"system1": {
296+
MicroCephInternalNetwork: newNetwork("eth0", "10.0.1.0/24"), // Same subnet
297+
OVNGeneveNetwork: newNetwork("eth1", "10.0.2.0/24"),
298+
MicroCloudInternalNetwork: newNetwork("eth2", "10.0.3.0/24"),
299+
},
300+
"system2": {
301+
OVNGeneveNetwork: newNetwork("eth3", "10.0.1.0/24"), // Same subnet for different network type
302+
MicroCloudInternalNetwork: newNetwork("eth4", "10.0.4.0/24"),
303+
},
304+
},
305+
expectedWarnings: []string{
306+
"- Ceph cluster network, OVN underlay sharing subnet \"10.0.1.0/24\"",
307+
},
308+
},
309+
{
310+
name: "mixed interface and cross-subnet collision",
311+
systems: map[string]InitSystem{
312+
"system1": {
313+
MicroCephInternalNetwork: newNetwork("eth0", "10.0.1.0/24"), // Interface and subnet collision
314+
OVNGeneveNetwork: newNetwork("eth0", "10.0.1.0/24"), // Same interface and subnet
315+
MicroCloudInternalNetwork: newNetwork("eth1", "10.0.2.0/24"),
316+
},
317+
"system2": {
318+
OVNGeneveNetwork: newNetwork("eth2", "10.0.1.0/24"), // Same subnet globally
319+
},
320+
},
321+
expectedWarnings: []string{
322+
"- Ceph cluster network, OVN underlay sharing network interface \"eth0\"",
323+
"- Ceph cluster network, OVN underlay sharing subnet \"10.0.1.0/24\"",
324+
},
325+
},
326+
{
327+
name: "ignore systems with missing networks",
328+
systems: map[string]InitSystem{
329+
"system1": { // Incomplete networks (skipped in checks)
330+
OVNGeneveNetwork: newNetwork("eth0", "10.0.1.0/24"),
331+
MicroCloudInternalNetwork: newNetwork("eth1", "10.0.2.0/24"),
332+
},
333+
},
334+
expectedWarnings: nil,
335+
},
336+
}
337+
338+
for _, tt := range tests {
339+
t.Run(tt.name, func(t *testing.T) {
340+
warnings := detectCollisions(tt.systems)
341+
assert.ElementsMatch(t, tt.expectedWarnings, warnings)
342+
})
343+
}
344+
}

0 commit comments

Comments
 (0)