field_lookup_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. "testing"
  16. "k8s.io/apimachinery/pkg/runtime/schema"
  17. )
  18. func TestFindField(t *testing.T) {
  19. schema := resources.LookupResource(schema.GroupVersionKind{
  20. Group: "",
  21. Version: "v1",
  22. Kind: "OneKind",
  23. })
  24. if schema == nil {
  25. t.Fatal("Counldn't find schema v1.OneKind")
  26. }
  27. tests := []struct {
  28. name string
  29. path []string
  30. err string
  31. expectedPath string
  32. }{
  33. {
  34. name: "test1",
  35. path: []string{},
  36. expectedPath: "OneKind",
  37. },
  38. {
  39. name: "test2",
  40. path: []string{"field1"},
  41. expectedPath: "OneKind.field1",
  42. },
  43. {
  44. name: "test3",
  45. path: []string{"field1", "array"},
  46. expectedPath: "OtherKind.array",
  47. },
  48. {
  49. name: "test4",
  50. path: []string{"field1", "what?"},
  51. err: `field "what?" does not exist`,
  52. },
  53. {
  54. name: "test5",
  55. path: []string{"field1", ""},
  56. err: `field "" does not exist`,
  57. },
  58. }
  59. for _, tt := range tests {
  60. t.Run(tt.name, func(t *testing.T) {
  61. path, err := LookupSchemaForField(schema, tt.path)
  62. gotErr := ""
  63. if err != nil {
  64. gotErr = err.Error()
  65. }
  66. gotPath := ""
  67. if path != nil {
  68. gotPath = path.GetPath().String()
  69. }
  70. if gotErr != tt.err || gotPath != tt.expectedPath {
  71. t.Errorf("LookupSchemaForField(schema, %v) = (path: %q, err: %q), expected (path: %q, err: %q)",
  72. tt.path, gotPath, gotErr, tt.expectedPath, tt.err)
  73. }
  74. })
  75. }
  76. }
  77. func TestCrdFindField(t *testing.T) {
  78. schema := resources.LookupResource(schema.GroupVersionKind{
  79. Group: "",
  80. Version: "v1",
  81. Kind: "CrdKind",
  82. })
  83. if schema == nil {
  84. t.Fatal("Counldn't find schema v1.CrdKind")
  85. }
  86. tests := []struct {
  87. name string
  88. path []string
  89. err string
  90. expectedPath string
  91. }{
  92. {
  93. name: "test1",
  94. path: []string{},
  95. expectedPath: "CrdKind",
  96. },
  97. }
  98. for _, tt := range tests {
  99. t.Run(tt.name, func(t *testing.T) {
  100. path, err := LookupSchemaForField(schema, tt.path)
  101. gotErr := ""
  102. if err != nil {
  103. gotErr = err.Error()
  104. }
  105. gotPath := ""
  106. if path != nil {
  107. gotPath = path.GetPath().String()
  108. }
  109. if gotErr != tt.err || gotPath != tt.expectedPath {
  110. t.Errorf("LookupSchemaForField(schema, %v) = (path: %q, err: %q), expected (path: %q, err: %q)",
  111. tt.path, gotPath, gotErr, tt.expectedPath, tt.err)
  112. }
  113. })
  114. }
  115. }