conversion_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 v1beta1_test
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/apiserver/pkg/authentication/user"
  18. "k8s.io/kubernetes/pkg/apis/abac"
  19. "k8s.io/kubernetes/pkg/apis/abac/v1beta1"
  20. )
  21. func TestV1Beta1Conversion(t *testing.T) {
  22. testcases := map[string]struct {
  23. old *v1beta1.Policy
  24. expected *abac.Policy
  25. }{
  26. // specifying a user is preserved
  27. "user": {
  28. old: &v1beta1.Policy{Spec: v1beta1.PolicySpec{User: "bob"}},
  29. expected: &abac.Policy{Spec: abac.PolicySpec{User: "bob"}},
  30. },
  31. // specifying a group is preserved
  32. "group": {
  33. old: &v1beta1.Policy{Spec: v1beta1.PolicySpec{Group: "mygroup"}},
  34. expected: &abac.Policy{Spec: abac.PolicySpec{Group: "mygroup"}},
  35. },
  36. // specifying * for user or group maps to all authenticated subjects
  37. "* user": {
  38. old: &v1beta1.Policy{Spec: v1beta1.PolicySpec{User: "*"}},
  39. expected: &abac.Policy{Spec: abac.PolicySpec{Group: user.AllAuthenticated}},
  40. },
  41. "* group": {
  42. old: &v1beta1.Policy{Spec: v1beta1.PolicySpec{Group: "*"}},
  43. expected: &abac.Policy{Spec: abac.PolicySpec{Group: user.AllAuthenticated}},
  44. },
  45. }
  46. for k, tc := range testcases {
  47. internal := &abac.Policy{}
  48. if err := abac.Scheme.Convert(tc.old, internal, nil); err != nil {
  49. t.Errorf("%s: unexpected error: %v", k, err)
  50. }
  51. if !reflect.DeepEqual(internal, tc.expected) {
  52. t.Errorf("%s: expected\n\t%#v, got \n\t%#v", k, tc.expected, internal)
  53. }
  54. }
  55. }