create_clusterrolebinding_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/v1beta1"
  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. func TestCreateClusterRoleBinding(t *testing.T) {
  32. expectBinding := &rbac.ClusterRoleBinding{
  33. ObjectMeta: v1.ObjectMeta{
  34. Name: "fake-binding",
  35. },
  36. TypeMeta: v1.TypeMeta{
  37. Kind: "ClusterRoleBinding",
  38. APIVersion: "rbac.authorization.k8s.io/v1beta1",
  39. },
  40. RoleRef: rbac.RoleRef{
  41. APIGroup: rbac.GroupName,
  42. Kind: "ClusterRole",
  43. Name: "fake-clusterrole",
  44. },
  45. Subjects: []rbac.Subject{
  46. {
  47. Kind: rbac.UserKind,
  48. APIGroup: "rbac.authorization.k8s.io",
  49. Name: "fake-user",
  50. },
  51. {
  52. Kind: rbac.GroupKind,
  53. APIGroup: "rbac.authorization.k8s.io",
  54. Name: "fake-group",
  55. },
  56. {
  57. Kind: rbac.ServiceAccountKind,
  58. Namespace: "fake-namespace",
  59. Name: "fake-account",
  60. },
  61. },
  62. }
  63. tf := cmdtesting.NewTestFactory().WithNamespace("test")
  64. defer tf.Cleanup()
  65. ns := scheme.Codecs
  66. info, _ := runtime.SerializerInfoForMediaType(ns.SupportedMediaTypes(), runtime.ContentTypeJSON)
  67. encoder := ns.EncoderForVersion(info.Serializer, groupVersion)
  68. decoder := ns.DecoderToVersion(info.Serializer, groupVersion)
  69. tf.Client = &ClusterRoleBindingRESTClient{
  70. RESTClient: &fake.RESTClient{
  71. NegotiatedSerializer: ns,
  72. Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
  73. switch p, m := req.URL.Path, req.Method; {
  74. case p == "/clusterrolebindings" && m == "POST":
  75. bodyBits, err := ioutil.ReadAll(req.Body)
  76. if err != nil {
  77. t.Fatalf("TestCreateClusterRoleBinding error: %v", err)
  78. return nil, nil
  79. }
  80. if obj, _, err := decoder.Decode(bodyBits, nil, &rbac.ClusterRoleBinding{}); err == nil {
  81. if !reflect.DeepEqual(obj.(*rbac.ClusterRoleBinding), expectBinding) {
  82. t.Fatalf("TestCreateClusterRoleBinding: expected:\n%#v\nsaw:\n%#v", expectBinding, obj.(*rbac.ClusterRoleBinding))
  83. return nil, nil
  84. }
  85. } else {
  86. t.Fatalf("TestCreateClusterRoleBinding error, could not decode the request body into rbac.ClusterRoleBinding object: %v", err)
  87. return nil, nil
  88. }
  89. responseBinding := &rbac.ClusterRoleBinding{}
  90. responseBinding.Name = "fake-binding"
  91. return &http.Response{StatusCode: 201, Header: cmdtesting.DefaultHeader(), Body: ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, responseBinding))))}, nil
  92. default:
  93. t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
  94. return nil, nil
  95. }
  96. }),
  97. },
  98. }
  99. expectedOutput := "clusterrolebinding.rbac.authorization.k8s.io/" + expectBinding.Name + "\n"
  100. ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
  101. cmd := NewCmdCreateClusterRoleBinding(tf, ioStreams)
  102. cmd.Flags().Set("clusterrole", "fake-clusterrole")
  103. cmd.Flags().Set("user", "fake-user")
  104. cmd.Flags().Set("group", "fake-group")
  105. cmd.Flags().Set("output", "name")
  106. cmd.Flags().Set("serviceaccount", "fake-namespace:fake-account")
  107. cmd.Run(cmd, []string{"fake-binding"})
  108. if buf.String() != expectedOutput {
  109. t.Errorf("TestCreateClusterRoleBinding: expected %v\n but got %v\n", expectedOutput, buf.String())
  110. }
  111. }
  112. type ClusterRoleBindingRESTClient struct {
  113. *fake.RESTClient
  114. }
  115. func (c *ClusterRoleBindingRESTClient) Post() *restclient.Request {
  116. config := restclient.ContentConfig{
  117. ContentType: runtime.ContentTypeJSON,
  118. NegotiatedSerializer: c.NegotiatedSerializer,
  119. }
  120. info, _ := runtime.SerializerInfoForMediaType(c.NegotiatedSerializer.SupportedMediaTypes(), runtime.ContentTypeJSON)
  121. serializers := restclient.Serializers{
  122. Encoder: c.NegotiatedSerializer.EncoderForVersion(info.Serializer, schema.GroupVersion{Group: "rbac.authorization.k8s.io", Version: "v1beta1"}),
  123. Decoder: c.NegotiatedSerializer.DecoderToVersion(info.Serializer, schema.GroupVersion{Group: "rbac.authorization.k8s.io", Version: "v1beta1"}),
  124. }
  125. if info.StreamSerializer != nil {
  126. serializers.StreamingSerializer = info.StreamSerializer.Serializer
  127. serializers.Framer = info.StreamSerializer.Framer
  128. }
  129. return restclient.NewRequest(c, "POST", &url.URL{Host: "localhost"}, c.VersionedAPIPath, config, serializers, nil, nil, 0)
  130. }