Skip to content

Commit

Permalink
simplify testing
Browse files Browse the repository at this point in the history
  • Loading branch information
eaudetcobello committed Sep 30, 2024
1 parent 6e0899d commit fd5e39d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 53 deletions.
4 changes: 4 additions & 0 deletions src/k8s/pkg/k8sd/types/cluster_config_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func validateIPv6CIDRSize(serviceCIDR string) error {
return fmt.Errorf("invalid CIDR: %w", err)
}

if svcIPv6CIDR == "" {
return nil
}

_, ipv6Net, err := net.ParseCIDR(svcIPv6CIDR)
if err != nil {
return fmt.Errorf("invalid CIDR: %w", err)
Expand Down
65 changes: 12 additions & 53 deletions src/k8s/pkg/k8sd/types/cluster_config_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ import (

func TestValidateCIDR(t *testing.T) {
for _, tc := range []struct {
cidr string
expectErr bool
cidr string
expectPodErr bool
expectSvcErr bool
}{
{cidr: "192.168.0.0/16"},
{cidr: "2001:0db8::/32"},
{cidr: "10.1.0.0/16,2001:0db8::/32"},
{cidr: "", expectErr: true},
{cidr: "bananas", expectErr: true},
{cidr: "fd01::/64,fd02::/64,fd03::/64", expectErr: true},
{cidr: "10.1.0.0/32", expectErr: true},
{cidr: "2001:0db8::/108"},
{cidr: "10.2.0.0/16,2001:0db8::/108"},
{cidr: "", expectPodErr: true, expectSvcErr: true},
{cidr: "bananas", expectPodErr: true, expectSvcErr: true},
{cidr: "fd01::/108,fd02::/108,fd03::/108", expectPodErr: true, expectSvcErr: true},
{cidr: "10.1.0.0/32", expectPodErr: true, expectSvcErr: true},
{cidr: "2001:0db8::/32", expectSvcErr: true},
} {
t.Run(tc.cidr, func(t *testing.T) {
t.Run("Pod", func(t *testing.T) {
Expand All @@ -31,7 +33,7 @@ func TestValidateCIDR(t *testing.T) {
},
}
err := config.Validate()
if tc.expectErr {
if tc.expectPodErr {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).To(BeNil())
Expand All @@ -46,7 +48,7 @@ func TestValidateCIDR(t *testing.T) {
},
}
err := config.Validate()
if tc.expectErr {
if tc.expectSvcErr {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).To(BeNil())
Expand Down Expand Up @@ -129,46 +131,3 @@ func TestValidateExternalServers(t *testing.T) {
})
}
}

func TestValidateCIDROverlap(t *testing.T) {
for _, tc := range []struct {
name string
podCIDR string
serviceCIDR string
expectErr bool
}{
{"RandomName", "nogood", "nogood", true},
{"BothValid", "10.1.0.0/16", "2001:0db8::/108", false},
//IPv4
{"SameIPv4CIDRs", "192.168.100.0/24", "192.168.100.0/24", true},
{"OverlappingIPv4CIDRs", "10.2.0.13/24", "10.2.0.0/24", true},
{"IPv4CIDRMinimumSize", "192.168.0.0/32", "10.0.0.0/24", true},
{"InvalidIPv4CIDRFormat", "192.168.1.1/33", "10.0.0.0/24", true},
{"MaxSizeIPv4CIDRs", "0.0.0.0/0", "0.0.0.0/0", true},
//IPv6
{"SameIPv6CIDRs", "fe80::1/32", "fe80::1/32", true},
{"OverlappingIPv6CIDRs", "fe80::/48", "fe80::dead/48", true},
{"IPv6CIDRMinimumSize", "2001:db8::1/128", "fc00::/32", true},
{"InvalidIPv6CIDRFormat", "2001:db8::1/129", "fc00::/64", true},
{"MaxSizeIPv6CIDRs", "::/0", "::/0", true},
//Mixed
{"IPv4AndIPv6MixedCIDRs", "192.168.0.0/16", "2001:db8::/32", true},
{"OnlyInvalidIPv6CIDR", "", "2001:db8::/65", true},
} {
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)
config := types.ClusterConfig{
Network: types.Network{
PodCIDR: utils.Pointer(tc.podCIDR),
ServiceCIDR: utils.Pointer(tc.serviceCIDR),
},
}
err := config.Validate()
if tc.expectErr {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).To(BeNil())
}
})
}
}

0 comments on commit fd5e39d

Please sign in to comment.