humanreadable_flags_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. "regexp"
  17. "strings"
  18. "testing"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/runtime/schema"
  21. "k8s.io/cli-runtime/pkg/genericclioptions"
  22. api "k8s.io/kubernetes/pkg/apis/core"
  23. )
  24. func TestHumanReadablePrinterSupportsExpectedOptions(t *testing.T) {
  25. testObject := &api.Pod{ObjectMeta: metav1.ObjectMeta{
  26. Name: "foo",
  27. Labels: map[string]string{
  28. "l1": "value",
  29. },
  30. }}
  31. testCases := []struct {
  32. name string
  33. showKind bool
  34. showLabels bool
  35. // TODO(juanvallejo): test sorting once it's moved to the HumanReadablePrinter
  36. sortBy string
  37. columnLabels []string
  38. noHeaders bool
  39. withNamespace bool
  40. outputFormat string
  41. expectedError string
  42. expectedOutput string
  43. expectNoMatch bool
  44. }{
  45. {
  46. name: "empty output format matches a humanreadable printer",
  47. expectedOutput: "NAME\\ +READY\\ +STATUS\\ +RESTARTS\\ +AGE\nfoo\\ +0/0\\ +0\\ +<unknown>\n",
  48. },
  49. {
  50. name: "\"wide\" output format prints",
  51. outputFormat: "wide",
  52. expectedOutput: "NAME\\ +READY\\ +STATUS\\ +RESTARTS\\ +AGE\\ +IP\\ +NODE\\ +NOMINATED NODE\\ +READINESS GATES\nfoo\\ +0/0\\ +0\\ +<unknown>\\ +<none>\\ +<none>\\ +<none>\\ +<none>\n",
  53. },
  54. {
  55. name: "no-headers prints output with no headers",
  56. noHeaders: true,
  57. expectedOutput: "foo\\ +0/0\\ +0\\ +<unknown>\n",
  58. },
  59. {
  60. name: "no-headers and a \"wide\" output format prints output with no headers and additional columns",
  61. outputFormat: "wide",
  62. noHeaders: true,
  63. expectedOutput: "foo\\ +0/0\\ +0\\ +<unknown>\\ +<none>\\ +<none>\\ +<none>\\ +<none>\n",
  64. },
  65. {
  66. name: "show-kind displays the resource's kind, even when printing a single type of resource",
  67. showKind: true,
  68. expectedOutput: "NAME\\ +READY\\ +STATUS\\ +RESTARTS\\ +AGE\npod/foo\\ +0/0\\ +0\\ +<unknown>\n",
  69. },
  70. {
  71. name: "label-columns prints specified label values in new column",
  72. columnLabels: []string{"l1"},
  73. expectedOutput: "NAME\\ +READY\\ +STATUS\\ +RESTARTS\\ +AGE\\ +L1\nfoo\\ +0/0\\ +0\\ +<unknown>\\ +value\n",
  74. },
  75. {
  76. name: "withNamespace displays an additional NAMESPACE column",
  77. withNamespace: true,
  78. expectedOutput: "NAMESPACE\\ +NAME\\ +READY\\ +STATUS\\ +RESTARTS\\ +AGE\n\\ +foo\\ +0/0\\ +0\\ +<unknown>\n",
  79. },
  80. {
  81. name: "no printer is matched on an invalid outputFormat",
  82. outputFormat: "invalid",
  83. expectNoMatch: true,
  84. },
  85. {
  86. name: "printer should not match on any other format supported by another printer",
  87. outputFormat: "go-template",
  88. expectNoMatch: true,
  89. },
  90. }
  91. for _, tc := range testCases {
  92. t.Run(tc.name, func(t *testing.T) {
  93. printFlags := HumanPrintFlags{
  94. ShowKind: &tc.showKind,
  95. ShowLabels: &tc.showLabels,
  96. SortBy: &tc.sortBy,
  97. ColumnLabels: &tc.columnLabels,
  98. NoHeaders: tc.noHeaders,
  99. WithNamespace: tc.withNamespace,
  100. }
  101. if tc.showKind {
  102. printFlags.Kind = schema.GroupKind{Kind: "pod"}
  103. }
  104. p, err := printFlags.ToPrinter(tc.outputFormat)
  105. if tc.expectNoMatch {
  106. if !genericclioptions.IsNoCompatiblePrinterError(err) {
  107. t.Fatalf("expected no printer matches for output format %q", tc.outputFormat)
  108. }
  109. return
  110. }
  111. if genericclioptions.IsNoCompatiblePrinterError(err) {
  112. t.Fatalf("expected to match template printer for output format %q", tc.outputFormat)
  113. }
  114. if len(tc.expectedError) > 0 {
  115. if err == nil || !strings.Contains(err.Error(), tc.expectedError) {
  116. t.Errorf("expecting error %q, got %v", tc.expectedError, err)
  117. }
  118. return
  119. }
  120. if err != nil {
  121. t.Fatalf("unexpected error: %v", err)
  122. }
  123. out := bytes.NewBuffer([]byte{})
  124. err = p.PrintObj(testObject, out)
  125. if err != nil {
  126. t.Errorf("unexpected error: %v", err)
  127. }
  128. match, err := regexp.Match(tc.expectedOutput, out.Bytes())
  129. if err != nil {
  130. t.Errorf("unexpected error: %v", err)
  131. }
  132. if !match {
  133. t.Errorf("unexpected output: expecting %q, got %q", tc.expectedOutput, out.String())
  134. }
  135. })
  136. }
  137. }