use subtest for table units (pkg/kubectl)

This commit is contained in:
Guoliang Wang
2018-05-12 17:24:28 +08:00
parent d057795f3b
commit bae074ef38
34 changed files with 1841 additions and 1301 deletions

View File

@@ -25,35 +25,43 @@ import (
func TestEnv(t *testing.T) {
tests := []struct {
name string
env Env
expected string
}{
{
name: "test1",
env: Env{"FOO", "BAR"},
expected: "FOO=BAR",
},
{
name: "test2",
env: Env{"FOO", "BAR="},
expected: "FOO=BAR=",
},
{
name: "test3",
env: Env{"FOO", ""},
expected: "FOO=",
},
}
for _, test := range tests {
if s := test.env.String(); s != test.expected {
t.Errorf("%v: expected string %q, got %q", test.env, test.expected, s)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if s := tt.env.String(); s != tt.expected {
t.Errorf("%v: expected string %q, got %q", tt.env, tt.expected, s)
}
})
}
}
func TestEnvListToSlice(t *testing.T) {
tests := []struct {
name string
env EnvList
expected []string
}{
{
name: "test1",
env: EnvList{
{"FOO", "BAR"},
{"ZEE", "YO"},
@@ -64,20 +72,24 @@ func TestEnvListToSlice(t *testing.T) {
expected: []string{"FOO=BAR", "ZEE=YO", "ONE=1", "EQUALS===", "EMPTY="},
},
}
for _, test := range tests {
if s := test.env.Slice(); !reflect.DeepEqual(test.expected, s) {
t.Errorf("%v: expected %v, got %v", test.env, test.expected, s)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if s := tt.env.Slice(); !reflect.DeepEqual(tt.expected, s) {
t.Errorf("%v: expected %v, got %v", tt.env, tt.expected, s)
}
})
}
}
func TestAddToEnvList(t *testing.T) {
tests := []struct {
name string
add []string
expected EnvList
}{
{
add: []string{"FOO=BAR", "EMPTY=", "EQUALS===", "JUSTNAME"},
name: "test1",
add: []string{"FOO=BAR", "EMPTY=", "EQUALS===", "JUSTNAME"},
expected: EnvList{
{"FOO", "BAR"},
{"EMPTY", ""},
@@ -86,11 +98,13 @@ func TestAddToEnvList(t *testing.T) {
},
},
}
for _, test := range tests {
env := EnvList{}.Merge(test.add...)
if !reflect.DeepEqual(test.expected, env) {
t.Errorf("%v: expected %v, got %v", test.add, test.expected, env)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
env := EnvList{}.Merge(tt.add...)
if !reflect.DeepEqual(tt.expected, env) {
t.Errorf("%v: expected %v, got %v", tt.add, tt.expected, env)
}
})
}
}
@@ -102,37 +116,45 @@ func TestFlagToEnv(t *testing.T) {
flags.Parse([]string{"--from-file=nondefault"})
tests := []struct {
name string
flag *pflag.Flag
prefix string
expected Env
}{
{
name: "test1",
flag: flags.Lookup("test"),
expected: Env{"TEST", "ok"},
},
{
name: "test2",
flag: flags.Lookup("kube-master"),
expected: Env{"KUBE_MASTER", "http://something"},
},
{
name: "test3",
prefix: "KUBECTL_",
flag: flags.Lookup("from-file"),
expected: Env{"KUBECTL_FROM_FILE", "nondefault"},
},
}
for _, test := range tests {
if env := FlagToEnv(test.flag, test.prefix); !reflect.DeepEqual(test.expected, env) {
t.Errorf("%v: expected %v, got %v", test.flag.Name, test.expected, env)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if env := FlagToEnv(tt.flag, tt.prefix); !reflect.DeepEqual(tt.expected, env) {
t.Errorf("%v: expected %v, got %v", tt.flag.Name, tt.expected, env)
}
})
}
}
func TestPluginDescriptorEnvProvider(t *testing.T) {
tests := []struct {
name string
plugin *Plugin
expected EnvList
}{
{
name: "test1",
plugin: &Plugin{
Description: Description{
Name: "test",
@@ -149,14 +171,16 @@ func TestPluginDescriptorEnvProvider(t *testing.T) {
},
},
}
for _, test := range tests {
provider := &PluginDescriptorEnvProvider{
Plugin: test.plugin,
}
env, _ := provider.Env()
if !reflect.DeepEqual(test.expected, env) {
t.Errorf("%v: expected %v, got %v", test.plugin.Name, test.expected, env)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
provider := &PluginDescriptorEnvProvider{
Plugin: tt.plugin,
}
env, _ := provider.Env()
if !reflect.DeepEqual(tt.expected, env) {
t.Errorf("%v: expected %v, got %v", tt.plugin.Name, tt.expected, env)
}
})
}
}

View File

@@ -20,10 +20,12 @@ import "testing"
func TestPlugin(t *testing.T) {
tests := []struct {
name string
plugin *Plugin
expectedErr error
}{
{
name: "test1",
plugin: &Plugin{
Description: Description{
Name: "test",
@@ -33,6 +35,7 @@ func TestPlugin(t *testing.T) {
},
},
{
name: "test2",
plugin: &Plugin{
Description: Description{
Name: "test",
@@ -42,10 +45,12 @@ func TestPlugin(t *testing.T) {
expectedErr: ErrIncompletePlugin,
},
{
name: "test3",
plugin: &Plugin{},
expectedErr: ErrIncompletePlugin,
},
{
name: "test4",
plugin: &Plugin{
Description: Description{
Name: "test spaces",
@@ -56,6 +61,7 @@ func TestPlugin(t *testing.T) {
expectedErr: ErrInvalidPluginName,
},
{
name: "test5",
plugin: &Plugin{
Description: Description{
Name: "test",
@@ -71,6 +77,7 @@ func TestPlugin(t *testing.T) {
expectedErr: ErrIncompleteFlag,
},
{
name: "test6",
plugin: &Plugin{
Description: Description{
Name: "test",
@@ -87,6 +94,7 @@ func TestPlugin(t *testing.T) {
expectedErr: ErrInvalidFlagName,
},
{
name: "test7",
plugin: &Plugin{
Description: Description{
Name: "test",
@@ -104,6 +112,7 @@ func TestPlugin(t *testing.T) {
expectedErr: ErrInvalidFlagShorthand,
},
{
name: "test8",
plugin: &Plugin{
Description: Description{
Name: "test",
@@ -121,6 +130,7 @@ func TestPlugin(t *testing.T) {
expectedErr: ErrInvalidFlagShorthand,
},
{
name: "test9",
plugin: &Plugin{
Description: Description{
Name: "test",
@@ -160,10 +170,12 @@ func TestPlugin(t *testing.T) {
},
}
for _, test := range tests {
err := test.plugin.Validate()
if err != test.expectedErr {
t.Errorf("%s: expected error %v, got %v", test.plugin.Name, test.expectedErr, err)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.plugin.Validate()
if err != tt.expectedErr {
t.Errorf("%s: expected error %v, got %v", tt.plugin.Name, tt.expectedErr, err)
}
})
}
}

View File

@@ -50,33 +50,34 @@ func TestExecRunner(t *testing.T) {
os.Setenv("KUBECTL_PLUGINS_TEST", "ok")
defer os.Unsetenv("KUBECTL_PLUGINS_TEST")
for _, test := range tests {
streams, _, outBuf, _ := genericclioptions.NewTestIOStreams()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
streams, _, outBuf, _ := genericclioptions.NewTestIOStreams()
plugin := &Plugin{
Description: Description{
Name: test.name,
ShortDesc: "Test Runner Plugin",
Command: test.command,
},
}
plugin := &Plugin{
Description: Description{
Name: tt.name,
ShortDesc: "Test Runner Plugin",
Command: tt.command,
},
}
ctx := RunningContext{
IOStreams: streams,
WorkingDir: ".",
EnvProvider: &EmptyEnvProvider{},
}
ctx := RunningContext{
IOStreams: streams,
WorkingDir: ".",
EnvProvider: &EmptyEnvProvider{},
}
runner := &ExecPluginRunner{}
err := runner.Run(plugin, ctx)
runner := &ExecPluginRunner{}
err := runner.Run(plugin, ctx)
if outBuf.String() != test.expectedMsg {
t.Errorf("%s: unexpected output: %q", test.name, outBuf.String())
}
if outBuf.String() != tt.expectedMsg {
t.Errorf("%s: unexpected output: %q", tt.name, outBuf.String())
}
if err != nil && err.Error() != test.expectedErr {
t.Errorf("%s: unexpected err output: %v", test.name, err)
}
if err != nil && err.Error() != tt.expectedErr {
t.Errorf("%s: unexpected err output: %v", tt.name, err)
}
})
}
}