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"
|
2015-04-16 23:21:13 +00:00
|
|
|
encjson "encoding/json"
|
2014-12-31 00:42:55 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2014-11-18 18:04:10 +00:00
|
|
|
"net/http"
|
|
|
|
"reflect"
|
2014-12-12 21:33:18 +00:00
|
|
|
"strings"
|
2014-11-18 18:04:10 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-01-25 13:39:54 +00:00
|
|
|
apiequality "k8s.io/apimachinery/pkg/api/equality"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-02-19 22:56:11 +00:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/serializer/json"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/serializer/streaming"
|
|
|
|
"k8s.io/apimachinery/pkg/watch"
|
2017-01-19 18:27:59 +00:00
|
|
|
restclient "k8s.io/client-go/rest"
|
2017-01-24 15:00:24 +00:00
|
|
|
"k8s.io/client-go/rest/fake"
|
2017-01-18 21:01:15 +00:00
|
|
|
restclientwatch "k8s.io/client-go/rest/watch"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
"k8s.io/kubernetes/pkg/api/testapi"
|
2015-09-14 21:56:51 +00:00
|
|
|
apitesting "k8s.io/kubernetes/pkg/api/testing"
|
2016-10-18 22:53:26 +00:00
|
|
|
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
|
2014-11-18 18:04:10 +00:00
|
|
|
)
|
|
|
|
|
2015-02-05 23:20:27 +00:00
|
|
|
func testData() (*api.PodList, *api.ServiceList, *api.ReplicationControllerList) {
|
2014-12-31 00:42:55 +00:00
|
|
|
pods := &api.PodList{
|
2016-12-03 18:57:26 +00:00
|
|
|
ListMeta: metav1.ListMeta{
|
2014-12-31 00:42:55 +00:00
|
|
|
ResourceVersion: "15",
|
|
|
|
},
|
|
|
|
Items: []api.Pod{
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "test", ResourceVersion: "10"},
|
2015-09-14 21:56:51 +00:00
|
|
|
Spec: apitesting.DeepEqualSafePodSpec(),
|
2014-12-31 00:42:55 +00:00
|
|
|
},
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "bar", Namespace: "test", ResourceVersion: "11"},
|
2015-09-14 21:56:51 +00:00
|
|
|
Spec: apitesting.DeepEqualSafePodSpec(),
|
2014-12-31 00:42:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
svc := &api.ServiceList{
|
2016-12-03 18:57:26 +00:00
|
|
|
ListMeta: metav1.ListMeta{
|
2014-12-31 00:42:55 +00:00
|
|
|
ResourceVersion: "16",
|
|
|
|
},
|
|
|
|
Items: []api.Service{
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"},
|
2015-01-26 17:52:50 +00:00
|
|
|
Spec: api.ServiceSpec{
|
|
|
|
SessionAffinity: "None",
|
2015-05-22 21:49:26 +00:00
|
|
|
Type: api.ServiceTypeClusterIP,
|
2015-01-26 17:52:50 +00:00
|
|
|
},
|
2014-12-31 00:42:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-02-05 23:20:27 +00:00
|
|
|
rc := &api.ReplicationControllerList{
|
2016-12-03 18:57:26 +00:00
|
|
|
ListMeta: metav1.ListMeta{
|
2015-02-05 23:20:27 +00:00
|
|
|
ResourceVersion: "17",
|
|
|
|
},
|
|
|
|
Items: []api.ReplicationController{
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "rc1", Namespace: "test", ResourceVersion: "18"},
|
2015-02-05 23:20:27 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Replicas: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return pods, svc, rc
|
2014-12-31 00:42:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 19:23:02 +00:00
|
|
|
func testComponentStatusData() *api.ComponentStatusList {
|
2015-10-01 01:07:03 +00:00
|
|
|
good := api.ComponentStatus{
|
2015-04-15 19:23:02 +00:00
|
|
|
Conditions: []api.ComponentCondition{
|
2015-11-05 21:26:03 +00:00
|
|
|
{Type: api.ComponentHealthy, Status: api.ConditionTrue, Message: "ok"},
|
2015-04-15 19:23:02 +00:00
|
|
|
},
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "servergood"},
|
2015-04-15 19:23:02 +00:00
|
|
|
}
|
2015-04-20 23:49:16 +00:00
|
|
|
|
2015-10-01 01:07:03 +00:00
|
|
|
bad := api.ComponentStatus{
|
2015-04-15 19:23:02 +00:00
|
|
|
Conditions: []api.ComponentCondition{
|
|
|
|
{Type: api.ComponentHealthy, Status: api.ConditionFalse, Message: "", Error: "bad status: 500"},
|
|
|
|
},
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "serverbad"},
|
2015-04-15 19:23:02 +00:00
|
|
|
}
|
2015-04-20 23:49:16 +00:00
|
|
|
|
2015-10-01 01:07:03 +00:00
|
|
|
unknown := api.ComponentStatus{
|
2015-04-15 19:23:02 +00:00
|
|
|
Conditions: []api.ComponentCondition{
|
|
|
|
{Type: api.ComponentHealthy, Status: api.ConditionUnknown, Message: "", Error: "fizzbuzz error"},
|
|
|
|
},
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "serverunknown"},
|
2015-04-15 19:23:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &api.ComponentStatusList{
|
2015-10-01 01:07:03 +00:00
|
|
|
Items: []api.ComponentStatus{good, bad, unknown},
|
2015-04-15 19:23:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 TestGetUnknownSchemaObject(t *testing.T) {
|
2016-11-02 16:29:01 +00:00
|
|
|
f, tf, _, _ := cmdtesting.NewAPIFactory()
|
|
|
|
_, _, codec, _ := cmdtesting.NewTestFactory()
|
2014-11-18 18:04:10 +00:00
|
|
|
tf.Printer = &testPrinter{}
|
2017-01-22 18:45:30 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-01-24 15:00:24 +00:00
|
|
|
APIRegistry: api.Registry,
|
2016-11-02 16:29:01 +00:00
|
|
|
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 = "test"
|
2017-01-12 18:17:43 +00:00
|
|
|
tf.ClientConfig = &restclient.Config{ContentConfig: restclient.ContentConfig{GroupVersion: &api.Registry.GroupOrDie(api.GroupName).GroupVersion}}
|
2014-11-18 18:04:10 +00:00
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-07-13 16:00:33 +00:00
|
|
|
errBuf := bytes.NewBuffer([]byte{})
|
2014-11-18 18:04:10 +00:00
|
|
|
|
2016-07-13 16:00:33 +00:00
|
|
|
cmd := NewCmdGet(f, buf, errBuf)
|
2014-12-31 00:42:55 +00:00
|
|
|
cmd.SetOutput(buf)
|
2014-11-18 18:04:10 +00:00
|
|
|
cmd.Run(cmd, []string{"type", "foo"})
|
|
|
|
|
2016-11-02 16:29:01 +00:00
|
|
|
expected := []runtime.Object{cmdtesting.NewInternalType("", "", "foo")}
|
|
|
|
actual := tf.Printer.(*testPrinter).Objects
|
|
|
|
if len(actual) != len(expected) {
|
|
|
|
t.Fatal(actual)
|
|
|
|
}
|
|
|
|
for i, obj := range actual {
|
|
|
|
expectedJSON := runtime.EncodeOrDie(codec, expected[i])
|
|
|
|
expectedMap := map[string]interface{}{}
|
|
|
|
if err := encjson.Unmarshal([]byte(expectedJSON), &expectedMap); err != nil {
|
|
|
|
t.Fatal(err)
|
2015-04-16 23:21:13 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 16:29:01 +00:00
|
|
|
actualJSON := runtime.EncodeOrDie(api.Codecs.LegacyCodec(), obj)
|
|
|
|
actualMap := map[string]interface{}{}
|
|
|
|
if err := encjson.Unmarshal([]byte(actualJSON), &actualMap); err != nil {
|
|
|
|
t.Fatal(err)
|
2015-04-16 23:21:13 +00:00
|
|
|
}
|
2016-11-02 16:29:01 +00:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(expectedMap, actualMap) {
|
|
|
|
t.Errorf("unexpected object: \n%#v\n%#v", expectedMap, actualMap)
|
2015-04-16 23:21:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-12 21:33:18 +00:00
|
|
|
// Verifies that schemas that are not in the master tree of Kubernetes can be retrieved via Get.
|
|
|
|
func TestGetSchemaObject(t *testing.T) {
|
2016-11-02 16:29:01 +00:00
|
|
|
f, tf, _, _ := cmdtesting.NewAPIFactory()
|
2015-09-11 00:20:53 +00:00
|
|
|
tf.Mapper = testapi.Default.RESTMapper()
|
2015-01-03 20:38:33 +00:00
|
|
|
tf.Typer = api.Scheme
|
2015-09-11 00:20:53 +00:00
|
|
|
codec := testapi.Default.Codec()
|
2014-12-12 21:33:18 +00:00
|
|
|
tf.Printer = &testPrinter{}
|
2017-01-22 18:45:30 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-01-24 15:00:24 +00:00
|
|
|
APIRegistry: api.Registry,
|
2016-11-02 16:29:01 +00:00
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2017-01-17 03:38:19 +00:00
|
|
|
Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &api.ReplicationController{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})},
|
2014-12-12 21:33:18 +00:00
|
|
|
}
|
2015-01-02 18:08:37 +00:00
|
|
|
tf.Namespace = "test"
|
2016-11-21 02:55:31 +00:00
|
|
|
tf.ClientConfig = &restclient.Config{ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Version: "v1"}}}
|
2014-12-12 21:33:18 +00:00
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-07-13 16:00:33 +00:00
|
|
|
errBuf := bytes.NewBuffer([]byte{})
|
2014-12-12 21:33:18 +00:00
|
|
|
|
2016-07-13 16:00:33 +00:00
|
|
|
cmd := NewCmdGet(f, buf, errBuf)
|
2014-12-12 21:33:18 +00:00
|
|
|
cmd.Run(cmd, []string{"replicationcontrollers", "foo"})
|
|
|
|
|
|
|
|
if !strings.Contains(buf.String(), "\"foo\"") {
|
|
|
|
t.Errorf("unexpected output: %s", buf.String())
|
|
|
|
}
|
|
|
|
}
|
2014-12-31 00:42:55 +00:00
|
|
|
|
|
|
|
func TestGetObjects(t *testing.T) {
|
2015-02-05 23:20:27 +00:00
|
|
|
pods, _, _ := testData()
|
2014-12-31 00:42:55 +00:00
|
|
|
|
2016-11-02 16:29:01 +00:00
|
|
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
2014-12-31 00:42:55 +00:00
|
|
|
tf.Printer = &testPrinter{}
|
2017-01-22 18:45:30 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-01-24 15:00:24 +00:00
|
|
|
APIRegistry: api.Registry,
|
2016-11-02 16:29:01 +00:00
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2016-05-04 10:24:16 +00:00
|
|
|
Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &pods.Items[0])},
|
2014-12-31 00:42:55 +00:00
|
|
|
}
|
2015-01-02 18:08:37 +00:00
|
|
|
tf.Namespace = "test"
|
2014-12-31 00:42:55 +00:00
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-07-13 16:00:33 +00:00
|
|
|
errBuf := bytes.NewBuffer([]byte{})
|
2014-12-31 00:42:55 +00:00
|
|
|
|
2016-07-13 16:00:33 +00:00
|
|
|
cmd := NewCmdGet(f, buf, errBuf)
|
2014-12-31 00:42:55 +00:00
|
|
|
cmd.SetOutput(buf)
|
|
|
|
cmd.Run(cmd, []string{"pods", "foo"})
|
|
|
|
|
|
|
|
expected := []runtime.Object{&pods.Items[0]}
|
2016-11-02 16:29:01 +00:00
|
|
|
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
|
|
|
|
|
2014-12-31 00:42:55 +00:00
|
|
|
if len(buf.String()) == 0 {
|
|
|
|
t.Errorf("unexpected empty output")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-28 02:36:42 +00:00
|
|
|
func TestGetObjectsFiltered(t *testing.T) {
|
|
|
|
initTestErrorHandler(t)
|
|
|
|
|
|
|
|
pods, _, _ := testData()
|
|
|
|
pods.Items[0].Status.Phase = api.PodFailed
|
|
|
|
first := &pods.Items[0]
|
|
|
|
second := &pods.Items[1]
|
|
|
|
|
|
|
|
testCases := []struct {
|
2017-05-23 02:47:20 +00:00
|
|
|
args []string
|
|
|
|
resp runtime.Object
|
|
|
|
flags map[string]string
|
|
|
|
expect []runtime.Object
|
|
|
|
genericPrinter bool
|
2017-02-28 02:36:42 +00:00
|
|
|
}{
|
2017-05-23 02:47:20 +00:00
|
|
|
{args: []string{"pods", "foo"}, resp: first, expect: []runtime.Object{first}, genericPrinter: true},
|
|
|
|
{args: []string{"pods", "foo"}, flags: map[string]string{"show-all": "false"}, resp: first, expect: []runtime.Object{first}, genericPrinter: true},
|
2017-02-28 02:36:42 +00:00
|
|
|
{args: []string{"pods"}, flags: map[string]string{"show-all": "true"}, resp: pods, expect: []runtime.Object{first, second}},
|
2017-05-23 02:47:20 +00:00
|
|
|
{args: []string{"pods/foo"}, resp: first, expect: []runtime.Object{first}, genericPrinter: true},
|
2017-03-01 23:33:08 +00:00
|
|
|
{args: []string{"pods"}, flags: map[string]string{"output": "yaml"}, resp: pods, expect: []runtime.Object{second}},
|
2017-02-28 02:36:42 +00:00
|
|
|
{args: []string{}, flags: map[string]string{"filename": "../../../examples/storage/cassandra/cassandra-controller.yaml"}, resp: pods, expect: []runtime.Object{first, second}},
|
|
|
|
|
|
|
|
{args: []string{"pods"}, resp: pods, expect: []runtime.Object{second}},
|
2017-03-01 23:33:08 +00:00
|
|
|
{args: []string{"pods"}, flags: map[string]string{"show-all": "true", "output": "yaml"}, resp: pods, expect: []runtime.Object{first, second}},
|
2017-02-28 02:36:42 +00:00
|
|
|
{args: []string{"pods"}, flags: map[string]string{"show-all": "false"}, resp: pods, expect: []runtime.Object{second}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range testCases {
|
|
|
|
t.Logf("%d", i)
|
|
|
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
2017-05-23 02:47:20 +00:00
|
|
|
tf.Printer = &testPrinter{GenericPrinter: test.genericPrinter}
|
2017-02-28 02:36:42 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
|
|
|
APIRegistry: api.Registry,
|
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
|
|
|
Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, test.resp)},
|
|
|
|
}
|
|
|
|
tf.Namespace = "test"
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
|
|
|
errBuf := bytes.NewBuffer([]byte{})
|
|
|
|
|
|
|
|
cmd := NewCmdGet(f, buf, errBuf)
|
|
|
|
cmd.SetOutput(buf)
|
|
|
|
for k, v := range test.flags {
|
|
|
|
cmd.Flags().Lookup(k).Value.Set(v)
|
|
|
|
}
|
|
|
|
cmd.Run(cmd, test.args)
|
|
|
|
|
|
|
|
verifyObjects(t, test.expect, tf.Printer.(*testPrinter).Objects)
|
|
|
|
|
|
|
|
if len(buf.String()) == 0 {
|
|
|
|
t.Errorf("%d: unexpected empty output", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-14 19:48:07 +00:00
|
|
|
func TestGetObjectIgnoreNotFound(t *testing.T) {
|
|
|
|
initTestErrorHandler(t)
|
|
|
|
|
|
|
|
ns := &api.NamespaceList{
|
|
|
|
ListMeta: metav1.ListMeta{
|
|
|
|
ResourceVersion: "1",
|
|
|
|
},
|
|
|
|
Items: []api.Namespace{
|
|
|
|
{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "testns", Namespace: "test", ResourceVersion: "11"},
|
|
|
|
Spec: api.NamespaceSpec{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
2017-05-23 02:47:20 +00:00
|
|
|
tf.Printer = &testPrinter{GenericPrinter: true}
|
2017-02-14 19:48:07 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
|
|
|
APIRegistry: api.Registry,
|
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
|
|
|
switch p, m := req.URL.Path, req.Method; {
|
|
|
|
case p == "/namespaces/test/pods/nonexistentpod" && m == "GET":
|
|
|
|
return &http.Response{StatusCode: 404, Header: defaultHeader(), Body: stringBody("")}, nil
|
|
|
|
case p == "/api/v1/namespaces/test" && m == "GET":
|
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &ns.Items[0])}, nil
|
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
tf.Namespace = "test"
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
|
|
|
errBuf := bytes.NewBuffer([]byte{})
|
|
|
|
|
|
|
|
cmd := NewCmdGet(f, buf, errBuf)
|
|
|
|
cmd.SetOutput(buf)
|
|
|
|
cmd.Flags().Set("ignore-not-found", "true")
|
|
|
|
cmd.Flags().Set("output", "yaml")
|
|
|
|
cmd.Run(cmd, []string{"pods", "nonexistentpod"})
|
|
|
|
|
|
|
|
if buf.String() != "" {
|
|
|
|
t.Errorf("unexpected output: %s", buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-29 22:17:48 +00:00
|
|
|
func TestGetSortedObjects(t *testing.T) {
|
|
|
|
pods := &api.PodList{
|
2016-12-03 18:57:26 +00:00
|
|
|
ListMeta: metav1.ListMeta{
|
2016-02-29 22:17:48 +00:00
|
|
|
ResourceVersion: "15",
|
|
|
|
},
|
|
|
|
Items: []api.Pod{
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "c", Namespace: "test", ResourceVersion: "10"},
|
2016-02-29 22:17:48 +00:00
|
|
|
Spec: apitesting.DeepEqualSafePodSpec(),
|
|
|
|
},
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "b", Namespace: "test", ResourceVersion: "11"},
|
2016-02-29 22:17:48 +00:00
|
|
|
Spec: apitesting.DeepEqualSafePodSpec(),
|
|
|
|
},
|
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "a", Namespace: "test", ResourceVersion: "9"},
|
2016-02-29 22:17:48 +00:00
|
|
|
Spec: apitesting.DeepEqualSafePodSpec(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-11-02 16:29:01 +00:00
|
|
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
2016-02-29 22:17:48 +00:00
|
|
|
tf.Printer = &testPrinter{}
|
2017-01-22 18:45:30 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-01-24 15:00:24 +00:00
|
|
|
APIRegistry: api.Registry,
|
2016-11-02 16:29:01 +00:00
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2016-05-04 10:24:16 +00:00
|
|
|
Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, pods)},
|
2016-02-29 22:17:48 +00:00
|
|
|
}
|
|
|
|
tf.Namespace = "test"
|
2016-11-21 02:55:31 +00:00
|
|
|
tf.ClientConfig = &restclient.Config{ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Version: "v1"}}}
|
2016-02-29 22:17:48 +00:00
|
|
|
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-07-13 16:00:33 +00:00
|
|
|
errBuf := bytes.NewBuffer([]byte{})
|
|
|
|
|
|
|
|
cmd := NewCmdGet(f, buf, errBuf)
|
2016-02-29 22:17:48 +00:00
|
|
|
cmd.SetOutput(buf)
|
|
|
|
|
|
|
|
// sorting with metedata.name
|
|
|
|
cmd.Flags().Set("sort-by", ".metadata.name")
|
|
|
|
cmd.Run(cmd, []string{"pods"})
|
|
|
|
|
|
|
|
// expect sorted: a,b,c
|
|
|
|
expected := []runtime.Object{&pods.Items[2], &pods.Items[1], &pods.Items[0]}
|
2016-11-02 16:29:01 +00:00
|
|
|
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
|
|
|
|
|
2016-02-29 22:17:48 +00:00
|
|
|
if len(buf.String()) == 0 {
|
|
|
|
t.Errorf("unexpected empty output")
|
|
|
|
}
|
2016-11-02 16:29:01 +00:00
|
|
|
}
|
2016-02-29 22:17:48 +00:00
|
|
|
|
2016-11-02 16:29:01 +00:00
|
|
|
func verifyObjects(t *testing.T, expected, actual []runtime.Object) {
|
2016-12-05 06:43:37 +00:00
|
|
|
var actualObj runtime.Object
|
|
|
|
var err error
|
|
|
|
|
2016-11-02 16:29:01 +00:00
|
|
|
if len(actual) != len(expected) {
|
2017-02-28 02:36:42 +00:00
|
|
|
t.Fatalf("expected %d, got %d", len(expected), len(actual))
|
2016-11-02 16:29:01 +00:00
|
|
|
}
|
|
|
|
for i, obj := range actual {
|
2016-12-05 06:43:37 +00:00
|
|
|
switch obj.(type) {
|
|
|
|
case runtime.Unstructured, *runtime.Unknown:
|
|
|
|
actualObj, err = runtime.Decode(
|
|
|
|
api.Codecs.UniversalDecoder(),
|
|
|
|
[]byte(runtime.EncodeOrDie(api.Codecs.LegacyCodec(), obj)))
|
|
|
|
default:
|
|
|
|
actualObj = obj
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
2016-11-02 16:29:01 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-01-25 13:39:54 +00:00
|
|
|
if !apiequality.Semantic.DeepEqual(expected[i], actualObj) {
|
2017-02-28 02:36:42 +00:00
|
|
|
t.Errorf("unexpected object: %d \n%#v\n%#v", i, expected[i], actualObj)
|
2016-11-02 16:29:01 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-29 22:17:48 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 11:02:14 +00:00
|
|
|
func TestGetObjectsIdentifiedByFile(t *testing.T) {
|
|
|
|
pods, _, _ := testData()
|
|
|
|
|
2016-11-02 16:29:01 +00:00
|
|
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
2017-05-23 02:47:20 +00:00
|
|
|
tf.Printer = &testPrinter{GenericPrinter: true}
|
2017-01-22 18:45:30 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-01-24 15:00:24 +00:00
|
|
|
APIRegistry: api.Registry,
|
2016-11-02 16:29:01 +00:00
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2016-05-04 10:24:16 +00:00
|
|
|
Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &pods.Items[0])},
|
2015-08-10 11:02:14 +00:00
|
|
|
}
|
|
|
|
tf.Namespace = "test"
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-07-13 16:00:33 +00:00
|
|
|
errBuf := bytes.NewBuffer([]byte{})
|
2015-08-10 11:02:14 +00:00
|
|
|
|
2016-07-13 16:00:33 +00:00
|
|
|
cmd := NewCmdGet(f, buf, errBuf)
|
2015-08-10 11:02:14 +00:00
|
|
|
cmd.SetOutput(buf)
|
2016-06-21 22:43:29 +00:00
|
|
|
cmd.Flags().Set("filename", "../../../examples/storage/cassandra/cassandra-controller.yaml")
|
2015-08-10 11:02:14 +00:00
|
|
|
cmd.Run(cmd, []string{})
|
|
|
|
|
|
|
|
expected := []runtime.Object{&pods.Items[0]}
|
2016-11-02 16:29:01 +00:00
|
|
|
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
|
|
|
|
|
2015-08-10 11:02:14 +00:00
|
|
|
if len(buf.String()) == 0 {
|
|
|
|
t.Errorf("unexpected empty output")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-31 00:42:55 +00:00
|
|
|
func TestGetListObjects(t *testing.T) {
|
2015-02-05 23:20:27 +00:00
|
|
|
pods, _, _ := testData()
|
2014-12-31 00:42:55 +00:00
|
|
|
|
2016-11-02 16:29:01 +00:00
|
|
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
2014-12-31 00:42:55 +00:00
|
|
|
tf.Printer = &testPrinter{}
|
2017-01-22 18:45:30 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-01-24 15:00:24 +00:00
|
|
|
APIRegistry: api.Registry,
|
2016-11-02 16:29:01 +00:00
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2016-05-04 10:24:16 +00:00
|
|
|
Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, pods)},
|
2014-12-31 00:42:55 +00:00
|
|
|
}
|
2015-01-02 18:08:37 +00:00
|
|
|
tf.Namespace = "test"
|
2014-12-31 00:42:55 +00:00
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-07-13 16:00:33 +00:00
|
|
|
errBuf := bytes.NewBuffer([]byte{})
|
2014-12-31 00:42:55 +00:00
|
|
|
|
2016-07-13 16:00:33 +00:00
|
|
|
cmd := NewCmdGet(f, buf, errBuf)
|
2014-12-31 00:42:55 +00:00
|
|
|
cmd.SetOutput(buf)
|
|
|
|
cmd.Run(cmd, []string{"pods"})
|
|
|
|
|
2015-10-01 01:07:03 +00:00
|
|
|
expected, err := extractResourceList([]runtime.Object{pods})
|
|
|
|
if err != nil {
|
2016-11-02 16:29:01 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 00:42:55 +00:00
|
|
|
}
|
2016-11-02 16:29:01 +00:00
|
|
|
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
|
|
|
|
|
2014-12-31 00:42:55 +00:00
|
|
|
if len(buf.String()) == 0 {
|
|
|
|
t.Errorf("unexpected empty output")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 01:07:03 +00:00
|
|
|
func extractResourceList(objs []runtime.Object) ([]runtime.Object, error) {
|
|
|
|
finalObjs := []runtime.Object{}
|
|
|
|
for _, obj := range objs {
|
2015-11-12 10:45:42 +00:00
|
|
|
items, err := meta.ExtractList(obj)
|
2015-10-01 01:07:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, item := range items {
|
|
|
|
finalObjs = append(finalObjs, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return finalObjs, nil
|
|
|
|
}
|
|
|
|
|
2015-07-31 23:42:34 +00:00
|
|
|
func TestGetAllListObjects(t *testing.T) {
|
|
|
|
pods, _, _ := testData()
|
|
|
|
|
2016-11-02 16:29:01 +00:00
|
|
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
2015-07-31 23:42:34 +00:00
|
|
|
tf.Printer = &testPrinter{}
|
2017-01-22 18:45:30 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-01-24 15:00:24 +00:00
|
|
|
APIRegistry: api.Registry,
|
2016-11-02 16:29:01 +00:00
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2016-05-04 10:24:16 +00:00
|
|
|
Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, pods)},
|
2015-07-31 23:42:34 +00:00
|
|
|
}
|
|
|
|
tf.Namespace = "test"
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-07-13 16:00:33 +00:00
|
|
|
errBuf := bytes.NewBuffer([]byte{})
|
2015-07-31 23:42:34 +00:00
|
|
|
|
2016-07-13 16:00:33 +00:00
|
|
|
cmd := NewCmdGet(f, buf, errBuf)
|
2015-07-31 23:42:34 +00:00
|
|
|
cmd.SetOutput(buf)
|
|
|
|
cmd.Flags().Set("show-all", "true")
|
|
|
|
cmd.Run(cmd, []string{"pods"})
|
|
|
|
|
2015-10-01 01:07:03 +00:00
|
|
|
expected, err := extractResourceList([]runtime.Object{pods})
|
|
|
|
if err != nil {
|
2016-11-02 16:29:01 +00:00
|
|
|
t.Fatal(err)
|
2015-07-31 23:42:34 +00:00
|
|
|
}
|
2016-11-02 16:29:01 +00:00
|
|
|
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
|
|
|
|
|
2015-07-31 23:42:34 +00:00
|
|
|
if len(buf.String()) == 0 {
|
|
|
|
t.Errorf("unexpected empty output")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 19:23:02 +00:00
|
|
|
func TestGetListComponentStatus(t *testing.T) {
|
|
|
|
statuses := testComponentStatusData()
|
|
|
|
|
2016-11-02 16:29:01 +00:00
|
|
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
2015-04-15 19:23:02 +00:00
|
|
|
tf.Printer = &testPrinter{}
|
2017-01-22 18:45:30 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-01-24 15:00:24 +00:00
|
|
|
APIRegistry: api.Registry,
|
2016-11-02 16:29:01 +00:00
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2016-05-04 10:24:16 +00:00
|
|
|
Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, statuses)},
|
2015-04-15 19:23:02 +00:00
|
|
|
}
|
|
|
|
tf.Namespace = "test"
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-07-13 16:00:33 +00:00
|
|
|
errBuf := bytes.NewBuffer([]byte{})
|
2015-04-15 19:23:02 +00:00
|
|
|
|
2016-07-13 16:00:33 +00:00
|
|
|
cmd := NewCmdGet(f, buf, errBuf)
|
2015-04-15 19:23:02 +00:00
|
|
|
cmd.SetOutput(buf)
|
|
|
|
cmd.Run(cmd, []string{"componentstatuses"})
|
|
|
|
|
2015-10-01 01:07:03 +00:00
|
|
|
expected, err := extractResourceList([]runtime.Object{statuses})
|
|
|
|
if err != nil {
|
2016-11-02 16:29:01 +00:00
|
|
|
t.Fatal(err)
|
2015-04-15 19:23:02 +00:00
|
|
|
}
|
2016-11-02 16:29:01 +00:00
|
|
|
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
|
|
|
|
|
2015-04-15 19:23:02 +00:00
|
|
|
if len(buf.String()) == 0 {
|
|
|
|
t.Errorf("unexpected empty output")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-31 00:42:55 +00:00
|
|
|
func TestGetMultipleTypeObjects(t *testing.T) {
|
2015-02-05 23:20:27 +00:00
|
|
|
pods, svc, _ := testData()
|
2014-12-31 00:42:55 +00:00
|
|
|
|
2016-11-02 16:29:01 +00:00
|
|
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
2014-12-31 00:42:55 +00:00
|
|
|
tf.Printer = &testPrinter{}
|
2017-01-22 18:45:30 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-01-24 15:00:24 +00:00
|
|
|
APIRegistry: api.Registry,
|
2016-11-02 16:29:01 +00:00
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2015-11-11 19:54:58 +00:00
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
2014-12-31 00:42:55 +00:00
|
|
|
switch req.URL.Path {
|
2015-01-19 21:50:00 +00:00
|
|
|
case "/namespaces/test/pods":
|
2016-05-11 07:50:47 +00:00
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, pods)}, nil
|
2015-01-19 21:50:00 +00:00
|
|
|
case "/namespaces/test/services":
|
2016-05-11 07:50:47 +00:00
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, svc)}, nil
|
2014-12-31 00:42:55 +00:00
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
2015-01-02 18:08:37 +00:00
|
|
|
tf.Namespace = "test"
|
2014-12-31 00:42:55 +00:00
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-07-13 16:00:33 +00:00
|
|
|
errBuf := bytes.NewBuffer([]byte{})
|
2014-12-31 00:42:55 +00:00
|
|
|
|
2016-07-13 16:00:33 +00:00
|
|
|
cmd := NewCmdGet(f, buf, errBuf)
|
2014-12-31 00:42:55 +00:00
|
|
|
cmd.SetOutput(buf)
|
|
|
|
cmd.Run(cmd, []string{"pods,services"})
|
|
|
|
|
2015-10-01 01:07:03 +00:00
|
|
|
expected, err := extractResourceList([]runtime.Object{pods, svc})
|
|
|
|
if err != nil {
|
2016-11-02 16:29:01 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 00:42:55 +00:00
|
|
|
}
|
2016-11-02 16:29:01 +00:00
|
|
|
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
|
|
|
|
|
2014-12-31 00:42:55 +00:00
|
|
|
if len(buf.String()) == 0 {
|
|
|
|
t.Errorf("unexpected empty output")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetMultipleTypeObjectsAsList(t *testing.T) {
|
2015-02-05 23:20:27 +00:00
|
|
|
pods, svc, _ := testData()
|
2014-12-31 00:42:55 +00:00
|
|
|
|
2016-11-02 16:29:01 +00:00
|
|
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
2017-05-23 02:47:20 +00:00
|
|
|
tf.Printer = &testPrinter{GenericPrinter: true}
|
2017-01-22 18:45:30 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-01-24 15:00:24 +00:00
|
|
|
APIRegistry: api.Registry,
|
2016-11-02 16:29:01 +00:00
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2015-11-11 19:54:58 +00:00
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
2014-12-31 00:42:55 +00:00
|
|
|
switch req.URL.Path {
|
2015-01-19 21:50:00 +00:00
|
|
|
case "/namespaces/test/pods":
|
2016-05-11 07:50:47 +00:00
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, pods)}, nil
|
2015-01-19 21:50:00 +00:00
|
|
|
case "/namespaces/test/services":
|
2016-05-11 07:50:47 +00:00
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, svc)}, nil
|
2014-12-31 00:42:55 +00:00
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
2015-01-02 18:08:37 +00:00
|
|
|
tf.Namespace = "test"
|
2017-01-12 18:17:43 +00:00
|
|
|
tf.ClientConfig = &restclient.Config{ContentConfig: restclient.ContentConfig{GroupVersion: &api.Registry.GroupOrDie(api.GroupName).GroupVersion}}
|
2014-12-31 00:42:55 +00:00
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-07-13 16:00:33 +00:00
|
|
|
errBuf := bytes.NewBuffer([]byte{})
|
2014-12-31 00:42:55 +00:00
|
|
|
|
2016-07-13 16:00:33 +00:00
|
|
|
cmd := NewCmdGet(f, buf, errBuf)
|
2014-12-31 00:42:55 +00:00
|
|
|
cmd.SetOutput(buf)
|
|
|
|
|
|
|
|
cmd.Flags().Set("output", "json")
|
|
|
|
cmd.Run(cmd, []string{"pods,services"})
|
|
|
|
|
2017-05-23 02:47:20 +00:00
|
|
|
actual := tf.Printer.(*testPrinter).Objects
|
2017-03-31 19:18:51 +00:00
|
|
|
fn := func(obj runtime.Object) unstructured.Unstructured {
|
2017-02-19 22:56:11 +00:00
|
|
|
data, err := runtime.Encode(api.Codecs.LegacyCodec(schema.GroupVersion{Version: "v1"}), obj)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
out := &unstructured.Unstructured{Object: make(map[string]interface{})}
|
|
|
|
if err := encjson.Unmarshal(data, &out.Object); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-03-31 19:18:51 +00:00
|
|
|
return *out
|
2014-12-31 00:42:55 +00:00
|
|
|
}
|
|
|
|
|
2017-02-19 22:56:11 +00:00
|
|
|
expected := &unstructured.UnstructuredList{
|
2017-03-31 09:27:25 +00:00
|
|
|
Object: map[string]interface{}{"kind": "List", "apiVersion": "v1", "metadata": map[string]interface{}{"selfLink": "", "resourceVersion": ""}},
|
2017-03-31 19:18:51 +00:00
|
|
|
Items: []unstructured.Unstructured{
|
2017-02-19 22:56:11 +00:00
|
|
|
fn(&pods.Items[0]),
|
|
|
|
fn(&pods.Items[1]),
|
|
|
|
fn(&svc.Items[0]),
|
|
|
|
},
|
2014-12-31 00:42:55 +00:00
|
|
|
}
|
2017-02-19 22:56:11 +00:00
|
|
|
actualBytes, err := encjson.Marshal(actual[0])
|
2015-04-29 03:15:16 +00:00
|
|
|
if err != nil {
|
2017-02-19 22:56:11 +00:00
|
|
|
t.Fatal(err)
|
2015-04-29 03:15:16 +00:00
|
|
|
}
|
2017-02-19 22:56:11 +00:00
|
|
|
expectedBytes, err := encjson.Marshal(expected)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2014-12-31 00:42:55 +00:00
|
|
|
}
|
2017-02-19 22:56:11 +00:00
|
|
|
if string(actualBytes) != string(expectedBytes) {
|
|
|
|
t.Errorf("unexpected object:\n%s\n%s", expectedBytes, actualBytes)
|
2014-12-31 00:42:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetMultipleTypeObjectsWithSelector(t *testing.T) {
|
2015-02-05 23:20:27 +00:00
|
|
|
pods, svc, _ := testData()
|
2014-12-31 00:42:55 +00:00
|
|
|
|
2016-11-02 16:29:01 +00:00
|
|
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
2014-12-31 00:42:55 +00:00
|
|
|
tf.Printer = &testPrinter{}
|
2017-01-22 18:45:30 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-01-24 15:00:24 +00:00
|
|
|
APIRegistry: api.Registry,
|
2016-11-02 16:29:01 +00:00
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2015-11-11 19:54:58 +00:00
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
2017-01-12 18:17:43 +00:00
|
|
|
if req.URL.Query().Get(metav1.LabelSelectorQueryParam(api.Registry.GroupOrDie(api.GroupName).GroupVersion.String())) != "a=b" {
|
2014-12-31 00:42:55 +00:00
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
}
|
|
|
|
switch req.URL.Path {
|
2015-01-19 21:50:00 +00:00
|
|
|
case "/namespaces/test/pods":
|
2016-05-11 07:50:47 +00:00
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, pods)}, nil
|
2015-01-19 21:50:00 +00:00
|
|
|
case "/namespaces/test/services":
|
2016-05-11 07:50:47 +00:00
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, svc)}, nil
|
2014-12-31 00:42:55 +00:00
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
2015-01-02 18:08:37 +00:00
|
|
|
tf.Namespace = "test"
|
2014-12-31 00:42:55 +00:00
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-07-13 16:00:33 +00:00
|
|
|
errBuf := bytes.NewBuffer([]byte{})
|
2014-12-31 00:42:55 +00:00
|
|
|
|
2016-07-13 16:00:33 +00:00
|
|
|
cmd := NewCmdGet(f, buf, errBuf)
|
2014-12-31 00:42:55 +00:00
|
|
|
cmd.SetOutput(buf)
|
|
|
|
|
|
|
|
cmd.Flags().Set("selector", "a=b")
|
|
|
|
cmd.Run(cmd, []string{"pods,services"})
|
|
|
|
|
2015-10-01 01:07:03 +00:00
|
|
|
expected, err := extractResourceList([]runtime.Object{pods, svc})
|
|
|
|
if err != nil {
|
2016-11-02 16:29:01 +00:00
|
|
|
t.Fatal(err)
|
2014-12-31 00:42:55 +00:00
|
|
|
}
|
2016-11-02 16:29:01 +00:00
|
|
|
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
|
|
|
|
|
2014-12-31 00:42:55 +00:00
|
|
|
if len(buf.String()) == 0 {
|
|
|
|
t.Errorf("unexpected empty output")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 05:01:07 +00:00
|
|
|
func TestGetMultipleTypeObjectsWithDirectReference(t *testing.T) {
|
|
|
|
_, svc, _ := testData()
|
|
|
|
node := &api.Node{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-03-25 05:01:07 +00:00
|
|
|
Name: "foo",
|
|
|
|
},
|
2015-04-01 23:11:33 +00:00
|
|
|
Spec: api.NodeSpec{
|
|
|
|
ExternalID: "ext",
|
|
|
|
},
|
2015-03-25 05:01:07 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 16:29:01 +00:00
|
|
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
2015-03-25 05:01:07 +00:00
|
|
|
tf.Printer = &testPrinter{}
|
2017-01-22 18:45:30 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-01-24 15:00:24 +00:00
|
|
|
APIRegistry: api.Registry,
|
2016-11-02 16:29:01 +00:00
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2015-11-11 19:54:58 +00:00
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
2015-03-25 05:01:07 +00:00
|
|
|
switch req.URL.Path {
|
|
|
|
case "/nodes/foo":
|
2016-05-11 07:50:47 +00:00
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, node)}, nil
|
2015-03-25 05:01:07 +00:00
|
|
|
case "/namespaces/test/services/bar":
|
2016-05-11 07:50:47 +00:00
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &svc.Items[0])}, nil
|
2015-03-25 05:01:07 +00:00
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
tf.Namespace = "test"
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-07-13 16:00:33 +00:00
|
|
|
errBuf := bytes.NewBuffer([]byte{})
|
2015-03-25 05:01:07 +00:00
|
|
|
|
2016-07-13 16:00:33 +00:00
|
|
|
cmd := NewCmdGet(f, buf, errBuf)
|
2015-03-25 05:01:07 +00:00
|
|
|
cmd.SetOutput(buf)
|
|
|
|
|
|
|
|
cmd.Run(cmd, []string{"services/bar", "node/foo"})
|
|
|
|
|
|
|
|
expected := []runtime.Object{&svc.Items[0], node}
|
2016-11-02 16:29:01 +00:00
|
|
|
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
|
|
|
|
|
2015-03-25 05:01:07 +00:00
|
|
|
if len(buf.String()) == 0 {
|
|
|
|
t.Errorf("unexpected empty output")
|
|
|
|
}
|
|
|
|
}
|
2015-12-03 19:32:22 +00:00
|
|
|
|
2017-02-28 02:36:42 +00:00
|
|
|
func TestGetByFormatForcesFlag(t *testing.T) {
|
2015-12-03 19:32:22 +00:00
|
|
|
pods, _, _ := testData()
|
|
|
|
|
2016-11-02 16:29:01 +00:00
|
|
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
2017-05-23 02:47:20 +00:00
|
|
|
tf.Printer = &testPrinter{GenericPrinter: true}
|
2017-01-22 18:45:30 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-01-24 15:00:24 +00:00
|
|
|
APIRegistry: api.Registry,
|
2016-11-02 16:29:01 +00:00
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2016-05-04 10:24:16 +00:00
|
|
|
Resp: &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &pods.Items[0])},
|
2015-12-03 19:32:22 +00:00
|
|
|
}
|
|
|
|
tf.Namespace = "test"
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-07-13 16:00:33 +00:00
|
|
|
errBuf := bytes.NewBuffer([]byte{})
|
2015-12-03 19:32:22 +00:00
|
|
|
|
2016-07-13 16:00:33 +00:00
|
|
|
cmd := NewCmdGet(f, buf, errBuf)
|
2015-12-03 19:32:22 +00:00
|
|
|
cmd.SetOutput(buf)
|
2017-02-28 02:36:42 +00:00
|
|
|
cmd.Flags().Lookup("output").Value.Set("yaml")
|
|
|
|
cmd.Run(cmd, []string{"pods"})
|
2015-12-03 19:32:22 +00:00
|
|
|
|
|
|
|
showAllFlag, _ := cmd.Flags().GetBool("show-all")
|
2017-03-01 23:33:08 +00:00
|
|
|
if showAllFlag {
|
|
|
|
t.Errorf("expected showAll to not be true when getting resource")
|
2015-12-03 19:32:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-31 00:42:55 +00:00
|
|
|
func watchTestData() ([]api.Pod, []watch.Event) {
|
|
|
|
pods := []api.Pod{
|
2016-06-15 20:21:10 +00:00
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-06-15 20:21:10 +00:00
|
|
|
Name: "bar",
|
|
|
|
Namespace: "test",
|
|
|
|
ResourceVersion: "9",
|
|
|
|
},
|
|
|
|
Spec: apitesting.DeepEqualSafePodSpec(),
|
|
|
|
},
|
2014-12-31 00:42:55 +00:00
|
|
|
{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2014-12-31 00:42:55 +00:00
|
|
|
Name: "foo",
|
|
|
|
Namespace: "test",
|
|
|
|
ResourceVersion: "10",
|
|
|
|
},
|
2015-09-14 21:56:51 +00:00
|
|
|
Spec: apitesting.DeepEqualSafePodSpec(),
|
2014-12-31 00:42:55 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
events := []watch.Event{
|
2016-06-15 20:21:10 +00:00
|
|
|
// current state events
|
|
|
|
{
|
|
|
|
Type: watch.Added,
|
|
|
|
Object: &api.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-06-15 20:21:10 +00:00
|
|
|
Name: "bar",
|
|
|
|
Namespace: "test",
|
|
|
|
ResourceVersion: "9",
|
|
|
|
},
|
|
|
|
Spec: apitesting.DeepEqualSafePodSpec(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: watch.Added,
|
|
|
|
Object: &api.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-06-15 20:21:10 +00:00
|
|
|
Name: "foo",
|
|
|
|
Namespace: "test",
|
|
|
|
ResourceVersion: "10",
|
|
|
|
},
|
|
|
|
Spec: apitesting.DeepEqualSafePodSpec(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// resource events
|
2014-12-31 00:42:55 +00:00
|
|
|
{
|
|
|
|
Type: watch.Modified,
|
|
|
|
Object: &api.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2014-12-31 00:42:55 +00:00
|
|
|
Name: "foo",
|
|
|
|
Namespace: "test",
|
|
|
|
ResourceVersion: "11",
|
|
|
|
},
|
2015-09-14 21:56:51 +00:00
|
|
|
Spec: apitesting.DeepEqualSafePodSpec(),
|
2014-12-31 00:42:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: watch.Deleted,
|
|
|
|
Object: &api.Pod{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2014-12-31 00:42:55 +00:00
|
|
|
Name: "foo",
|
|
|
|
Namespace: "test",
|
|
|
|
ResourceVersion: "12",
|
|
|
|
},
|
2015-09-14 21:56:51 +00:00
|
|
|
Spec: apitesting.DeepEqualSafePodSpec(),
|
2014-12-31 00:42:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return pods, events
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWatchSelector(t *testing.T) {
|
|
|
|
pods, events := watchTestData()
|
|
|
|
|
2016-11-02 16:29:01 +00:00
|
|
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
2014-12-31 00:42:55 +00:00
|
|
|
tf.Printer = &testPrinter{}
|
2016-06-15 20:21:10 +00:00
|
|
|
podList := &api.PodList{
|
|
|
|
Items: pods,
|
2016-12-03 18:57:26 +00:00
|
|
|
ListMeta: metav1.ListMeta{
|
2016-06-15 20:21:10 +00:00
|
|
|
ResourceVersion: "10",
|
|
|
|
},
|
|
|
|
}
|
2017-01-22 18:45:30 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-01-24 15:00:24 +00:00
|
|
|
APIRegistry: api.Registry,
|
2016-11-02 16:29:01 +00:00
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2015-11-11 19:54:58 +00:00
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
2017-01-12 18:17:43 +00:00
|
|
|
if req.URL.Query().Get(metav1.LabelSelectorQueryParam(api.Registry.GroupOrDie(api.GroupName).GroupVersion.String())) != "a=b" {
|
2014-12-31 00:42:55 +00:00
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
}
|
|
|
|
switch req.URL.Path {
|
2015-01-19 21:50:00 +00:00
|
|
|
case "/namespaces/test/pods":
|
2017-01-09 21:21:23 +00:00
|
|
|
if req.URL.Query().Get("watch") == "true" {
|
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: watchBody(codec, events[2:])}, nil
|
|
|
|
} else {
|
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, podList)}, nil
|
|
|
|
}
|
2014-12-31 00:42:55 +00:00
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
2015-01-02 18:08:37 +00:00
|
|
|
tf.Namespace = "test"
|
2014-12-31 00:42:55 +00:00
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-07-13 16:00:33 +00:00
|
|
|
errBuf := bytes.NewBuffer([]byte{})
|
2014-12-31 00:42:55 +00:00
|
|
|
|
2016-07-13 16:00:33 +00:00
|
|
|
cmd := NewCmdGet(f, buf, errBuf)
|
2014-12-31 00:42:55 +00:00
|
|
|
cmd.SetOutput(buf)
|
|
|
|
|
|
|
|
cmd.Flags().Set("watch", "true")
|
|
|
|
cmd.Flags().Set("selector", "a=b")
|
|
|
|
cmd.Run(cmd, []string{"pods"})
|
|
|
|
|
2017-02-28 22:45:09 +00:00
|
|
|
expected := []runtime.Object{&pods[0], &pods[1], events[2].Object, events[3].Object}
|
2016-11-02 16:29:01 +00:00
|
|
|
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
|
|
|
|
|
2014-12-31 00:42:55 +00:00
|
|
|
if len(buf.String()) == 0 {
|
|
|
|
t.Errorf("unexpected empty output")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWatchResource(t *testing.T) {
|
|
|
|
pods, events := watchTestData()
|
|
|
|
|
2016-11-02 16:29:01 +00:00
|
|
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
2014-12-31 00:42:55 +00:00
|
|
|
tf.Printer = &testPrinter{}
|
2017-01-22 18:45:30 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-01-24 15:00:24 +00:00
|
|
|
APIRegistry: api.Registry,
|
2016-11-02 16:29:01 +00:00
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2015-11-11 19:54:58 +00:00
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
2014-12-31 00:42:55 +00:00
|
|
|
switch req.URL.Path {
|
2015-01-19 21:50:00 +00:00
|
|
|
case "/namespaces/test/pods/foo":
|
2016-06-15 20:21:10 +00:00
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &pods[1])}, nil
|
2017-01-09 21:21:23 +00:00
|
|
|
case "/namespaces/test/pods":
|
|
|
|
if req.URL.Query().Get("watch") == "true" && req.URL.Query().Get("fieldSelector") == "metadata.name=foo" {
|
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: watchBody(codec, events[1:])}, nil
|
|
|
|
}
|
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
return nil, nil
|
2014-12-31 00:42:55 +00:00
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
2015-01-02 18:08:37 +00:00
|
|
|
tf.Namespace = "test"
|
2014-12-31 00:42:55 +00:00
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-07-13 16:00:33 +00:00
|
|
|
errBuf := bytes.NewBuffer([]byte{})
|
2014-12-31 00:42:55 +00:00
|
|
|
|
2016-07-13 16:00:33 +00:00
|
|
|
cmd := NewCmdGet(f, buf, errBuf)
|
2014-12-31 00:42:55 +00:00
|
|
|
cmd.SetOutput(buf)
|
|
|
|
|
|
|
|
cmd.Flags().Set("watch", "true")
|
|
|
|
cmd.Run(cmd, []string{"pods", "foo"})
|
|
|
|
|
2016-06-15 20:21:10 +00:00
|
|
|
expected := []runtime.Object{&pods[1], events[2].Object, events[3].Object}
|
2016-11-02 16:29:01 +00:00
|
|
|
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
|
|
|
|
|
2014-12-31 00:42:55 +00:00
|
|
|
if len(buf.String()) == 0 {
|
|
|
|
t.Errorf("unexpected empty output")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-10 11:02:14 +00:00
|
|
|
func TestWatchResourceIdentifiedByFile(t *testing.T) {
|
|
|
|
pods, events := watchTestData()
|
|
|
|
|
2016-11-02 16:29:01 +00:00
|
|
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
2015-08-10 11:02:14 +00:00
|
|
|
tf.Printer = &testPrinter{}
|
2017-01-22 18:45:30 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-01-24 15:00:24 +00:00
|
|
|
APIRegistry: api.Registry,
|
2016-11-02 16:29:01 +00:00
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2015-11-11 19:54:58 +00:00
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
2015-08-10 11:02:14 +00:00
|
|
|
switch req.URL.Path {
|
2016-03-10 17:26:39 +00:00
|
|
|
case "/namespaces/test/replicationcontrollers/cassandra":
|
2016-06-15 20:21:10 +00:00
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &pods[1])}, nil
|
2017-01-09 21:21:23 +00:00
|
|
|
case "/namespaces/test/replicationcontrollers":
|
|
|
|
if req.URL.Query().Get("watch") == "true" && req.URL.Query().Get("fieldSelector") == "metadata.name=cassandra" {
|
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: watchBody(codec, events[1:])}, nil
|
|
|
|
}
|
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
return nil, nil
|
2015-08-10 11:02:14 +00:00
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
tf.Namespace = "test"
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-07-13 16:00:33 +00:00
|
|
|
errBuf := bytes.NewBuffer([]byte{})
|
|
|
|
|
|
|
|
cmd := NewCmdGet(f, buf, errBuf)
|
2015-08-10 11:02:14 +00:00
|
|
|
cmd.SetOutput(buf)
|
|
|
|
|
|
|
|
cmd.Flags().Set("watch", "true")
|
2016-06-21 22:43:29 +00:00
|
|
|
cmd.Flags().Set("filename", "../../../examples/storage/cassandra/cassandra-controller.yaml")
|
2015-08-10 11:02:14 +00:00
|
|
|
cmd.Run(cmd, []string{})
|
|
|
|
|
2016-06-15 20:21:10 +00:00
|
|
|
expected := []runtime.Object{&pods[1], events[2].Object, events[3].Object}
|
2016-11-02 16:29:01 +00:00
|
|
|
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
|
2015-08-10 11:02:14 +00:00
|
|
|
|
|
|
|
if len(buf.String()) == 0 {
|
|
|
|
t.Errorf("unexpected empty output")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-31 00:42:55 +00:00
|
|
|
func TestWatchOnlyResource(t *testing.T) {
|
|
|
|
pods, events := watchTestData()
|
|
|
|
|
2016-11-02 16:29:01 +00:00
|
|
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
2014-12-31 00:42:55 +00:00
|
|
|
tf.Printer = &testPrinter{}
|
2017-01-22 18:45:30 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-01-24 15:00:24 +00:00
|
|
|
APIRegistry: api.Registry,
|
2016-11-02 16:29:01 +00:00
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2015-11-11 19:54:58 +00:00
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
2014-12-31 00:42:55 +00:00
|
|
|
switch req.URL.Path {
|
2015-01-19 21:50:00 +00:00
|
|
|
case "/namespaces/test/pods/foo":
|
2016-06-15 20:21:10 +00:00
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, &pods[1])}, nil
|
2017-01-09 21:21:23 +00:00
|
|
|
case "/namespaces/test/pods":
|
|
|
|
if req.URL.Query().Get("watch") == "true" && req.URL.Query().Get("fieldSelector") == "metadata.name=foo" {
|
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: watchBody(codec, events[1:])}, nil
|
|
|
|
}
|
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
return nil, nil
|
2014-12-31 00:42:55 +00:00
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
2015-01-02 18:08:37 +00:00
|
|
|
tf.Namespace = "test"
|
2014-12-31 00:42:55 +00:00
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-07-13 16:00:33 +00:00
|
|
|
errBuf := bytes.NewBuffer([]byte{})
|
2014-12-31 00:42:55 +00:00
|
|
|
|
2016-07-13 16:00:33 +00:00
|
|
|
cmd := NewCmdGet(f, buf, errBuf)
|
2014-12-31 00:42:55 +00:00
|
|
|
cmd.SetOutput(buf)
|
|
|
|
|
|
|
|
cmd.Flags().Set("watch-only", "true")
|
|
|
|
cmd.Run(cmd, []string{"pods", "foo"})
|
|
|
|
|
2016-06-15 20:21:10 +00:00
|
|
|
expected := []runtime.Object{events[2].Object, events[3].Object}
|
2016-11-02 16:29:01 +00:00
|
|
|
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
|
|
|
|
|
2016-06-15 20:21:10 +00:00
|
|
|
if len(buf.String()) == 0 {
|
|
|
|
t.Errorf("unexpected empty output")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWatchOnlyList(t *testing.T) {
|
|
|
|
pods, events := watchTestData()
|
|
|
|
|
2016-11-02 16:29:01 +00:00
|
|
|
f, tf, codec, _ := cmdtesting.NewAPIFactory()
|
2016-06-15 20:21:10 +00:00
|
|
|
tf.Printer = &testPrinter{}
|
|
|
|
podList := &api.PodList{
|
|
|
|
Items: pods,
|
2016-12-03 18:57:26 +00:00
|
|
|
ListMeta: metav1.ListMeta{
|
2016-06-15 20:21:10 +00:00
|
|
|
ResourceVersion: "10",
|
|
|
|
},
|
|
|
|
}
|
2017-01-22 18:45:30 +00:00
|
|
|
tf.UnstructuredClient = &fake.RESTClient{
|
2017-01-24 15:00:24 +00:00
|
|
|
APIRegistry: api.Registry,
|
2016-11-02 16:29:01 +00:00
|
|
|
NegotiatedSerializer: unstructuredSerializer,
|
2016-06-15 20:21:10 +00:00
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
|
|
|
switch req.URL.Path {
|
|
|
|
case "/namespaces/test/pods":
|
2017-01-09 21:21:23 +00:00
|
|
|
if req.URL.Query().Get("watch") == "true" {
|
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: watchBody(codec, events[2:])}, nil
|
|
|
|
} else {
|
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, podList)}, nil
|
|
|
|
}
|
2016-06-15 20:21:10 +00:00
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
tf.Namespace = "test"
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-07-13 16:00:33 +00:00
|
|
|
errBuf := bytes.NewBuffer([]byte{})
|
2016-06-15 20:21:10 +00:00
|
|
|
|
2016-07-13 16:00:33 +00:00
|
|
|
cmd := NewCmdGet(f, buf, errBuf)
|
2016-06-15 20:21:10 +00:00
|
|
|
cmd.SetOutput(buf)
|
|
|
|
|
|
|
|
cmd.Flags().Set("watch-only", "true")
|
|
|
|
cmd.Run(cmd, []string{"pods"})
|
|
|
|
|
|
|
|
expected := []runtime.Object{events[2].Object, events[3].Object}
|
2016-11-02 16:29:01 +00:00
|
|
|
verifyObjects(t, expected, tf.Printer.(*testPrinter).Objects)
|
|
|
|
|
2014-12-31 00:42:55 +00:00
|
|
|
if len(buf.String()) == 0 {
|
|
|
|
t.Errorf("unexpected empty output")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func watchBody(codec runtime.Codec, events []watch.Event) io.ReadCloser {
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2016-12-11 20:59:34 +00:00
|
|
|
enc := restclientwatch.NewEncoder(streaming.NewEncoder(buf, codec), codec)
|
2014-12-31 00:42:55 +00:00
|
|
|
for i := range events {
|
|
|
|
enc.Encode(&events[i])
|
|
|
|
}
|
2016-04-26 07:05:40 +00:00
|
|
|
return json.Framer.NewFrameReader(ioutil.NopCloser(buf))
|
2014-12-31 00:42:55 +00:00
|
|
|
}
|