@@ -14,16 +14,85 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
- package cidr_test
17
+ package cidr
18
18
19
19
import (
20
+ "net"
20
21
"testing"
21
22
22
23
. "github.com/onsi/gomega"
23
-
24
- "sigs.k8s.io/cluster-api-provider-aws/pkg/internal/cidr"
25
24
)
26
25
26
+ func TestSplitIntoSubnetsIPv4 (t * testing.T ) {
27
+ RegisterTestingT (t )
28
+ tests := []struct {
29
+ name string
30
+ cidrblock string
31
+ subnetcount int
32
+ expected []* net.IPNet
33
+ }{
34
+ {
35
+ // https://aws.amazon.com/about-aws/whats-new/2018/10/amazon-eks-now-supports-additional-vpc-cidr-blocks/
36
+ name : "default secondary cidr block configuration with primary cidr" ,
37
+ cidrblock : "100.64.0.0/16" ,
38
+ subnetcount : 3 ,
39
+ expected : []* net.IPNet {
40
+ {
41
+ IP : net .IPv4 (100 , 64 , 0 , 0 ).To4 (),
42
+ Mask : net .IPv4Mask (255 , 255 , 192 , 0 ),
43
+ },
44
+ {
45
+ IP : net .IPv4 (100 , 64 , 64 , 0 ).To4 (),
46
+ Mask : net .IPv4Mask (255 , 255 , 192 , 0 ),
47
+ },
48
+ {
49
+ IP : net .IPv4 (100 , 64 , 128 , 0 ).To4 (),
50
+ Mask : net .IPv4Mask (255 , 255 , 192 , 0 ),
51
+ },
52
+ },
53
+ },
54
+ {
55
+ // https://aws.amazon.com/about-aws/whats-new/2018/10/amazon-eks-now-supports-additional-vpc-cidr-blocks/
56
+ name : "default secondary cidr block configuration with alternative cidr" ,
57
+ cidrblock : "198.19.0.0/16" ,
58
+ subnetcount : 3 ,
59
+ expected : []* net.IPNet {
60
+ {
61
+ IP : net .IPv4 (198 , 19 , 0 , 0 ).To4 (),
62
+ Mask : net .IPv4Mask (255 , 255 , 192 , 0 ),
63
+ },
64
+ {
65
+ IP : net .IPv4 (198 , 19 , 64 , 0 ).To4 (),
66
+ Mask : net .IPv4Mask (255 , 255 , 192 , 0 ),
67
+ },
68
+ {
69
+ IP : net .IPv4 (198 , 19 , 128 , 0 ).To4 (),
70
+ Mask : net .IPv4Mask (255 , 255 , 192 , 0 ),
71
+ },
72
+ },
73
+ },
74
+ {
75
+ name : "slash 16 cidr with one subnet" ,
76
+ cidrblock : "1.1.0.0/16" ,
77
+ subnetcount : 1 ,
78
+ expected : []* net.IPNet {
79
+ {
80
+ IP : net .IPv4 (1 , 1 , 0 , 0 ).To4 (),
81
+ Mask : net .IPv4Mask (255 , 255 , 0 , 0 ),
82
+ },
83
+ },
84
+ },
85
+ }
86
+
87
+ for _ , tc := range tests {
88
+ t .Run (tc .name , func (t * testing.T ) {
89
+ output , err := SplitIntoSubnetsIPv4 (tc .cidrblock , tc .subnetcount )
90
+ Expect (err ).NotTo (HaveOccurred ())
91
+ Expect (output ).To (ConsistOf (tc .expected ))
92
+ })
93
+ }
94
+ }
95
+
27
96
func TestParseIPv4CIDR (t * testing.T ) {
28
97
RegisterTestingT (t )
29
98
@@ -33,7 +102,7 @@ func TestParseIPv4CIDR(t *testing.T) {
33
102
"193.168.3.20/7" ,
34
103
}
35
104
36
- output , err := cidr . GetIPv4Cidrs (input )
105
+ output , err := GetIPv4Cidrs (input )
37
106
Expect (err ).NotTo (HaveOccurred ())
38
107
Expect (output ).To (HaveLen (1 ))
39
108
}
@@ -47,7 +116,7 @@ func TestParseIPv6CIDR(t *testing.T) {
47
116
"193.168.3.20/7" ,
48
117
}
49
118
50
- output , err := cidr . GetIPv6Cidrs (input )
119
+ output , err := GetIPv6Cidrs (input )
51
120
Expect (err ).NotTo (HaveOccurred ())
52
121
Expect (output ).To (HaveLen (2 ))
53
122
}
0 commit comments