describe_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. "fmt"
  17. "net/http"
  18. "testing"
  19. "k8s.io/kubernetes/pkg/client/unversioned/fake"
  20. )
  21. // Verifies that schemas that are not in the master tree of Kubernetes can be retrieved via Get.
  22. func TestDescribeUnknownSchemaObject(t *testing.T) {
  23. d := &testDescriber{Output: "test output"}
  24. f, tf, codec, ns := NewTestFactory()
  25. tf.Describer = d
  26. tf.Client = &fake.RESTClient{
  27. NegotiatedSerializer: ns,
  28. Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &internalType{Name: "foo"})},
  29. }
  30. tf.Namespace = "non-default"
  31. buf := bytes.NewBuffer([]byte{})
  32. buferr := bytes.NewBuffer([]byte{})
  33. cmd := NewCmdDescribe(f, buf, buferr)
  34. cmd.Run(cmd, []string{"type", "foo"})
  35. if d.Name != "foo" || d.Namespace != "non-default" {
  36. t.Errorf("unexpected describer: %#v", d)
  37. }
  38. if buf.String() != fmt.Sprintf("%s", d.Output) {
  39. t.Errorf("unexpected output: %s", buf.String())
  40. }
  41. }
  42. func TestDescribeObject(t *testing.T) {
  43. _, _, rc := testData()
  44. f, tf, codec, ns := NewAPIFactory()
  45. d := &testDescriber{Output: "test output"}
  46. tf.Describer = d
  47. tf.Client = &fake.RESTClient{
  48. NegotiatedSerializer: ns,
  49. Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
  50. switch p, m := req.URL.Path, req.Method; {
  51. case p == "/namespaces/test/replicationcontrollers/redis-master" && m == "GET":
  52. return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &rc.Items[0])}, nil
  53. default:
  54. t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
  55. return nil, nil
  56. }
  57. }),
  58. }
  59. tf.Namespace = "test"
  60. buf := bytes.NewBuffer([]byte{})
  61. buferr := bytes.NewBuffer([]byte{})
  62. cmd := NewCmdDescribe(f, buf, buferr)
  63. cmd.Flags().Set("filename", "../../../examples/guestbook/legacy/redis-master-controller.yaml")
  64. cmd.Run(cmd, []string{})
  65. if d.Name != "redis-master" || d.Namespace != "test" {
  66. t.Errorf("unexpected describer: %#v", d)
  67. }
  68. if buf.String() != fmt.Sprintf("%s", d.Output) {
  69. t.Errorf("unexpected output: %s", buf.String())
  70. }
  71. }
  72. func TestDescribeListObjects(t *testing.T) {
  73. pods, _, _ := testData()
  74. f, tf, codec, ns := NewAPIFactory()
  75. d := &testDescriber{Output: "test output"}
  76. tf.Describer = d
  77. tf.Client = &fake.RESTClient{
  78. NegotiatedSerializer: ns,
  79. Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, pods)},
  80. }
  81. tf.Namespace = "test"
  82. buf := bytes.NewBuffer([]byte{})
  83. buferr := bytes.NewBuffer([]byte{})
  84. cmd := NewCmdDescribe(f, buf, buferr)
  85. cmd.Run(cmd, []string{"pods"})
  86. if buf.String() != fmt.Sprintf("%s\n\n%s", d.Output, d.Output) {
  87. t.Errorf("unexpected output: %s", buf.String())
  88. }
  89. }
  90. func TestDescribeObjectShowEvents(t *testing.T) {
  91. pods, _, _ := testData()
  92. f, tf, codec, ns := NewAPIFactory()
  93. d := &testDescriber{Output: "test output"}
  94. tf.Describer = d
  95. tf.Client = &fake.RESTClient{
  96. NegotiatedSerializer: ns,
  97. Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, pods)},
  98. }
  99. tf.Namespace = "test"
  100. buf := bytes.NewBuffer([]byte{})
  101. buferr := bytes.NewBuffer([]byte{})
  102. cmd := NewCmdDescribe(f, buf, buferr)
  103. cmd.Flags().Set("show-events", "true")
  104. cmd.Run(cmd, []string{"pods"})
  105. if d.Settings.ShowEvents != true {
  106. t.Errorf("ShowEvents = true expected, got ShowEvents = %v", d.Settings.ShowEvents)
  107. }
  108. }
  109. func TestDescribeObjectSkipEvents(t *testing.T) {
  110. pods, _, _ := testData()
  111. f, tf, codec, ns := NewAPIFactory()
  112. d := &testDescriber{Output: "test output"}
  113. tf.Describer = d
  114. tf.Client = &fake.RESTClient{
  115. NegotiatedSerializer: ns,
  116. Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, pods)},
  117. }
  118. tf.Namespace = "test"
  119. buf := bytes.NewBuffer([]byte{})
  120. buferr := bytes.NewBuffer([]byte{})
  121. cmd := NewCmdDescribe(f, buf, buferr)
  122. cmd.Flags().Set("show-events", "false")
  123. cmd.Run(cmd, []string{"pods"})
  124. if d.Settings.ShowEvents != false {
  125. t.Errorf("ShowEvents = false expected, got ShowEvents = %v", d.Settings.ShowEvents)
  126. }
  127. }