scheme_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Copyright 2016 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 meta_test
  14. import (
  15. "fmt"
  16. "reflect"
  17. "strings"
  18. "testing"
  19. "k8s.io/kubernetes/pkg/api/testapi"
  20. "k8s.io/kubernetes/pkg/api/unversioned"
  21. "k8s.io/kubernetes/pkg/util/sets"
  22. )
  23. // These types do not follow the list convention as documented in
  24. // docs/devel/api-convention.md
  25. var listTypeExceptions = sets.NewString("APIGroupList", "APIResourceList")
  26. func validateListType(target reflect.Type) error {
  27. // exceptions
  28. if listTypeExceptions.Has(target.Name()) {
  29. return nil
  30. }
  31. hasListSuffix := strings.HasSuffix(target.Name(), "List")
  32. hasMetadata := false
  33. hasItems := false
  34. for i := 0; i < target.NumField(); i++ {
  35. field := target.Field(i)
  36. tag := field.Tag.Get("json")
  37. switch {
  38. case strings.HasPrefix(tag, "metadata"):
  39. hasMetadata = true
  40. case tag == "items":
  41. hasItems = true
  42. if field.Type.Kind() != reflect.Slice {
  43. return fmt.Errorf("Expected items to be slice, got %s", field.Type.Kind())
  44. }
  45. }
  46. }
  47. if hasListSuffix && !hasMetadata {
  48. return fmt.Errorf("Expected type %s to contain \"metadata\"", target.Name())
  49. }
  50. if hasListSuffix && !hasItems {
  51. return fmt.Errorf("Expected type %s to contain \"items\"", target.Name())
  52. }
  53. // if a type contains field Items with JSON tag "items", its name should end with List.
  54. if !hasListSuffix && hasItems {
  55. return fmt.Errorf("Type %s has Items, its name is expected to end with \"List\"", target.Name())
  56. }
  57. return nil
  58. }
  59. // TestListTypes verifies that no external type violates the api convention of
  60. // list types.
  61. func TestListTypes(t *testing.T) {
  62. for groupKey, group := range testapi.Groups {
  63. for kind, target := range group.ExternalTypes() {
  64. t.Logf("working on %v in %v", kind, groupKey)
  65. err := validateListType(target)
  66. if err != nil {
  67. t.Error(err)
  68. }
  69. }
  70. }
  71. }
  72. type WithoutMetaDataList struct {
  73. unversioned.TypeMeta `json:",inline"`
  74. unversioned.ListMeta
  75. Items []interface{} `json:"items"`
  76. }
  77. type WithoutItemsList struct {
  78. unversioned.TypeMeta `json:",inline"`
  79. unversioned.ListMeta `json:"metadata,omitempty"`
  80. }
  81. type WrongItemsJSONTagList struct {
  82. unversioned.TypeMeta `json:",inline"`
  83. unversioned.ListMeta `json:"metadata,omitempty"`
  84. Items []interface{} `json:"items,omitempty"`
  85. }
  86. // If a type has Items, its name should end with "List"
  87. type ListWithWrongName struct {
  88. unversioned.TypeMeta `json:",inline"`
  89. unversioned.ListMeta `json:"metadata,omitempty"`
  90. Items []interface{} `json:"items"`
  91. }
  92. // TestValidateListType verifies the validateListType function reports error on
  93. // types that violate the api convention.
  94. func TestValidateListType(t *testing.T) {
  95. var testTypes = []interface{}{
  96. WithoutMetaDataList{},
  97. WithoutItemsList{},
  98. WrongItemsJSONTagList{},
  99. ListWithWrongName{},
  100. }
  101. for _, testType := range testTypes {
  102. err := validateListType(reflect.TypeOf(testType))
  103. if err == nil {
  104. t.Errorf("Expected error")
  105. }
  106. }
  107. }