2014-11-18 18:04:10 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2014-11-18 18:04:10 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-03-26 01:52:12 +00:00
|
|
|
package cmd
|
2014-11-18 18:04:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2017-10-11 06:26:02 +00:00
|
|
|
"strings"
|
2014-11-18 18:04:10 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-01-24 15:00:24 +00:00
|
|
|
"k8s.io/client-go/rest/fake"
|
2016-10-18 22:53:26 +00:00
|
|
|
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
|
2014-11-18 18:04:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Verifies that schemas that are not in the master tree of Kubernetes can be retrieved via Get.
|
|
|
|
func TestDescribeUnknownSchemaObject(t *testing.T) {
|
|
|
|
d := &testDescriber{Output: "test output"}
|
2017-01-22 22:11:33 +00:00
|
|
|
f, tf, codec, _ := cmdtesting.NewTestFactory()
|
2014-11-18 18:04:10 +00:00
|
|
|
tf.Describer = d
|
2017-01-22 22:11:33 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2016-10-18 22:53:26 +00:00
|
|
|
Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, cmdtesting.NewInternalType("", "", "foo"))},
|
2014-11-18 18:04:10 +00:00
|
|
|
}
|
2015-01-02 18:08:37 +00:00
|
|
|
tf.Namespace = "non-default"
|
2014-11-18 18:04:10 +00:00
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-08-23 03:11:05 +00:00
|
|
|
buferr := bytes.NewBuffer([]byte{})
|
|
|
|
cmd := NewCmdDescribe(f, buf, buferr)
|
2014-11-18 18:04:10 +00:00
|
|
|
cmd.Run(cmd, []string{"type", "foo"})
|
|
|
|
|
2017-01-22 22:11:33 +00:00
|
|
|
if d.Name != "foo" || d.Namespace != "" {
|
|
|
|
t.Errorf("unexpected describer: %#v", d)
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf.String() != fmt.Sprintf("%s", d.Output) {
|
|
|
|
t.Errorf("unexpected output: %s", buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verifies that schemas that are not in the master tree of Kubernetes can be retrieved via Get.
|
|
|
|
func TestDescribeUnknownNamespacedSchemaObject(t *testing.T) {
|
|
|
|
d := &testDescriber{Output: "test output"}
|
|
|
|
f, tf, codec, _ := cmdtesting.NewTestFactory()
|
|
|
|
tf.Describer = d
|
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
|
|
|
Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, cmdtesting.NewInternalNamespacedType("", "", "foo", "non-default"))},
|
|
|
|
}
|
|
|
|
tf.Namespace = "non-default"
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
|
|
|
buferr := bytes.NewBuffer([]byte{})
|
|
|
|
cmd := NewCmdDescribe(f, buf, buferr)
|
|
|
|
cmd.Run(cmd, []string{"namespacedtype", "foo"})
|
|
|
|
|
2015-01-02 18:08:37 +00:00
|
|
|
if d.Name != "foo" || d.Namespace != "non-default" {
|
2014-11-18 18:04:10 +00:00
|
|
|
t.Errorf("unexpected describer: %#v", d)
|
|
|
|
}
|
|
|
|
|
2016-08-13 23:07:28 +00:00
|
|
|
if buf.String() != fmt.Sprintf("%s", d.Output) {
|
2014-11-18 18:04:10 +00:00
|
|
|
t.Errorf("unexpected output: %s", buf.String())
|
|
|
|
}
|
|
|
|
}
|
2015-07-07 07:31:08 +00:00
|
|
|
|
|
|
|
func TestDescribeObject(t *testing.T) {
|
|
|
|
_, _, rc := testData()
|
2017-01-22 22:11:33 +00:00
|
|
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
2015-07-07 07:31:08 +00:00
|
|
|
d := &testDescriber{Output: "test output"}
|
|
|
|
tf.Describer = d
|
2017-01-22 22:11:33 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2015-11-11 19:54:58 +00:00
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
2015-07-07 07:31:08 +00:00
|
|
|
switch p, m := req.URL.Path, req.Method; {
|
|
|
|
case p == "/namespaces/test/replicationcontrollers/redis-master" && m == "GET":
|
2016-05-11 07:50:47 +00:00
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &rc.Items[0])}, nil
|
2015-07-07 07:31:08 +00:00
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
tf.Namespace = "test"
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-08-23 03:11:05 +00:00
|
|
|
buferr := bytes.NewBuffer([]byte{})
|
|
|
|
cmd := NewCmdDescribe(f, buf, buferr)
|
2016-03-23 23:03:07 +00:00
|
|
|
cmd.Flags().Set("filename", "../../../examples/guestbook/legacy/redis-master-controller.yaml")
|
2015-07-07 07:31:08 +00:00
|
|
|
cmd.Run(cmd, []string{})
|
|
|
|
|
|
|
|
if d.Name != "redis-master" || d.Namespace != "test" {
|
|
|
|
t.Errorf("unexpected describer: %#v", d)
|
|
|
|
}
|
|
|
|
|
2016-08-13 23:07:28 +00:00
|
|
|
if buf.String() != fmt.Sprintf("%s", d.Output) {
|
2015-07-07 07:31:08 +00:00
|
|
|
t.Errorf("unexpected output: %s", buf.String())
|
|
|
|
}
|
|
|
|
}
|
2015-08-04 07:12:29 +00:00
|
|
|
|
|
|
|
func TestDescribeListObjects(t *testing.T) {
|
|
|
|
pods, _, _ := testData()
|
2017-01-22 22:11:33 +00:00
|
|
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
2015-08-04 07:12:29 +00:00
|
|
|
d := &testDescriber{Output: "test output"}
|
|
|
|
tf.Describer = d
|
2017-01-22 22:11:33 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2016-05-04 10:24:16 +00:00
|
|
|
Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, pods)},
|
2015-08-04 07:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tf.Namespace = "test"
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-08-23 03:11:05 +00:00
|
|
|
buferr := bytes.NewBuffer([]byte{})
|
|
|
|
cmd := NewCmdDescribe(f, buf, buferr)
|
2015-08-04 07:12:29 +00:00
|
|
|
cmd.Run(cmd, []string{"pods"})
|
2016-08-13 23:07:28 +00:00
|
|
|
if buf.String() != fmt.Sprintf("%s\n\n%s", d.Output, d.Output) {
|
2015-08-04 07:12:29 +00:00
|
|
|
t.Errorf("unexpected output: %s", buf.String())
|
|
|
|
}
|
|
|
|
}
|
2016-04-20 17:27:32 +00:00
|
|
|
|
|
|
|
func TestDescribeObjectShowEvents(t *testing.T) {
|
|
|
|
pods, _, _ := testData()
|
2017-01-22 22:11:33 +00:00
|
|
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
2016-04-20 17:27:32 +00:00
|
|
|
d := &testDescriber{Output: "test output"}
|
|
|
|
tf.Describer = d
|
2017-01-22 22:11:33 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2016-05-04 10:24:16 +00:00
|
|
|
Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, pods)},
|
2016-04-20 17:27:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tf.Namespace = "test"
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-08-23 03:11:05 +00:00
|
|
|
buferr := bytes.NewBuffer([]byte{})
|
|
|
|
cmd := NewCmdDescribe(f, buf, buferr)
|
2016-04-20 17:27:32 +00:00
|
|
|
cmd.Flags().Set("show-events", "true")
|
|
|
|
cmd.Run(cmd, []string{"pods"})
|
|
|
|
if d.Settings.ShowEvents != true {
|
|
|
|
t.Errorf("ShowEvents = true expected, got ShowEvents = %v", d.Settings.ShowEvents)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDescribeObjectSkipEvents(t *testing.T) {
|
|
|
|
pods, _, _ := testData()
|
2017-01-22 22:11:33 +00:00
|
|
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
2016-04-20 17:27:32 +00:00
|
|
|
d := &testDescriber{Output: "test output"}
|
|
|
|
tf.Describer = d
|
2017-01-22 22:11:33 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2016-05-04 10:24:16 +00:00
|
|
|
Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, pods)},
|
2016-04-20 17:27:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tf.Namespace = "test"
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-08-23 03:11:05 +00:00
|
|
|
buferr := bytes.NewBuffer([]byte{})
|
|
|
|
cmd := NewCmdDescribe(f, buf, buferr)
|
2016-04-20 17:27:32 +00:00
|
|
|
cmd.Flags().Set("show-events", "false")
|
|
|
|
cmd.Run(cmd, []string{"pods"})
|
|
|
|
if d.Settings.ShowEvents != false {
|
|
|
|
t.Errorf("ShowEvents = false expected, got ShowEvents = %v", d.Settings.ShowEvents)
|
|
|
|
}
|
|
|
|
}
|
2017-10-11 06:26:02 +00:00
|
|
|
|
|
|
|
func TestDescribeHelpMessage(t *testing.T) {
|
|
|
|
f, _, _, _ := cmdtesting.NewAPIFactory()
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
|
|
|
buferr := bytes.NewBuffer([]byte{})
|
|
|
|
cmd := NewCmdDescribe(f, buf, buferr)
|
|
|
|
cmd.SetArgs([]string{"-h"})
|
|
|
|
cmd.SetOutput(buf)
|
|
|
|
_, err := cmd.ExecuteC()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
got := buf.String()
|
|
|
|
|
|
|
|
expected := `describe (-f FILENAME | TYPE [NAME_PREFIX | -l label] | TYPE/NAME)`
|
|
|
|
if !strings.Contains(got, expected) {
|
|
|
|
t.Errorf("Expected to contain: \n %v\nGot:\n %v\n", expected, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
unexpected := `describe (-f FILENAME | TYPE [NAME_PREFIX | -l label] | TYPE/NAME) [flags]`
|
|
|
|
if strings.Contains(got, unexpected) {
|
|
|
|
t.Errorf("Expected not to contain: \n %v\nGot:\n %v\n", unexpected, got)
|
|
|
|
}
|
|
|
|
}
|