File: util_test.go

package info (click to toggle)
golang-github-containers-common 0.64.2%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,528 kB
  • sloc: makefile: 130; sh: 102
file content (115 lines) | stat: -rw-r--r-- 2,675 bytes parent folder | download | duplicates (2)
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
package util

import (
	"net"
	"reflect"
	"testing"

	"github.com/containers/common/libnetwork/types"
	"github.com/containers/common/pkg/config"
)

func parseIPNet(subnet string) *types.IPNet {
	n, _ := types.ParseCIDR(subnet)
	return &n
}

func parseSubnet(subnet string) *types.Subnet {
	n := parseIPNet(subnet)
	return &types.Subnet{Subnet: *n}
}

func TestGetFreeIPv4NetworkSubnet(t *testing.T) {
	type args struct {
		usedNetworks []*net.IPNet
		subnetPools  []config.SubnetPool
	}
	tests := []struct {
		name    string
		args    args
		want    *types.Subnet
		wantErr bool
	}{
		{
			name: "single subnet pool",
			args: args{
				subnetPools: []config.SubnetPool{
					{Base: parseIPNet("10.89.0.0/16"), Size: 24},
				},
			},
			want: parseSubnet("10.89.0.0/24"),
		},
		{
			name: "single subnet pool with used nets",
			args: args{
				subnetPools: []config.SubnetPool{
					{Base: parseIPNet("10.89.0.0/16"), Size: 24},
				},
				usedNetworks: []*net.IPNet{
					parseCIDR("10.89.0.0/25"),
				},
			},
			want: parseSubnet("10.89.1.0/24"),
		},
		{
			name: "single subnet pool with no free nets",
			args: args{
				subnetPools: []config.SubnetPool{
					{Base: parseIPNet("10.89.0.0/16"), Size: 24},
				},
				usedNetworks: []*net.IPNet{
					parseCIDR("10.89.0.0/16"),
				},
			},
			wantErr: true,
		},
		{
			name: "two subnet pools",
			args: args{
				subnetPools: []config.SubnetPool{
					{Base: parseIPNet("10.89.0.0/16"), Size: 24},
					{Base: parseIPNet("10.90.0.0/16"), Size: 25},
				},
			},
			want: parseSubnet("10.89.0.0/24"),
		},
		{
			name: "two subnet pools with no free subnet in first pool",
			args: args{
				subnetPools: []config.SubnetPool{
					{Base: parseIPNet("10.89.0.0/16"), Size: 24},
					{Base: parseIPNet("10.90.0.0/16"), Size: 25},
				},
				usedNetworks: []*net.IPNet{
					parseCIDR("10.89.0.0/16"),
				},
			},
			want: parseSubnet("10.90.0.0/25"),
		},
		{
			name: "two subnet pools with no free subnet",
			args: args{
				subnetPools: []config.SubnetPool{
					{Base: parseIPNet("10.89.0.0/16"), Size: 24},
					{Base: parseIPNet("10.90.0.0/16"), Size: 25},
				},
				usedNetworks: []*net.IPNet{
					parseCIDR("10.89.0.0/8"),
				},
			},
			wantErr: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := GetFreeIPv4NetworkSubnet(tt.args.usedNetworks, tt.args.subnetPools)
			if (err != nil) != tt.wantErr {
				t.Errorf("GetFreeIPv4NetworkSubnet() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("GetFreeIPv4NetworkSubnet() = %v, want %v", got.Subnet.String(), tt.want.Subnet.String())
			}
		})
	}
}