From e4dbaef3ccbd88d9e65befd8d0d666a793509ea2 Mon Sep 17 00:00:00 2001 From: Sebastian Laskawiec Date: Thu, 20 Jan 2022 15:04:15 +0100 Subject: [PATCH] Fix printing control character in kubectl explain --- .../kubectl/pkg/explain/model_printer.go | 2 +- .../kubectl/pkg/explain/model_printer_test.go | 36 ++++++++++++++++--- .../kubectl/pkg/explain/test-swagger.json | 15 ++++++++ 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/staging/src/k8s.io/kubectl/pkg/explain/model_printer.go b/staging/src/k8s.io/kubectl/pkg/explain/model_printer.go index 2de90c11f34..8bd145dd20a 100644 --- a/staging/src/k8s.io/kubectl/pkg/explain/model_printer.go +++ b/staging/src/k8s.io/kubectl/pkg/explain/model_printer.go @@ -68,7 +68,7 @@ func (m *modelPrinter) PrintDescription(schema proto.Schema) error { return err } } - if err := m.Writer.Indent(descriptionIndentLevel).WriteWrapped(desc); err != nil { + if err := m.Writer.Indent(descriptionIndentLevel).WriteWrapped("%s", desc); err != nil { return err } } diff --git a/staging/src/k8s.io/kubectl/pkg/explain/model_printer_test.go b/staging/src/k8s.io/kubectl/pkg/explain/model_printer_test.go index a6692df3755..4b639a2817b 100644 --- a/staging/src/k8s.io/kubectl/pkg/explain/model_printer_test.go +++ b/staging/src/k8s.io/kubectl/pkg/explain/model_printer_test.go @@ -24,19 +24,22 @@ import ( ) func TestModel(t *testing.T) { - gvk := schema.GroupVersionKind{ + oneKind := schema.GroupVersionKind{ Group: "", Version: "v1", Kind: "OneKind", } - schema := resources.LookupResource(gvk) - if schema == nil { - t.Fatal("Couldn't find schema v1.OneKind") + + controlCharacterKind := schema.GroupVersionKind{ + Group: "", + Version: "v1", + Kind: "ControlCharacterKind", } tests := []struct { path []string want string + gvk schema.GroupVersionKind }{ { want: `KIND: OneKind @@ -60,6 +63,7 @@ FIELDS: `, path: []string{}, + gvk: oneKind, }, { want: `KIND: OneKind @@ -95,6 +99,7 @@ FIELDS: `, path: []string{"field1"}, + gvk: oneKind, }, { want: `KIND: OneKind @@ -106,6 +111,7 @@ DESCRIPTION: This string must be a string `, path: []string{"field1", "string"}, + gvk: oneKind, }, { want: `KIND: OneKind @@ -119,12 +125,32 @@ DESCRIPTION: This is an int in an array `, path: []string{"field1", "array"}, + gvk: oneKind, + }, + { + want: `KIND: ControlCharacterKind +VERSION: v1 + +DESCRIPTION: + Control character % + +FIELDS: + field1 <> + Control character % + +`, + path: []string{}, + gvk: controlCharacterKind, }, } for _, test := range tests { + schema := resources.LookupResource(test.gvk) + if schema == nil { + t.Fatalf("Couldn't find schema %v", test.gvk) + } buf := bytes.Buffer{} - if err := PrintModelDescription(test.path, &buf, schema, gvk, false); err != nil { + if err := PrintModelDescription(test.path, &buf, schema, test.gvk, false); err != nil { t.Fatalf("Failed to PrintModelDescription for path %v: %v", test.path, err) } got := buf.String() diff --git a/staging/src/k8s.io/kubectl/pkg/explain/test-swagger.json b/staging/src/k8s.io/kubectl/pkg/explain/test-swagger.json index 9c137fb946c..ba5fa1ff71c 100644 --- a/staging/src/k8s.io/kubectl/pkg/explain/test-swagger.json +++ b/staging/src/k8s.io/kubectl/pkg/explain/test-swagger.json @@ -39,6 +39,21 @@ } ] }, + "ControlCharacterKind": { + "description": "Control character %", + "properties": { + "field1": { + "description": "Control character %", + } + }, + "x-kubernetes-group-version-kind": [ + { + "group": "", + "kind": "ControlCharacterKind", + "version": "v1" + } + ] + }, "OtherKind": { "description": "This is another kind of Kind", "required": [