@@ -229,78 +229,6 @@ func (obj *TestUnknownType) DeepCopyObject() runtime.Object {
229
229
return & clone
230
230
}
231
231
232
- func TestPrinter (t * testing.T ) {
233
- //test inputs
234
- simpleTest := & TestPrintType {"foo" }
235
- podTest := & api.Pod {ObjectMeta : metav1.ObjectMeta {Name : "foo" }}
236
- podListTest := & api.PodList {
237
- Items : []api.Pod {
238
- {ObjectMeta : metav1.ObjectMeta {Name : "foo" }},
239
- {ObjectMeta : metav1.ObjectMeta {Name : "bar" }},
240
- },
241
- }
242
- emptyListTest := & api.PodList {}
243
- testapi , err := legacyscheme .Scheme .ConvertToVersion (podTest , schema.GroupVersion {Group : "" , Version : "v1" })
244
- if err != nil {
245
- t .Fatalf ("unexpected error: %v" , err )
246
- }
247
-
248
- printerTests := []struct {
249
- Name string
250
- PrintOpts * printers.PrintOptions
251
- Input runtime.Object
252
- OutputVersions []schema.GroupVersion
253
- Expect string
254
- }{
255
- {"test json" , & printers.PrintOptions {OutputFormatType : "json" , AllowMissingKeys : true }, simpleTest , nil , "{\n \" Data\" : \" foo\" \n }\n " },
256
- {"test yaml" , & printers.PrintOptions {OutputFormatType : "yaml" , AllowMissingKeys : true }, simpleTest , nil , "Data: foo\n " },
257
- {"test template" , & printers.PrintOptions {OutputFormatType : "template" , OutputFormatArgument : "{{if .id}}{{.id}}{{end}}{{if .metadata.name}}{{.metadata.name}}{{end}}" , AllowMissingKeys : true },
258
- podTest , []schema.GroupVersion {v1 .SchemeGroupVersion }, "foo" },
259
- {"test jsonpath" , & printers.PrintOptions {OutputFormatType : "jsonpath" , OutputFormatArgument : "{.metadata.name}" , AllowMissingKeys : true }, podTest , []schema.GroupVersion {v1 .SchemeGroupVersion }, "foo" },
260
- {"test jsonpath list" , & printers.PrintOptions {OutputFormatType : "jsonpath" , OutputFormatArgument : "{.items[*].metadata.name}" , AllowMissingKeys : true }, podListTest , []schema.GroupVersion {v1 .SchemeGroupVersion }, "foo bar" },
261
- {"test jsonpath empty list" , & printers.PrintOptions {OutputFormatType : "jsonpath" , OutputFormatArgument : "{.items[*].metadata.name}" , AllowMissingKeys : true }, emptyListTest , []schema.GroupVersion {v1 .SchemeGroupVersion }, "" },
262
- {"test name" , & printers.PrintOptions {OutputFormatType : "name" , AllowMissingKeys : true }, podTest , []schema.GroupVersion {v1 .SchemeGroupVersion }, "pod/foo\n " },
263
- {"emits versioned objects" , & printers.PrintOptions {OutputFormatType : "template" , OutputFormatArgument : "{{.kind}}" , AllowMissingKeys : true }, testapi , []schema.GroupVersion {v1 .SchemeGroupVersion }, "Pod" },
264
- }
265
- for _ , test := range printerTests {
266
- buf := bytes .NewBuffer ([]byte {})
267
- printer , err := printers .GetStandardPrinter (legacyscheme .Scheme , legacyscheme .Codecs .LegacyCodec (legacyscheme .Registry .RegisteredGroupVersions ()... ), []runtime.Decoder {legacyscheme .Codecs .UniversalDecoder (), unstructured .UnstructuredJSONScheme }, * test .PrintOpts )
268
- if err != nil {
269
- t .Errorf ("in %s, unexpected error: %#v" , test .Name , err )
270
- }
271
- if len (test .OutputVersions ) > 0 {
272
- printer = printers .NewVersionedPrinter (printer , legacyscheme .Scheme , legacyscheme .Scheme , test .OutputVersions ... )
273
- }
274
- if err := printer .PrintObj (test .Input , buf ); err != nil {
275
- t .Errorf ("in %s, unexpected error: %#v" , test .Name , err )
276
- }
277
- if buf .String () != test .Expect {
278
- t .Errorf ("in %s, expect %q, got %q" , test .Name , test .Expect , buf .String ())
279
- }
280
- }
281
-
282
- }
283
-
284
- func TestBadPrinter (t * testing.T ) {
285
- badPrinterTests := []struct {
286
- Name string
287
- PrintOpts * printers.PrintOptions
288
- Error error
289
- }{
290
- {"empty template" , & printers.PrintOptions {OutputFormatType : "template" , AllowMissingKeys : false }, fmt .Errorf ("template format specified but no template given" )},
291
- {"bad template" , & printers.PrintOptions {OutputFormatType : "template" , OutputFormatArgument : "{{ .Name" , AllowMissingKeys : false }, fmt .Errorf ("error parsing template {{ .Name, template: output:1: unclosed action\n " )},
292
- {"bad templatefile" , & printers.PrintOptions {OutputFormatType : "templatefile" , AllowMissingKeys : false }, fmt .Errorf ("template format specified but no template given" )},
293
- {"bad jsonpath" , & printers.PrintOptions {OutputFormatType : "jsonpath" , OutputFormatArgument : "{.Name" , AllowMissingKeys : false }, fmt .Errorf ("error parsing jsonpath {.Name, unclosed action\n " )},
294
- {"unknown format" , & printers.PrintOptions {OutputFormatType : "anUnknownFormat" , OutputFormatArgument : "" , AllowMissingKeys : false }, fmt .Errorf ("output format \" anUnknownFormat\" not recognized" )},
295
- }
296
- for _ , test := range badPrinterTests {
297
- _ , err := printers .GetStandardPrinter (legacyscheme .Scheme , legacyscheme .Codecs .LegacyCodec (legacyscheme .Registry .RegisteredGroupVersions ()... ), []runtime.Decoder {legacyscheme .Codecs .UniversalDecoder (), unstructured .UnstructuredJSONScheme }, * test .PrintOpts )
298
- if err == nil || err .Error () != test .Error .Error () {
299
- t .Errorf ("in %s, expect %s, got %s" , test .Name , test .Error , err )
300
- }
301
- }
302
- }
303
-
304
232
func testPrinter (t * testing.T , printer printers.ResourcePrinter , unmarshalFunc func (data []byte , v interface {}) error ) {
305
233
buf := bytes .NewBuffer ([]byte {})
306
234
@@ -471,8 +399,13 @@ func TestNamePrinter(t *testing.T) {
471
399
},
472
400
"pod/foo\n pod/bar\n " },
473
401
}
474
- printOpts := & printers.PrintOptions {OutputFormatType : "name" , AllowMissingKeys : false }
475
- printer , _ := printers .GetStandardPrinter (legacyscheme .Scheme , legacyscheme .Codecs .LegacyCodec (legacyscheme .Registry .RegisteredGroupVersions ()... ), []runtime.Decoder {legacyscheme .Codecs .UniversalDecoder (), unstructured .UnstructuredJSONScheme }, * printOpts )
402
+
403
+ printFlags := printers .NewPrintFlags ("" ).WithDefaultOutput ("name" )
404
+ printer , err := printFlags .ToPrinter ()
405
+ if err != nil {
406
+ t .Fatalf ("unexpected err: %v" , err )
407
+ }
408
+
476
409
for name , item := range tests {
477
410
buff := & bytes.Buffer {}
478
411
err := printer .PrintObj (item .obj , buff )
@@ -2987,44 +2920,6 @@ func TestPrintPodDisruptionBudget(t *testing.T) {
2987
2920
}
2988
2921
}
2989
2922
2990
- func TestAllowMissingKeys (t * testing.T ) {
2991
- tests := []struct {
2992
- Name string
2993
- PrintOpts * printers.PrintOptions
2994
- Input runtime.Object
2995
- Expect string
2996
- Error string
2997
- }{
2998
- {"test template, allow missing keys" , & printers.PrintOptions {OutputFormatType : "template" , OutputFormatArgument : "{{.blarg}}" , AllowMissingKeys : true }, & api.Pod {}, "<no value>" , "" },
2999
- {"test template, strict" , & printers.PrintOptions {OutputFormatType : "template" , OutputFormatArgument : "{{.blarg}}" , AllowMissingKeys : false }, & api.Pod {}, "" , `error executing template "{{.blarg}}": template: output:1:2: executing "output" at <.blarg>: map has no entry for key "blarg"` },
3000
- {"test jsonpath, allow missing keys" , & printers.PrintOptions {OutputFormatType : "jsonpath" , OutputFormatArgument : "{.blarg}" , AllowMissingKeys : true }, & api.Pod {}, "" , "" },
3001
- {"test jsonpath, strict" , & printers.PrintOptions {OutputFormatType : "jsonpath" , OutputFormatArgument : "{.blarg}" , AllowMissingKeys : false }, & api.Pod {}, "" , "error executing jsonpath \" {.blarg}\" : blarg is not found\n " },
3002
- }
3003
- for _ , test := range tests {
3004
- buf := bytes .NewBuffer ([]byte {})
3005
- printer , err := printers .GetStandardPrinter (legacyscheme .Scheme , legacyscheme .Codecs .LegacyCodec (legacyscheme .Registry .RegisteredGroupVersions ()... ), []runtime.Decoder {legacyscheme .Codecs .UniversalDecoder (), unstructured .UnstructuredJSONScheme }, * test .PrintOpts )
3006
- if err != nil {
3007
- t .Errorf ("in %s, unexpected error: %#v" , test .Name , err )
3008
- }
3009
- err = printer .PrintObj (test .Input , buf )
3010
- if len (test .Error ) == 0 && err != nil {
3011
- t .Errorf ("in %s, unexpected error: %v" , test .Name , err )
3012
- continue
3013
- }
3014
- if len (test .Error ) > 0 {
3015
- if err == nil {
3016
- t .Errorf ("in %s, expected to get error: %v" , test .Name , test .Error )
3017
- } else if e , a := test .Error , err .Error (); e != a {
3018
- t .Errorf ("in %s, expected error %q, got %q" , test .Name , e , a )
3019
- }
3020
- continue
3021
- }
3022
- if buf .String () != test .Expect {
3023
- t .Errorf ("in %s, expect %q, got %q" , test .Name , test .Expect , buf .String ())
3024
- }
3025
- }
3026
- }
3027
-
3028
2923
func TestPrintControllerRevision (t * testing.T ) {
3029
2924
tests := []struct {
3030
2925
history apps.ControllerRevision
0 commit comments