...
1
16
17 package genericclioptions
18
19 import (
20 "bytes"
21 "testing"
22
23 "k8s.io/api/core/v1"
24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
26 "k8s.io/apimachinery/pkg/runtime"
27 "k8s.io/client-go/kubernetes/scheme"
28 )
29
30 func TestNamePrinter(t *testing.T) {
31 tests := map[string]struct {
32 obj runtime.Object
33 expect string
34 }{
35 "singleObject": {
36 &v1.Pod{
37 TypeMeta: metav1.TypeMeta{
38 Kind: "Pod",
39 },
40 ObjectMeta: metav1.ObjectMeta{
41 Name: "foo",
42 },
43 },
44 "pod/foo\n"},
45 "List": {
46 &unstructured.UnstructuredList{
47 Object: map[string]interface{}{
48 "kind": "List",
49 "apiVersion": "v1",
50 },
51 Items: []unstructured.Unstructured{
52 {
53 Object: map[string]interface{}{
54 "kind": "Pod",
55 "apiVersion": "v1",
56 "metadata": map[string]interface{}{
57 "name": "bar",
58 },
59 },
60 },
61 },
62 },
63 "pod/bar\n"},
64 }
65
66 printFlags := NewPrintFlags("").WithTypeSetter(scheme.Scheme).WithDefaultOutput("name")
67 printer, err := printFlags.ToPrinter()
68 if err != nil {
69 t.Fatalf("unexpected err: %v", err)
70 }
71
72 for name, item := range tests {
73 buff := &bytes.Buffer{}
74 err := printer.PrintObj(item.obj, buff)
75 if err != nil {
76 t.Errorf("%v: unexpected err: %v", name, err)
77 continue
78 }
79 got := buff.String()
80 if item.expect != got {
81 t.Errorf("%v: expected %v, got %v", name, item.expect, got)
82 }
83 }
84 }
85
View as plain text