create_rolebinding_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 create
  14. import (
  15. "bytes"
  16. "io/ioutil"
  17. "net/http"
  18. "net/url"
  19. "reflect"
  20. "testing"
  21. rbac "k8s.io/api/rbac/v1"
  22. "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/runtime"
  24. "k8s.io/apimachinery/pkg/runtime/schema"
  25. "k8s.io/cli-runtime/pkg/genericclioptions"
  26. restclient "k8s.io/client-go/rest"
  27. "k8s.io/client-go/rest/fake"
  28. cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
  29. "k8s.io/kubernetes/pkg/kubectl/scheme"
  30. )
  31. var groupVersion = schema.GroupVersion{Group: "rbac.authorization.k8s.io", Version: "v1"}
  32. func TestCreateRoleBinding(t *testing.T) {
  33. expectBinding := &rbac.RoleBinding{
  34. TypeMeta: v1.TypeMeta{
  35. APIVersion: "rbac.authorization.k8s.io/v1",
  36. Kind: "RoleBinding",
  37. },
  38. ObjectMeta: v1.ObjectMeta{
  39. Name: "fake-binding",
  40. },
  41. RoleRef: rbac.RoleRef{
  42. APIGroup: rbac.GroupName,
  43. Kind: "Role",
  44. Name: "fake-role",
  45. },
  46. Subjects: []rbac.Subject{
  47. {
  48. Kind: rbac.UserKind,
  49. APIGroup: "rbac.authorization.k8s.io",
  50. Name: "fake-user",
  51. },
  52. {
  53. Kind: rbac.GroupKind,
  54. APIGroup: "rbac.authorization.k8s.io",
  55. Name: "fake-group",
  56. },
  57. {
  58. Kind: rbac.ServiceAccountKind,
  59. Namespace: "fake-namespace",
  60. Name: "fake-account",
  61. },
  62. },
  63. }
  64. tf := cmdtesting.NewTestFactory().WithNamespace("test")
  65. defer tf.Cleanup()
  66. ns := scheme.Codecs
  67. info, _ := runtime.SerializerInfoForMediaType(ns.SupportedMediaTypes(), runtime.ContentTypeJSON)
  68. encoder := ns.EncoderForVersion(info.Serializer, groupVersion)
  69. decoder := ns.DecoderToVersion(info.Serializer, groupVersion)
  70. tf.Client = &RoleBindingRESTClient{
  71. RESTClient: &fake.RESTClient{
  72. NegotiatedSerializer: ns,
  73. Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
  74. switch p, m := req.URL.Path, req.Method; {
  75. case p == "/namespaces/test/rolebindings" && m == "POST":
  76. bodyBits, err := ioutil.ReadAll(req.Body)
  77. if err != nil {
  78. t.Fatalf("TestCreateRoleBinding error: %v", err)
  79. return nil, nil
  80. }
  81. if obj, _, err := decoder.Decode(bodyBits, nil, &rbac.RoleBinding{}); err == nil {
  82. if !reflect.DeepEqual(obj.(*rbac.RoleBinding), expectBinding) {
  83. t.Fatalf("TestCreateRoleBinding: expected:\n%#v\nsaw:\n%#v", expectBinding, obj.(*rbac.RoleBinding))
  84. return nil, nil
  85. }
  86. } else {
  87. t.Fatalf("TestCreateRoleBinding error, could not decode the request body into rbac.RoleBinding object: %v", err)
  88. return nil, nil
  89. }
  90. responseBinding := &rbac.RoleBinding{}
  91. responseBinding.Name = "fake-binding"
  92. return &http.Response{StatusCode: 201, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, responseBinding))))}, nil
  93. default:
  94. t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
  95. return nil, nil
  96. }
  97. }),
  98. },
  99. }
  100. cmd := NewCmdCreateRoleBinding(tf, genericclioptions.NewTestIOStreamsDiscard())
  101. cmd.Flags().Set("role", "fake-role")
  102. cmd.Flags().Set("user", "fake-user")
  103. cmd.Flags().Set("group", "fake-group")
  104. cmd.Flags().Set("serviceaccount", "fake-namespace:fake-account")
  105. cmd.Run(cmd, []string{"fake-binding"})
  106. }
  107. type RoleBindingRESTClient struct {
  108. *fake.RESTClient
  109. }
  110. func (c *RoleBindingRESTClient) Post() *restclient.Request {
  111. config := restclient.ContentConfig{
  112. ContentType: runtime.ContentTypeJSON,
  113. GroupVersion: &groupVersion,
  114. NegotiatedSerializer: c.NegotiatedSerializer,
  115. }
  116. info, _ := runtime.SerializerInfoForMediaType(c.NegotiatedSerializer.SupportedMediaTypes(), runtime.ContentTypeJSON)
  117. serializers := restclient.Serializers{
  118. Encoder: c.NegotiatedSerializer.EncoderForVersion(info.Serializer, groupVersion),
  119. Decoder: c.NegotiatedSerializer.DecoderToVersion(info.Serializer, groupVersion),
  120. }
  121. if info.StreamSerializer != nil {
  122. serializers.StreamingSerializer = info.StreamSerializer.Serializer
  123. serializers.Framer = info.StreamSerializer.Framer
  124. }
  125. return restclient.NewRequest(c, "POST", &url.URL{Host: "localhost"}, c.VersionedAPIPath, config, serializers, nil, nil, 0)
  126. }