mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 12:15:52 +00:00
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:
commit
a915811a98
@ -47,20 +47,22 @@ func TestMassageJSONPath(t *testing.T) {
|
||||
{input: "{{foo.bar}", expectErr: true},
|
||||
}
|
||||
for _, test := range tests {
|
||||
output, err := printers.RelaxedJSONPathExpression(test.input)
|
||||
if err != nil && !test.expectErr {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
continue
|
||||
}
|
||||
if test.expectErr {
|
||||
if err == nil {
|
||||
t.Error("unexpected non-error")
|
||||
t.Run(test.input, func(t *testing.T) {
|
||||
output, err := printers.RelaxedJSONPathExpression(test.input)
|
||||
if err != nil && !test.expectErr {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
continue
|
||||
}
|
||||
if output != test.expectedOutput {
|
||||
t.Errorf("input: %s, expected: %s, saw: %s", test.input, test.expectedOutput, output)
|
||||
}
|
||||
if test.expectErr {
|
||||
if err == nil {
|
||||
t.Error("unexpected non-error")
|
||||
}
|
||||
return
|
||||
}
|
||||
if output != test.expectedOutput {
|
||||
t.Errorf("input: %s, expected: %s, saw: %s", test.input, test.expectedOutput, output)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -113,33 +115,34 @@ func TestNewColumnPrinterFromSpec(t *testing.T) {
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
printer, err := printers.NewCustomColumnsPrinterFromSpec(test.spec, legacyscheme.Codecs.UniversalDecoder(), test.noHeaders)
|
||||
if test.expectErr {
|
||||
if err == nil {
|
||||
t.Errorf("[%s] unexpected non-error", test.name)
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
printer, err := printers.NewCustomColumnsPrinterFromSpec(test.spec, legacyscheme.Codecs.UniversalDecoder(), test.noHeaders)
|
||||
if test.expectErr {
|
||||
if err == nil {
|
||||
t.Errorf("[%s] unexpected non-error", test.name)
|
||||
}
|
||||
return
|
||||
}
|
||||
continue
|
||||
}
|
||||
if !test.expectErr && err != nil {
|
||||
t.Errorf("[%s] unexpected error: %v", test.name, err)
|
||||
continue
|
||||
}
|
||||
if test.noHeaders {
|
||||
buffer := &bytes.Buffer{}
|
||||
|
||||
printer.PrintObj(&api.Pod{}, buffer)
|
||||
if err != nil {
|
||||
t.Fatalf("An error occurred printing Pod: %#v", err)
|
||||
if !test.expectErr && err != nil {
|
||||
t.Errorf("[%s] unexpected error: %v", test.name, err)
|
||||
return
|
||||
}
|
||||
if test.noHeaders {
|
||||
buffer := &bytes.Buffer{}
|
||||
|
||||
if contains(strings.Fields(buffer.String()), "API_VERSION") {
|
||||
t.Errorf("unexpected header API_VERSION")
|
||||
printer.PrintObj(&api.Pod{}, buffer)
|
||||
if err != nil {
|
||||
t.Fatalf("An error occurred printing Pod: %#v", err)
|
||||
}
|
||||
|
||||
if contains(strings.Fields(buffer.String()), "API_VERSION") {
|
||||
t.Errorf("unexpected header API_VERSION")
|
||||
}
|
||||
|
||||
} else if !reflect.DeepEqual(test.expectedColumns, printer.Columns) {
|
||||
t.Errorf("[%s]\nexpected:\n%v\nsaw:\n%v\n", test.name, 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)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -215,23 +218,24 @@ func TestNewColumnPrinterFromTemplate(t *testing.T) {
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
reader := bytes.NewBufferString(test.spec)
|
||||
printer, err := printers.NewCustomColumnsPrinterFromTemplate(reader, legacyscheme.Codecs.UniversalDecoder())
|
||||
if test.expectErr {
|
||||
if err == nil {
|
||||
t.Errorf("[%s] unexpected non-error", test.name)
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
reader := bytes.NewBufferString(test.spec)
|
||||
printer, err := printers.NewCustomColumnsPrinterFromTemplate(reader, legacyscheme.Codecs.UniversalDecoder())
|
||||
if test.expectErr {
|
||||
if err == nil {
|
||||
t.Errorf("[%s] unexpected non-error", test.name)
|
||||
}
|
||||
return
|
||||
}
|
||||
if !test.expectErr && err != nil {
|
||||
t.Errorf("[%s] unexpected error: %v", test.name, err)
|
||||
return
|
||||
}
|
||||
continue
|
||||
}
|
||||
if !test.expectErr && err != nil {
|
||||
t.Errorf("[%s] unexpected error: %v", test.name, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(test.expectedColumns, printer.Columns) {
|
||||
t.Errorf("[%s]\nexpected:\n%v\nsaw:\n%v\n", test.name, 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -310,17 +314,19 @@ foo baz <none>
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
printer := &printers.CustomColumnsPrinter{
|
||||
Columns: test.columns,
|
||||
Decoder: legacyscheme.Codecs.UniversalDecoder(),
|
||||
}
|
||||
buffer := &bytes.Buffer{}
|
||||
if err := printer.PrintObj(test.obj, buffer); err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if buffer.String() != test.expectedOutput {
|
||||
t.Errorf("\nexpected:\n'%s'\nsaw\n'%s'\n", test.expectedOutput, buffer.String())
|
||||
}
|
||||
t.Run(test.expectedOutput, func(t *testing.T) {
|
||||
printer := &printers.CustomColumnsPrinter{
|
||||
Columns: test.columns,
|
||||
Decoder: legacyscheme.Codecs.UniversalDecoder(),
|
||||
}
|
||||
buffer := &bytes.Buffer{}
|
||||
if err := printer.PrintObj(test.obj, buffer); err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if buffer.String() != test.expectedOutput {
|
||||
t.Errorf("\nexpected:\n'%s'\nsaw\n'%s'\n", test.expectedOutput, buffer.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -48,7 +48,8 @@ func testPrintNamespace(obj *api.Namespace, options PrintOptions) ([]metav1beta1
|
||||
func TestPrintRowsForHandlerEntry(t *testing.T) {
|
||||
printFunc := reflect.ValueOf(testPrintNamespace)
|
||||
|
||||
testCase := map[string]struct {
|
||||
testCase := []struct {
|
||||
name string
|
||||
h *handlerEntry
|
||||
opt PrintOptions
|
||||
obj runtime.Object
|
||||
@ -56,7 +57,8 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
|
||||
expectOut string
|
||||
expectErr string
|
||||
}{
|
||||
"no tablecolumndefinition and includeheader flase": {
|
||||
{
|
||||
name: "no tablecolumndefinition and includeheader flase",
|
||||
h: &handlerEntry{
|
||||
columnDefinitions: []metav1beta1.TableColumnDefinition{},
|
||||
printRows: true,
|
||||
@ -69,7 +71,8 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
|
||||
includeHeader: false,
|
||||
expectOut: "test\t\t<unknow>\n",
|
||||
},
|
||||
"no tablecolumndefinition and includeheader true": {
|
||||
{
|
||||
name: "no tablecolumndefinition and includeheader true",
|
||||
h: &handlerEntry{
|
||||
columnDefinitions: []metav1beta1.TableColumnDefinition{},
|
||||
printRows: true,
|
||||
@ -82,7 +85,8 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
|
||||
includeHeader: true,
|
||||
expectOut: "\ntest\t\t<unknow>\n",
|
||||
},
|
||||
"have tablecolumndefinition and includeheader true": {
|
||||
{
|
||||
name: "have tablecolumndefinition and includeheader true",
|
||||
h: &handlerEntry{
|
||||
columnDefinitions: testNamespaceColumnDefinitions,
|
||||
printRows: true,
|
||||
@ -95,7 +99,8 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
|
||||
includeHeader: true,
|
||||
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{
|
||||
columnDefinitions: testNamespaceColumnDefinitions,
|
||||
printRows: true,
|
||||
@ -112,16 +117,18 @@ func TestPrintRowsForHandlerEntry(t *testing.T) {
|
||||
expectErr: "namespace is not namespaced",
|
||||
},
|
||||
}
|
||||
for name, test := range testCase {
|
||||
buffer := &bytes.Buffer{}
|
||||
err := printRowsForHandlerEntry(buffer, test.h, test.obj, test.opt, test.includeHeader)
|
||||
if err != nil {
|
||||
if err.Error() != test.expectErr {
|
||||
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", name, test.expectErr, err)
|
||||
for _, test := range testCase {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
buffer := &bytes.Buffer{}
|
||||
err := printRowsForHandlerEntry(buffer, test.h, test.obj, test.opt, test.includeHeader)
|
||||
if err != nil {
|
||||
if err.Error() != test.expectErr {
|
||||
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", test.name, test.expectErr, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if test.expectOut != buffer.String() {
|
||||
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", name, test.expectOut, buffer.String())
|
||||
}
|
||||
if test.expectOut != buffer.String() {
|
||||
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", test.name, test.expectOut, buffer.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -283,10 +283,12 @@ func getResourceList(cpu, memory string) api.ResourceList {
|
||||
|
||||
func TestDescribeService(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
service *api.Service
|
||||
expect []string
|
||||
}{
|
||||
{
|
||||
name: "test1",
|
||||
service: &api.Service{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "bar",
|
||||
@ -324,6 +326,7 @@ func TestDescribeService(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "test2",
|
||||
service: &api.Service{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "bar",
|
||||
@ -362,18 +365,20 @@ func TestDescribeService(t *testing.T) {
|
||||
},
|
||||
}
|
||||
for _, testCase := range testCases {
|
||||
fake := fake.NewSimpleClientset(testCase.service)
|
||||
c := &describeClient{T: t, Namespace: "foo", Interface: fake}
|
||||
d := ServiceDescriber{c}
|
||||
out, err := d.Describe("foo", "bar", printers.DescriberSettings{ShowEvents: true})
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
for _, expected := range testCase.expect {
|
||||
if !strings.Contains(out, expected) {
|
||||
t.Errorf("expected to find %q in output: %q", expected, out)
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
fake := fake.NewSimpleClientset(testCase.service)
|
||||
c := &describeClient{T: t, Namespace: "foo", Interface: fake}
|
||||
d := ServiceDescriber{c}
|
||||
out, err := d.Describe("foo", "bar", printers.DescriberSettings{ShowEvents: true})
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
for _, expected := range testCase.expect {
|
||||
if !strings.Contains(out, expected) {
|
||||
t.Errorf("expected to find %q in output: %q", expected, out)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -453,12 +458,14 @@ func VerifyDatesInOrder(
|
||||
func TestDescribeContainers(t *testing.T) {
|
||||
trueVal := true
|
||||
testCases := []struct {
|
||||
name string
|
||||
container api.Container
|
||||
status api.ContainerStatus
|
||||
expectedElements []string
|
||||
}{
|
||||
// Running state.
|
||||
{
|
||||
name: "test1",
|
||||
container: api.Container{Name: "test", Image: "image"},
|
||||
status: api.ContainerStatus{
|
||||
Name: "test",
|
||||
@ -474,6 +481,7 @@ func TestDescribeContainers(t *testing.T) {
|
||||
},
|
||||
// Waiting state.
|
||||
{
|
||||
name: "test2",
|
||||
container: api.Container{Name: "test", Image: "image"},
|
||||
status: api.ContainerStatus{
|
||||
Name: "test",
|
||||
@ -489,6 +497,7 @@ func TestDescribeContainers(t *testing.T) {
|
||||
},
|
||||
// Terminated state.
|
||||
{
|
||||
name: "test3",
|
||||
container: api.Container{Name: "test", Image: "image"},
|
||||
status: api.ContainerStatus{
|
||||
Name: "test",
|
||||
@ -507,6 +516,7 @@ func TestDescribeContainers(t *testing.T) {
|
||||
},
|
||||
// Last Terminated
|
||||
{
|
||||
name: "test4",
|
||||
container: api.Container{Name: "test", Image: "image"},
|
||||
status: api.ContainerStatus{
|
||||
Name: "test",
|
||||
@ -530,6 +540,7 @@ func TestDescribeContainers(t *testing.T) {
|
||||
},
|
||||
// No state defaults to waiting.
|
||||
{
|
||||
name: "test5",
|
||||
container: api.Container{Name: "test", Image: "image"},
|
||||
status: api.ContainerStatus{
|
||||
Name: "test",
|
||||
@ -540,6 +551,7 @@ func TestDescribeContainers(t *testing.T) {
|
||||
},
|
||||
// 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"}}}}},
|
||||
status: api.ContainerStatus{
|
||||
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"},
|
||||
},
|
||||
{
|
||||
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"}}}}},
|
||||
status: api.ContainerStatus{
|
||||
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"},
|
||||
},
|
||||
{
|
||||
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"}}}}},
|
||||
status: api.ContainerStatus{
|
||||
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"},
|
||||
},
|
||||
{
|
||||
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}}}},
|
||||
status: api.ContainerStatus{
|
||||
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"},
|
||||
},
|
||||
{
|
||||
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"}}}}},
|
||||
status: api.ContainerStatus{
|
||||
Name: "test",
|
||||
@ -586,6 +602,7 @@ func TestDescribeContainers(t *testing.T) {
|
||||
},
|
||||
// Command
|
||||
{
|
||||
name: "test11",
|
||||
container: api.Container{Name: "test", Image: "image", Command: []string{"sleep", "1000"}},
|
||||
status: api.ContainerStatus{
|
||||
Name: "test",
|
||||
@ -596,6 +613,7 @@ func TestDescribeContainers(t *testing.T) {
|
||||
},
|
||||
// Args
|
||||
{
|
||||
name: "test12",
|
||||
container: api.Container{Name: "test", Image: "image", Args: []string{"time", "1000"}},
|
||||
status: api.ContainerStatus{
|
||||
Name: "test",
|
||||
@ -606,6 +624,7 @@ func TestDescribeContainers(t *testing.T) {
|
||||
},
|
||||
// Using limits.
|
||||
{
|
||||
name: "test13",
|
||||
container: api.Container{
|
||||
Name: "test",
|
||||
Image: "image",
|
||||
@ -626,6 +645,7 @@ func TestDescribeContainers(t *testing.T) {
|
||||
},
|
||||
// Using requests.
|
||||
{
|
||||
name: "test14",
|
||||
container: api.Container{
|
||||
Name: "test",
|
||||
Image: "image",
|
||||
@ -641,6 +661,7 @@ func TestDescribeContainers(t *testing.T) {
|
||||
},
|
||||
// volumeMounts read/write
|
||||
{
|
||||
name: "test15",
|
||||
container: api.Container{
|
||||
Name: "test",
|
||||
Image: "image",
|
||||
@ -655,6 +676,7 @@ func TestDescribeContainers(t *testing.T) {
|
||||
},
|
||||
// volumeMounts readonly
|
||||
{
|
||||
name: "test16",
|
||||
container: api.Container{
|
||||
Name: "test",
|
||||
Image: "image",
|
||||
@ -671,6 +693,7 @@ func TestDescribeContainers(t *testing.T) {
|
||||
|
||||
// volumeDevices
|
||||
{
|
||||
name: "test17",
|
||||
container: api.Container{
|
||||
Name: "test",
|
||||
Image: "image",
|
||||
@ -686,23 +709,25 @@ func TestDescribeContainers(t *testing.T) {
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
out := new(bytes.Buffer)
|
||||
pod := api.Pod{
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{testCase.container},
|
||||
},
|
||||
Status: api.PodStatus{
|
||||
ContainerStatuses: []api.ContainerStatus{testCase.status},
|
||||
},
|
||||
}
|
||||
writer := NewPrefixWriter(out)
|
||||
describeContainers("Containers", pod.Spec.Containers, pod.Status.ContainerStatuses, EnvValueRetriever(&pod), writer, "")
|
||||
output := out.String()
|
||||
for _, expected := range testCase.expectedElements {
|
||||
if !strings.Contains(output, expected) {
|
||||
t.Errorf("Test case %d: expected to find %q in output: %q", i, expected, output)
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
out := new(bytes.Buffer)
|
||||
pod := api.Pod{
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{testCase.container},
|
||||
},
|
||||
Status: api.PodStatus{
|
||||
ContainerStatuses: []api.ContainerStatus{testCase.status},
|
||||
},
|
||||
}
|
||||
}
|
||||
writer := NewPrefixWriter(out)
|
||||
describeContainers("Containers", pod.Spec.Containers, pod.Status.ContainerStatuses, EnvValueRetriever(&pod), writer, "")
|
||||
output := out.String()
|
||||
for _, expected := range testCase.expectedElements {
|
||||
if !strings.Contains(output, expected) {
|
||||
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) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
pods *api.PodList
|
||||
expectedReqs map[api.ResourceName]resource.Quantity
|
||||
}{
|
||||
{
|
||||
name: "test1",
|
||||
pods: &api.PodList{
|
||||
Items: []api.Pod{
|
||||
{
|
||||
@ -853,10 +880,12 @@ func TestGetPodsTotalRequests(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
reqs, _ := getPodsTotalRequestsAndLimits(testCase.pods)
|
||||
if !apiequality.Semantic.DeepEqual(reqs, testCase.expectedReqs) {
|
||||
t.Errorf("Expected %v, got %v", testCase.expectedReqs, reqs)
|
||||
}
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
reqs, _ := getPodsTotalRequestsAndLimits(testCase.pods)
|
||||
if !apiequality.Semantic.DeepEqual(reqs, testCase.expectedReqs) {
|
||||
t.Errorf("Expected %v, got %v", testCase.expectedReqs, reqs)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -864,12 +893,14 @@ func TestPersistentVolumeDescriber(t *testing.T) {
|
||||
block := api.PersistentVolumeBlock
|
||||
file := api.PersistentVolumeFilesystem
|
||||
testCases := []struct {
|
||||
name string
|
||||
plugin string
|
||||
pv *api.PersistentVolume
|
||||
expectedElements []string
|
||||
unexpectedElements []string
|
||||
}{
|
||||
{
|
||||
name: "test0",
|
||||
plugin: "hostpath",
|
||||
pv: &api.PersistentVolume{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "bar"},
|
||||
@ -882,6 +913,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
|
||||
unexpectedElements: []string{"VolumeMode", "Filesystem"},
|
||||
},
|
||||
{
|
||||
name: "test1",
|
||||
plugin: "gce",
|
||||
pv: &api.PersistentVolume{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "bar"},
|
||||
@ -895,6 +927,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
|
||||
expectedElements: []string{"VolumeMode", "Filesystem"},
|
||||
},
|
||||
{
|
||||
name: "test2",
|
||||
plugin: "ebs",
|
||||
pv: &api.PersistentVolume{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "bar"},
|
||||
@ -907,6 +940,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
|
||||
unexpectedElements: []string{"VolumeMode", "Filesystem"},
|
||||
},
|
||||
{
|
||||
name: "test3",
|
||||
plugin: "nfs",
|
||||
pv: &api.PersistentVolume{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "bar"},
|
||||
@ -919,6 +953,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
|
||||
unexpectedElements: []string{"VolumeMode", "Filesystem"},
|
||||
},
|
||||
{
|
||||
name: "test4",
|
||||
plugin: "iscsi",
|
||||
pv: &api.PersistentVolume{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "bar"},
|
||||
@ -932,6 +967,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
|
||||
expectedElements: []string{"VolumeMode", "Block"},
|
||||
},
|
||||
{
|
||||
name: "test5",
|
||||
plugin: "gluster",
|
||||
pv: &api.PersistentVolume{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "bar"},
|
||||
@ -944,6 +980,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
|
||||
unexpectedElements: []string{"VolumeMode", "Filesystem"},
|
||||
},
|
||||
{
|
||||
name: "test6",
|
||||
plugin: "rbd",
|
||||
pv: &api.PersistentVolume{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "bar"},
|
||||
@ -956,6 +993,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
|
||||
unexpectedElements: []string{"VolumeMode", "Filesystem"},
|
||||
},
|
||||
{
|
||||
name: "test7",
|
||||
plugin: "quobyte",
|
||||
pv: &api.PersistentVolume{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "bar"},
|
||||
@ -968,6 +1006,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
|
||||
unexpectedElements: []string{"VolumeMode", "Filesystem"},
|
||||
},
|
||||
{
|
||||
name: "test8",
|
||||
plugin: "cinder",
|
||||
pv: &api.PersistentVolume{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "bar"},
|
||||
@ -980,6 +1019,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
|
||||
unexpectedElements: []string{"VolumeMode", "Filesystem"},
|
||||
},
|
||||
{
|
||||
name: "test9",
|
||||
plugin: "fc",
|
||||
pv: &api.PersistentVolume{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "bar"},
|
||||
@ -993,6 +1033,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
|
||||
expectedElements: []string{"VolumeMode", "Block"},
|
||||
},
|
||||
{
|
||||
name: "test10",
|
||||
plugin: "local",
|
||||
pv: &api.PersistentVolume{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "bar"},
|
||||
@ -1006,6 +1047,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
|
||||
unexpectedElements: []string{"Required Terms", "Term "},
|
||||
},
|
||||
{
|
||||
name: "test11",
|
||||
plugin: "local",
|
||||
pv: &api.PersistentVolume{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "bar"},
|
||||
@ -1020,6 +1062,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
|
||||
unexpectedElements: []string{"Required Terms", "Term "},
|
||||
},
|
||||
{
|
||||
name: "test12",
|
||||
plugin: "local",
|
||||
pv: &api.PersistentVolume{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "bar"},
|
||||
@ -1036,6 +1079,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
|
||||
unexpectedElements: []string{"Term "},
|
||||
},
|
||||
{
|
||||
name: "test13",
|
||||
plugin: "local",
|
||||
pv: &api.PersistentVolume{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "bar"},
|
||||
@ -1060,6 +1104,7 @@ func TestPersistentVolumeDescriber(t *testing.T) {
|
||||
expectedElements: []string{"Node Affinity", "Required Terms", "Term 0", "Term 1"},
|
||||
},
|
||||
{
|
||||
name: "test14",
|
||||
plugin: "local",
|
||||
pv: &api.PersistentVolume{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "bar"},
|
||||
@ -1095,25 +1140,27 @@ func TestPersistentVolumeDescriber(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
fake := fake.NewSimpleClientset(test.pv)
|
||||
c := PersistentVolumeDescriber{fake}
|
||||
str, err := c.Describe("foo", "bar", printers.DescriberSettings{ShowEvents: true})
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error for test %s: %v", test.plugin, err)
|
||||
}
|
||||
if str == "" {
|
||||
t.Errorf("Unexpected empty string for test %s. Expected PV Describer output", test.plugin)
|
||||
}
|
||||
for _, expected := range test.expectedElements {
|
||||
if !strings.Contains(str, expected) {
|
||||
t.Errorf("expected to find %q in output: %q", expected, str)
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
fake := fake.NewSimpleClientset(test.pv)
|
||||
c := PersistentVolumeDescriber{fake}
|
||||
str, err := c.Describe("foo", "bar", printers.DescriberSettings{ShowEvents: true})
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error for test %s: %v", test.plugin, err)
|
||||
}
|
||||
}
|
||||
for _, unexpected := range test.unexpectedElements {
|
||||
if strings.Contains(str, unexpected) {
|
||||
t.Errorf("unexpected to find %q in output: %q", unexpected, str)
|
||||
if str == "" {
|
||||
t.Errorf("Unexpected empty string for test %s. Expected PV Describer output", test.plugin)
|
||||
}
|
||||
}
|
||||
for _, expected := range test.expectedElements {
|
||||
if !strings.Contains(str, expected) {
|
||||
t.Errorf("expected to find %q in output: %q", expected, str)
|
||||
}
|
||||
}
|
||||
for _, unexpected := range test.unexpectedElements {
|
||||
if strings.Contains(str, unexpected) {
|
||||
t.Errorf("unexpected to find %q in output: %q", unexpected, str)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -1272,25 +1319,27 @@ func TestPersistentVolumeClaimDescriber(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
fake := fake.NewSimpleClientset(test.pvc)
|
||||
c := PersistentVolumeClaimDescriber{fake}
|
||||
str, err := c.Describe("foo", "bar", printers.DescriberSettings{ShowEvents: true})
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error for test %s: %v", test.name, err)
|
||||
}
|
||||
if str == "" {
|
||||
t.Errorf("Unexpected empty string for test %s. Expected PVC Describer output", test.name)
|
||||
}
|
||||
for _, expected := range test.expectedElements {
|
||||
if !strings.Contains(str, expected) {
|
||||
t.Errorf("expected to find %q in output: %q", expected, str)
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
fake := fake.NewSimpleClientset(test.pvc)
|
||||
c := PersistentVolumeClaimDescriber{fake}
|
||||
str, err := c.Describe("foo", "bar", printers.DescriberSettings{ShowEvents: true})
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error for test %s: %v", test.name, err)
|
||||
}
|
||||
}
|
||||
for _, unexpected := range test.unexpectedElements {
|
||||
if strings.Contains(str, unexpected) {
|
||||
t.Errorf("unexpected to find %q in output: %q", unexpected, str)
|
||||
if str == "" {
|
||||
t.Errorf("Unexpected empty string for test %s. Expected PVC Describer output", test.name)
|
||||
}
|
||||
}
|
||||
for _, expected := range test.expectedElements {
|
||||
if !strings.Contains(str, expected) {
|
||||
t.Errorf("expected to find %q in output: %q", expected, str)
|
||||
}
|
||||
}
|
||||
for _, unexpected := range test.unexpectedElements {
|
||||
if strings.Contains(str, unexpected) {
|
||||
t.Errorf("unexpected to find %q in output: %q", unexpected, str)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -1880,20 +1929,22 @@ func TestDescribeHorizontalPodAutoscaler(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
test.hpa.ObjectMeta = metav1.ObjectMeta{
|
||||
Name: "bar",
|
||||
Namespace: "foo",
|
||||
}
|
||||
fake := fake.NewSimpleClientset(&test.hpa)
|
||||
desc := HorizontalPodAutoscalerDescriber{fake}
|
||||
str, err := desc.Describe("foo", "bar", printers.DescriberSettings{ShowEvents: true})
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error for test %s: %v", test.name, err)
|
||||
}
|
||||
if str == "" {
|
||||
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.Run(test.name, func(t *testing.T) {
|
||||
test.hpa.ObjectMeta = metav1.ObjectMeta{
|
||||
Name: "bar",
|
||||
Namespace: "foo",
|
||||
}
|
||||
fake := fake.NewSimpleClientset(&test.hpa)
|
||||
desc := HorizontalPodAutoscalerDescriber{fake}
|
||||
str, err := desc.Describe("foo", "bar", printers.DescriberSettings{ShowEvents: true})
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error for test %s: %v", test.name, err)
|
||||
}
|
||||
if str == "" {
|
||||
t.Errorf("Unexpected empty string for test %s. Expected HPA Describer output", test.name)
|
||||
}
|
||||
t.Logf("Description for %q:\n%s", test.name, str)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -2023,27 +2074,29 @@ func TestDescribeEvents(t *testing.T) {
|
||||
}
|
||||
|
||||
for name, d := range m {
|
||||
out, err := d.Describe("foo", "bar", printers.DescriberSettings{ShowEvents: true})
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error for %q: %v", name, err)
|
||||
}
|
||||
if !strings.Contains(out, "bar") {
|
||||
t.Errorf("unexpected out for %q: %s", name, out)
|
||||
}
|
||||
if !strings.Contains(out, "Events:") {
|
||||
t.Errorf("events not found for %q when ShowEvents=true: %s", name, out)
|
||||
}
|
||||
t.Run(name, func(t *testing.T) {
|
||||
out, err := d.Describe("foo", "bar", printers.DescriberSettings{ShowEvents: true})
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error for %q: %v", name, err)
|
||||
}
|
||||
if !strings.Contains(out, "bar") {
|
||||
t.Errorf("unexpected out for %q: %s", name, out)
|
||||
}
|
||||
if !strings.Contains(out, "Events:") {
|
||||
t.Errorf("events not found for %q when ShowEvents=true: %s", name, out)
|
||||
}
|
||||
|
||||
out, err = d.Describe("foo", "bar", printers.DescriberSettings{ShowEvents: false})
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error for %q: %s", name, err)
|
||||
}
|
||||
if !strings.Contains(out, "bar") {
|
||||
t.Errorf("unexpected out for %q: %s", name, out)
|
||||
}
|
||||
if strings.Contains(out, "Events:") {
|
||||
t.Errorf("events found for %q when ShowEvents=false: %s", name, out)
|
||||
}
|
||||
out, err = d.Describe("foo", "bar", printers.DescriberSettings{ShowEvents: false})
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error for %q: %s", name, err)
|
||||
}
|
||||
if !strings.Contains(out, "bar") {
|
||||
t.Errorf("unexpected out for %q: %s", name, out)
|
||||
}
|
||||
if strings.Contains(out, "Events:") {
|
||||
t.Errorf("events found for %q when ShowEvents=false: %s", name, out)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -2071,13 +2124,15 @@ func TestPrintLabelsMultiline(t *testing.T) {
|
||||
},
|
||||
}
|
||||
for i, testCase := range testCases {
|
||||
out := new(bytes.Buffer)
|
||||
writer := NewPrefixWriter(out)
|
||||
printAnnotationsMultiline(writer, "Annotations", testCase.annotations)
|
||||
output := out.String()
|
||||
if output != testCase.expectPrint {
|
||||
t.Errorf("Test case %d: expected to find %q in output: %q", i, testCase.expectPrint, output)
|
||||
}
|
||||
t.Run(testCase.expectPrint, func(t *testing.T) {
|
||||
out := new(bytes.Buffer)
|
||||
writer := NewPrefixWriter(out)
|
||||
printAnnotationsMultiline(writer, "Annotations", testCase.annotations)
|
||||
output := out.String()
|
||||
if output != testCase.expectPrint {
|
||||
t.Errorf("Test case %d: expected to find %q in output: %q", i, testCase.expectPrint, output)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -26,13 +26,15 @@ import (
|
||||
)
|
||||
|
||||
func TestTemplate(t *testing.T) {
|
||||
testCase := map[string]struct {
|
||||
testCase := []struct {
|
||||
name string
|
||||
template string
|
||||
obj runtime.Object
|
||||
expectOut string
|
||||
expectErr func(error) (string, bool)
|
||||
}{
|
||||
"support base64 decoding of secret data": {
|
||||
{
|
||||
name: "support base64 decoding of secret data",
|
||||
template: "{{ .data.username | base64decode }}",
|
||||
obj: &v1.Secret{
|
||||
Data: map[string][]byte{
|
||||
@ -41,7 +43,8 @@ func TestTemplate(t *testing.T) {
|
||||
},
|
||||
expectOut: "hunter",
|
||||
},
|
||||
"test error path for base64 decoding": {
|
||||
{
|
||||
name: "test error path for base64 decoding",
|
||||
template: "{{ .data.username | base64decode }}",
|
||||
obj: &badlyMarshaledSecret{},
|
||||
expectErr: func(err error) (string, bool) {
|
||||
@ -50,41 +53,43 @@ func TestTemplate(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
for name, test := range testCase {
|
||||
buffer := &bytes.Buffer{}
|
||||
for _, test := range testCase {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
buffer := &bytes.Buffer{}
|
||||
|
||||
p, err := NewGoTemplatePrinter([]byte(test.template))
|
||||
if err != nil {
|
||||
if test.expectErr == nil {
|
||||
t.Errorf("[%s]expected success but got:\n %v\n", name, err)
|
||||
continue
|
||||
p, err := NewGoTemplatePrinter([]byte(test.template))
|
||||
if err != nil {
|
||||
if test.expectErr == nil {
|
||||
t.Errorf("[%s]expected success but got:\n %v\n", test.name, err)
|
||||
return
|
||||
}
|
||||
if expected, ok := test.expectErr(err); !ok {
|
||||
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", test.name, expected, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if expected, ok := test.expectErr(err); !ok {
|
||||
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", name, expected, err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
err = p.PrintObj(test.obj, buffer)
|
||||
if err != nil {
|
||||
if test.expectErr == nil {
|
||||
t.Errorf("[%s]expected success but got:\n %v\n", name, err)
|
||||
continue
|
||||
err = p.PrintObj(test.obj, buffer)
|
||||
if err != nil {
|
||||
if test.expectErr == nil {
|
||||
t.Errorf("[%s]expected success but got:\n %v\n", test.name, err)
|
||||
return
|
||||
}
|
||||
if expected, ok := test.expectErr(err); !ok {
|
||||
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", test.name, expected, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if expected, ok := test.expectErr(err); !ok {
|
||||
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", name, expected, err)
|
||||
|
||||
if test.expectErr != nil {
|
||||
t.Errorf("[%s]expect:\n error\n but got:\n no error\n", test.name)
|
||||
return
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if test.expectErr != nil {
|
||||
t.Errorf("[%s]expect:\n error\n but got:\n no error\n", name)
|
||||
continue
|
||||
}
|
||||
|
||||
if test.expectOut != buffer.String() {
|
||||
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", name, test.expectOut, buffer.String())
|
||||
}
|
||||
if test.expectOut != buffer.String() {
|
||||
t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", test.name, test.expectOut, buffer.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user