patch_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. Copyright 2015 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/unversioned/fake"
  19. )
  20. func TestPatchObject(t *testing.T) {
  21. _, svc, _ := testData()
  22. f, tf, codec, ns := NewAPIFactory()
  23. tf.Printer = &testPrinter{}
  24. tf.Client = &fake.RESTClient{
  25. NegotiatedSerializer: ns,
  26. Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
  27. switch p, m := req.URL.Path, req.Method; {
  28. case p == "/namespaces/test/services/frontend" && (m == "PATCH" || m == "GET"):
  29. return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &svc.Items[0])}, nil
  30. default:
  31. t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
  32. return nil, nil
  33. }
  34. }),
  35. }
  36. tf.Namespace = "test"
  37. buf := bytes.NewBuffer([]byte{})
  38. cmd := NewCmdPatch(f, buf)
  39. cmd.Flags().Set("namespace", "test")
  40. cmd.Flags().Set("patch", `{"spec":{"type":"NodePort"}}`)
  41. cmd.Flags().Set("output", "name")
  42. cmd.Run(cmd, []string{"services/frontend"})
  43. // uses the name from the file, not the response
  44. if buf.String() != "frontend\n" {
  45. t.Errorf("unexpected output: %s", buf.String())
  46. }
  47. }
  48. func TestPatchObjectFromFile(t *testing.T) {
  49. _, svc, _ := testData()
  50. f, tf, codec, ns := NewAPIFactory()
  51. tf.Printer = &testPrinter{}
  52. tf.Client = &fake.RESTClient{
  53. NegotiatedSerializer: ns,
  54. Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
  55. switch p, m := req.URL.Path, req.Method; {
  56. case p == "/namespaces/test/services/frontend" && (m == "PATCH" || m == "GET"):
  57. return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &svc.Items[0])}, nil
  58. default:
  59. t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
  60. return nil, nil
  61. }
  62. }),
  63. }
  64. tf.Namespace = "test"
  65. buf := bytes.NewBuffer([]byte{})
  66. cmd := NewCmdPatch(f, buf)
  67. cmd.Flags().Set("namespace", "test")
  68. cmd.Flags().Set("patch", `{"spec":{"type":"NodePort"}}`)
  69. cmd.Flags().Set("output", "name")
  70. cmd.Flags().Set("filename", "../../../examples/guestbook/frontend-service.yaml")
  71. cmd.Run(cmd, []string{})
  72. // uses the name from the file, not the response
  73. if buf.String() != "frontend\n" {
  74. t.Errorf("unexpected output: %s", buf.String())
  75. }
  76. }