create_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 cmd
  14. import (
  15. "bytes"
  16. "net/http"
  17. "testing"
  18. "k8s.io/kubernetes/pkg/client/typed/dynamic"
  19. "k8s.io/kubernetes/pkg/client/unversioned/fake"
  20. )
  21. func TestExtraArgsFail(t *testing.T) {
  22. initTestErrorHandler(t)
  23. buf := bytes.NewBuffer([]byte{})
  24. f, _, _, _ := NewAPIFactory()
  25. c := NewCmdCreate(f, buf)
  26. if ValidateArgs(c, []string{"rc"}) == nil {
  27. t.Errorf("unexpected non-error")
  28. }
  29. }
  30. func TestCreateObject(t *testing.T) {
  31. initTestErrorHandler(t)
  32. _, _, rc := testData()
  33. rc.Items[0].Name = "redis-master-controller"
  34. f, tf, codec, _ := NewAPIFactory()
  35. ns := dynamic.ContentConfig().NegotiatedSerializer
  36. tf.Printer = &testPrinter{}
  37. tf.Client = &fake.RESTClient{
  38. NegotiatedSerializer: ns,
  39. Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
  40. switch p, m := req.URL.Path, req.Method; {
  41. case p == "/namespaces/test/replicationcontrollers" && m == http.MethodPost:
  42. return &http.Response{StatusCode: http.StatusCreated, Header: defaultHeader(), Body: objBody(codec, &rc.Items[0])}, nil
  43. default:
  44. t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
  45. return nil, nil
  46. }
  47. }),
  48. }
  49. tf.Namespace = "test"
  50. buf := bytes.NewBuffer([]byte{})
  51. cmd := NewCmdCreate(f, buf)
  52. cmd.Flags().Set("filename", "../../../examples/guestbook/legacy/redis-master-controller.yaml")
  53. cmd.Flags().Set("output", "name")
  54. cmd.Run(cmd, []string{})
  55. // uses the name from the file, not the response
  56. if buf.String() != "replicationcontroller/redis-master-controller\n" {
  57. t.Errorf("unexpected output: %s", buf.String())
  58. }
  59. }
  60. func TestCreateMultipleObject(t *testing.T) {
  61. initTestErrorHandler(t)
  62. _, svc, rc := testData()
  63. f, tf, codec, _ := NewAPIFactory()
  64. ns := dynamic.ContentConfig().NegotiatedSerializer
  65. tf.Printer = &testPrinter{}
  66. tf.Client = &fake.RESTClient{
  67. NegotiatedSerializer: ns,
  68. Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
  69. switch p, m := req.URL.Path, req.Method; {
  70. case p == "/namespaces/test/services" && m == http.MethodPost:
  71. return &http.Response{StatusCode: http.StatusCreated, Header: defaultHeader(), Body: objBody(codec, &svc.Items[0])}, nil
  72. case p == "/namespaces/test/replicationcontrollers" && m == http.MethodPost:
  73. return &http.Response{StatusCode: http.StatusCreated, Header: defaultHeader(), Body: objBody(codec, &rc.Items[0])}, nil
  74. default:
  75. t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
  76. return nil, nil
  77. }
  78. }),
  79. }
  80. tf.Namespace = "test"
  81. buf := bytes.NewBuffer([]byte{})
  82. cmd := NewCmdCreate(f, buf)
  83. cmd.Flags().Set("filename", "../../../examples/guestbook/legacy/redis-master-controller.yaml")
  84. cmd.Flags().Set("filename", "../../../examples/guestbook/frontend-service.yaml")
  85. cmd.Flags().Set("output", "name")
  86. cmd.Run(cmd, []string{})
  87. // Names should come from the REST response, NOT the files
  88. if buf.String() != "replicationcontroller/rc1\nservice/baz\n" {
  89. t.Errorf("unexpected output: %s", buf.String())
  90. }
  91. }
  92. func TestCreateDirectory(t *testing.T) {
  93. initTestErrorHandler(t)
  94. _, _, rc := testData()
  95. rc.Items[0].Name = "name"
  96. f, tf, codec, _ := NewAPIFactory()
  97. ns := dynamic.ContentConfig().NegotiatedSerializer
  98. tf.Printer = &testPrinter{}
  99. tf.Client = &fake.RESTClient{
  100. NegotiatedSerializer: ns,
  101. Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
  102. switch p, m := req.URL.Path, req.Method; {
  103. case p == "/namespaces/test/replicationcontrollers" && m == http.MethodPost:
  104. return &http.Response{StatusCode: http.StatusCreated, Header: defaultHeader(), Body: objBody(codec, &rc.Items[0])}, nil
  105. default:
  106. t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
  107. return nil, nil
  108. }
  109. }),
  110. }
  111. tf.Namespace = "test"
  112. buf := bytes.NewBuffer([]byte{})
  113. cmd := NewCmdCreate(f, buf)
  114. cmd.Flags().Set("filename", "../../../examples/guestbook/legacy")
  115. cmd.Flags().Set("output", "name")
  116. cmd.Run(cmd, []string{})
  117. if buf.String() != "replicationcontroller/name\nreplicationcontroller/name\nreplicationcontroller/name\n" {
  118. t.Errorf("unexpected output: %s", buf.String())
  119. }
  120. }