List output with differing types should be more resilient
kubectl get can output a series of objects as a List in versioned form, but not all API objects are available in the same schema. Make the act of converting a []runtime.Object to api.List more robust and add a test to verify its behavior in Get. Makes it easier for client code to output unified objects.
This commit is contained in:
@@ -18,6 +18,7 @@ package cmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
encjson "encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
@@ -138,6 +139,84 @@ func TestGetUnknownSchemaObject(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Verifies that schemas that are not in the master tree of Kubernetes can be retrieved via Get.
|
||||
// Because api.List is part of the Kube API, resource.Builder has to perform a conversion on
|
||||
// api.Scheme, which may not have access to all objects, and not all objects are at the same
|
||||
// internal versioning scheme. This test verifies that two isolated schemes (Test, and api.Scheme)
|
||||
// can be conjoined into a single output object.
|
||||
func TestGetUnknownSchemaObjectListGeneric(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
output string
|
||||
list string
|
||||
obj1 string
|
||||
obj2 string
|
||||
}{
|
||||
"handles specific version": {
|
||||
output: "v1beta3",
|
||||
list: "v1beta3",
|
||||
obj1: "unlikelyversion",
|
||||
obj2: "v1beta3",
|
||||
},
|
||||
"handles second specific version": {
|
||||
output: "unlikelyversion",
|
||||
list: "v1beta3",
|
||||
obj1: "unlikelyversion", // doesn't have v1beta3
|
||||
obj2: "v1beta1", // version of the API response
|
||||
},
|
||||
"handles common version": {
|
||||
output: "v1beta1",
|
||||
list: "v1beta1",
|
||||
obj1: "unlikelyversion", // because test scheme defaults to unlikelyversion
|
||||
obj2: "v1beta1",
|
||||
},
|
||||
}
|
||||
for k, test := range testCases {
|
||||
apiCodec := runtime.CodecFor(api.Scheme, "v1beta1")
|
||||
regularClient := &client.FakeRESTClient{
|
||||
Codec: apiCodec,
|
||||
Client: client.HTTPClientFunc(func(req *http.Request) (*http.Response, error) {
|
||||
return &http.Response{StatusCode: 200, Body: objBody(apiCodec, &api.ReplicationController{ObjectMeta: api.ObjectMeta{Name: "foo"}})}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
f, tf, codec := NewMixedFactory(regularClient)
|
||||
tf.Printer = &testPrinter{}
|
||||
tf.Client = &client.FakeRESTClient{
|
||||
Codec: codec,
|
||||
Client: client.HTTPClientFunc(func(req *http.Request) (*http.Response, error) {
|
||||
return &http.Response{StatusCode: 200, Body: objBody(codec, &internalType{Name: "foo"})}, nil
|
||||
}),
|
||||
}
|
||||
tf.Namespace = "test"
|
||||
tf.ClientConfig = &client.Config{Version: latest.Version}
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
cmd := NewCmdGet(f, buf)
|
||||
cmd.SetOutput(buf)
|
||||
cmd.Flags().Set("output", "json")
|
||||
cmd.Flags().Set("output-version", test.output)
|
||||
err := RunGet(f, buf, cmd, []string{"type/foo", "replicationcontrollers/foo"})
|
||||
if err != nil {
|
||||
t.Errorf("%s: unexpected error: %v", k, err)
|
||||
continue
|
||||
}
|
||||
out := make(map[string]interface{})
|
||||
if err := encjson.Unmarshal(buf.Bytes(), &out); err != nil {
|
||||
t.Errorf("%s: unexpected error: %v\n%s", k, err, buf.String())
|
||||
continue
|
||||
}
|
||||
if out["apiVersion"] != test.list {
|
||||
t.Errorf("%s: unexpected list: %#v", k, out)
|
||||
}
|
||||
arr := out["items"].([]interface{})
|
||||
if arr[0].(map[string]interface{})["apiVersion"] != test.obj1 {
|
||||
t.Errorf("%s: unexpected list: %#v", k, out)
|
||||
}
|
||||
if arr[1].(map[string]interface{})["apiVersion"] != test.obj2 {
|
||||
t.Errorf("%s: unexpected list: %#v", k, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Verifies that schemas that are not in the master tree of Kubernetes can be retrieved via Get.
|
||||
func TestGetSchemaObject(t *testing.T) {
|
||||
f, tf, _ := NewTestFactory()
|
||||
|
Reference in New Issue
Block a user