k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/cmd/kube-apiserver/app/options/completion_test.go (about) 1 /* 2 Copyright 2019 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 options 18 19 import ( 20 "testing" 21 ) 22 23 func TestGetServiceIPAndRanges(t *testing.T) { 24 tests := []struct { 25 body string 26 apiServerServiceIP string 27 primaryServiceIPRange string 28 secondaryServiceIPRange string 29 expectedError bool 30 }{ 31 {"", "10.0.0.1", "10.0.0.0/24", "<nil>", false}, 32 {"192.0.2.1/24", "192.0.2.1", "192.0.2.0/24", "<nil>", false}, 33 {"192.0.2.1/24,192.168.128.0/17", "192.0.2.1", "192.0.2.0/24", "192.168.128.0/17", false}, 34 // Dual stack IPv4/IPv6 35 {"192.0.2.1/24,2001:db2:1:3:4::1/112", "192.0.2.1", "192.0.2.0/24", "2001:db2:1:3:4::/112", false}, 36 // Dual stack IPv6/IPv4 37 {"2001:db2:1:3:4::1/112,192.0.2.1/24", "2001:db2:1:3:4::1", "2001:db2:1:3:4::/112", "192.0.2.0/24", false}, 38 39 {"192.0.2.1/30,192.168.128.0/17", "<nil>", "<nil>", "<nil>", true}, 40 // Invalid ip range[0] IPv4 mask 41 {"192.0.2.1/33,192.168.128.0/17", "<nil>", "<nil>", "<nil>", true}, 42 // Invalid ip range[1] IPv4 mask 43 {"192.0.2.1/24,192.168.128.0/33", "<nil>", "<nil>", "<nil>", true}, 44 // Invalid ip range[0] IPv6 mask 45 {"2001:db2:1:3:4::1/129,192.0.2.1/24", "<nil>", "<nil>", "<nil>", true}, 46 // Invalid ip range[1] IPv6 mask 47 {"192.0.2.1/24,2001:db2:1:3:4::1/129", "<nil>", "<nil>", "<nil>", true}, 48 // Invalid ip range[0] missing IPv4 mask 49 {"192.0.2.1,192.168.128.0/17", "<nil>", "<nil>", "<nil>", true}, 50 // Invalid ip range[1] missing IPv4 mask 51 {"192.0.2.1/24,192.168.128.1", "<nil>", "<nil>", "<nil>", true}, 52 // Invalid ip range[0] missing IPv6 mask 53 {"2001:db2:1:3:4::1,192.0.2.1/24", "<nil>", "<nil>", "<nil>", true}, 54 // Invalid ip range[1] missing IPv6 mask 55 {"192.0.2.1/24,2001:db2:1:3:4::1", "<nil>", "<nil>", "<nil>", true}, 56 // Invalid ip range[0] IP address format 57 {"bad.ip.range,192.168.0.2/24", "<nil>", "<nil>", "<nil>", true}, 58 // Invalid ip range[1] IP address format 59 {"192.168.0.2/24,bad.ip.range", "<nil>", "<nil>", "<nil>", true}, 60 } 61 62 for _, test := range tests { 63 apiServerServiceIP, primaryServiceIPRange, secondaryServiceIPRange, err := getServiceIPAndRanges(test.body) 64 65 if apiServerServiceIP.String() != test.apiServerServiceIP { 66 t.Errorf("expected apiServerServiceIP: %s, got: %s", test.apiServerServiceIP, apiServerServiceIP.String()) 67 } 68 69 if primaryServiceIPRange.String() != test.primaryServiceIPRange { 70 t.Errorf("expected primaryServiceIPRange: %s, got: %s", test.primaryServiceIPRange, primaryServiceIPRange.String()) 71 } 72 73 if secondaryServiceIPRange.String() != test.secondaryServiceIPRange { 74 t.Errorf("expected secondaryServiceIPRange: %s, got: %s", test.secondaryServiceIPRange, secondaryServiceIPRange.String()) 75 } 76 77 if (err == nil) == test.expectedError { 78 t.Errorf("expected err to be: %t, but it was %t", test.expectedError, !test.expectedError) 79 } 80 } 81 }