customcolumn_flags_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package get
  14. import (
  15. "bytes"
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "strings"
  20. "testing"
  21. corev1 "k8s.io/api/core/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/cli-runtime/pkg/genericclioptions"
  24. )
  25. func TestPrinterSupportsExpectedCustomColumnFormats(t *testing.T) {
  26. testObject := &corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
  27. customColumnsFile, err := ioutil.TempFile("", "printers_jsonpath_flags")
  28. if err != nil {
  29. t.Fatalf("unexpected error: %v", err)
  30. }
  31. defer func(tempFile *os.File) {
  32. tempFile.Close()
  33. os.Remove(tempFile.Name())
  34. }(customColumnsFile)
  35. fmt.Fprintf(customColumnsFile, "NAME\n.metadata.name")
  36. testCases := []struct {
  37. name string
  38. outputFormat string
  39. templateArg string
  40. expectedError string
  41. expectedParseError string
  42. expectedOutput string
  43. expectNoMatch bool
  44. }{
  45. {
  46. name: "valid output format also containing the custom-columns argument succeeds",
  47. outputFormat: "custom-columns=NAME:.metadata.name",
  48. expectedOutput: "foo",
  49. },
  50. {
  51. name: "valid output format and no --template argument results in an error",
  52. outputFormat: "custom-columns",
  53. expectedError: "custom-columns format specified but no custom columns given",
  54. },
  55. {
  56. name: "valid output format and --template argument succeeds",
  57. outputFormat: "custom-columns",
  58. templateArg: "NAME:.metadata.name",
  59. expectedOutput: "foo",
  60. },
  61. {
  62. name: "custom-columns template file should match, and successfully return correct value",
  63. outputFormat: "custom-columns-file",
  64. templateArg: customColumnsFile.Name(),
  65. expectedOutput: "foo",
  66. },
  67. {
  68. name: "valid output format and invalid --template argument results in a parsing error from the printer",
  69. outputFormat: "custom-columns",
  70. templateArg: "invalid",
  71. expectedError: "unexpected custom-columns spec: invalid, expected <header>:<json-path-expr>",
  72. },
  73. {
  74. name: "no printer is matched on an invalid outputFormat",
  75. outputFormat: "invalid",
  76. expectNoMatch: true,
  77. },
  78. {
  79. name: "custom-columns printer should not match on any other format supported by another printer",
  80. outputFormat: "go-template",
  81. expectNoMatch: true,
  82. },
  83. }
  84. for _, tc := range testCases {
  85. t.Run(tc.name, func(t *testing.T) {
  86. printFlags := CustomColumnsPrintFlags{
  87. TemplateArgument: tc.templateArg,
  88. }
  89. p, err := printFlags.ToPrinter(tc.outputFormat)
  90. if tc.expectNoMatch {
  91. if !genericclioptions.IsNoCompatiblePrinterError(err) {
  92. t.Fatalf("expected no printer matches for output format %q", tc.outputFormat)
  93. }
  94. return
  95. }
  96. if genericclioptions.IsNoCompatiblePrinterError(err) {
  97. t.Fatalf("expected to match template printer for output format %q", tc.outputFormat)
  98. }
  99. if len(tc.expectedError) > 0 {
  100. if err == nil || !strings.Contains(err.Error(), tc.expectedError) {
  101. t.Errorf("expecting error %q, got %v", tc.expectedError, err)
  102. }
  103. return
  104. }
  105. if err != nil {
  106. t.Fatalf("unexpected error: %v", err)
  107. }
  108. out := bytes.NewBuffer([]byte{})
  109. err = p.PrintObj(testObject, out)
  110. if len(tc.expectedParseError) > 0 {
  111. if err == nil || !strings.Contains(err.Error(), tc.expectedParseError) {
  112. t.Errorf("expecting error %q, got %v", tc.expectedError, err)
  113. }
  114. return
  115. }
  116. if err != nil {
  117. t.Errorf("unexpected error: %v", err)
  118. }
  119. if !strings.Contains(out.String(), tc.expectedOutput) {
  120. t.Errorf("unexpected output: expecting %q, got %q", tc.expectedOutput, out.String())
  121. }
  122. })
  123. }
  124. }