mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 19:01:49 +00:00
kubeadm: use T.Run API in test/cmd
Used T.Run API for kubeadm tests in cmd/kubeadm/test/cmd/ This should improve testing output and make it more visible which test is doing what.
This commit is contained in:
parent
bb7973a34c
commit
639101289c
@ -27,14 +27,16 @@ func TestCmdCompletion(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
|
name string
|
||||||
args string
|
args string
|
||||||
expected bool
|
expected bool
|
||||||
}{
|
}{
|
||||||
{"", false}, // shell not specified
|
{"shell not expected", "", false},
|
||||||
{"foo", false}, // unsupported shell type
|
{"unsupported shell type", "foo", false},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, rt := range tests {
|
for _, rt := range tests {
|
||||||
|
t.Run(rt.name, func(t *testing.T) {
|
||||||
_, _, actual := RunCmd(kubeadmPath, "completion", rt.args)
|
_, _, actual := RunCmd(kubeadmPath, "completion", rt.args)
|
||||||
if (actual == nil) != rt.expected {
|
if (actual == nil) != rt.expected {
|
||||||
t.Errorf(
|
t.Errorf(
|
||||||
@ -45,5 +47,6 @@ func TestCmdCompletion(t *testing.T) {
|
|||||||
(actual == nil),
|
(actual == nil),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,15 +32,17 @@ func TestCmdJoinConfig(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var initTest = []struct {
|
var initTest = []struct {
|
||||||
|
name string
|
||||||
args string
|
args string
|
||||||
expected bool
|
expected bool
|
||||||
}{
|
}{
|
||||||
{"--config=foobar", false},
|
{"config", "--config=foobar", false},
|
||||||
{"--config=/does/not/exist/foo/bar", false},
|
{"config path", "--config=/does/not/exist/foo/bar", false},
|
||||||
}
|
}
|
||||||
|
|
||||||
kubeadmPath := getKubeadmPath()
|
kubeadmPath := getKubeadmPath()
|
||||||
for _, rt := range initTest {
|
for _, rt := range initTest {
|
||||||
|
t.Run(rt.name, func(t *testing.T) {
|
||||||
_, _, actual := RunCmd(kubeadmPath, "join", rt.args, "--ignore-preflight-errors=all")
|
_, _, actual := RunCmd(kubeadmPath, "join", rt.args, "--ignore-preflight-errors=all")
|
||||||
if (actual == nil) != rt.expected {
|
if (actual == nil) != rt.expected {
|
||||||
t.Errorf(
|
t.Errorf(
|
||||||
@ -52,6 +54,7 @@ func TestCmdJoinConfig(t *testing.T) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
kubeadmReset()
|
kubeadmReset()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,15 +65,17 @@ func TestCmdJoinDiscoveryFile(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var initTest = []struct {
|
var initTest = []struct {
|
||||||
|
name string
|
||||||
args string
|
args string
|
||||||
expected bool
|
expected bool
|
||||||
}{
|
}{
|
||||||
{"--discovery-file=foobar", false},
|
{"valid discovery file", "--discovery-file=foobar", false},
|
||||||
{"--discovery-file=file:wrong", false},
|
{"invalid discovery file", "--discovery-file=file:wrong", false},
|
||||||
}
|
}
|
||||||
|
|
||||||
kubeadmPath := getKubeadmPath()
|
kubeadmPath := getKubeadmPath()
|
||||||
for _, rt := range initTest {
|
for _, rt := range initTest {
|
||||||
|
t.Run(rt.name, func(t *testing.T) {
|
||||||
_, _, actual := RunCmd(kubeadmPath, "join", rt.args, "--ignore-preflight-errors=all")
|
_, _, actual := RunCmd(kubeadmPath, "join", rt.args, "--ignore-preflight-errors=all")
|
||||||
if (actual == nil) != rt.expected {
|
if (actual == nil) != rt.expected {
|
||||||
t.Errorf(
|
t.Errorf(
|
||||||
@ -82,6 +87,7 @@ func TestCmdJoinDiscoveryFile(t *testing.T) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
kubeadmReset()
|
kubeadmReset()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,15 +98,17 @@ func TestCmdJoinDiscoveryToken(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var initTest = []struct {
|
var initTest = []struct {
|
||||||
|
name string
|
||||||
args string
|
args string
|
||||||
expected bool
|
expected bool
|
||||||
}{
|
}{
|
||||||
{"--discovery-token=foobar", false},
|
{"valid discovery token", "--discovery-token=foobar", false},
|
||||||
{"--discovery-token=token://asdf:asdf", false},
|
{"valid discovery token url", "--discovery-token=token://asdf:asdf", false},
|
||||||
}
|
}
|
||||||
|
|
||||||
kubeadmPath := getKubeadmPath()
|
kubeadmPath := getKubeadmPath()
|
||||||
for _, rt := range initTest {
|
for _, rt := range initTest {
|
||||||
|
t.Run(rt.name, func(t *testing.T) {
|
||||||
_, _, actual := RunCmd(kubeadmPath, "join", rt.args, "--ignore-preflight-errors=all")
|
_, _, actual := RunCmd(kubeadmPath, "join", rt.args, "--ignore-preflight-errors=all")
|
||||||
if (actual == nil) != rt.expected {
|
if (actual == nil) != rt.expected {
|
||||||
t.Errorf(
|
t.Errorf(
|
||||||
@ -112,6 +120,7 @@ func TestCmdJoinDiscoveryToken(t *testing.T) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
kubeadmReset()
|
kubeadmReset()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,14 +131,16 @@ func TestCmdJoinNodeName(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var initTest = []struct {
|
var initTest = []struct {
|
||||||
|
name string
|
||||||
args string
|
args string
|
||||||
expected bool
|
expected bool
|
||||||
}{
|
}{
|
||||||
{"--node-name=foobar", false},
|
{"valid node name", "--node-name=foobar", false},
|
||||||
}
|
}
|
||||||
|
|
||||||
kubeadmPath := getKubeadmPath()
|
kubeadmPath := getKubeadmPath()
|
||||||
for _, rt := range initTest {
|
for _, rt := range initTest {
|
||||||
|
t.Run(rt.name, func(t *testing.T) {
|
||||||
_, _, actual := RunCmd(kubeadmPath, "join", rt.args, "--ignore-preflight-errors=all")
|
_, _, actual := RunCmd(kubeadmPath, "join", rt.args, "--ignore-preflight-errors=all")
|
||||||
if (actual == nil) != rt.expected {
|
if (actual == nil) != rt.expected {
|
||||||
t.Errorf(
|
t.Errorf(
|
||||||
@ -141,6 +152,7 @@ func TestCmdJoinNodeName(t *testing.T) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
kubeadmReset()
|
kubeadmReset()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,15 +163,17 @@ func TestCmdJoinTLSBootstrapToken(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var initTest = []struct {
|
var initTest = []struct {
|
||||||
|
name string
|
||||||
args string
|
args string
|
||||||
expected bool
|
expected bool
|
||||||
}{
|
}{
|
||||||
{"--tls-bootstrap-token=foobar", false},
|
{"valid bootstrap token", "--tls-bootstrap-token=foobar", false},
|
||||||
{"--tls-bootstrap-token=token://asdf:asdf", false},
|
{"valid bootstrap token url", "--tls-bootstrap-token=token://asdf:asdf", false},
|
||||||
}
|
}
|
||||||
|
|
||||||
kubeadmPath := getKubeadmPath()
|
kubeadmPath := getKubeadmPath()
|
||||||
for _, rt := range initTest {
|
for _, rt := range initTest {
|
||||||
|
t.Run(rt.name, func(t *testing.T) {
|
||||||
_, _, actual := RunCmd(kubeadmPath, "join", rt.args, "--ignore-preflight-errors=all")
|
_, _, actual := RunCmd(kubeadmPath, "join", rt.args, "--ignore-preflight-errors=all")
|
||||||
if (actual == nil) != rt.expected {
|
if (actual == nil) != rt.expected {
|
||||||
t.Errorf(
|
t.Errorf(
|
||||||
@ -171,6 +185,7 @@ func TestCmdJoinTLSBootstrapToken(t *testing.T) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
kubeadmReset()
|
kubeadmReset()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -181,15 +196,17 @@ func TestCmdJoinToken(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var initTest = []struct {
|
var initTest = []struct {
|
||||||
|
name string
|
||||||
args string
|
args string
|
||||||
expected bool
|
expected bool
|
||||||
}{
|
}{
|
||||||
{"--token=foobar", false},
|
{"valid token", "--token=foobar", false},
|
||||||
{"--token=token://asdf:asdf", false},
|
{"valid token url", "--token=token://asdf:asdf", false},
|
||||||
}
|
}
|
||||||
|
|
||||||
kubeadmPath := getKubeadmPath()
|
kubeadmPath := getKubeadmPath()
|
||||||
for _, rt := range initTest {
|
for _, rt := range initTest {
|
||||||
|
t.Run(rt.name, func(t *testing.T) {
|
||||||
_, _, actual := RunCmd(kubeadmPath, "join", rt.args, "--ignore-preflight-errors=all")
|
_, _, actual := RunCmd(kubeadmPath, "join", rt.args, "--ignore-preflight-errors=all")
|
||||||
if (actual == nil) != rt.expected {
|
if (actual == nil) != rt.expected {
|
||||||
t.Errorf(
|
t.Errorf(
|
||||||
@ -201,6 +218,7 @@ func TestCmdJoinToken(t *testing.T) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
kubeadmReset()
|
kubeadmReset()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -212,14 +230,16 @@ func TestCmdJoinBadArgs(t *testing.T) {
|
|||||||
|
|
||||||
kubeadmPath := getKubeadmPath()
|
kubeadmPath := getKubeadmPath()
|
||||||
var initTest = []struct {
|
var initTest = []struct {
|
||||||
|
name string
|
||||||
args string
|
args string
|
||||||
expected bool
|
expected bool
|
||||||
}{
|
}{
|
||||||
{"--discovery-token=abcdef.1234567890123456 --discovery-file=file:///tmp/foo.bar", false}, // DiscoveryToken, DiscoveryFile can't both be set
|
{"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
|
||||||
{"", false}, // DiscoveryToken or DiscoveryFile must be set
|
{"discovery-token or discovery-file must be set", "", false}, // DiscoveryToken or DiscoveryFile must be set
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, rt := range initTest {
|
for _, rt := range initTest {
|
||||||
|
t.Run(rt.name, func(t *testing.T) {
|
||||||
_, _, actual := RunCmd(kubeadmPath, "join", rt.args, "--ignore-preflight-errors=all")
|
_, _, actual := RunCmd(kubeadmPath, "join", rt.args, "--ignore-preflight-errors=all")
|
||||||
if (actual == nil) != rt.expected {
|
if (actual == nil) != rt.expected {
|
||||||
t.Errorf(
|
t.Errorf(
|
||||||
@ -231,6 +251,7 @@ func TestCmdJoinBadArgs(t *testing.T) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
kubeadmReset()
|
kubeadmReset()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -241,14 +262,16 @@ func TestCmdJoinArgsMixed(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var initTest = []struct {
|
var initTest = []struct {
|
||||||
|
name string
|
||||||
args string
|
args string
|
||||||
expected bool
|
expected bool
|
||||||
}{
|
}{
|
||||||
{"--discovery-token=abcdef.1234567890abcdef --config=/etc/kubernetes/kubeadm.config", false},
|
{"discovery-token and config", "--discovery-token=abcdef.1234567890abcdef --config=/etc/kubernetes/kubeadm.config", false},
|
||||||
}
|
}
|
||||||
|
|
||||||
kubeadmPath := getKubeadmPath()
|
kubeadmPath := getKubeadmPath()
|
||||||
for _, rt := range initTest {
|
for _, rt := range initTest {
|
||||||
|
t.Run(rt.name, func(t *testing.T) {
|
||||||
_, _, actual := RunCmd(kubeadmPath, "join", rt.args, "--ignore-preflight-errors=all")
|
_, _, actual := RunCmd(kubeadmPath, "join", rt.args, "--ignore-preflight-errors=all")
|
||||||
if (actual == nil) != rt.expected {
|
if (actual == nil) != rt.expected {
|
||||||
t.Errorf(
|
t.Errorf(
|
||||||
@ -260,5 +283,6 @@ func TestCmdJoinArgsMixed(t *testing.T) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
kubeadmReset()
|
kubeadmReset()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -90,15 +90,17 @@ func TestCmdTokenDelete(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
|
name string
|
||||||
args string
|
args string
|
||||||
expected bool
|
expected bool
|
||||||
}{
|
}{
|
||||||
{"", false}, // no token provided
|
{"no token provided", "", false},
|
||||||
{"foobar", false}, // invalid token
|
{"invalid token", "foobar", false},
|
||||||
}
|
}
|
||||||
|
|
||||||
kubeadmPath := getKubeadmPath()
|
kubeadmPath := getKubeadmPath()
|
||||||
for _, rt := range tests {
|
for _, rt := range tests {
|
||||||
|
t.Run(rt.name, func(t *testing.T) {
|
||||||
_, _, actual := RunCmd(kubeadmPath, "token", "delete", rt.args)
|
_, _, actual := RunCmd(kubeadmPath, "token", "delete", rt.args)
|
||||||
if (actual == nil) != rt.expected {
|
if (actual == nil) != rt.expected {
|
||||||
t.Errorf(
|
t.Errorf(
|
||||||
@ -110,5 +112,6 @@ func TestCmdTokenDelete(t *testing.T) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
kubeadmReset()
|
kubeadmReset()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,17 +40,19 @@ func TestCmdVersion(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var versionTest = []struct {
|
var versionTest = []struct {
|
||||||
|
name string
|
||||||
args string
|
args string
|
||||||
regex string
|
regex string
|
||||||
expected bool
|
expected bool
|
||||||
}{
|
}{
|
||||||
{"--output=valid", "", false},
|
{"invalid output option", "--output=valid", "", false},
|
||||||
{"--output=short", ShortExpectedRegex, true},
|
{"short output", "--output=short", ShortExpectedRegex, true},
|
||||||
{"", NormalExpectedRegex, true},
|
{"default output", "", NormalExpectedRegex, true},
|
||||||
}
|
}
|
||||||
|
|
||||||
kubeadmPath := getKubeadmPath()
|
kubeadmPath := getKubeadmPath()
|
||||||
for _, rt := range versionTest {
|
for _, rt := range versionTest {
|
||||||
|
t.Run(rt.name, func(t *testing.T) {
|
||||||
stdout, _, actual := RunCmd(kubeadmPath, "version", rt.args)
|
stdout, _, actual := RunCmd(kubeadmPath, "version", rt.args)
|
||||||
if (actual == nil) != rt.expected {
|
if (actual == nil) != rt.expected {
|
||||||
t.Errorf(
|
t.Errorf(
|
||||||
@ -71,6 +73,7 @@ func TestCmdVersion(t *testing.T) {
|
|||||||
t.Errorf("'kubeadm version %s' stdout did not match expected regex; wanted: [%q], got: [%s]", rt.args, rt.regex, stdout)
|
t.Errorf("'kubeadm version %s' stdout did not match expected regex; wanted: [%q], got: [%s]", rt.args, rt.regex, stdout)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,16 +84,18 @@ func TestCmdVersionOutputJsonOrYaml(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var versionTest = []struct {
|
var versionTest = []struct {
|
||||||
|
name string
|
||||||
args string
|
args string
|
||||||
format string
|
format string
|
||||||
expected bool
|
expected bool
|
||||||
}{
|
}{
|
||||||
{"--output=json", "json", true},
|
{"json output", "--output=json", "json", true},
|
||||||
{"--output=yaml", "yaml", true},
|
{"yaml output", "--output=yaml", "yaml", true},
|
||||||
}
|
}
|
||||||
|
|
||||||
kubeadmPath := getKubeadmPath()
|
kubeadmPath := getKubeadmPath()
|
||||||
for _, rt := range versionTest {
|
for _, rt := range versionTest {
|
||||||
|
t.Run(rt.name, func(t *testing.T) {
|
||||||
stdout, _, actual := RunCmd(kubeadmPath, "version", rt.args)
|
stdout, _, actual := RunCmd(kubeadmPath, "version", rt.args)
|
||||||
if (actual == nil) != rt.expected {
|
if (actual == nil) != rt.expected {
|
||||||
t.Errorf(
|
t.Errorf(
|
||||||
@ -128,5 +133,6 @@ func TestCmdVersionOutputJsonOrYaml(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user