123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 |
- /*
- Copyright 2017 The Kubernetes Authors.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package util
- import (
- "testing"
- kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
- )
- func TestGetControlPlaneEndpoint(t *testing.T) {
- var tests = []struct {
- name string
- cfg *kubeadmapi.InitConfiguration
- expectedEndpoint string
- expectedError bool
- }{
- {
- name: "use ControlPlaneEndpoint (dns) if fully defined",
- cfg: &kubeadmapi.InitConfiguration{
- LocalAPIEndpoint: kubeadmapi.APIEndpoint{
- BindPort: 4567,
- AdvertiseAddress: "4.5.6.7",
- },
- ClusterConfiguration: kubeadmapi.ClusterConfiguration{
- ControlPlaneEndpoint: "cp.k8s.io:1234",
- },
- },
- expectedEndpoint: "https://cp.k8s.io:1234",
- },
- {
- name: "use ControlPlaneEndpoint (ipv4) if fully defined",
- cfg: &kubeadmapi.InitConfiguration{
- LocalAPIEndpoint: kubeadmapi.APIEndpoint{
- BindPort: 4567,
- AdvertiseAddress: "4.5.6.7",
- },
- ClusterConfiguration: kubeadmapi.ClusterConfiguration{
- ControlPlaneEndpoint: "1.2.3.4:1234",
- },
- },
- expectedEndpoint: "https://1.2.3.4:1234",
- },
- {
- name: "use ControlPlaneEndpoint (ipv6) if fully defined",
- cfg: &kubeadmapi.InitConfiguration{
- LocalAPIEndpoint: kubeadmapi.APIEndpoint{
- BindPort: 4567,
- AdvertiseAddress: "4.5.6.7",
- },
- ClusterConfiguration: kubeadmapi.ClusterConfiguration{
- ControlPlaneEndpoint: "[2001:db8::1]:1234",
- },
- },
- expectedEndpoint: "https://[2001:db8::1]:1234",
- },
- {
- name: "use ControlPlaneEndpoint (dns) + BindPort if ControlPlaneEndpoint defined without port",
- cfg: &kubeadmapi.InitConfiguration{
- LocalAPIEndpoint: kubeadmapi.APIEndpoint{
- BindPort: 4567,
- AdvertiseAddress: "4.5.6.7",
- },
- ClusterConfiguration: kubeadmapi.ClusterConfiguration{
- ControlPlaneEndpoint: "cp.k8s.io",
- },
- },
- expectedEndpoint: "https://cp.k8s.io:4567",
- },
- {
- name: "use ControlPlaneEndpoint (ipv4) + BindPort if ControlPlaneEndpoint defined without port",
- cfg: &kubeadmapi.InitConfiguration{
- LocalAPIEndpoint: kubeadmapi.APIEndpoint{
- BindPort: 4567,
- AdvertiseAddress: "4.5.6.7",
- },
- ClusterConfiguration: kubeadmapi.ClusterConfiguration{
- ControlPlaneEndpoint: "1.2.3.4",
- },
- },
- expectedEndpoint: "https://1.2.3.4:4567",
- },
- {
- name: "use ControlPlaneEndpoint (ipv6) + BindPort if ControlPlaneEndpoint defined without port",
- cfg: &kubeadmapi.InitConfiguration{
- LocalAPIEndpoint: kubeadmapi.APIEndpoint{
- BindPort: 4567,
- AdvertiseAddress: "4.5.6.7",
- },
- ClusterConfiguration: kubeadmapi.ClusterConfiguration{
- ControlPlaneEndpoint: "2001:db8::1",
- },
- },
- expectedEndpoint: "https://[2001:db8::1]:4567",
- },
- {
- name: "use AdvertiseAddress (ipv4) + BindPort if ControlPlaneEndpoint is not defined",
- cfg: &kubeadmapi.InitConfiguration{
- LocalAPIEndpoint: kubeadmapi.APIEndpoint{
- BindPort: 4567,
- AdvertiseAddress: "4.5.6.7",
- },
- },
- expectedEndpoint: "https://4.5.6.7:4567",
- },
- {
- name: "use AdvertiseAddress (ipv6) + BindPort if ControlPlaneEndpoint is not defined",
- cfg: &kubeadmapi.InitConfiguration{
- LocalAPIEndpoint: kubeadmapi.APIEndpoint{
- BindPort: 4567,
- AdvertiseAddress: "2001:db8::1",
- },
- },
- expectedEndpoint: "https://[2001:db8::1]:4567",
- },
- {
- name: "fail if invalid BindPort",
- cfg: &kubeadmapi.InitConfiguration{
- LocalAPIEndpoint: kubeadmapi.APIEndpoint{
- BindPort: 0,
- },
- },
- expectedError: true,
- },
- {
- name: "fail if invalid ControlPlaneEndpoint (dns)",
- cfg: &kubeadmapi.InitConfiguration{
- ClusterConfiguration: kubeadmapi.ClusterConfiguration{
- ControlPlaneEndpoint: "bad!!.cp.k8s.io",
- },
- },
- expectedError: true,
- },
- {
- name: "fail if invalid ControlPlaneEndpoint (ip4)",
- cfg: &kubeadmapi.InitConfiguration{
- ClusterConfiguration: kubeadmapi.ClusterConfiguration{
- ControlPlaneEndpoint: "1..0",
- },
- },
- expectedError: true,
- },
- {
- name: "fail if invalid ControlPlaneEndpoint (ip6)",
- cfg: &kubeadmapi.InitConfiguration{
- ClusterConfiguration: kubeadmapi.ClusterConfiguration{
- ControlPlaneEndpoint: "1200::AB00:1234::2552:7777:1313",
- },
- },
- expectedError: true,
- },
- {
- name: "fail if invalid ControlPlaneEndpoint (port)",
- cfg: &kubeadmapi.InitConfiguration{
- ClusterConfiguration: kubeadmapi.ClusterConfiguration{
- ControlPlaneEndpoint: "cp.k8s.io:0",
- },
- },
- expectedError: true,
- },
- {
- name: "fail if invalid AdvertiseAddress (ip4)",
- cfg: &kubeadmapi.InitConfiguration{
- LocalAPIEndpoint: kubeadmapi.APIEndpoint{
- AdvertiseAddress: "1..0",
- BindPort: 4567,
- },
- },
- expectedError: true,
- },
- {
- name: "fail if invalid AdvertiseAddress (ip6)",
- cfg: &kubeadmapi.InitConfiguration{
- LocalAPIEndpoint: kubeadmapi.APIEndpoint{
- AdvertiseAddress: "1200::AB00:1234::2552:7777:1313",
- BindPort: 4567,
- },
- },
- expectedError: true,
- },
- }
- for _, rt := range tests {
- t.Run(rt.name, func(t *testing.T) {
- actualEndpoint, actualError := GetControlPlaneEndpoint(rt.cfg.ControlPlaneEndpoint, &rt.cfg.LocalAPIEndpoint)
- if (actualError != nil) && !rt.expectedError {
- t.Errorf("%s unexpected failure: %v", rt.name, actualError)
- return
- } else if (actualError == nil) && rt.expectedError {
- t.Errorf("%s passed when expected to fail", rt.name)
- return
- }
- if actualEndpoint != rt.expectedEndpoint {
- t.Errorf("%s returned invalid endpoint %s, expected %s", rt.name, actualEndpoint, rt.expectedEndpoint)
- }
- })
- }
- }
- func TestParseHostPort(t *testing.T) {
- var tests = []struct {
- name string
- hostport string
- expectedHost string
- expectedPort string
- expectedError bool
- }{
- {
- name: "valid dns",
- hostport: "cp.k8s.io",
- expectedHost: "cp.k8s.io",
- expectedPort: "",
- },
- {
- name: "valid dns:port",
- hostport: "cp.k8s.io:1234",
- expectedHost: "cp.k8s.io",
- expectedPort: "1234",
- },
- {
- name: "valid ip4",
- hostport: "1.2.3.4",
- expectedHost: "1.2.3.4",
- expectedPort: "",
- },
- {
- name: "valid ipv4:port",
- hostport: "1.2.3.4:1234",
- expectedHost: "1.2.3.4",
- expectedPort: "1234",
- },
- {
- name: "valid ipv6",
- hostport: "2001:db8::1",
- expectedHost: "2001:db8::1",
- expectedPort: "",
- },
- {
- name: "valid ipv6:port",
- hostport: "[2001:db8::1]:1234",
- expectedHost: "2001:db8::1",
- expectedPort: "1234",
- },
- {
- name: "invalid port(not a number)",
- hostport: "cp.k8s.io:aaa",
- expectedError: true,
- },
- {
- name: "invalid port(out of range, positive port number)",
- hostport: "cp.k8s.io:987654321",
- expectedError: true,
- },
- {
- name: "invalid port(out of range, negative port number)",
- hostport: "cp.k8s.io:-987654321",
- expectedError: true,
- },
- {
- name: "invalid port(out of range, negative port number)",
- hostport: "cp.k8s.io:123:123",
- expectedError: true,
- },
- {
- name: "invalid dns",
- hostport: "bad!!cp.k8s.io",
- expectedError: true,
- },
- {
- name: "invalid valid dns:port",
- hostport: "bad!!cp.k8s.io:1234",
- expectedError: true,
- },
- {
- name: "invalid ip4, but valid DNS",
- hostport: "259.2.3.4",
- expectedHost: "259.2.3.4",
- },
- {
- name: "invalid ip4",
- hostport: "1..3.4",
- expectedError: true,
- },
- {
- name: "invalid ip4(2):port",
- hostport: "1..3.4:1234",
- expectedError: true,
- },
- {
- name: "invalid ipv6",
- hostport: "1200::AB00:1234::2552:7777:1313",
- expectedError: true,
- },
- {
- name: "invalid ipv6:port",
- hostport: "[1200::AB00:1234::2552:7777:1313]:1234",
- expectedError: true,
- },
- }
- for _, rt := range tests {
- t.Run(rt.name, func(t *testing.T) {
- actualHost, actualPort, actualError := ParseHostPort(rt.hostport)
- if (actualError != nil) && !rt.expectedError {
- t.Errorf("%s unexpected failure: %v", rt.name, actualError)
- return
- } else if (actualError == nil) && rt.expectedError {
- t.Errorf("%s passed when expected to fail", rt.name)
- return
- }
- if actualHost != rt.expectedHost {
- t.Errorf("%s returned invalid host %s, expected %s", rt.name, actualHost, rt.expectedHost)
- return
- }
- if actualPort != rt.expectedPort {
- t.Errorf("%s returned invalid port %s, expected %s", rt.name, actualPort, rt.expectedPort)
- }
- })
- }
- }
- func TestParsePort(t *testing.T) {
- var tests = []struct {
- name string
- port string
- expectedPort int
- expectedError bool
- }{
- {
- name: "valid port",
- port: "1234",
- expectedPort: 1234,
- },
- {
- name: "invalid port (not a number)",
- port: "a",
- expectedError: true,
- },
- {
- name: "invalid port (<1)",
- port: "-10",
- expectedError: true,
- },
- {
- name: "invalid port (>65535)",
- port: "66535",
- expectedError: true,
- },
- }
- for _, rt := range tests {
- t.Run(rt.name, func(t *testing.T) {
- actualPort, actualError := ParsePort(rt.port)
- if (actualError != nil) && !rt.expectedError {
- t.Errorf("%s unexpected failure: %v", rt.name, actualError)
- return
- } else if (actualError == nil) && rt.expectedError {
- t.Errorf("%s passed when expected to fail", rt.name)
- return
- }
- if actualPort != rt.expectedPort {
- t.Errorf("%s returned invalid port %d, expected %d", rt.name, actualPort, rt.expectedPort)
- }
- })
- }
- }
|