create_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. Copyright 2014 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. "net/http"
  16. "testing"
  17. "k8s.io/apimachinery/pkg/runtime/schema"
  18. "k8s.io/cli-runtime/pkg/genericclioptions"
  19. "k8s.io/cli-runtime/pkg/resource"
  20. "k8s.io/client-go/rest/fake"
  21. cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
  22. "k8s.io/kubernetes/pkg/kubectl/scheme"
  23. )
  24. func TestExtraArgsFail(t *testing.T) {
  25. cmdtesting.InitTestErrorHandler(t)
  26. f := cmdtesting.NewTestFactory()
  27. defer f.Cleanup()
  28. c := NewCmdCreate(f, genericclioptions.NewTestIOStreamsDiscard())
  29. options := CreateOptions{}
  30. if options.ValidateArgs(c, []string{"rc"}) == nil {
  31. t.Errorf("unexpected non-error")
  32. }
  33. }
  34. func TestCreateObject(t *testing.T) {
  35. cmdtesting.InitTestErrorHandler(t)
  36. _, _, rc := cmdtesting.TestData()
  37. rc.Items[0].Name = "redis-master-controller"
  38. tf := cmdtesting.NewTestFactory().WithNamespace("test")
  39. defer tf.Cleanup()
  40. codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
  41. tf.UnstructuredClient = &fake.RESTClient{
  42. GroupVersion: schema.GroupVersion{Version: "v1"},
  43. NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
  44. Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
  45. switch p, m := req.URL.Path, req.Method; {
  46. case p == "/namespaces/test/replicationcontrollers" && m == http.MethodPost:
  47. return &http.Response{StatusCode: http.StatusCreated, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &rc.Items[0])}, nil
  48. default:
  49. t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
  50. return nil, nil
  51. }
  52. }),
  53. }
  54. ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
  55. cmd := NewCmdCreate(tf, ioStreams)
  56. cmd.Flags().Set("filename", "../../../../test/e2e/testing-manifests/guestbook/legacy/redis-master-controller.yaml")
  57. cmd.Flags().Set("output", "name")
  58. cmd.Run(cmd, []string{})
  59. // uses the name from the file, not the response
  60. if buf.String() != "replicationcontroller/redis-master-controller\n" {
  61. t.Errorf("unexpected output: %s", buf.String())
  62. }
  63. }
  64. func TestCreateMultipleObject(t *testing.T) {
  65. cmdtesting.InitTestErrorHandler(t)
  66. _, svc, rc := cmdtesting.TestData()
  67. tf := cmdtesting.NewTestFactory().WithNamespace("test")
  68. defer tf.Cleanup()
  69. codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
  70. tf.UnstructuredClient = &fake.RESTClient{
  71. GroupVersion: schema.GroupVersion{Version: "v1"},
  72. NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
  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/services" && m == http.MethodPost:
  76. return &http.Response{StatusCode: http.StatusCreated, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &svc.Items[0])}, nil
  77. case p == "/namespaces/test/replicationcontrollers" && m == http.MethodPost:
  78. return &http.Response{StatusCode: http.StatusCreated, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &rc.Items[0])}, nil
  79. default:
  80. t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
  81. return nil, nil
  82. }
  83. }),
  84. }
  85. ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
  86. cmd := NewCmdCreate(tf, ioStreams)
  87. cmd.Flags().Set("filename", "../../../../test/e2e/testing-manifests/guestbook/legacy/redis-master-controller.yaml")
  88. cmd.Flags().Set("filename", "../../../../test/e2e/testing-manifests/guestbook/frontend-service.yaml")
  89. cmd.Flags().Set("output", "name")
  90. cmd.Run(cmd, []string{})
  91. // Names should come from the REST response, NOT the files
  92. if buf.String() != "replicationcontroller/rc1\nservice/baz\n" {
  93. t.Errorf("unexpected output: %s", buf.String())
  94. }
  95. }
  96. func TestCreateDirectory(t *testing.T) {
  97. cmdtesting.InitTestErrorHandler(t)
  98. _, _, rc := cmdtesting.TestData()
  99. rc.Items[0].Name = "name"
  100. tf := cmdtesting.NewTestFactory().WithNamespace("test")
  101. defer tf.Cleanup()
  102. codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
  103. tf.UnstructuredClient = &fake.RESTClient{
  104. GroupVersion: schema.GroupVersion{Version: "v1"},
  105. NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
  106. Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
  107. switch p, m := req.URL.Path, req.Method; {
  108. case p == "/namespaces/test/replicationcontrollers" && m == http.MethodPost:
  109. return &http.Response{StatusCode: http.StatusCreated, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &rc.Items[0])}, nil
  110. default:
  111. t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
  112. return nil, nil
  113. }
  114. }),
  115. }
  116. ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
  117. cmd := NewCmdCreate(tf, ioStreams)
  118. cmd.Flags().Set("filename", "../../../../test/e2e/testing-manifests/guestbook/legacy")
  119. cmd.Flags().Set("output", "name")
  120. cmd.Run(cmd, []string{})
  121. if buf.String() != "replicationcontroller/name\nreplicationcontroller/name\nreplicationcontroller/name\n" {
  122. t.Errorf("unexpected output: %s", buf.String())
  123. }
  124. }