From 290407c7eb2022ee39c075fd57401a62daca69ef Mon Sep 17 00:00:00 2001 From: Sean Sullivan Date: Thu, 31 Oct 2019 16:04:01 -0700 Subject: [PATCH] Moves test to new print_flags_test.go --- pkg/printers/internalversion/BUILD | 2 - .../additional_printers_test.go | 58 ------------- .../cli-runtime/pkg/genericclioptions/BUILD | 4 + .../pkg/genericclioptions/print_flags_test.go | 84 +++++++++++++++++++ 4 files changed, 88 insertions(+), 60 deletions(-) create mode 100644 staging/src/k8s.io/cli-runtime/pkg/genericclioptions/print_flags_test.go diff --git a/pkg/printers/internalversion/BUILD b/pkg/printers/internalversion/BUILD index 29661f62372..5cd02833b91 100644 --- a/pkg/printers/internalversion/BUILD +++ b/pkg/printers/internalversion/BUILD @@ -34,7 +34,6 @@ go_test( "//staging/src/k8s.io/api/core/v1:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/api/resource:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", - "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1beta1:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library", @@ -42,7 +41,6 @@ go_test( "//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/util/intstr:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library", - "//staging/src/k8s.io/cli-runtime/pkg/genericclioptions:go_default_library", "//staging/src/k8s.io/cli-runtime/pkg/printers:go_default_library", "//vendor/gopkg.in/yaml.v2:go_default_library", "//vendor/k8s.io/utils/pointer:go_default_library", diff --git a/pkg/printers/internalversion/additional_printers_test.go b/pkg/printers/internalversion/additional_printers_test.go index 6c1002b80ee..132d5e1d9c3 100644 --- a/pkg/printers/internalversion/additional_printers_test.go +++ b/pkg/printers/internalversion/additional_printers_test.go @@ -26,14 +26,12 @@ import ( yaml "gopkg.in/yaml.v2" "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" yamlserializer "k8s.io/apimachinery/pkg/runtime/serializer/yaml" "k8s.io/apimachinery/pkg/util/diff" "k8s.io/apimachinery/pkg/util/sets" - "k8s.io/cli-runtime/pkg/genericclioptions" genericprinters "k8s.io/cli-runtime/pkg/printers" "k8s.io/kubernetes/pkg/api/legacyscheme" "k8s.io/kubernetes/pkg/api/testapi" @@ -202,62 +200,6 @@ func TestPrintHandlerError(t *testing.T) { } } -func TestNamePrinter(t *testing.T) { - tests := map[string]struct { - obj runtime.Object - expect string - }{ - "singleObject": { - &v1.Pod{ - TypeMeta: metav1.TypeMeta{ - Kind: "Pod", - }, - ObjectMeta: metav1.ObjectMeta{ - Name: "foo", - }, - }, - "pod/foo\n"}, - "List": { - &unstructured.UnstructuredList{ - Object: map[string]interface{}{ - "kind": "List", - "apiVersion": "v1", - }, - Items: []unstructured.Unstructured{ - { - Object: map[string]interface{}{ - "kind": "Pod", - "apiVersion": "v1", - "metadata": map[string]interface{}{ - "name": "bar", - }, - }, - }, - }, - }, - "pod/bar\n"}, - } - - printFlags := genericclioptions.NewPrintFlags("").WithTypeSetter(legacyscheme.Scheme).WithDefaultOutput("name") - printer, err := printFlags.ToPrinter() - if err != nil { - t.Fatalf("unexpected err: %v", err) - } - - for name, item := range tests { - buff := &bytes.Buffer{} - err := printer.PrintObj(item.obj, buff) - if err != nil { - t.Errorf("%v: unexpected err: %v", name, err) - continue - } - got := buff.String() - if item.expect != got { - t.Errorf("%v: expected %v, got %v", name, item.expect, got) - } - } -} - // 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/genericclioptions/BUILD b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/BUILD index 9b535f5c649..6bad9bedbf2 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/BUILD +++ b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/BUILD @@ -60,11 +60,15 @@ go_test( "json_yaml_flags_test.go", "jsonpath_flags_test.go", "name_flags_test.go", + "print_flags_test.go", "template_flags_test.go", ], 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/apis/meta/v1/unstructured:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library", + "//staging/src/k8s.io/client-go/kubernetes/scheme:go_default_library", ], ) diff --git a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/print_flags_test.go b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/print_flags_test.go new file mode 100644 index 00000000000..bad517d9d0b --- /dev/null +++ b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/print_flags_test.go @@ -0,0 +1,84 @@ +/* +Copyright 2019 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package genericclioptions + +import ( + "bytes" + "testing" + + "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/kubernetes/scheme" +) + +func TestNamePrinter(t *testing.T) { + tests := map[string]struct { + obj runtime.Object + expect string + }{ + "singleObject": { + &v1.Pod{ + TypeMeta: metav1.TypeMeta{ + Kind: "Pod", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + }, + }, + "pod/foo\n"}, + "List": { + &unstructured.UnstructuredList{ + Object: map[string]interface{}{ + "kind": "List", + "apiVersion": "v1", + }, + Items: []unstructured.Unstructured{ + { + Object: map[string]interface{}{ + "kind": "Pod", + "apiVersion": "v1", + "metadata": map[string]interface{}{ + "name": "bar", + }, + }, + }, + }, + }, + "pod/bar\n"}, + } + + printFlags := NewPrintFlags("").WithTypeSetter(scheme.Scheme).WithDefaultOutput("name") + printer, err := printFlags.ToPrinter() + if err != nil { + t.Fatalf("unexpected err: %v", err) + } + + for name, item := range tests { + buff := &bytes.Buffer{} + err := printer.PrintObj(item.obj, buff) + if err != nil { + t.Errorf("%v: unexpected err: %v", name, err) + continue + } + got := buff.String() + if item.expect != got { + t.Errorf("%v: expected %v, got %v", name, item.expect, got) + } + } +}