conversion_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. Copyright 2015 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 v0_test
  14. import (
  15. "reflect"
  16. "testing"
  17. api "k8s.io/kubernetes/pkg/apis/abac"
  18. "k8s.io/kubernetes/pkg/apis/abac/v0"
  19. )
  20. func TestConversion(t *testing.T) {
  21. testcases := map[string]struct {
  22. old *v0.Policy
  23. expected *api.Policy
  24. }{
  25. // a completely empty policy rule allows everything to all users
  26. "empty": {
  27. old: &v0.Policy{},
  28. expected: &api.Policy{Spec: api.PolicySpec{User: "*", Readonly: false, NonResourcePath: "*", Namespace: "*", Resource: "*", APIGroup: "*"}},
  29. },
  30. // specifying a user is preserved
  31. "user": {
  32. old: &v0.Policy{User: "bob"},
  33. expected: &api.Policy{Spec: api.PolicySpec{User: "bob", Readonly: false, NonResourcePath: "*", Namespace: "*", Resource: "*", APIGroup: "*"}},
  34. },
  35. // specifying a group is preserved (and no longer matches all users)
  36. "group": {
  37. old: &v0.Policy{Group: "mygroup"},
  38. expected: &api.Policy{Spec: api.PolicySpec{Group: "mygroup", Readonly: false, NonResourcePath: "*", Namespace: "*", Resource: "*", APIGroup: "*"}},
  39. },
  40. // specifying a namespace removes the * match on non-resource path
  41. "namespace": {
  42. old: &v0.Policy{Namespace: "myns"},
  43. expected: &api.Policy{Spec: api.PolicySpec{User: "*", Readonly: false, NonResourcePath: "", Namespace: "myns", Resource: "*", APIGroup: "*"}},
  44. },
  45. // specifying a resource removes the * match on non-resource path
  46. "resource": {
  47. old: &v0.Policy{Resource: "myresource"},
  48. expected: &api.Policy{Spec: api.PolicySpec{User: "*", Readonly: false, NonResourcePath: "", Namespace: "*", Resource: "myresource", APIGroup: "*"}},
  49. },
  50. // specifying a namespace+resource removes the * match on non-resource path
  51. "namespace+resource": {
  52. old: &v0.Policy{Namespace: "myns", Resource: "myresource"},
  53. expected: &api.Policy{Spec: api.PolicySpec{User: "*", Readonly: false, NonResourcePath: "", Namespace: "myns", Resource: "myresource", APIGroup: "*"}},
  54. },
  55. }
  56. for k, tc := range testcases {
  57. internal := &api.Policy{}
  58. if err := api.Scheme.Convert(tc.old, internal, nil); err != nil {
  59. t.Errorf("%s: unexpected error: %v", k, err)
  60. }
  61. if !reflect.DeepEqual(internal, tc.expected) {
  62. t.Errorf("%s: expected\n\t%#v, got \n\t%#v", k, tc.expected, internal)
  63. }
  64. }
  65. }