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.
This commit is contained in:
Lubomir I. Ivanov 2020-04-09 00:28:18 +03:00
parent 9bbd321fec
commit 04933f3a94
7 changed files with 11 additions and 118 deletions

View File

@ -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 = [

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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
}

View File

@ -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