Merge pull request #63776 from wgliang/master.test-printers

Automatic merge from submit-queue (batch tested with PRs 63770, 63776, 64001, 64028, 63984). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

use subtest for table units (pkg/printers)

**What this PR does / why we need it**:

Go 1.7 added the subtest feature which can make table-driven tests much easier to run and debug. Many table-driven tests in pkg/kubectl are not using this feature.

/kind cleanup

Further reading: [Using Subtests and Sub-benchmarks](https://blog.golang.org/subtests)

**Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*:
Fixes #

**Special notes for your reviewer**:

**Release note**:

```release-note
NONE
```
This commit is contained in:
Kubernetes Submit Queue 2018-05-19 02:11:30 -07:00 committed by GitHub
commit a915811a98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 288 additions and 215 deletions

View File

@ -47,20 +47,22 @@ func TestMassageJSONPath(t *testing.T) {
{input: "{{foo.bar}", expectErr: true}, {input: "{{foo.bar}", expectErr: true},
} }
for _, test := range tests { for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
output, err := printers.RelaxedJSONPathExpression(test.input) output, err := printers.RelaxedJSONPathExpression(test.input)
if err != nil && !test.expectErr { if err != nil && !test.expectErr {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
continue return
} }
if test.expectErr { if test.expectErr {
if err == nil { if err == nil {
t.Error("unexpected non-error") t.Error("unexpected non-error")
} }
continue return
} }
if output != test.expectedOutput { if output != test.expectedOutput {
t.Errorf("input: %s, expected: %s, saw: %s", test.input, test.expectedOutput, output) t.Errorf("input: %s, expected: %s, saw: %s", test.input, test.expectedOutput, output)
} }
})
} }
} }
@ -113,16 +115,17 @@ func TestNewColumnPrinterFromSpec(t *testing.T) {
}, },
} }
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
printer, err := printers.NewCustomColumnsPrinterFromSpec(test.spec, legacyscheme.Codecs.UniversalDecoder(), test.noHeaders) printer, err := printers.NewCustomColumnsPrinterFromSpec(test.spec, legacyscheme.Codecs.UniversalDecoder(), test.noHeaders)
if test.expectErr { if test.expectErr {
if err == nil { if err == nil {
t.Errorf("[%s] unexpected non-error", test.name) t.Errorf("[%s] unexpected non-error", test.name)
} }
continue return
} }
if !test.expectErr && err != nil { if !test.expectErr && err != nil {
t.Errorf("[%s] unexpected error: %v", test.name, err) t.Errorf("[%s] unexpected error: %v", test.name, err)
continue return
} }
if test.noHeaders { if test.noHeaders {
buffer := &bytes.Buffer{} buffer := &bytes.Buffer{}
@ -139,7 +142,7 @@ func TestNewColumnPrinterFromSpec(t *testing.T) {
} else if !reflect.DeepEqual(test.expectedColumns, printer.Columns) { } else if !reflect.DeepEqual(test.expectedColumns, printer.Columns) {
t.Errorf("[%s]\nexpected:\n%v\nsaw:\n%v\n", test.name, test.expectedColumns, printer.Columns) t.Errorf("[%s]\nexpected:\n%v\nsaw:\n%v\n", test.name, test.expectedColumns, printer.Columns)
} }
})
} }
} }
@ -215,23 +218,24 @@ func TestNewColumnPrinterFromTemplate(t *testing.T) {
}, },
} }
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
reader := bytes.NewBufferString(test.spec) reader := bytes.NewBufferString(test.spec)
printer, err := printers.NewCustomColumnsPrinterFromTemplate(reader, legacyscheme.Codecs.UniversalDecoder()) printer, err := printers.NewCustomColumnsPrinterFromTemplate(reader, legacyscheme.Codecs.UniversalDecoder())
if test.expectErr { if test.expectErr {
if err == nil { if err == nil {
t.Errorf("[%s] unexpected non-error", test.name) t.Errorf("[%s] unexpected non-error", test.name)
} }
continue return
} }
if !test.expectErr && err != nil { if !test.expectErr && err != nil {
t.Errorf("[%s] unexpected error: %v", test.name, err) t.Errorf("[%s] unexpected error: %v", test.name, err)
continue return
} }
if !reflect.DeepEqual(test.expectedColumns, printer.Columns) { if !reflect.DeepEqual(test.expectedColumns, printer.Columns) {
t.Errorf("[%s]\nexpected:\n%v\nsaw:\n%v\n", test.name, test.expectedColumns, printer.Columns) t.Errorf("[%s]\nexpected:\n%v\nsaw:\n%v\n", test.name, test.expectedColumns, printer.Columns)
} }
})
} }
} }
@ -310,6 +314,7 @@ foo baz <none>
} }
for _, test := range tests { for _, test := range tests {
t.Run(test.expectedOutput, func(t *testing.T) {
printer := &printers.CustomColumnsPrinter{ printer := &printers.CustomColumnsPrinter{
Columns: test.columns, Columns: test.columns,
Decoder: legacyscheme.Codecs.UniversalDecoder(), Decoder: legacyscheme.Codecs.UniversalDecoder(),
@ -321,6 +326,7 @@ foo baz <none>
if buffer.String() != test.expectedOutput { if buffer.String() != test.expectedOutput {
t.Errorf("\nexpected:\n'%s'\nsaw\n'%s'\n", test.expectedOutput, buffer.String()) t.Errorf("\nexpected:\n'%s'\nsaw\n'%s'\n", test.expectedOutput, buffer.String())
} }
})
} }
} }

View File

@ -48,7 +48,8 @@ func testPrintNamespace(obj *api.Namespace, options PrintOptions) ([]metav1beta1
func TestPrintRowsForHandlerEntry(t *testing.T) { func TestPrintRowsForHandlerEntry(t *testing.T) {
printFunc := reflect.ValueOf(testPrintNamespace) printFunc := reflect.ValueOf(testPrintNamespace)
testCase := map[string]struct { testCase := []struct {
name string
h *handlerEntry h *handlerEntry
opt PrintOptions opt PrintOptions
obj runtime.Object obj runtime.Object
@ -56,7 +57,8 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
expectOut string expectOut string
expectErr string expectErr string
}{ }{
"no tablecolumndefinition and includeheader flase": { {
name: "no tablecolumndefinition and includeheader flase",
h: &handlerEntry{ h: &handlerEntry{
columnDefinitions: []metav1beta1.TableColumnDefinition{}, columnDefinitions: []metav1beta1.TableColumnDefinition{},
printRows: true, printRows: true,
@ -69,7 +71,8 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
includeHeader: false, includeHeader: false,
expectOut: "test\t\t<unknow>\n", expectOut: "test\t\t<unknow>\n",
}, },
"no tablecolumndefinition and includeheader true": { {
name: "no tablecolumndefinition and includeheader true",
h: &handlerEntry{ h: &handlerEntry{
columnDefinitions: []metav1beta1.TableColumnDefinition{}, columnDefinitions: []metav1beta1.TableColumnDefinition{},
printRows: true, printRows: true,
@ -82,7 +85,8 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
includeHeader: true, includeHeader: true,
expectOut: "\ntest\t\t<unknow>\n", expectOut: "\ntest\t\t<unknow>\n",
}, },
"have tablecolumndefinition and includeheader true": { {
name: "have tablecolumndefinition and includeheader true",
h: &handlerEntry{ h: &handlerEntry{
columnDefinitions: testNamespaceColumnDefinitions, columnDefinitions: testNamespaceColumnDefinitions,
printRows: true, printRows: true,
@ -95,7 +99,8 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
includeHeader: true, includeHeader: true,
expectOut: "NAME\tSTATUS\tAGE\ntest\t\t<unknow>\n", expectOut: "NAME\tSTATUS\tAGE\ntest\t\t<unknow>\n",
}, },
"print namespace and withnamespace true, should not print header": { {
name: "print namespace and withnamespace true, should not print header",
h: &handlerEntry{ h: &handlerEntry{
columnDefinitions: testNamespaceColumnDefinitions, columnDefinitions: testNamespaceColumnDefinitions,
printRows: true, printRows: true,
@ -112,16 +117,18 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
expectErr: "namespace is not namespaced", expectErr: "namespace is not namespaced",
}, },
} }
for name, test := range testCase { for _, test := range testCase {
t.Run(test.name, func(t *testing.T) {
buffer := &bytes.Buffer{} buffer := &bytes.Buffer{}
err := printRowsForHandlerEntry(buffer, test.h, test.obj, test.opt, test.includeHeader) err := printRowsForHandlerEntry(buffer, test.h, test.obj, test.opt, test.includeHeader)
if err != nil { if err != nil {
if err.Error() != test.expectErr { if err.Error() != test.expectErr {
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", name, test.expectErr, err) t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", test.name, test.expectErr, err)
} }
} }
if test.expectOut != buffer.String() { if test.expectOut != buffer.String() {
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", name, test.expectOut, buffer.String()) t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", test.name, test.expectOut, buffer.String())
} }
})
} }
} }

View File

@ -283,10 +283,12 @@ func getResourceList(cpu, memory string) api.ResourceList {
func TestDescribeService(t *testing.T) { func TestDescribeService(t *testing.T) {
testCases := []struct { testCases := []struct {
name string
service *api.Service service *api.Service
expect []string expect []string
}{ }{
{ {
name: "test1",
service: &api.Service{ service: &api.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "bar", Name: "bar",
@ -324,6 +326,7 @@ func TestDescribeService(t *testing.T) {
}, },
}, },
{ {
name: "test2",
service: &api.Service{ service: &api.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "bar", Name: "bar",
@ -362,6 +365,7 @@ func TestDescribeService(t *testing.T) {
}, },
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
fake := fake.NewSimpleClientset(testCase.service) fake := fake.NewSimpleClientset(testCase.service)
c := &describeClient{T: t, Namespace: "foo", Interface: fake} c := &describeClient{T: t, Namespace: "foo", Interface: fake}
d := ServiceDescriber{c} d := ServiceDescriber{c}
@ -374,6 +378,7 @@ func TestDescribeService(t *testing.T) {
t.Errorf("expected to find %q in output: %q", expected, out) t.Errorf("expected to find %q in output: %q", expected, out)
} }
} }
})
} }
} }
@ -453,12 +458,14 @@ func VerifyDatesInOrder(
func TestDescribeContainers(t *testing.T) { func TestDescribeContainers(t *testing.T) {
trueVal := true trueVal := true
testCases := []struct { testCases := []struct {
name string
container api.Container container api.Container
status api.ContainerStatus status api.ContainerStatus
expectedElements []string expectedElements []string
}{ }{
// Running state. // Running state.
{ {
name: "test1",
container: api.Container{Name: "test", Image: "image"}, container: api.Container{Name: "test", Image: "image"},
status: api.ContainerStatus{ status: api.ContainerStatus{
Name: "test", Name: "test",
@ -474,6 +481,7 @@ func TestDescribeContainers(t *testing.T) {
}, },
// Waiting state. // Waiting state.
{ {
name: "test2",
container: api.Container{Name: "test", Image: "image"}, container: api.Container{Name: "test", Image: "image"},
status: api.ContainerStatus{ status: api.ContainerStatus{
Name: "test", Name: "test",
@ -489,6 +497,7 @@ func TestDescribeContainers(t *testing.T) {
}, },
// Terminated state. // Terminated state.
{ {
name: "test3",
container: api.Container{Name: "test", Image: "image"}, container: api.Container{Name: "test", Image: "image"},
status: api.ContainerStatus{ status: api.ContainerStatus{
Name: "test", Name: "test",
@ -507,6 +516,7 @@ func TestDescribeContainers(t *testing.T) {
}, },
// Last Terminated // Last Terminated
{ {
name: "test4",
container: api.Container{Name: "test", Image: "image"}, container: api.Container{Name: "test", Image: "image"},
status: api.ContainerStatus{ status: api.ContainerStatus{
Name: "test", Name: "test",
@ -530,6 +540,7 @@ func TestDescribeContainers(t *testing.T) {
}, },
// No state defaults to waiting. // No state defaults to waiting.
{ {
name: "test5",
container: api.Container{Name: "test", Image: "image"}, container: api.Container{Name: "test", Image: "image"},
status: api.ContainerStatus{ status: api.ContainerStatus{
Name: "test", Name: "test",
@ -540,6 +551,7 @@ func TestDescribeContainers(t *testing.T) {
}, },
// Env // Env
{ {
name: "test6",
container: api.Container{Name: "test", Image: "image", Env: []api.EnvVar{{Name: "envname", Value: "xyz"}}, EnvFrom: []api.EnvFromSource{{ConfigMapRef: &api.ConfigMapEnvSource{LocalObjectReference: api.LocalObjectReference{Name: "a123"}}}}}, container: api.Container{Name: "test", Image: "image", Env: []api.EnvVar{{Name: "envname", Value: "xyz"}}, EnvFrom: []api.EnvFromSource{{ConfigMapRef: &api.ConfigMapEnvSource{LocalObjectReference: api.LocalObjectReference{Name: "a123"}}}}},
status: api.ContainerStatus{ status: api.ContainerStatus{
Name: "test", Name: "test",
@ -549,6 +561,7 @@ func TestDescribeContainers(t *testing.T) {
expectedElements: []string{"test", "State", "Waiting", "Ready", "True", "Restart Count", "7", "Image", "image", "envname", "xyz", "a123\tConfigMap\tOptional: false"}, expectedElements: []string{"test", "State", "Waiting", "Ready", "True", "Restart Count", "7", "Image", "image", "envname", "xyz", "a123\tConfigMap\tOptional: false"},
}, },
{ {
name: "test7",
container: api.Container{Name: "test", Image: "image", Env: []api.EnvVar{{Name: "envname", Value: "xyz"}}, EnvFrom: []api.EnvFromSource{{Prefix: "p_", ConfigMapRef: &api.ConfigMapEnvSource{LocalObjectReference: api.LocalObjectReference{Name: "a123"}}}}}, container: api.Container{Name: "test", Image: "image", Env: []api.EnvVar{{Name: "envname", Value: "xyz"}}, EnvFrom: []api.EnvFromSource{{Prefix: "p_", ConfigMapRef: &api.ConfigMapEnvSource{LocalObjectReference: api.LocalObjectReference{Name: "a123"}}}}},
status: api.ContainerStatus{ status: api.ContainerStatus{
Name: "test", Name: "test",
@ -558,6 +571,7 @@ func TestDescribeContainers(t *testing.T) {
expectedElements: []string{"test", "State", "Waiting", "Ready", "True", "Restart Count", "7", "Image", "image", "envname", "xyz", "a123\tConfigMap with prefix 'p_'\tOptional: false"}, expectedElements: []string{"test", "State", "Waiting", "Ready", "True", "Restart Count", "7", "Image", "image", "envname", "xyz", "a123\tConfigMap with prefix 'p_'\tOptional: false"},
}, },
{ {
name: "test8",
container: api.Container{Name: "test", Image: "image", Env: []api.EnvVar{{Name: "envname", Value: "xyz"}}, EnvFrom: []api.EnvFromSource{{ConfigMapRef: &api.ConfigMapEnvSource{Optional: &trueVal, LocalObjectReference: api.LocalObjectReference{Name: "a123"}}}}}, container: api.Container{Name: "test", Image: "image", Env: []api.EnvVar{{Name: "envname", Value: "xyz"}}, EnvFrom: []api.EnvFromSource{{ConfigMapRef: &api.ConfigMapEnvSource{Optional: &trueVal, LocalObjectReference: api.LocalObjectReference{Name: "a123"}}}}},
status: api.ContainerStatus{ status: api.ContainerStatus{
Name: "test", Name: "test",
@ -567,6 +581,7 @@ func TestDescribeContainers(t *testing.T) {
expectedElements: []string{"test", "State", "Waiting", "Ready", "True", "Restart Count", "7", "Image", "image", "envname", "xyz", "a123\tConfigMap\tOptional: true"}, expectedElements: []string{"test", "State", "Waiting", "Ready", "True", "Restart Count", "7", "Image", "image", "envname", "xyz", "a123\tConfigMap\tOptional: true"},
}, },
{ {
name: "test9",
container: api.Container{Name: "test", Image: "image", Env: []api.EnvVar{{Name: "envname", Value: "xyz"}}, EnvFrom: []api.EnvFromSource{{SecretRef: &api.SecretEnvSource{LocalObjectReference: api.LocalObjectReference{Name: "a123"}, Optional: &trueVal}}}}, container: api.Container{Name: "test", Image: "image", Env: []api.EnvVar{{Name: "envname", Value: "xyz"}}, EnvFrom: []api.EnvFromSource{{SecretRef: &api.SecretEnvSource{LocalObjectReference: api.LocalObjectReference{Name: "a123"}, Optional: &trueVal}}}},
status: api.ContainerStatus{ status: api.ContainerStatus{
Name: "test", Name: "test",
@ -576,6 +591,7 @@ func TestDescribeContainers(t *testing.T) {
expectedElements: []string{"test", "State", "Waiting", "Ready", "True", "Restart Count", "7", "Image", "image", "envname", "xyz", "a123\tSecret\tOptional: true"}, expectedElements: []string{"test", "State", "Waiting", "Ready", "True", "Restart Count", "7", "Image", "image", "envname", "xyz", "a123\tSecret\tOptional: true"},
}, },
{ {
name: "test10",
container: api.Container{Name: "test", Image: "image", Env: []api.EnvVar{{Name: "envname", Value: "xyz"}}, EnvFrom: []api.EnvFromSource{{Prefix: "p_", SecretRef: &api.SecretEnvSource{LocalObjectReference: api.LocalObjectReference{Name: "a123"}}}}}, container: api.Container{Name: "test", Image: "image", Env: []api.EnvVar{{Name: "envname", Value: "xyz"}}, EnvFrom: []api.EnvFromSource{{Prefix: "p_", SecretRef: &api.SecretEnvSource{LocalObjectReference: api.LocalObjectReference{Name: "a123"}}}}},
status: api.ContainerStatus{ status: api.ContainerStatus{
Name: "test", Name: "test",
@ -586,6 +602,7 @@ func TestDescribeContainers(t *testing.T) {
}, },
// Command // Command
{ {
name: "test11",
container: api.Container{Name: "test", Image: "image", Command: []string{"sleep", "1000"}}, container: api.Container{Name: "test", Image: "image", Command: []string{"sleep", "1000"}},
status: api.ContainerStatus{ status: api.ContainerStatus{
Name: "test", Name: "test",
@ -596,6 +613,7 @@ func TestDescribeContainers(t *testing.T) {
}, },
// Args // Args
{ {
name: "test12",
container: api.Container{Name: "test", Image: "image", Args: []string{"time", "1000"}}, container: api.Container{Name: "test", Image: "image", Args: []string{"time", "1000"}},
status: api.ContainerStatus{ status: api.ContainerStatus{
Name: "test", Name: "test",
@ -606,6 +624,7 @@ func TestDescribeContainers(t *testing.T) {
}, },
// Using limits. // Using limits.
{ {
name: "test13",
container: api.Container{ container: api.Container{
Name: "test", Name: "test",
Image: "image", Image: "image",
@ -626,6 +645,7 @@ func TestDescribeContainers(t *testing.T) {
}, },
// Using requests. // Using requests.
{ {
name: "test14",
container: api.Container{ container: api.Container{
Name: "test", Name: "test",
Image: "image", Image: "image",
@ -641,6 +661,7 @@ func TestDescribeContainers(t *testing.T) {
}, },
// volumeMounts read/write // volumeMounts read/write
{ {
name: "test15",
container: api.Container{ container: api.Container{
Name: "test", Name: "test",
Image: "image", Image: "image",
@ -655,6 +676,7 @@ func TestDescribeContainers(t *testing.T) {
}, },
// volumeMounts readonly // volumeMounts readonly
{ {
name: "test16",
container: api.Container{ container: api.Container{
Name: "test", Name: "test",
Image: "image", Image: "image",
@ -671,6 +693,7 @@ func TestDescribeContainers(t *testing.T) {
// volumeDevices // volumeDevices
{ {
name: "test17",
container: api.Container{ container: api.Container{
Name: "test", Name: "test",
Image: "image", Image: "image",
@ -686,6 +709,7 @@ func TestDescribeContainers(t *testing.T) {
} }
for i, testCase := range testCases { for i, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
out := new(bytes.Buffer) out := new(bytes.Buffer)
pod := api.Pod{ pod := api.Pod{
Spec: api.PodSpec{ Spec: api.PodSpec{
@ -703,6 +727,7 @@ func TestDescribeContainers(t *testing.T) {
t.Errorf("Test case %d: expected to find %q in output: %q", i, expected, output) t.Errorf("Test case %d: expected to find %q in output: %q", i, expected, output)
} }
} }
})
} }
} }
@ -788,10 +813,12 @@ func TestDefaultDescribers(t *testing.T) {
func TestGetPodsTotalRequests(t *testing.T) { func TestGetPodsTotalRequests(t *testing.T) {
testCases := []struct { testCases := []struct {
name string
pods *api.PodList pods *api.PodList
expectedReqs map[api.ResourceName]resource.Quantity expectedReqs map[api.ResourceName]resource.Quantity
}{ }{
{ {
name: "test1",
pods: &api.PodList{ pods: &api.PodList{
Items: []api.Pod{ Items: []api.Pod{
{ {
@ -853,10 +880,12 @@ func TestGetPodsTotalRequests(t *testing.T) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
reqs, _ := getPodsTotalRequestsAndLimits(testCase.pods) reqs, _ := getPodsTotalRequestsAndLimits(testCase.pods)
if !apiequality.Semantic.DeepEqual(reqs, testCase.expectedReqs) { if !apiequality.Semantic.DeepEqual(reqs, testCase.expectedReqs) {
t.Errorf("Expected %v, got %v", testCase.expectedReqs, reqs) t.Errorf("Expected %v, got %v", testCase.expectedReqs, reqs)
} }
})
} }
} }
@ -864,12 +893,14 @@ func TestPersistentVolumeDescriber(t *testing.T) {
block := api.PersistentVolumeBlock block := api.PersistentVolumeBlock
file := api.PersistentVolumeFilesystem file := api.PersistentVolumeFilesystem
testCases := []struct { testCases := []struct {
name string
plugin string plugin string
pv *api.PersistentVolume pv *api.PersistentVolume
expectedElements []string expectedElements []string
unexpectedElements []string unexpectedElements []string
}{ }{
{ {
name: "test0",
plugin: "hostpath", plugin: "hostpath",
pv: &api.PersistentVolume{ pv: &api.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{Name: "bar"}, ObjectMeta: metav1.ObjectMeta{Name: "bar"},
@ -882,6 +913,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
unexpectedElements: []string{"VolumeMode", "Filesystem"}, unexpectedElements: []string{"VolumeMode", "Filesystem"},
}, },
{ {
name: "test1",
plugin: "gce", plugin: "gce",
pv: &api.PersistentVolume{ pv: &api.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{Name: "bar"}, ObjectMeta: metav1.ObjectMeta{Name: "bar"},
@ -895,6 +927,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
expectedElements: []string{"VolumeMode", "Filesystem"}, expectedElements: []string{"VolumeMode", "Filesystem"},
}, },
{ {
name: "test2",
plugin: "ebs", plugin: "ebs",
pv: &api.PersistentVolume{ pv: &api.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{Name: "bar"}, ObjectMeta: metav1.ObjectMeta{Name: "bar"},
@ -907,6 +940,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
unexpectedElements: []string{"VolumeMode", "Filesystem"}, unexpectedElements: []string{"VolumeMode", "Filesystem"},
}, },
{ {
name: "test3",
plugin: "nfs", plugin: "nfs",
pv: &api.PersistentVolume{ pv: &api.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{Name: "bar"}, ObjectMeta: metav1.ObjectMeta{Name: "bar"},
@ -919,6 +953,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
unexpectedElements: []string{"VolumeMode", "Filesystem"}, unexpectedElements: []string{"VolumeMode", "Filesystem"},
}, },
{ {
name: "test4",
plugin: "iscsi", plugin: "iscsi",
pv: &api.PersistentVolume{ pv: &api.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{Name: "bar"}, ObjectMeta: metav1.ObjectMeta{Name: "bar"},
@ -932,6 +967,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
expectedElements: []string{"VolumeMode", "Block"}, expectedElements: []string{"VolumeMode", "Block"},
}, },
{ {
name: "test5",
plugin: "gluster", plugin: "gluster",
pv: &api.PersistentVolume{ pv: &api.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{Name: "bar"}, ObjectMeta: metav1.ObjectMeta{Name: "bar"},
@ -944,6 +980,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
unexpectedElements: []string{"VolumeMode", "Filesystem"}, unexpectedElements: []string{"VolumeMode", "Filesystem"},
}, },
{ {
name: "test6",
plugin: "rbd", plugin: "rbd",
pv: &api.PersistentVolume{ pv: &api.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{Name: "bar"}, ObjectMeta: metav1.ObjectMeta{Name: "bar"},
@ -956,6 +993,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
unexpectedElements: []string{"VolumeMode", "Filesystem"}, unexpectedElements: []string{"VolumeMode", "Filesystem"},
}, },
{ {
name: "test7",
plugin: "quobyte", plugin: "quobyte",
pv: &api.PersistentVolume{ pv: &api.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{Name: "bar"}, ObjectMeta: metav1.ObjectMeta{Name: "bar"},
@ -968,6 +1006,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
unexpectedElements: []string{"VolumeMode", "Filesystem"}, unexpectedElements: []string{"VolumeMode", "Filesystem"},
}, },
{ {
name: "test8",
plugin: "cinder", plugin: "cinder",
pv: &api.PersistentVolume{ pv: &api.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{Name: "bar"}, ObjectMeta: metav1.ObjectMeta{Name: "bar"},
@ -980,6 +1019,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
unexpectedElements: []string{"VolumeMode", "Filesystem"}, unexpectedElements: []string{"VolumeMode", "Filesystem"},
}, },
{ {
name: "test9",
plugin: "fc", plugin: "fc",
pv: &api.PersistentVolume{ pv: &api.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{Name: "bar"}, ObjectMeta: metav1.ObjectMeta{Name: "bar"},
@ -993,6 +1033,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
expectedElements: []string{"VolumeMode", "Block"}, expectedElements: []string{"VolumeMode", "Block"},
}, },
{ {
name: "test10",
plugin: "local", plugin: "local",
pv: &api.PersistentVolume{ pv: &api.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{Name: "bar"}, ObjectMeta: metav1.ObjectMeta{Name: "bar"},
@ -1006,6 +1047,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
unexpectedElements: []string{"Required Terms", "Term "}, unexpectedElements: []string{"Required Terms", "Term "},
}, },
{ {
name: "test11",
plugin: "local", plugin: "local",
pv: &api.PersistentVolume{ pv: &api.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{Name: "bar"}, ObjectMeta: metav1.ObjectMeta{Name: "bar"},
@ -1020,6 +1062,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
unexpectedElements: []string{"Required Terms", "Term "}, unexpectedElements: []string{"Required Terms", "Term "},
}, },
{ {
name: "test12",
plugin: "local", plugin: "local",
pv: &api.PersistentVolume{ pv: &api.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{Name: "bar"}, ObjectMeta: metav1.ObjectMeta{Name: "bar"},
@ -1036,6 +1079,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
unexpectedElements: []string{"Term "}, unexpectedElements: []string{"Term "},
}, },
{ {
name: "test13",
plugin: "local", plugin: "local",
pv: &api.PersistentVolume{ pv: &api.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{Name: "bar"}, ObjectMeta: metav1.ObjectMeta{Name: "bar"},
@ -1060,6 +1104,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
expectedElements: []string{"Node Affinity", "Required Terms", "Term 0", "Term 1"}, expectedElements: []string{"Node Affinity", "Required Terms", "Term 0", "Term 1"},
}, },
{ {
name: "test14",
plugin: "local", plugin: "local",
pv: &api.PersistentVolume{ pv: &api.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{Name: "bar"}, ObjectMeta: metav1.ObjectMeta{Name: "bar"},
@ -1095,6 +1140,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
} }
for _, test := range testCases { for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
fake := fake.NewSimpleClientset(test.pv) fake := fake.NewSimpleClientset(test.pv)
c := PersistentVolumeDescriber{fake} c := PersistentVolumeDescriber{fake}
str, err := c.Describe("foo", "bar", printers.DescriberSettings{ShowEvents: true}) str, err := c.Describe("foo", "bar", printers.DescriberSettings{ShowEvents: true})
@ -1114,6 +1160,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
t.Errorf("unexpected to find %q in output: %q", unexpected, str) t.Errorf("unexpected to find %q in output: %q", unexpected, str)
} }
} }
})
} }
} }
@ -1272,6 +1319,7 @@ func TestPersistentVolumeClaimDescriber(t *testing.T) {
} }
for _, test := range testCases { for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
fake := fake.NewSimpleClientset(test.pvc) fake := fake.NewSimpleClientset(test.pvc)
c := PersistentVolumeClaimDescriber{fake} c := PersistentVolumeClaimDescriber{fake}
str, err := c.Describe("foo", "bar", printers.DescriberSettings{ShowEvents: true}) str, err := c.Describe("foo", "bar", printers.DescriberSettings{ShowEvents: true})
@ -1291,6 +1339,7 @@ func TestPersistentVolumeClaimDescriber(t *testing.T) {
t.Errorf("unexpected to find %q in output: %q", unexpected, str) t.Errorf("unexpected to find %q in output: %q", unexpected, str)
} }
} }
})
} }
} }
@ -1880,6 +1929,7 @@ func TestDescribeHorizontalPodAutoscaler(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
test.hpa.ObjectMeta = metav1.ObjectMeta{ test.hpa.ObjectMeta = metav1.ObjectMeta{
Name: "bar", Name: "bar",
Namespace: "foo", Namespace: "foo",
@ -1894,6 +1944,7 @@ func TestDescribeHorizontalPodAutoscaler(t *testing.T) {
t.Errorf("Unexpected empty string for test %s. Expected HPA Describer output", test.name) t.Errorf("Unexpected empty string for test %s. Expected HPA Describer output", test.name)
} }
t.Logf("Description for %q:\n%s", test.name, str) t.Logf("Description for %q:\n%s", test.name, str)
})
} }
} }
@ -2023,6 +2074,7 @@ func TestDescribeEvents(t *testing.T) {
} }
for name, d := range m { for name, d := range m {
t.Run(name, func(t *testing.T) {
out, err := d.Describe("foo", "bar", printers.DescriberSettings{ShowEvents: true}) out, err := d.Describe("foo", "bar", printers.DescriberSettings{ShowEvents: true})
if err != nil { if err != nil {
t.Errorf("unexpected error for %q: %v", name, err) t.Errorf("unexpected error for %q: %v", name, err)
@ -2044,6 +2096,7 @@ func TestDescribeEvents(t *testing.T) {
if strings.Contains(out, "Events:") { if strings.Contains(out, "Events:") {
t.Errorf("events found for %q when ShowEvents=false: %s", name, out) t.Errorf("events found for %q when ShowEvents=false: %s", name, out)
} }
})
} }
} }
@ -2071,6 +2124,7 @@ func TestPrintLabelsMultiline(t *testing.T) {
}, },
} }
for i, testCase := range testCases { for i, testCase := range testCases {
t.Run(testCase.expectPrint, func(t *testing.T) {
out := new(bytes.Buffer) out := new(bytes.Buffer)
writer := NewPrefixWriter(out) writer := NewPrefixWriter(out)
printAnnotationsMultiline(writer, "Annotations", testCase.annotations) printAnnotationsMultiline(writer, "Annotations", testCase.annotations)
@ -2078,6 +2132,7 @@ func TestPrintLabelsMultiline(t *testing.T) {
if output != testCase.expectPrint { if output != testCase.expectPrint {
t.Errorf("Test case %d: expected to find %q in output: %q", i, testCase.expectPrint, output) t.Errorf("Test case %d: expected to find %q in output: %q", i, testCase.expectPrint, output)
} }
})
} }
} }

View File

@ -26,13 +26,15 @@ import (
) )
func TestTemplate(t *testing.T) { func TestTemplate(t *testing.T) {
testCase := map[string]struct { testCase := []struct {
name string
template string template string
obj runtime.Object obj runtime.Object
expectOut string expectOut string
expectErr func(error) (string, bool) expectErr func(error) (string, bool)
}{ }{
"support base64 decoding of secret data": { {
name: "support base64 decoding of secret data",
template: "{{ .data.username | base64decode }}", template: "{{ .data.username | base64decode }}",
obj: &v1.Secret{ obj: &v1.Secret{
Data: map[string][]byte{ Data: map[string][]byte{
@ -41,7 +43,8 @@ func TestTemplate(t *testing.T) {
}, },
expectOut: "hunter", expectOut: "hunter",
}, },
"test error path for base64 decoding": { {
name: "test error path for base64 decoding",
template: "{{ .data.username | base64decode }}", template: "{{ .data.username | base64decode }}",
obj: &badlyMarshaledSecret{}, obj: &badlyMarshaledSecret{},
expectErr: func(err error) (string, bool) { expectErr: func(err error) (string, bool) {
@ -50,41 +53,43 @@ func TestTemplate(t *testing.T) {
}, },
}, },
} }
for name, test := range testCase { for _, test := range testCase {
t.Run(test.name, func(t *testing.T) {
buffer := &bytes.Buffer{} buffer := &bytes.Buffer{}
p, err := NewGoTemplatePrinter([]byte(test.template)) p, err := NewGoTemplatePrinter([]byte(test.template))
if err != nil { if err != nil {
if test.expectErr == nil { if test.expectErr == nil {
t.Errorf("[%s]expected success but got:\n %v\n", name, err) t.Errorf("[%s]expected success but got:\n %v\n", test.name, err)
continue return
} }
if expected, ok := test.expectErr(err); !ok { if expected, ok := test.expectErr(err); !ok {
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", name, expected, err) t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", test.name, expected, err)
} }
continue return
} }
err = p.PrintObj(test.obj, buffer) err = p.PrintObj(test.obj, buffer)
if err != nil { if err != nil {
if test.expectErr == nil { if test.expectErr == nil {
t.Errorf("[%s]expected success but got:\n %v\n", name, err) t.Errorf("[%s]expected success but got:\n %v\n", test.name, err)
continue return
} }
if expected, ok := test.expectErr(err); !ok { if expected, ok := test.expectErr(err); !ok {
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", name, expected, err) t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", test.name, expected, err)
} }
continue return
} }
if test.expectErr != nil { if test.expectErr != nil {
t.Errorf("[%s]expect:\n error\n but got:\n no error\n", name) t.Errorf("[%s]expect:\n error\n but got:\n no error\n", test.name)
continue return
} }
if test.expectOut != buffer.String() { if test.expectOut != buffer.String() {
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", name, test.expectOut, buffer.String()) t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", test.name, test.expectOut, buffer.String())
} }
})
} }
} }