Skip to content

Commit f22d6f6

Browse files
authored
Merge pull request #3686 from luthermonson/cidr-tests
fixing package issues and adding SplitIntoSubnetsIPv4 unit tests
2 parents ab047bf + e7e1146 commit f22d6f6

File tree

1 file changed

+74
-5
lines changed

1 file changed

+74
-5
lines changed

pkg/internal/cidr/cidr_test.go

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,85 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package cidr_test
17+
package cidr
1818

1919
import (
20+
"net"
2021
"testing"
2122

2223
. "github.com/onsi/gomega"
23-
24-
"sigs.k8s.io/cluster-api-provider-aws/pkg/internal/cidr"
2524
)
2625

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+
2796
func TestParseIPv4CIDR(t *testing.T) {
2897
RegisterTestingT(t)
2998

@@ -33,7 +102,7 @@ func TestParseIPv4CIDR(t *testing.T) {
33102
"193.168.3.20/7",
34103
}
35104

36-
output, err := cidr.GetIPv4Cidrs(input)
105+
output, err := GetIPv4Cidrs(input)
37106
Expect(err).NotTo(HaveOccurred())
38107
Expect(output).To(HaveLen(1))
39108
}
@@ -47,7 +116,7 @@ func TestParseIPv6CIDR(t *testing.T) {
47116
"193.168.3.20/7",
48117
}
49118

50-
output, err := cidr.GetIPv6Cidrs(input)
119+
output, err := GetIPv6Cidrs(input)
51120
Expect(err).NotTo(HaveOccurred())
52121
Expect(output).To(HaveLen(2))
53122
}

0 commit comments

Comments
 (0)