k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/cmd/kubeadm/test/cmd/join_test.go (about) 1 /* 2 Copyright 2016 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 kubeadm 18 19 import "testing" 20 21 // kubeadmReset executes "kubeadm reset" and restarts kubelet. 22 func kubeadmReset() error { 23 kubeadmPath := getKubeadmPath() 24 _, _, _, err := RunCmd(kubeadmPath, "reset") 25 return err 26 } 27 28 func TestCmdJoinConfig(t *testing.T) { 29 var initTest = []struct { 30 name string 31 args string 32 expected bool 33 }{ 34 {"config", "--config=foobar", false}, 35 {"config path", "--config=/does/not/exist/foo/bar", false}, 36 } 37 38 kubeadmPath := getKubeadmPath() 39 for _, rt := range initTest { 40 t.Run(rt.name, func(t *testing.T) { 41 _, _, _, actual := RunCmd(kubeadmPath, "join", rt.args, "--ignore-preflight-errors=all") 42 if (actual == nil) != rt.expected { 43 t.Errorf( 44 "failed CmdJoinConfig running 'kubeadm join %s' with an error: %v\n\texpected: %t\n\t actual: %t", 45 rt.args, 46 actual, 47 rt.expected, 48 (actual == nil), 49 ) 50 } 51 kubeadmReset() 52 }) 53 } 54 } 55 56 func TestCmdJoinDiscoveryFile(t *testing.T) { 57 var initTest = []struct { 58 name string 59 args string 60 expected bool 61 }{ 62 {"valid discovery file", "--discovery-file=foobar", false}, 63 {"invalid discovery file", "--discovery-file=file:wrong", false}, 64 } 65 66 kubeadmPath := getKubeadmPath() 67 for _, rt := range initTest { 68 t.Run(rt.name, func(t *testing.T) { 69 _, _, _, actual := RunCmd(kubeadmPath, "join", rt.args, "--ignore-preflight-errors=all") 70 if (actual == nil) != rt.expected { 71 t.Errorf( 72 "failed CmdJoinDiscoveryFile running 'kubeadm join %s' with an error: %v\n\texpected: %t\n\t actual: %t", 73 rt.args, 74 actual, 75 rt.expected, 76 (actual == nil), 77 ) 78 } 79 kubeadmReset() 80 }) 81 } 82 } 83 84 func TestCmdJoinDiscoveryToken(t *testing.T) { 85 var initTest = []struct { 86 name string 87 args string 88 expected bool 89 }{ 90 {"valid discovery token", "--discovery-token=foobar", false}, 91 {"valid discovery token url", "--discovery-token=token://asdf:asdf", false}, 92 } 93 94 kubeadmPath := getKubeadmPath() 95 for _, rt := range initTest { 96 t.Run(rt.name, func(t *testing.T) { 97 _, _, _, actual := RunCmd(kubeadmPath, "join", rt.args, "--ignore-preflight-errors=all") 98 if (actual == nil) != rt.expected { 99 t.Errorf( 100 "failed CmdJoinDiscoveryToken running 'kubeadm join %s' with an error: %v\n\texpected: %t\n\t actual: %t", 101 rt.args, 102 actual, 103 rt.expected, 104 (actual == nil), 105 ) 106 } 107 kubeadmReset() 108 }) 109 } 110 } 111 112 func TestCmdJoinNodeName(t *testing.T) { 113 var initTest = []struct { 114 name string 115 args string 116 expected bool 117 }{ 118 {"valid node name", "--node-name=foobar", false}, 119 } 120 121 kubeadmPath := getKubeadmPath() 122 for _, rt := range initTest { 123 t.Run(rt.name, func(t *testing.T) { 124 _, _, _, actual := RunCmd(kubeadmPath, "join", rt.args, "--ignore-preflight-errors=all") 125 if (actual == nil) != rt.expected { 126 t.Errorf( 127 "failed CmdJoinNodeName running 'kubeadm join %s' with an error: %v\n\texpected: %t\n\t actual: %t", 128 rt.args, 129 actual, 130 rt.expected, 131 (actual == nil), 132 ) 133 } 134 kubeadmReset() 135 }) 136 } 137 } 138 139 func TestCmdJoinTLSBootstrapToken(t *testing.T) { 140 var initTest = []struct { 141 name string 142 args string 143 expected bool 144 }{ 145 {"valid bootstrap token", "--tls-bootstrap-token=foobar", false}, 146 {"valid bootstrap token url", "--tls-bootstrap-token=token://asdf:asdf", false}, 147 } 148 149 kubeadmPath := getKubeadmPath() 150 for _, rt := range initTest { 151 t.Run(rt.name, func(t *testing.T) { 152 _, _, _, actual := RunCmd(kubeadmPath, "join", rt.args, "--ignore-preflight-errors=all") 153 if (actual == nil) != rt.expected { 154 t.Errorf( 155 "failed CmdJoinTLSBootstrapToken running 'kubeadm join %s' with an error: %v\n\texpected: %t\n\t actual: %t", 156 rt.args, 157 actual, 158 rt.expected, 159 (actual == nil), 160 ) 161 } 162 kubeadmReset() 163 }) 164 } 165 } 166 167 func TestCmdJoinToken(t *testing.T) { 168 var initTest = []struct { 169 name string 170 args string 171 expected bool 172 }{ 173 {"valid token", "--token=foobar", false}, 174 {"valid token url", "--token=token://asdf:asdf", false}, 175 } 176 177 kubeadmPath := getKubeadmPath() 178 for _, rt := range initTest { 179 t.Run(rt.name, func(t *testing.T) { 180 _, _, _, actual := RunCmd(kubeadmPath, "join", rt.args, "--ignore-preflight-errors=all") 181 if (actual == nil) != rt.expected { 182 t.Errorf( 183 "failed CmdJoinToken running 'kubeadm join %s' with an error: %v\n\texpected: %t\n\t actual: %t", 184 rt.args, 185 actual, 186 rt.expected, 187 (actual == nil), 188 ) 189 } 190 kubeadmReset() 191 }) 192 } 193 } 194 195 func TestCmdJoinBadArgs(t *testing.T) { 196 kubeadmPath := getKubeadmPath() 197 var initTest = []struct { 198 name string 199 args string 200 expected bool 201 }{ 202 {"discovery-token and discovery-file can't both be set", "--discovery-token=abcdef.1234567890123456 --discovery-file=file:///tmp/foo.bar", false}, // DiscoveryToken, DiscoveryFile can't both be set 203 {"discovery-token or discovery-file must be set", "", false}, // DiscoveryToken or DiscoveryFile must be set 204 } 205 206 for _, rt := range initTest { 207 t.Run(rt.name, func(t *testing.T) { 208 _, _, _, actual := RunCmd(kubeadmPath, "join", rt.args, "--ignore-preflight-errors=all") 209 if (actual == nil) != rt.expected { 210 t.Errorf( 211 "failed CmdJoinBadArgs 'kubeadm join %s' with an error: %v\n\texpected: %t\n\t actual: %t", 212 rt.args, 213 actual, 214 rt.expected, 215 (actual == nil), 216 ) 217 } 218 kubeadmReset() 219 }) 220 } 221 } 222 223 func TestCmdJoinArgsMixed(t *testing.T) { 224 var initTest = []struct { 225 name string 226 args string 227 expected bool 228 }{ 229 {"discovery-token and config", "--discovery-token=abcdef.1234567890abcdef --config=/etc/kubernetes/kubeadm.config", false}, 230 } 231 232 kubeadmPath := getKubeadmPath() 233 for _, rt := range initTest { 234 t.Run(rt.name, func(t *testing.T) { 235 _, _, _, actual := RunCmd(kubeadmPath, "join", rt.args, "--ignore-preflight-errors=all") 236 if (actual == nil) != rt.expected { 237 t.Errorf( 238 "failed CmdJoinArgsMixed running 'kubeadm join %s' with an error: %v\n\texpected: %t\n\t actual: %t", 239 rt.args, 240 actual, 241 rt.expected, 242 (actual == nil), 243 ) 244 } 245 kubeadmReset() 246 }) 247 } 248 }