model_printer_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. Copyright 2017 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 explain
  14. import (
  15. "bytes"
  16. "testing"
  17. "k8s.io/apimachinery/pkg/runtime/schema"
  18. )
  19. func TestModel(t *testing.T) {
  20. gvk := schema.GroupVersionKind{
  21. Group: "",
  22. Version: "v1",
  23. Kind: "OneKind",
  24. }
  25. schema := resources.LookupResource(gvk)
  26. if schema == nil {
  27. t.Fatal("Couldn't find schema v1.OneKind")
  28. }
  29. tests := []struct {
  30. path []string
  31. want string
  32. }{
  33. {
  34. want: `KIND: OneKind
  35. VERSION: v1
  36. DESCRIPTION:
  37. OneKind has a short description
  38. FIELDS:
  39. field1 <Object> -required-
  40. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla ut lacus ac
  41. enim vulputate imperdiet ac accumsan risus. Integer vel accumsan lectus.
  42. Praesent tempus nulla id tortor luctus, quis varius nulla laoreet. Ut orci
  43. nisi, suscipit id velit sed, blandit eleifend turpis. Curabitur tempus ante
  44. at lectus viverra, a mattis augue euismod. Morbi quam ligula, porttitor sit
  45. amet lacus non, interdum pulvinar tortor. Praesent accumsan risus et ipsum
  46. dictum, vel ullamcorper lorem egestas.
  47. field2 <[]map[string]string>
  48. This is an array of object of PrimitiveDef
  49. `,
  50. path: []string{},
  51. },
  52. {
  53. want: `KIND: OneKind
  54. VERSION: v1
  55. RESOURCE: field1 <Object>
  56. DESCRIPTION:
  57. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla ut lacus ac
  58. enim vulputate imperdiet ac accumsan risus. Integer vel accumsan lectus.
  59. Praesent tempus nulla id tortor luctus, quis varius nulla laoreet. Ut orci
  60. nisi, suscipit id velit sed, blandit eleifend turpis. Curabitur tempus ante
  61. at lectus viverra, a mattis augue euismod. Morbi quam ligula, porttitor sit
  62. amet lacus non, interdum pulvinar tortor. Praesent accumsan risus et ipsum
  63. dictum, vel ullamcorper lorem egestas.
  64. This is another kind of Kind
  65. FIELDS:
  66. array <[]integer>
  67. This array must be an array of int
  68. int <integer>
  69. This int must be an int
  70. object <map[string]string>
  71. This is an object of string
  72. primitive <string>
  73. string <string> -required-
  74. This string must be a string
  75. `,
  76. path: []string{"field1"},
  77. },
  78. {
  79. want: `KIND: OneKind
  80. VERSION: v1
  81. FIELD: string <string>
  82. DESCRIPTION:
  83. This string must be a string
  84. `,
  85. path: []string{"field1", "string"},
  86. },
  87. {
  88. want: `KIND: OneKind
  89. VERSION: v1
  90. FIELD: array <[]integer>
  91. DESCRIPTION:
  92. This array must be an array of int
  93. This is an int in an array
  94. `,
  95. path: []string{"field1", "array"},
  96. },
  97. }
  98. for _, test := range tests {
  99. buf := bytes.Buffer{}
  100. if err := PrintModelDescription(test.path, &buf, schema, gvk, false); err != nil {
  101. t.Fatalf("Failed to PrintModelDescription for path %v: %v", test.path, err)
  102. }
  103. got := buf.String()
  104. if got != test.want {
  105. t.Errorf("Got:\n%v\nWant:\n%v\n", buf.String(), test.want)
  106. }
  107. }
  108. }
  109. func TestCRDModel(t *testing.T) {
  110. gvk := schema.GroupVersionKind{
  111. Group: "",
  112. Version: "v1",
  113. Kind: "CrdKind",
  114. }
  115. schema := resources.LookupResource(gvk)
  116. if schema == nil {
  117. t.Fatal("Couldn't find schema v1.CrdKind")
  118. }
  119. tests := []struct {
  120. path []string
  121. want string
  122. }{
  123. {
  124. path: []string{},
  125. want: `KIND: CrdKind
  126. VERSION: v1
  127. DESCRIPTION:
  128. <empty>
  129. `,
  130. },
  131. }
  132. for _, test := range tests {
  133. buf := bytes.Buffer{}
  134. if err := PrintModelDescription(test.path, &buf, schema, gvk, false); err != nil {
  135. t.Fatalf("Failed to PrintModelDescription for path %v: %v", test.path, err)
  136. }
  137. got := buf.String()
  138. if got != test.want {
  139. t.Errorf("Got:\n%v\nWant:\n%v\n", buf.String(), test.want)
  140. }
  141. }
  142. }