From 9bbd321fec45f9319f928d7d690b5a1879b98b51 Mon Sep 17 00:00:00 2001 From: "Lubomir I. Ivanov" Date: Thu, 9 Apr 2020 00:26:46 +0300 Subject: [PATCH 1/3] remove /cluster/kubeadm.sh and /test/cmd/kubeadm.sh /cluster/kubeadm.sh is used to find the kubeadm binary. This file is legacy and is removed. Remove /test/cmd/kubeadm.sh. This file contains a function that is used to build kubeadm and invoke "make test". Move the function contents to hack/make-rules/test-cmd.cmd. Stop sourcing /test/cmd/kubeadm.sh in /test/cmd/legacy-script.sh. Also remove the --kubeadm-path invocation as this can be handled with an env. variable directly. --- cluster/kubeadm.sh | 42 ------------------------------------- hack/make-rules/test-cmd.sh | 10 ++++++++- test/cmd/kubeadm.sh | 36 ------------------------------- test/cmd/legacy-script.sh | 1 - 4 files changed, 9 insertions(+), 80 deletions(-) delete mode 100755 cluster/kubeadm.sh delete mode 100755 test/cmd/kubeadm.sh diff --git a/cluster/kubeadm.sh b/cluster/kubeadm.sh deleted file mode 100755 index 3035e6c17e2..00000000000 --- a/cluster/kubeadm.sh +++ /dev/null @@ -1,42 +0,0 @@ -#!/usr/bin/env bash - -# Copyright 2016 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. - -set -o errexit -set -o nounset -set -o pipefail - -KUBE_ROOT=${KUBE_ROOT:-$(dirname "${BASH_SOURCE[0]}")/..} -source "${KUBE_ROOT}/cluster/clientbin.sh" - -# If KUBEADM_PATH isn't set, gather up the list of likely places and use ls -# to find the latest one. -if [[ -z "${KUBEADM_PATH:-}" ]]; then - kubeadm=$( get_bin "kubeadm" "cmd/kubeadm" ) - - if [[ ! -x "$kubeadm" ]]; then - print_error "kubeadm" - exit 1 - fi -elif [[ ! -x "${KUBEADM_PATH}" ]]; then - { - echo "KUBEADM_PATH environment variable set to '${KUBEADM_PATH}', but " - echo "this doesn't seem to be a valid executable." - } >&2 - exit 1 -fi -kubeadm="${KUBEADM_PATH:-${kubeadm}}" - -"${kubeadm}" "${@+$@}" diff --git a/hack/make-rules/test-cmd.sh b/hack/make-rules/test-cmd.sh index b8adc81af19..0762e633b54 100755 --- a/hack/make-rules/test-cmd.sh +++ b/hack/make-rules/test-cmd.sh @@ -122,7 +122,15 @@ __EOF__ WHAT=${WHAT:-} if [[ ${WHAT} == "" || ${WHAT} =~ .*kubeadm.* ]] ; then kube::log::status "Running kubeadm tests" - run_kubeadm_tests + + # build kubeadm + make all -C "${KUBE_ROOT}" WHAT=cmd/kubeadm + # unless the user sets KUBEADM_PATH, assume that "make all..." just built it + export KUBEADM_PATH="${KUBEADM_PATH:=$(kube::realpath "${KUBE_ROOT}")/_output/local/go/bin/kubeadm}" + # invoke the tests + make -C "${KUBE_ROOT}" test \ + WHAT=k8s.io/kubernetes/cmd/kubeadm/test/cmd + # if we ONLY want to run kubeadm, then exit here. if [[ ${WHAT} == "kubeadm" ]]; then kube::log::status "TESTS PASSED" diff --git a/test/cmd/kubeadm.sh b/test/cmd/kubeadm.sh deleted file mode 100755 index 8edc613c246..00000000000 --- a/test/cmd/kubeadm.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env bash - -# Copyright 2016 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. - -set -o errexit -set -o nounset -set -o pipefail - -run_kubeadm_tests() { - set -o nounset - set -o errexit - - KUBEADM_PATH="${KUBEADM_PATH:=$(kube::realpath "${KUBE_ROOT}")/cluster/kubeadm.sh}" - - # If testing a different version of kubeadm than the current build, you can - # comment this out to save yourself from needlessly building here. - make -C "${KUBE_ROOT}" WHAT=cmd/kubeadm - - make -C "${KUBE_ROOT}" test \ - WHAT=k8s.io/kubernetes/cmd/kubeadm/test/cmd \ - KUBE_TEST_ARGS="--kubeadm-path '${KUBEADM_PATH}'" - set +o nounset - set +o errexit -} diff --git a/test/cmd/legacy-script.sh b/test/cmd/legacy-script.sh index 650575bcb4e..ec16f804a41 100755 --- a/test/cmd/legacy-script.sh +++ b/test/cmd/legacy-script.sh @@ -41,7 +41,6 @@ source "${KUBE_ROOT}/test/cmd/discovery.sh" source "${KUBE_ROOT}/test/cmd/exec.sh" source "${KUBE_ROOT}/test/cmd/generic-resources.sh" source "${KUBE_ROOT}/test/cmd/get.sh" -source "${KUBE_ROOT}/test/cmd/kubeadm.sh" source "${KUBE_ROOT}/test/cmd/kubeconfig.sh" source "${KUBE_ROOT}/test/cmd/node-management.sh" source "${KUBE_ROOT}/test/cmd/plugins.sh" From 04933f3a9416363668ddab3846e949cb351f88fc Mon Sep 17 00:00:00 2001 From: "Lubomir I. Ivanov" Date: Thu, 9 Apr 2020 00:28:18 +0300 Subject: [PATCH 2/3] cmd/kubeadm/test/cmd: refactor _test.go files Make getKubeadmPath() fetch the KUBEADM_PATH env. variable. Panic if it's missing. Don't handle the "--kubeadm-path" flag. Remove the same flag from the BUILD bazel test rule. Don't handle "--kubeadm-cmd-skip" usage of this flag is missing from the code base. Remove usage of "kubeadmCmdSkip" as the flag "--kubeadm-cmd-skip" is never passed. --- cmd/kubeadm/test/cmd/BUILD | 1 - cmd/kubeadm/test/cmd/completion_test.go | 6 ---- cmd/kubeadm/test/cmd/init_test.go | 30 ------------------- cmd/kubeadm/test/cmd/join_test.go | 40 ------------------------- cmd/kubeadm/test/cmd/token_test.go | 31 ------------------- cmd/kubeadm/test/cmd/util.go | 11 +++++++ cmd/kubeadm/test/cmd/version_test.go | 10 ------- 7 files changed, 11 insertions(+), 118 deletions(-) diff --git a/cmd/kubeadm/test/cmd/BUILD b/cmd/kubeadm/test/cmd/BUILD index 4d4644fa3d2..4d3ba384b82 100644 --- a/cmd/kubeadm/test/cmd/BUILD +++ b/cmd/kubeadm/test/cmd/BUILD @@ -25,7 +25,6 @@ go_test( "token_test.go", "version_test.go", ], - args = ["--kubeadm-path=$(location //cmd/kubeadm:kubeadm)"], data = ["//cmd/kubeadm"] + glob(["testdata/**"]), embed = [":go_default_library"], tags = [ diff --git a/cmd/kubeadm/test/cmd/completion_test.go b/cmd/kubeadm/test/cmd/completion_test.go index d5dc7bd5179..f61372fc112 100644 --- a/cmd/kubeadm/test/cmd/completion_test.go +++ b/cmd/kubeadm/test/cmd/completion_test.go @@ -20,12 +20,6 @@ import "testing" func TestCmdCompletion(t *testing.T) { kubeadmPath := getKubeadmPath() - - if *kubeadmCmdSkip { - t.Log("kubeadm cmd tests being skipped") - t.Skip() - } - var tests = []struct { name string args string diff --git a/cmd/kubeadm/test/cmd/init_test.go b/cmd/kubeadm/test/cmd/init_test.go index e51c944599a..888e92df5a6 100644 --- a/cmd/kubeadm/test/cmd/init_test.go +++ b/cmd/kubeadm/test/cmd/init_test.go @@ -37,11 +37,6 @@ func runKubeadmInit(args ...string) (string, string, int, error) { } func TestCmdInitToken(t *testing.T) { - if *kubeadmCmdSkip { - t.Log("kubeadm cmd tests being skipped") - t.Skip() - } - initTest := []struct { name string args string @@ -86,11 +81,6 @@ func TestCmdInitToken(t *testing.T) { } func TestCmdInitKubernetesVersion(t *testing.T) { - if *kubeadmCmdSkip { - t.Log("kubeadm cmd tests being skipped") - t.Skip() - } - initTest := []struct { name string args string @@ -130,11 +120,6 @@ func TestCmdInitKubernetesVersion(t *testing.T) { } func TestCmdInitConfig(t *testing.T) { - if *kubeadmCmdSkip { - t.Log("kubeadm cmd tests being skipped") - t.Skip() - } - initTest := []struct { name string args string @@ -214,11 +199,6 @@ func TestCmdInitConfig(t *testing.T) { } func TestCmdInitCertPhaseCSR(t *testing.T) { - if *kubeadmCmdSkip { - t.Log("kubeadm cmd tests being skipped") - t.Skip() - } - tests := []struct { name string baseName string @@ -279,11 +259,6 @@ func TestCmdInitCertPhaseCSR(t *testing.T) { } func TestCmdInitAPIPort(t *testing.T) { - if *kubeadmCmdSkip { - t.Log("kubeadm cmd tests being skipped") - t.Skip() - } - initTest := []struct { name string args string @@ -339,11 +314,6 @@ func TestCmdInitAPIPort(t *testing.T) { func TestCmdInitFeatureGates(t *testing.T) { const PanicExitcode = 2 - if *kubeadmCmdSkip { - t.Log("kubeadm cmd tests being skipped") - t.Skip() - } - initTest := []struct { name string args string diff --git a/cmd/kubeadm/test/cmd/join_test.go b/cmd/kubeadm/test/cmd/join_test.go index e4c8efdbce2..3d5c70605f5 100644 --- a/cmd/kubeadm/test/cmd/join_test.go +++ b/cmd/kubeadm/test/cmd/join_test.go @@ -26,11 +26,6 @@ func kubeadmReset() error { } func TestCmdJoinConfig(t *testing.T) { - if *kubeadmCmdSkip { - t.Log("kubeadm cmd tests being skipped") - t.Skip() - } - var initTest = []struct { name string args string @@ -59,11 +54,6 @@ func TestCmdJoinConfig(t *testing.T) { } func TestCmdJoinDiscoveryFile(t *testing.T) { - if *kubeadmCmdSkip { - t.Log("kubeadm cmd tests being skipped") - t.Skip() - } - var initTest = []struct { name string args string @@ -92,11 +82,6 @@ func TestCmdJoinDiscoveryFile(t *testing.T) { } func TestCmdJoinDiscoveryToken(t *testing.T) { - if *kubeadmCmdSkip { - t.Log("kubeadm cmd tests being skipped") - t.Skip() - } - var initTest = []struct { name string args string @@ -125,11 +110,6 @@ func TestCmdJoinDiscoveryToken(t *testing.T) { } func TestCmdJoinNodeName(t *testing.T) { - if *kubeadmCmdSkip { - t.Log("kubeadm cmd tests being skipped") - t.Skip() - } - var initTest = []struct { name string args string @@ -157,11 +137,6 @@ func TestCmdJoinNodeName(t *testing.T) { } func TestCmdJoinTLSBootstrapToken(t *testing.T) { - if *kubeadmCmdSkip { - t.Log("kubeadm cmd tests being skipped") - t.Skip() - } - var initTest = []struct { name string args string @@ -190,11 +165,6 @@ func TestCmdJoinTLSBootstrapToken(t *testing.T) { } func TestCmdJoinToken(t *testing.T) { - if *kubeadmCmdSkip { - t.Log("kubeadm cmd tests being skipped") - t.Skip() - } - var initTest = []struct { name string args string @@ -223,11 +193,6 @@ func TestCmdJoinToken(t *testing.T) { } func TestCmdJoinBadArgs(t *testing.T) { - if *kubeadmCmdSkip { - t.Log("kubeadm cmd tests being skipped") - t.Skip() - } - kubeadmPath := getKubeadmPath() var initTest = []struct { name string @@ -256,11 +221,6 @@ func TestCmdJoinBadArgs(t *testing.T) { } func TestCmdJoinArgsMixed(t *testing.T) { - if *kubeadmCmdSkip { - t.Log("kubeadm cmd tests being skipped") - t.Skip() - } - var initTest = []struct { name string args string diff --git a/cmd/kubeadm/test/cmd/token_test.go b/cmd/kubeadm/test/cmd/token_test.go index 7d61a519db1..9a1b8964940 100644 --- a/cmd/kubeadm/test/cmd/token_test.go +++ b/cmd/kubeadm/test/cmd/token_test.go @@ -17,10 +17,6 @@ limitations under the License. package kubeadm import ( - "flag" - "os" - "path" - "path/filepath" "regexp" "testing" ) @@ -29,25 +25,8 @@ const ( TokenExpectedRegex = "^\\S{6}\\.\\S{16}\n$" ) -var kubeadmPathFlag = flag.String("kubeadm-path", filepath.Join(os.Getenv("KUBE_ROOT"), "cluster/kubeadm.sh"), "Location of kubeadm") - -func getKubeadmPath() string { - kubeadmPath := *kubeadmPathFlag // TEST_SRCDIR is provided by Bazel. - if srcDir := os.Getenv("TEST_SRCDIR"); srcDir != "" { - kubeadmPath = path.Join(srcDir, os.Getenv("TEST_WORKSPACE"), kubeadmPath) - } - - return kubeadmPath -} - -var kubeadmCmdSkip = flag.Bool("kubeadm-cmd-skip", false, "Skip kubeadm cmd tests") - func TestCmdTokenGenerate(t *testing.T) { kubeadmPath := getKubeadmPath() - if *kubeadmCmdSkip { - t.Log("kubeadm cmd tests being skipped") - t.Skip() - } stdout, _, _, err := RunCmd(kubeadmPath, "token", "generate") if err != nil { t.Fatalf("'kubeadm token generate' exited uncleanly: %v", err) @@ -72,11 +51,6 @@ func TestCmdTokenGenerateTypoError(t *testing.T) { with a non-zero status code after showing the command's usage, so that the usage itself isn't captured as a token without the user noticing. */ - if *kubeadmCmdSkip { - t.Log("kubeadm cmd tests being skipped") - t.Skip() - } - kubeadmPath := getKubeadmPath() _, _, _, err := RunCmd(kubeadmPath, "token", "genorate") // subtle typo if err == nil { @@ -84,11 +58,6 @@ func TestCmdTokenGenerateTypoError(t *testing.T) { } } func TestCmdTokenDelete(t *testing.T) { - if *kubeadmCmdSkip { - t.Log("kubeadm cmd tests being skipped") - t.Skip() - } - var tests = []struct { name string args string diff --git a/cmd/kubeadm/test/cmd/util.go b/cmd/kubeadm/test/cmd/util.go index babf6684a90..2c33f99cbc5 100644 --- a/cmd/kubeadm/test/cmd/util.go +++ b/cmd/kubeadm/test/cmd/util.go @@ -18,6 +18,7 @@ package kubeadm import ( "bytes" + "os" "os/exec" "testing" @@ -79,3 +80,13 @@ func getSubCommand(t *testing.T, subCmds []*cobra.Command, name string) *cobra.C return nil } + +// getKubeadmPath returns the contents of the environment variable KUBEADM_PATH +// or panics if it's empty +func getKubeadmPath() string { + kubeadmPath := os.Getenv("KUBEADM_PATH") + if len(kubeadmPath) == 0 { + panic("the environment variable KUBEADM_PATH must point to the kubeadm binary path") + } + return kubeadmPath +} diff --git a/cmd/kubeadm/test/cmd/version_test.go b/cmd/kubeadm/test/cmd/version_test.go index 98511b6aa6a..cf0211dcafa 100644 --- a/cmd/kubeadm/test/cmd/version_test.go +++ b/cmd/kubeadm/test/cmd/version_test.go @@ -34,11 +34,6 @@ var ( ) func TestCmdVersion(t *testing.T) { - if *kubeadmCmdSkip { - t.Log("kubeadm cmd tests being skipped") - t.Skip() - } - var versionTest = []struct { name string args string @@ -78,11 +73,6 @@ func TestCmdVersion(t *testing.T) { } func TestCmdVersionOutputJsonOrYaml(t *testing.T) { - if *kubeadmCmdSkip { - t.Log("kubeadm cmd tests being skipped") - t.Skip() - } - var versionTest = []struct { name string args string From a18502615ea7900d3848f89d4d3599a37d8e3026 Mon Sep 17 00:00:00 2001 From: "Lubomir I. Ivanov" Date: Thu, 9 Apr 2020 01:11:30 +0300 Subject: [PATCH 3/3] kubeadm-init: allow overriding the dry-run temp directory Allow overriding the dry-run temporary directory with an env. variable (KUBEADM_INIT_DRYRUN_DIR). Use the same variable in test/cmd/init_test.go. This allows running integration tests as non-root. --- cmd/kubeadm/app/cmd/init.go | 5 ++++- cmd/kubeadm/test/cmd/init_test.go | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/cmd/kubeadm/app/cmd/init.go b/cmd/kubeadm/app/cmd/init.go index 57e87d6603e..43aab68780a 100644 --- a/cmd/kubeadm/app/cmd/init.go +++ b/cmd/kubeadm/app/cmd/init.go @@ -357,7 +357,10 @@ func newInitData(cmd *cobra.Command, args []string, options *initOptions, out io // if dry running creates a temporary folder for saving kubeadm generated files dryRunDir := "" if options.dryRun { - if dryRunDir, err = kubeadmconstants.CreateTempDirForKubeadm("", "kubeadm-init-dryrun"); err != nil { + // the KUBEADM_INIT_DRYRUN_DIR environment variable allows overriding the dry-run temporary + // directory from the command line. This makes it possible to run "kubeadm init" integration + // tests without root. + if dryRunDir, err = kubeadmconstants.CreateTempDirForKubeadm(os.Getenv("KUBEADM_INIT_DRYRUN_DIR"), "kubeadm-init-dryrun"); err != nil { return nil, errors.Wrap(err, "couldn't create a temporary directory") } } diff --git a/cmd/kubeadm/test/cmd/init_test.go b/cmd/kubeadm/test/cmd/init_test.go index 888e92df5a6..6a323923d9f 100644 --- a/cmd/kubeadm/test/cmd/init_test.go +++ b/cmd/kubeadm/test/cmd/init_test.go @@ -17,6 +17,8 @@ limitations under the License. package kubeadm import ( + "fmt" + "os" "os/exec" "strings" "testing" @@ -30,6 +32,10 @@ import ( ) func runKubeadmInit(args ...string) (string, string, int, error) { + const dryRunDir = "KUBEADM_INIT_DRYRUN_DIR" + if err := os.Setenv(dryRunDir, os.TempDir()); err != nil { + panic(fmt.Sprintf("could not set the %s environment variable", dryRunDir)) + } kubeadmPath := getKubeadmPath() kubeadmArgs := []string{"init", "--dry-run", "--ignore-preflight-errors=all"} kubeadmArgs = append(kubeadmArgs, args...)