From d9ded62fb9a5dc465dc2918d4bcb1f2cdae4b06e Mon Sep 17 00:00:00 2001 From: Sean Sullivan Date: Wed, 16 Oct 2019 16:27:56 -0700 Subject: [PATCH] Moves TestTemplateStrings to correct location of template_test.go --- .../additional_printers_test.go | 118 ------------------ .../src/k8s.io/cli-runtime/pkg/printers/BUILD | 1 + .../cli-runtime/pkg/printers/template_test.go | 118 ++++++++++++++++++ 3 files changed, 119 insertions(+), 118 deletions(-) diff --git a/pkg/printers/internalversion/additional_printers_test.go b/pkg/printers/internalversion/additional_printers_test.go index 0e5057b452e..fdb91744ff1 100644 --- a/pkg/printers/internalversion/additional_printers_test.go +++ b/pkg/printers/internalversion/additional_printers_test.go @@ -274,124 +274,6 @@ func TestNamePrinter(t *testing.T) { } } -// TODO(seans3): Move this test to cli-runtime/pkg/printers. -func TestTemplateStrings(t *testing.T) { - // This unit tests the "exists" function as well as the template from update.sh - table := map[string]struct { - pod v1.Pod - expect string - }{ - "nilInfo": {v1.Pod{}, "false"}, - "emptyInfo": {v1.Pod{Status: v1.PodStatus{ContainerStatuses: []v1.ContainerStatus{}}}, "false"}, - "fooExists": { - v1.Pod{ - Status: v1.PodStatus{ - ContainerStatuses: []v1.ContainerStatus{ - { - Name: "foo", - }, - }, - }, - }, - "false", - }, - "barExists": { - v1.Pod{ - Status: v1.PodStatus{ - ContainerStatuses: []v1.ContainerStatus{ - { - Name: "bar", - }, - }, - }, - }, - "false", - }, - "bothExist": { - v1.Pod{ - Status: v1.PodStatus{ - ContainerStatuses: []v1.ContainerStatus{ - { - Name: "foo", - }, - { - Name: "bar", - }, - }, - }, - }, - "false", - }, - "barValid": { - v1.Pod{ - Status: v1.PodStatus{ - ContainerStatuses: []v1.ContainerStatus{ - { - Name: "foo", - }, - { - Name: "bar", - State: v1.ContainerState{ - Running: &v1.ContainerStateRunning{ - StartedAt: metav1.Time{}, - }, - }, - }, - }, - }, - }, - "false", - }, - "bothValid": { - v1.Pod{ - Status: v1.PodStatus{ - ContainerStatuses: []v1.ContainerStatus{ - { - Name: "foo", - State: v1.ContainerState{ - Running: &v1.ContainerStateRunning{ - StartedAt: metav1.Time{}, - }, - }, - }, - { - Name: "bar", - State: v1.ContainerState{ - Running: &v1.ContainerStateRunning{ - StartedAt: metav1.Time{}, - }, - }, - }, - }, - }, - }, - "true", - }, - } - // The point of this test is to verify that the below template works. - tmpl := `{{if (exists . "status" "containerStatuses")}}{{range .status.containerStatuses}}{{if (and (eq .name "foo") (exists . "state" "running"))}}true{{end}}{{end}}{{end}}` - printer, err := genericprinters.NewGoTemplatePrinter([]byte(tmpl)) - if err != nil { - t.Fatalf("tmpl fail: %v", err) - } - - for name, item := range table { - buffer := &bytes.Buffer{} - err = printer.PrintObj(&item.pod, buffer) - if err != nil { - t.Errorf("%v: unexpected err: %v", name, err) - continue - } - actual := buffer.String() - if len(actual) == 0 { - actual = "false" - } - if e := item.expect; e != actual { - t.Errorf("%v: expected %v, got %v", name, e, actual) - } - } -} - // TODO(seans3): Move this test to cli-runtime/pkg/printers. func TestPrinters(t *testing.T) { om := func(name string) metav1.ObjectMeta { return metav1.ObjectMeta{Name: name} } diff --git a/staging/src/k8s.io/cli-runtime/pkg/printers/BUILD b/staging/src/k8s.io/cli-runtime/pkg/printers/BUILD index 918aca289a1..f7515915dd7 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/printers/BUILD +++ b/staging/src/k8s.io/cli-runtime/pkg/printers/BUILD @@ -37,6 +37,7 @@ go_test( embed = [":go_default_library"], deps = [ "//staging/src/k8s.io/api/core/v1:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library", ], ) diff --git a/staging/src/k8s.io/cli-runtime/pkg/printers/template_test.go b/staging/src/k8s.io/cli-runtime/pkg/printers/template_test.go index e16f6181bcd..633ee6c5a3a 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/printers/template_test.go +++ b/staging/src/k8s.io/cli-runtime/pkg/printers/template_test.go @@ -22,6 +22,7 @@ import ( "testing" "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" ) @@ -108,3 +109,120 @@ type badlyMarshaledSecret struct { func (a badlyMarshaledSecret) MarshalJSON() ([]byte, error) { return []byte(`{"apiVersion":"v1","data":{"username":"--THIS IS NOT BASE64--"},"kind":"Secret"}`), nil } + +func TestTemplateStrings(t *testing.T) { + // This unit tests the "exists" function as well as the template from update.sh + table := map[string]struct { + pod v1.Pod + expect string + }{ + "nilInfo": {v1.Pod{}, "false"}, + "emptyInfo": {v1.Pod{Status: v1.PodStatus{ContainerStatuses: []v1.ContainerStatus{}}}, "false"}, + "fooExists": { + v1.Pod{ + Status: v1.PodStatus{ + ContainerStatuses: []v1.ContainerStatus{ + { + Name: "foo", + }, + }, + }, + }, + "false", + }, + "barExists": { + v1.Pod{ + Status: v1.PodStatus{ + ContainerStatuses: []v1.ContainerStatus{ + { + Name: "bar", + }, + }, + }, + }, + "false", + }, + "bothExist": { + v1.Pod{ + Status: v1.PodStatus{ + ContainerStatuses: []v1.ContainerStatus{ + { + Name: "foo", + }, + { + Name: "bar", + }, + }, + }, + }, + "false", + }, + "barValid": { + v1.Pod{ + Status: v1.PodStatus{ + ContainerStatuses: []v1.ContainerStatus{ + { + Name: "foo", + }, + { + Name: "bar", + State: v1.ContainerState{ + Running: &v1.ContainerStateRunning{ + StartedAt: metav1.Time{}, + }, + }, + }, + }, + }, + }, + "false", + }, + "bothValid": { + v1.Pod{ + Status: v1.PodStatus{ + ContainerStatuses: []v1.ContainerStatus{ + { + Name: "foo", + State: v1.ContainerState{ + Running: &v1.ContainerStateRunning{ + StartedAt: metav1.Time{}, + }, + }, + }, + { + Name: "bar", + State: v1.ContainerState{ + Running: &v1.ContainerStateRunning{ + StartedAt: metav1.Time{}, + }, + }, + }, + }, + }, + }, + "true", + }, + } + // The point of this test is to verify that the below template works. + tmpl := `{{if (exists . "status" "containerStatuses")}}{{range .status.containerStatuses}}{{if (and (eq .name "foo") (exists . "state" "running"))}}true{{end}}{{end}}{{end}}` + printer, err := NewGoTemplatePrinter([]byte(tmpl)) + if err != nil { + t.Fatalf("tmpl fail: %v", err) + } + + for name, item := range table { + buffer := &bytes.Buffer{} + err = printer.PrintObj(&item.pod, buffer) + if err != nil { + t.Errorf("%v: unexpected err: %v", name, err) + continue + } + actual := buffer.String() + if len(actual) == 0 { + actual = "false" + } + if e := item.expect; e != actual { + t.Errorf("%v: expected %v, got %v", name, e, actual) + } + } +}