|
| 1 | +/* |
| 2 | +Copyright 2021 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package v1alpha4 |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + . "github.com/onsi/gomega" |
| 23 | +) |
| 24 | + |
| 25 | +func TestClusterIPFamily(t *testing.T) { |
| 26 | + clusterWithNetwork := func(podCIDRs, serviceCIDRs []string) *Cluster { |
| 27 | + return &Cluster{ |
| 28 | + Spec: ClusterSpec{ |
| 29 | + ClusterNetwork: &ClusterNetwork{ |
| 30 | + Pods: &NetworkRanges{ |
| 31 | + CIDRBlocks: podCIDRs, |
| 32 | + }, |
| 33 | + Services: &NetworkRanges{ |
| 34 | + CIDRBlocks: serviceCIDRs, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + validAndUnambiguous := []struct { |
| 42 | + name string |
| 43 | + expectRes ClusterIPFamily |
| 44 | + c *Cluster |
| 45 | + }{ |
| 46 | + { |
| 47 | + name: "pods: ipv4, services: ipv4", |
| 48 | + expectRes: IPv4IPFamily, |
| 49 | + c: clusterWithNetwork([]string{"192.168.0.0/16"}, []string{"10.128.0.0/12"}), |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "pods: ipv4, services: nil", |
| 53 | + expectRes: IPv4IPFamily, |
| 54 | + c: clusterWithNetwork([]string{"192.168.0.0/16"}, nil), |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "pods: ipv6, services: nil", |
| 58 | + expectRes: IPv6IPFamily, |
| 59 | + c: clusterWithNetwork([]string{"fd00:100:96::/48"}, nil), |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "pods: ipv6, services: ipv6", |
| 63 | + expectRes: IPv6IPFamily, |
| 64 | + c: clusterWithNetwork([]string{"fd00:100:96::/48"}, []string{"fd00:100:64::/108"}), |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "pods: dual-stack, services: nil", |
| 68 | + expectRes: DualStackIPFamily, |
| 69 | + c: clusterWithNetwork([]string{"192.168.0.0/16", "fd00:100:96::/48"}, nil), |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "pods: dual-stack, services: ipv4", |
| 73 | + expectRes: DualStackIPFamily, |
| 74 | + c: clusterWithNetwork([]string{"192.168.0.0/16", "fd00:100:96::/48"}, []string{"10.128.0.0/12"}), |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "pods: dual-stack, services: ipv6", |
| 78 | + expectRes: DualStackIPFamily, |
| 79 | + c: clusterWithNetwork([]string{"192.168.0.0/16", "fd00:100:96::/48"}, []string{"fd00:100:64::/108"}), |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "pods: dual-stack, services: dual-stack", |
| 83 | + expectRes: DualStackIPFamily, |
| 84 | + c: clusterWithNetwork([]string{"192.168.0.0/16", "fd00:100:96::/48"}, []string{"10.128.0.0/12", "fd00:100:64::/108"}), |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "pods: nil, services: dual-stack", |
| 88 | + expectRes: DualStackIPFamily, |
| 89 | + c: clusterWithNetwork(nil, []string{"10.128.0.0/12", "fd00:100:64::/108"}), |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + for _, tt := range validAndUnambiguous { |
| 94 | + t.Run(tt.name, func(t *testing.T) { |
| 95 | + g := NewWithT(t) |
| 96 | + ipFamily, err := tt.c.GetIPFamily() |
| 97 | + g.Expect(ipFamily).To(Equal(tt.expectRes)) |
| 98 | + g.Expect(err).NotTo(HaveOccurred()) |
| 99 | + }) |
| 100 | + } |
| 101 | + |
| 102 | + validButAmbiguous := []struct { |
| 103 | + name string |
| 104 | + expectRes ClusterIPFamily |
| 105 | + c *Cluster |
| 106 | + }{ |
| 107 | + { |
| 108 | + name: "pods: nil, services: nil", |
| 109 | + // this could be ipv4, ipv6, or dual-stack; assume ipv4 for now though |
| 110 | + expectRes: IPv4IPFamily, |
| 111 | + c: clusterWithNetwork(nil, nil), |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "pods: nil, services: ipv4", |
| 115 | + // this could be a dual-stack; assume ipv4 for now though |
| 116 | + expectRes: IPv4IPFamily, |
| 117 | + c: clusterWithNetwork(nil, []string{"10.128.0.0/12"}), |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "pods: nil, services: ipv6", |
| 121 | + // this could be dual-stack; assume ipv6 for now though |
| 122 | + expectRes: IPv6IPFamily, |
| 123 | + c: clusterWithNetwork(nil, []string{"fd00:100:64::/108"}), |
| 124 | + }, |
| 125 | + } |
| 126 | + |
| 127 | + for _, tt := range validButAmbiguous { |
| 128 | + t.Run(tt.name, func(t *testing.T) { |
| 129 | + g := NewWithT(t) |
| 130 | + ipFamily, err := tt.c.GetIPFamily() |
| 131 | + g.Expect(ipFamily).To(Equal(tt.expectRes)) |
| 132 | + g.Expect(err).NotTo(HaveOccurred()) |
| 133 | + }) |
| 134 | + } |
| 135 | + |
| 136 | + invalid := []struct { |
| 137 | + name string |
| 138 | + expectErr string |
| 139 | + c *Cluster |
| 140 | + }{ |
| 141 | + { |
| 142 | + name: "pods: ipv4, services: ipv6", |
| 143 | + expectErr: "pods and services IP family mismatch", |
| 144 | + c: clusterWithNetwork([]string{"192.168.0.0/16"}, []string{"fd00:100:64::/108"}), |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "pods: ipv6, services: ipv4", |
| 148 | + expectErr: "pods and services IP family mismatch", |
| 149 | + c: clusterWithNetwork([]string{"fd00:100:96::/48"}, []string{"10.128.0.0/12"}), |
| 150 | + }, |
| 151 | + { |
| 152 | + name: "pods: ipv6, services: dual-stack", |
| 153 | + expectErr: "pods and services IP family mismatch", |
| 154 | + c: clusterWithNetwork([]string{"fd00:100:96::/48"}, []string{"10.128.0.0/12", "fd00:100:64::/108"}), |
| 155 | + }, |
| 156 | + { |
| 157 | + name: "pods: ipv4, services: dual-stack", |
| 158 | + expectErr: "pods and services IP family mismatch", |
| 159 | + c: clusterWithNetwork([]string{"192.168.0.0/16"}, []string{"10.128.0.0/12", "fd00:100:64::/108"}), |
| 160 | + }, |
| 161 | + { |
| 162 | + name: "pods: ipv4, services: dual-stack", |
| 163 | + expectErr: "pods and services IP family mismatch", |
| 164 | + c: clusterWithNetwork([]string{"192.168.0.0/16"}, []string{"10.128.0.0/12", "fd00:100:64::/108"}), |
| 165 | + }, |
| 166 | + { |
| 167 | + name: "pods: bad cidr", |
| 168 | + expectErr: "pods: could not parse CIDR", |
| 169 | + c: clusterWithNetwork([]string{"foo"}, nil), |
| 170 | + }, |
| 171 | + { |
| 172 | + name: "services: bad cidr", |
| 173 | + expectErr: "services: could not parse CIDR", |
| 174 | + c: clusterWithNetwork([]string{"192.168.0.0/16"}, []string{"foo"}), |
| 175 | + }, |
| 176 | + { |
| 177 | + name: "pods: too many cidrs", |
| 178 | + expectErr: "pods: too many CIDRs specified", |
| 179 | + c: clusterWithNetwork([]string{"192.168.0.0/16", "fd00:100:96::/48", "10.128.0.0/12"}, nil), |
| 180 | + }, |
| 181 | + { |
| 182 | + name: "services: too many cidrs", |
| 183 | + expectErr: "services: too many CIDRs specified", |
| 184 | + c: clusterWithNetwork(nil, []string{"192.168.0.0/16", "fd00:100:96::/48", "10.128.0.0/12"}), |
| 185 | + }, |
| 186 | + } |
| 187 | + |
| 188 | + for _, tt := range invalid { |
| 189 | + t.Run(tt.name, func(t *testing.T) { |
| 190 | + g := NewWithT(t) |
| 191 | + ipFamily, err := tt.c.GetIPFamily() |
| 192 | + g.Expect(err).To(HaveOccurred()) |
| 193 | + g.Expect(err).To(MatchError(ContainSubstring(tt.expectErr))) |
| 194 | + g.Expect(ipFamily).To(Equal(InvalidIPFamily)) |
| 195 | + }) |
| 196 | + } |
| 197 | +} |
0 commit comments