tableprinter_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Copyright 2019 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 printers
  14. import (
  15. "bytes"
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. corev1 "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
  22. "k8s.io/apimachinery/pkg/runtime"
  23. )
  24. var testNamespaceColumnDefinitions = []metav1beta1.TableColumnDefinition{
  25. {Name: "Name", Type: "string", Format: "name", Description: metav1.ObjectMeta{}.SwaggerDoc()["name"]},
  26. {Name: "Status", Type: "string", Description: "The status of the namespace"},
  27. {Name: "Age", Type: "string", Description: metav1.ObjectMeta{}.SwaggerDoc()["creationTimestamp"]},
  28. }
  29. func testPrintNamespace(obj *corev1.Namespace, options PrintOptions) ([]metav1beta1.TableRow, error) {
  30. if options.WithNamespace {
  31. return nil, fmt.Errorf("namespace is not namespaced")
  32. }
  33. row := metav1beta1.TableRow{
  34. Object: runtime.RawExtension{Object: obj},
  35. }
  36. row.Cells = append(row.Cells, obj.Name, obj.Status.Phase, "<unknow>")
  37. return []metav1beta1.TableRow{row}, nil
  38. }
  39. func TestPrintRowsForHandlerEntry(t *testing.T) {
  40. printFunc := reflect.ValueOf(testPrintNamespace)
  41. testCase := []struct {
  42. name string
  43. h *handlerEntry
  44. opt PrintOptions
  45. obj runtime.Object
  46. includeHeader bool
  47. expectOut string
  48. expectErr string
  49. }{
  50. {
  51. name: "no tablecolumndefinition and includeheader flase",
  52. h: &handlerEntry{
  53. columnDefinitions: []metav1beta1.TableColumnDefinition{},
  54. printFunc: printFunc,
  55. },
  56. opt: PrintOptions{},
  57. obj: &corev1.Namespace{
  58. ObjectMeta: metav1.ObjectMeta{Name: "test"},
  59. },
  60. includeHeader: false,
  61. expectOut: "test\t\t<unknow>\n",
  62. },
  63. {
  64. name: "no tablecolumndefinition and includeheader true",
  65. h: &handlerEntry{
  66. columnDefinitions: []metav1beta1.TableColumnDefinition{},
  67. printFunc: printFunc,
  68. },
  69. opt: PrintOptions{},
  70. obj: &corev1.Namespace{
  71. ObjectMeta: metav1.ObjectMeta{Name: "test"},
  72. },
  73. includeHeader: true,
  74. expectOut: "\ntest\t\t<unknow>\n",
  75. },
  76. {
  77. name: "have tablecolumndefinition and includeheader true",
  78. h: &handlerEntry{
  79. columnDefinitions: testNamespaceColumnDefinitions,
  80. printFunc: printFunc,
  81. },
  82. opt: PrintOptions{},
  83. obj: &corev1.Namespace{
  84. ObjectMeta: metav1.ObjectMeta{Name: "test"},
  85. },
  86. includeHeader: true,
  87. expectOut: "NAME\tSTATUS\tAGE\ntest\t\t<unknow>\n",
  88. },
  89. {
  90. name: "print namespace and withnamespace true, should not print header",
  91. h: &handlerEntry{
  92. columnDefinitions: testNamespaceColumnDefinitions,
  93. printFunc: printFunc,
  94. },
  95. opt: PrintOptions{
  96. WithNamespace: true,
  97. },
  98. obj: &corev1.Namespace{
  99. ObjectMeta: metav1.ObjectMeta{Name: "test"},
  100. },
  101. includeHeader: true,
  102. expectOut: "",
  103. expectErr: "namespace is not namespaced",
  104. },
  105. }
  106. for _, test := range testCase {
  107. t.Run(test.name, func(t *testing.T) {
  108. buffer := &bytes.Buffer{}
  109. err := printRowsForHandlerEntry(buffer, test.h, test.obj, test.opt, test.includeHeader)
  110. if err != nil {
  111. if err.Error() != test.expectErr {
  112. t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", test.name, test.expectErr, err)
  113. }
  114. }
  115. if test.expectOut != buffer.String() {
  116. t.Errorf("[%s]expect:\n %v\n but got:\n %v\n", test.name, test.expectOut, buffer.String())
  117. }
  118. })
  119. }
  120. }