helpers_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 util
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/apimachinery/pkg/util/sets"
  18. "k8s.io/apiserver/pkg/authorization/authorizer"
  19. authorizationapi "k8s.io/kubernetes/pkg/apis/authorization"
  20. )
  21. func TestResourceAttributesFrom(t *testing.T) {
  22. knownResourceAttributesNames := sets.NewString(
  23. // Fields we copy in ResourceAttributesFrom
  24. "Verb",
  25. "Namespace",
  26. "Group",
  27. "Version",
  28. "Resource",
  29. "Subresource",
  30. "Name",
  31. // Fields we copy in NonResourceAttributesFrom
  32. "Path",
  33. "Verb",
  34. )
  35. reflect.TypeOf(authorizationapi.ResourceAttributes{}).FieldByNameFunc(func(name string) bool {
  36. if !knownResourceAttributesNames.Has(name) {
  37. t.Errorf("authorizationapi.ResourceAttributes has a new field: %q. Add to ResourceAttributesFrom/NonResourceAttributesFrom as appropriate, then add to knownResourceAttributesNames", name)
  38. }
  39. return false
  40. })
  41. knownAttributesRecordFieldNames := sets.NewString(
  42. // Fields we set in ResourceAttributesFrom
  43. "User",
  44. "Verb",
  45. "Namespace",
  46. "APIGroup",
  47. "APIVersion",
  48. "Resource",
  49. "Subresource",
  50. "Name",
  51. "ResourceRequest",
  52. // Fields we set in NonResourceAttributesFrom
  53. "User",
  54. "ResourceRequest",
  55. "Path",
  56. "Verb",
  57. )
  58. reflect.TypeOf(authorizer.AttributesRecord{}).FieldByNameFunc(func(name string) bool {
  59. if !knownAttributesRecordFieldNames.Has(name) {
  60. t.Errorf("authorizer.AttributesRecord has a new field: %q. Add to ResourceAttributesFrom/NonResourceAttributesFrom as appropriate, then add to knownAttributesRecordFieldNames", name)
  61. }
  62. return false
  63. })
  64. }