logs_test.go 3.9 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. "io/ioutil"
  17. "net/http"
  18. "os"
  19. "strings"
  20. "testing"
  21. "github.com/spf13/cobra"
  22. "k8s.io/kubernetes/pkg/api"
  23. "k8s.io/kubernetes/pkg/api/unversioned"
  24. "k8s.io/kubernetes/pkg/client/restclient"
  25. "k8s.io/kubernetes/pkg/client/unversioned/fake"
  26. )
  27. func TestLog(t *testing.T) {
  28. tests := []struct {
  29. name, version, podPath, logPath, container string
  30. pod *api.Pod
  31. }{
  32. {
  33. name: "v1 - pod log",
  34. version: "v1",
  35. podPath: "/namespaces/test/pods/foo",
  36. logPath: "/api/v1/namespaces/test/pods/foo/log",
  37. pod: testPod(),
  38. },
  39. }
  40. for _, test := range tests {
  41. logContent := "test log content"
  42. f, tf, codec, ns := NewAPIFactory()
  43. tf.Client = &fake.RESTClient{
  44. NegotiatedSerializer: ns,
  45. Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
  46. switch p, m := req.URL.Path, req.Method; {
  47. case p == test.podPath && m == "GET":
  48. body := objBody(codec, test.pod)
  49. return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: body}, nil
  50. case p == test.logPath && m == "GET":
  51. body := ioutil.NopCloser(bytes.NewBufferString(logContent))
  52. return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: body}, nil
  53. default:
  54. // Ensures no GET is performed when deleting by name
  55. t.Errorf("%s: unexpected request: %#v\n%#v", test.name, req.URL, req)
  56. return nil, nil
  57. }
  58. }),
  59. }
  60. tf.Namespace = "test"
  61. tf.ClientConfig = &restclient.Config{ContentConfig: restclient.ContentConfig{GroupVersion: &unversioned.GroupVersion{Version: test.version}}}
  62. buf := bytes.NewBuffer([]byte{})
  63. cmd := NewCmdLogs(f, buf)
  64. cmd.Flags().Set("namespace", "test")
  65. cmd.Run(cmd, []string{"foo"})
  66. if buf.String() != logContent {
  67. t.Errorf("%s: did not get expected log content. Got: %s", test.name, buf.String())
  68. }
  69. }
  70. }
  71. func testPod() *api.Pod {
  72. return &api.Pod{
  73. ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "test", ResourceVersion: "10"},
  74. Spec: api.PodSpec{
  75. RestartPolicy: api.RestartPolicyAlways,
  76. DNSPolicy: api.DNSClusterFirst,
  77. Containers: []api.Container{
  78. {
  79. Name: "bar",
  80. },
  81. },
  82. },
  83. }
  84. }
  85. func TestValidateLogFlags(t *testing.T) {
  86. f, _, _, _ := NewAPIFactory()
  87. tests := []struct {
  88. name string
  89. flags map[string]string
  90. expected string
  91. }{
  92. {
  93. name: "since & since-time",
  94. flags: map[string]string{"since": "1h", "since-time": "2006-01-02T15:04:05Z"},
  95. expected: "at most one of `sinceTime` or `sinceSeconds` may be specified",
  96. },
  97. {
  98. name: "negative limit-bytes",
  99. flags: map[string]string{"limit-bytes": "-100"},
  100. expected: "must be greater than 0",
  101. },
  102. {
  103. name: "negative tail",
  104. flags: map[string]string{"tail": "-100"},
  105. expected: "must be greater than or equal to 0",
  106. },
  107. }
  108. for _, test := range tests {
  109. cmd := NewCmdLogs(f, bytes.NewBuffer([]byte{}))
  110. out := ""
  111. for flag, value := range test.flags {
  112. cmd.Flags().Set(flag, value)
  113. }
  114. // checkErr breaks tests in case of errors, plus we just
  115. // need to check errors returned by the command validation
  116. o := &LogsOptions{}
  117. cmd.Run = func(cmd *cobra.Command, args []string) {
  118. o.Complete(f, os.Stdout, cmd, args)
  119. out = o.Validate().Error()
  120. }
  121. cmd.Run(cmd, []string{"foo"})
  122. if !strings.Contains(out, test.expected) {
  123. t.Errorf("%s: expected to find:\n\t%s\nfound:\n\t%s\n", test.name, test.expected, out)
  124. }
  125. }
  126. }