Merge pull request #72457 from bart0sh/PR0053-kubeadm-app-cmd-use-T.Run

kubeadm: use T.Run API in app/cmd
This commit is contained in:
Kubernetes Prow Robot 2019-01-01 04:00:36 -08:00 committed by GitHub
commit b502b99d25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 263 additions and 215 deletions

View File

@ -52,16 +52,19 @@ func TestKubeConfigSubCommandsThatWritesToOut(t *testing.T) {
} }
var tests = []struct { var tests = []struct {
name string
command string command string
withClientCert bool withClientCert bool
withToken bool withToken bool
additionalFlags []string additionalFlags []string
}{ }{
{ // Test user subCommand withClientCert {
name: "user subCommand withClientCert",
command: "user", command: "user",
withClientCert: true, withClientCert: true,
}, },
{ // Test user subCommand withToken {
name: "user subCommand withToken",
withToken: true, withToken: true,
command: "user", command: "user",
additionalFlags: []string{"--token=123456"}, additionalFlags: []string{"--token=123456"},
@ -69,6 +72,7 @@ func TestKubeConfigSubCommandsThatWritesToOut(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
// Get subcommands working in the temporary directory // Get subcommands working in the temporary directory
@ -85,7 +89,7 @@ func TestKubeConfigSubCommandsThatWritesToOut(t *testing.T) {
config, err := clientcmd.Load(buf.Bytes()) config, err := clientcmd.Load(buf.Bytes())
if err != nil { if err != nil {
t.Errorf("couldn't read kubeconfig file from buffer: %v", err) t.Errorf("couldn't read kubeconfig file from buffer: %v", err)
continue return
} }
// checks that CLI flags are properly propagated // checks that CLI flags are properly propagated
@ -100,5 +104,6 @@ func TestKubeConfigSubCommandsThatWritesToOut(t *testing.T) {
// checks that kubeconfig files have expected token // checks that kubeconfig files have expected token
kubeconfigtestutil.AssertKubeConfigCurrentAuthInfoWithToken(t, config, "myUser", "123456") kubeconfigtestutil.AssertKubeConfigCurrentAuthInfoWithToken(t, config, "myUser", "123456")
} }
})
} }
} }

View File

@ -88,8 +88,10 @@ func TestRunCompletion(t *testing.T) {
parentCmd.AddCommand(cmd) parentCmd.AddCommand(cmd)
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if err := RunCompletion(&out, "", cmd, tc.args); (err != nil) != tc.expectedError { if err := RunCompletion(&out, "", cmd, tc.args); (err != nil) != tc.expectedError {
t.Errorf("Test case %q: TestRunCompletion expected error: %v, saw: %v", tc.name, tc.expectedError, (err != nil)) t.Errorf("Test case %q: TestRunCompletion expected error: %v, saw: %v", tc.name, tc.expectedError, (err != nil))
} }
})
} }
} }

View File

@ -195,6 +195,7 @@ func TestConfigDirCleaner(t *testing.T) {
} }
for name, test := range tests { for name, test := range tests {
t.Run(name, func(t *testing.T) {
t.Logf("Running test: %s", name) t.Logf("Running test: %s", name)
// Create a temporary directory for our fake config dir: // Create a temporary directory for our fake config dir:
@ -240,6 +241,7 @@ func TestConfigDirCleaner(t *testing.T) {
} }
os.RemoveAll(tmpDir) os.RemoveAll(tmpDir)
})
} }
} }
@ -309,6 +311,7 @@ func TestGetEtcdDataDir(t *testing.T) {
} }
for name, test := range tests { for name, test := range tests {
t.Run(name, func(t *testing.T) {
tmpdir := testutil.SetupTempDir(t) tmpdir := testutil.SetupTempDir(t)
defer os.RemoveAll(tmpdir) defer os.RemoveAll(tmpdir)
@ -342,5 +345,6 @@ func TestGetEtcdDataDir(t *testing.T) {
if dataDir != test.dataDir { if dataDir != test.dataDir {
t.Fatalf(dedent.Dedent("getEtcdDataDir failed\n%s\n\texpected: %s\ngot: %s"), name, test.dataDir, dataDir) t.Fatalf(dedent.Dedent("getEtcdDataDir failed\n%s\n\texpected: %s\ngot: %s"), name, test.dataDir, dataDir)
} }
})
} }
} }

View File

@ -167,6 +167,7 @@ func TestRunCreateToken(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
bts, err := kubeadmapiv1beta1.NewBootstrapTokenString(tc.token) bts, err := kubeadmapiv1beta1.NewBootstrapTokenString(tc.token)
if err != nil && len(tc.token) != 0 { // if tc.token is "" it's okay as it will be generated later at runtime if err != nil && len(tc.token) != 0 { // if tc.token is "" it's okay as it will be generated later at runtime
t.Fatalf("token couldn't be parsed for testing: %v", err) t.Fatalf("token couldn't be parsed for testing: %v", err)
@ -192,6 +193,7 @@ func TestRunCreateToken(t *testing.T) {
if (err != nil) != tc.expectedError { if (err != nil) != tc.expectedError {
t.Errorf("Test case %s: RunCreateToken expected error: %v, saw: %v", tc.name, tc.expectedError, (err != nil)) t.Errorf("Test case %s: RunCreateToken expected error: %v, saw: %v", tc.name, tc.expectedError, (err != nil))
} }
})
} }
} }
@ -253,6 +255,7 @@ func TestNewCmdToken(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// the command is created for each test so that the kubeConfigFile // the command is created for each test so that the kubeConfigFile
// variable in NewCmdToken() is reset. // variable in NewCmdToken() is reset.
cmd := NewCmdToken(&buf, &bufErr) cmd := NewCmdToken(&buf, &bufErr)
@ -271,6 +274,7 @@ func TestNewCmdToken(t *testing.T) {
} }
// restore the environment variable. // restore the environment variable.
os.Setenv(clientcmd.RecommendedConfigPathEnvVar, storedEnv) os.Setenv(clientcmd.RecommendedConfigPathEnvVar, storedEnv)
})
} }
} }

View File

@ -28,11 +28,13 @@ import (
func TestSetImplicitFlags(t *testing.T) { func TestSetImplicitFlags(t *testing.T) {
var tests = []struct { var tests = []struct {
name string
flags *applyFlags flags *applyFlags
expectedFlags applyFlags expectedFlags applyFlags
errExpected bool errExpected bool
}{ }{
{ // if not dryRun or force is set; the nonInteractiveMode field should not be touched {
name: "if not dryRun or force is set; the nonInteractiveMode field should not be touched",
flags: &applyFlags{ flags: &applyFlags{
newK8sVersionStr: "v1.8.0", newK8sVersionStr: "v1.8.0",
dryRun: false, dryRun: false,
@ -46,7 +48,8 @@ func TestSetImplicitFlags(t *testing.T) {
nonInteractiveMode: false, nonInteractiveMode: false,
}, },
}, },
{ // if not dryRun or force is set; the nonInteractiveMode field should not be touched {
name: "if not dryRun or force is set; the nonInteractiveMode field should not be touched",
flags: &applyFlags{ flags: &applyFlags{
newK8sVersionStr: "v1.8.0", newK8sVersionStr: "v1.8.0",
dryRun: false, dryRun: false,
@ -60,7 +63,8 @@ func TestSetImplicitFlags(t *testing.T) {
nonInteractiveMode: true, nonInteractiveMode: true,
}, },
}, },
{ // if dryRun or force is set; the nonInteractiveMode field should be set to true {
name: "if dryRun or force is set; the nonInteractiveMode field should be set to true",
flags: &applyFlags{ flags: &applyFlags{
newK8sVersionStr: "v1.8.0", newK8sVersionStr: "v1.8.0",
dryRun: true, dryRun: true,
@ -74,7 +78,8 @@ func TestSetImplicitFlags(t *testing.T) {
nonInteractiveMode: true, nonInteractiveMode: true,
}, },
}, },
{ // if dryRun or force is set; the nonInteractiveMode field should be set to true {
name: "if dryRun or force is set; the nonInteractiveMode field should be set to true",
flags: &applyFlags{ flags: &applyFlags{
newK8sVersionStr: "v1.8.0", newK8sVersionStr: "v1.8.0",
dryRun: false, dryRun: false,
@ -88,7 +93,8 @@ func TestSetImplicitFlags(t *testing.T) {
nonInteractiveMode: true, nonInteractiveMode: true,
}, },
}, },
{ // if dryRun or force is set; the nonInteractiveMode field should be set to true {
name: "if dryRun or force is set; the nonInteractiveMode field should be set to true",
flags: &applyFlags{ flags: &applyFlags{
newK8sVersionStr: "v1.8.0", newK8sVersionStr: "v1.8.0",
dryRun: true, dryRun: true,
@ -102,7 +108,8 @@ func TestSetImplicitFlags(t *testing.T) {
nonInteractiveMode: true, nonInteractiveMode: true,
}, },
}, },
{ // if dryRun or force is set; the nonInteractiveMode field should be set to true {
name: "if dryRun or force is set; the nonInteractiveMode field should be set to true",
flags: &applyFlags{ flags: &applyFlags{
newK8sVersionStr: "v1.8.0", newK8sVersionStr: "v1.8.0",
dryRun: true, dryRun: true,
@ -116,7 +123,8 @@ func TestSetImplicitFlags(t *testing.T) {
nonInteractiveMode: true, nonInteractiveMode: true,
}, },
}, },
{ // if the new version is empty; it should error out {
name: "if the new version is empty; it should error out",
flags: &applyFlags{ flags: &applyFlags{
newK8sVersionStr: "", newK8sVersionStr: "",
}, },
@ -127,6 +135,7 @@ func TestSetImplicitFlags(t *testing.T) {
}, },
} }
for _, rt := range tests { for _, rt := range tests {
t.Run(rt.name, func(t *testing.T) {
actualErr := SetImplicitFlags(rt.flags) actualErr := SetImplicitFlags(rt.flags)
// If an error was returned; make newK8sVersion nil so it's easy to match using reflect.DeepEqual later (instead of a random pointer) // If an error was returned; make newK8sVersion nil so it's easy to match using reflect.DeepEqual later (instead of a random pointer)
@ -148,6 +157,7 @@ func TestSetImplicitFlags(t *testing.T) {
(actualErr != nil), (actualErr != nil),
) )
} }
})
} }
} }

View File

@ -25,15 +25,18 @@ import (
func TestPrintConfiguration(t *testing.T) { func TestPrintConfiguration(t *testing.T) {
var tests = []struct { var tests = []struct {
name string
cfg *kubeadmapi.ClusterConfiguration cfg *kubeadmapi.ClusterConfiguration
buf *bytes.Buffer buf *bytes.Buffer
expectedBytes []byte expectedBytes []byte
}{ }{
{ {
name: "config is nil",
cfg: nil, cfg: nil,
expectedBytes: []byte(""), expectedBytes: []byte(""),
}, },
{ {
name: "cluster config with local Etcd",
cfg: &kubeadmapi.ClusterConfiguration{ cfg: &kubeadmapi.ClusterConfiguration{
KubernetesVersion: "v1.7.1", KubernetesVersion: "v1.7.1",
Etcd: kubeadmapi.Etcd{ Etcd: kubeadmapi.Etcd{
@ -67,6 +70,7 @@ func TestPrintConfiguration(t *testing.T) {
`), `),
}, },
{ {
name: "cluster config with ServiceSubnet and external Etcd",
cfg: &kubeadmapi.ClusterConfiguration{ cfg: &kubeadmapi.ClusterConfiguration{
KubernetesVersion: "v1.7.1", KubernetesVersion: "v1.7.1",
Networking: kubeadmapi.Networking{ Networking: kubeadmapi.Networking{
@ -108,6 +112,7 @@ func TestPrintConfiguration(t *testing.T) {
}, },
} }
for _, rt := range tests { for _, rt := range tests {
t.Run(rt.name, func(t *testing.T) {
rt.buf = bytes.NewBufferString("") rt.buf = bytes.NewBufferString("")
printConfiguration(rt.cfg, rt.buf) printConfiguration(rt.cfg, rt.buf)
actualBytes := rt.buf.Bytes() actualBytes := rt.buf.Bytes()
@ -118,5 +123,6 @@ func TestPrintConfiguration(t *testing.T) {
string(actualBytes), string(actualBytes),
) )
} }
})
} }
} }

View File

@ -27,27 +27,33 @@ import (
func TestSortedSliceFromStringIntMap(t *testing.T) { func TestSortedSliceFromStringIntMap(t *testing.T) {
var tests = []struct { var tests = []struct {
name string
strMap map[string]uint16 strMap map[string]uint16
expectedSlice []string expectedSlice []string
}{ // The returned slice should be alphabetically sorted based on the string keys in the map }{
{ {
name: "the returned slice should be alphabetically sorted based on the string keys in the map",
strMap: map[string]uint16{"foo": 1, "bar": 2}, strMap: map[string]uint16{"foo": 1, "bar": 2},
expectedSlice: []string{"bar", "foo"}, expectedSlice: []string{"bar", "foo"},
}, },
{ // The int value should not affect this func {
name: "the int value should not affect this func",
strMap: map[string]uint16{"foo": 2, "bar": 1}, strMap: map[string]uint16{"foo": 2, "bar": 1},
expectedSlice: []string{"bar", "foo"}, expectedSlice: []string{"bar", "foo"},
}, },
{ {
name: "slice with 4 keys and different values",
strMap: map[string]uint16{"b": 2, "a": 1, "cb": 0, "ca": 1000}, strMap: map[string]uint16{"b": 2, "a": 1, "cb": 0, "ca": 1000},
expectedSlice: []string{"a", "b", "ca", "cb"}, expectedSlice: []string{"a", "b", "ca", "cb"},
}, },
{ // This should work for version numbers as well; and the lowest version should come first {
name: "this should work for version numbers as well; and the lowest version should come first",
strMap: map[string]uint16{"v1.7.0": 1, "v1.6.1": 1, "v1.6.2": 1, "v1.8.0": 1, "v1.8.0-alpha.1": 1}, strMap: map[string]uint16{"v1.7.0": 1, "v1.6.1": 1, "v1.6.2": 1, "v1.8.0": 1, "v1.8.0-alpha.1": 1},
expectedSlice: []string{"v1.6.1", "v1.6.2", "v1.7.0", "v1.8.0", "v1.8.0-alpha.1"}, expectedSlice: []string{"v1.6.1", "v1.6.2", "v1.7.0", "v1.8.0", "v1.8.0-alpha.1"},
}, },
} }
for _, rt := range tests { for _, rt := range tests {
t.Run(rt.name, func(t *testing.T) {
actualSlice := sortedSliceFromStringIntMap(rt.strMap) actualSlice := sortedSliceFromStringIntMap(rt.strMap)
if !reflect.DeepEqual(actualSlice, rt.expectedSlice) { if !reflect.DeepEqual(actualSlice, rt.expectedSlice) {
t.Errorf( t.Errorf(
@ -56,6 +62,7 @@ func TestSortedSliceFromStringIntMap(t *testing.T) {
actualSlice, actualSlice,
) )
} }
})
} }
} }

View File

@ -22,36 +22,43 @@ import (
func TestValidateExactArgNumber(t *testing.T) { func TestValidateExactArgNumber(t *testing.T) {
var tests = []struct { var tests = []struct {
name string
args, supportedArgs []string args, supportedArgs []string
expectedErr bool expectedErr bool
}{ }{
{ // one arg given and one arg expected {
name: "one arg given and one arg expected",
args: []string{"my-node-1234"}, args: []string{"my-node-1234"},
supportedArgs: []string{"node-name"}, supportedArgs: []string{"node-name"},
expectedErr: false, expectedErr: false,
}, },
{ // two args given and two args expected {
name: "two args given and two args expected",
args: []string{"my-node-1234", "foo"}, args: []string{"my-node-1234", "foo"},
supportedArgs: []string{"node-name", "second-toplevel-arg"}, supportedArgs: []string{"node-name", "second-toplevel-arg"},
expectedErr: false, expectedErr: false,
}, },
{ // too few supplied args {
name: "too few supplied args",
args: []string{}, args: []string{},
supportedArgs: []string{"node-name"}, supportedArgs: []string{"node-name"},
expectedErr: true, expectedErr: true,
}, },
{ // too few non-empty args {
name: "too few non-empty args",
args: []string{""}, args: []string{""},
supportedArgs: []string{"node-name"}, supportedArgs: []string{"node-name"},
expectedErr: true, expectedErr: true,
}, },
{ // too many args {
name: "too many args",
args: []string{"my-node-1234", "foo"}, args: []string{"my-node-1234", "foo"},
supportedArgs: []string{"node-name"}, supportedArgs: []string{"node-name"},
expectedErr: true, expectedErr: true,
}, },
} }
for _, rt := range tests { for _, rt := range tests {
t.Run(rt.name, func(t *testing.T) {
actual := ValidateExactArgNumber(rt.args, rt.supportedArgs) actual := ValidateExactArgNumber(rt.args, rt.supportedArgs)
if (actual != nil) != rt.expectedErr { if (actual != nil) != rt.expectedErr {
t.Errorf( t.Errorf(
@ -60,5 +67,6 @@ func TestValidateExactArgNumber(t *testing.T) {
(actual != nil), (actual != nil),
) )
} }
})
} }
} }

View File

@ -69,6 +69,7 @@ func TestRunVersion(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var err error var err error
if len(tc.flag) > 0 { if len(tc.flag) > 0 {
if err = cmd.Flags().Set(flagNameOutput, tc.flag); err != nil { if err = cmd.Flags().Set(flagNameOutput, tc.flag); err != nil {
@ -92,5 +93,6 @@ func TestRunVersion(t *testing.T) {
if (err != nil) != tc.expectedError { if (err != nil) != tc.expectedError {
t.Errorf("Test case %q: RunVersion expected error: %v, saw: %v; %v", tc.name, tc.expectedError, err != nil, err) t.Errorf("Test case %q: RunVersion expected error: %v, saw: %v; %v", tc.name, tc.expectedError, err != nil, err)
} }
})
} }
} }