2014-10-06 01:24:19 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package kubectl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2014-12-16 22:20:51 +00:00
|
|
|
"time"
|
2014-10-06 01:24:19 +00:00
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-11-18 18:04:10 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2014-11-11 23:29:01 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
|
|
|
|
2014-12-01 04:12:37 +00:00
|
|
|
"github.com/ghodss/yaml"
|
2014-10-06 01:24:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type testStruct struct {
|
2014-12-01 05:31:52 +00:00
|
|
|
api.TypeMeta `json:",inline"`
|
|
|
|
api.ObjectMeta `json:"metadata,omitempty"`
|
|
|
|
Key string `json:"Key"`
|
|
|
|
Map map[string]int `json:"Map"`
|
|
|
|
StringList []string `json:"StringList"`
|
|
|
|
IntList []int `json:"IntList"`
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *testStruct) IsAnAPIObject() {}
|
|
|
|
|
2014-11-11 23:29:01 +00:00
|
|
|
func init() {
|
|
|
|
api.Scheme.AddKnownTypes("", &testStruct{})
|
2014-11-18 18:04:10 +00:00
|
|
|
api.Scheme.AddKnownTypes(testapi.Version(), &testStruct{})
|
2014-11-11 23:29:01 +00:00
|
|
|
}
|
|
|
|
|
2014-10-06 01:24:19 +00:00
|
|
|
var testData = testStruct{
|
2014-11-11 23:29:01 +00:00
|
|
|
Key: "testValue",
|
|
|
|
Map: map[string]int{"TestSubkey": 1},
|
|
|
|
StringList: []string{"a", "b", "c"},
|
|
|
|
IntList: []int{1, 2, 3},
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestYAMLPrinter(t *testing.T) {
|
2014-11-18 18:04:10 +00:00
|
|
|
testPrinter(t, &YAMLPrinter{testapi.Version(), api.Scheme}, yaml.Unmarshal)
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestJSONPrinter(t *testing.T) {
|
2014-11-18 18:04:10 +00:00
|
|
|
testPrinter(t, &JSONPrinter{testapi.Version(), api.Scheme}, json.Unmarshal)
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 18:04:10 +00:00
|
|
|
type internalType struct {
|
|
|
|
Name string
|
|
|
|
Kind string
|
|
|
|
}
|
|
|
|
|
|
|
|
type externalType struct {
|
|
|
|
Name string
|
|
|
|
Kind string `json:"kind"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*internalType) IsAnAPIObject() {}
|
|
|
|
func (*externalType) IsAnAPIObject() {}
|
|
|
|
|
|
|
|
func newExternalScheme() *runtime.Scheme {
|
|
|
|
scheme := runtime.NewScheme()
|
|
|
|
scheme.AddKnownTypeWithName("", "Type", &internalType{})
|
|
|
|
scheme.AddKnownTypeWithName("unlikelyversion", "Type", &externalType{})
|
|
|
|
return scheme
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintJSONForUnknownSchema(t *testing.T) {
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
|
|
|
printer, err := GetPrinter("json", "", "unlikelyversion", newExternalScheme(), nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %#v", err)
|
|
|
|
}
|
|
|
|
if err := printer.PrintObj(&internalType{Name: "foo"}, buf); err != nil {
|
|
|
|
t.Fatalf("unexpected error: %#v", err)
|
|
|
|
}
|
|
|
|
obj := map[string]interface{}{}
|
|
|
|
if err := json.Unmarshal(buf.Bytes(), &obj); err != nil {
|
|
|
|
t.Fatalf("unexpected error: %#v\n%s", err, buf.String())
|
|
|
|
}
|
|
|
|
if obj["Name"] != "foo" {
|
|
|
|
t.Errorf("unexpected field: %#v", obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintJSONForUnknownSchemaAndWrongVersion(t *testing.T) {
|
2014-10-30 02:32:25 +00:00
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2014-11-18 18:04:10 +00:00
|
|
|
printer, err := GetPrinter("json", "", "badversion", newExternalScheme(), nil)
|
2014-11-01 22:44:03 +00:00
|
|
|
if err != nil {
|
2014-11-18 18:04:10 +00:00
|
|
|
t.Fatalf("unexpected error: %#v", err)
|
2014-10-30 02:32:25 +00:00
|
|
|
}
|
2014-11-18 18:04:10 +00:00
|
|
|
if err := printer.PrintObj(&internalType{Name: "foo"}, buf); err == nil {
|
|
|
|
t.Errorf("unexpected non-error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintJSON(t *testing.T) {
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
|
|
|
printer, err := GetPrinter("json", "", testapi.Version(), api.Scheme, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %#v", err)
|
2014-11-01 22:44:03 +00:00
|
|
|
}
|
|
|
|
printer.PrintObj(&api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, buf)
|
2014-10-30 02:32:25 +00:00
|
|
|
obj := map[string]interface{}{}
|
|
|
|
if err := json.Unmarshal(buf.Bytes(), &obj); err != nil {
|
|
|
|
t.Errorf("unexpected error: %#v\n%s", err, buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintYAML(t *testing.T) {
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2014-11-18 18:04:10 +00:00
|
|
|
printer, err := GetPrinter("yaml", "", testapi.Version(), api.Scheme, nil)
|
2014-11-01 22:44:03 +00:00
|
|
|
if err != nil {
|
2014-11-18 18:04:10 +00:00
|
|
|
t.Fatalf("unexpected error: %#v", err)
|
2014-11-01 22:44:03 +00:00
|
|
|
}
|
|
|
|
printer.PrintObj(&api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, buf)
|
2014-10-30 02:32:25 +00:00
|
|
|
obj := map[string]interface{}{}
|
|
|
|
if err := yaml.Unmarshal(buf.Bytes(), &obj); err != nil {
|
|
|
|
t.Errorf("unexpected error: %#v\n%s", err, buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintTemplate(t *testing.T) {
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2014-11-18 18:04:10 +00:00
|
|
|
printer, err := GetPrinter("template", "{{.id}}", "v1beta1", api.Scheme, nil)
|
2014-11-01 22:44:03 +00:00
|
|
|
if err != nil {
|
2014-11-07 22:41:59 +00:00
|
|
|
t.Fatalf("unexpected error: %#v", err)
|
2014-10-30 02:32:25 +00:00
|
|
|
}
|
2014-11-07 22:41:59 +00:00
|
|
|
err = printer.PrintObj(&api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %#v", err)
|
|
|
|
}
|
2014-10-30 02:32:25 +00:00
|
|
|
if buf.String() != "foo" {
|
|
|
|
t.Errorf("unexpected output: %s", buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintEmptyTemplate(t *testing.T) {
|
2014-11-18 18:04:10 +00:00
|
|
|
if _, err := GetPrinter("template", "", testapi.Version(), api.Scheme, nil); err == nil {
|
2014-10-30 02:32:25 +00:00
|
|
|
t.Errorf("unexpected non-error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBadTemplate(t *testing.T) {
|
2014-11-18 18:04:10 +00:00
|
|
|
if _, err := GetPrinter("template", "{{ .Name", testapi.Version(), api.Scheme, nil); err == nil {
|
2014-10-30 02:32:25 +00:00
|
|
|
t.Errorf("unexpected non-error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBadTemplateFile(t *testing.T) {
|
2014-11-18 18:04:10 +00:00
|
|
|
if _, err := GetPrinter("templatefile", "", testapi.Version(), api.Scheme, nil); err == nil {
|
2014-10-30 02:32:25 +00:00
|
|
|
t.Errorf("unexpected non-error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-06 01:24:19 +00:00
|
|
|
func testPrinter(t *testing.T, printer ResourcePrinter, unmarshalFunc func(data []byte, v interface{}) error) {
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
|
|
|
|
|
|
|
err := printer.PrintObj(&testData, buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var poutput testStruct
|
2014-11-11 23:29:01 +00:00
|
|
|
// Verify that given function runs without error.
|
|
|
|
err = unmarshalFunc(buf.Bytes(), &poutput)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Use real decode function to undo the versioning process.
|
|
|
|
poutput = testStruct{}
|
2014-11-18 18:04:10 +00:00
|
|
|
err = testapi.Codec().DecodeInto(buf.Bytes(), &poutput)
|
2014-10-06 01:24:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(testData, poutput) {
|
2014-11-11 23:29:01 +00:00
|
|
|
t.Errorf("Test data and unmarshaled data are not equal: %v", util.ObjectDiff(poutput, testData))
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
obj := &api.Pod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
buf.Reset()
|
|
|
|
printer.PrintObj(obj, buf)
|
|
|
|
var objOut api.Pod
|
2014-11-11 23:29:01 +00:00
|
|
|
// Verify that given function runs without error.
|
|
|
|
err = unmarshalFunc(buf.Bytes(), &objOut)
|
2014-10-06 01:24:19 +00:00
|
|
|
if err != nil {
|
2014-11-18 18:04:10 +00:00
|
|
|
t.Fatalf("unexpected error: %#v", err)
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
2014-11-11 23:29:01 +00:00
|
|
|
// Use real decode function to undo the versioning process.
|
|
|
|
objOut = api.Pod{}
|
2014-11-18 18:04:10 +00:00
|
|
|
err = testapi.Codec().DecodeInto(buf.Bytes(), &objOut)
|
2014-11-11 23:29:01 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-10-06 01:24:19 +00:00
|
|
|
if !reflect.DeepEqual(obj, &objOut) {
|
2014-11-11 23:29:01 +00:00
|
|
|
t.Errorf("Unexpected inequality:\n%v", util.ObjectDiff(obj, &objOut))
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestPrintType struct {
|
|
|
|
Data string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*TestPrintType) IsAnAPIObject() {}
|
|
|
|
|
|
|
|
type TestUnknownType struct{}
|
|
|
|
|
|
|
|
func (*TestUnknownType) IsAnAPIObject() {}
|
|
|
|
|
|
|
|
func PrintCustomType(obj *TestPrintType, w io.Writer) error {
|
|
|
|
_, err := fmt.Fprintf(w, "%s", obj.Data)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func ErrorPrintHandler(obj *TestPrintType, w io.Writer) error {
|
|
|
|
return fmt.Errorf("ErrorPrintHandler error")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCustomTypePrinting(t *testing.T) {
|
|
|
|
columns := []string{"Data"}
|
2014-10-16 01:54:46 +00:00
|
|
|
printer := NewHumanReadablePrinter(false)
|
2014-10-06 01:24:19 +00:00
|
|
|
printer.Handler(columns, PrintCustomType)
|
|
|
|
|
|
|
|
obj := TestPrintType{"test object"}
|
|
|
|
buffer := &bytes.Buffer{}
|
|
|
|
err := printer.PrintObj(&obj, buffer)
|
|
|
|
if err != nil {
|
2014-11-18 18:04:10 +00:00
|
|
|
t.Fatalf("An error occurred printing the custom type: %#v", err)
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
expectedOutput := "Data\ntest object"
|
|
|
|
if buffer.String() != expectedOutput {
|
|
|
|
t.Errorf("The data was not printed as expected. Expected:\n%s\nGot:\n%s", expectedOutput, buffer.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintHandlerError(t *testing.T) {
|
|
|
|
columns := []string{"Data"}
|
2014-10-16 01:54:46 +00:00
|
|
|
printer := NewHumanReadablePrinter(false)
|
2014-10-06 01:24:19 +00:00
|
|
|
printer.Handler(columns, ErrorPrintHandler)
|
|
|
|
obj := TestPrintType{"test object"}
|
|
|
|
buffer := &bytes.Buffer{}
|
|
|
|
err := printer.PrintObj(&obj, buffer)
|
|
|
|
if err == nil || err.Error() != "ErrorPrintHandler error" {
|
|
|
|
t.Errorf("Did not get the expected error: %#v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnknownTypePrinting(t *testing.T) {
|
2014-10-16 01:54:46 +00:00
|
|
|
printer := NewHumanReadablePrinter(false)
|
2014-10-06 01:24:19 +00:00
|
|
|
buffer := &bytes.Buffer{}
|
|
|
|
err := printer.PrintObj(&TestUnknownType{}, buffer)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("An error was expected from printing unknown type")
|
|
|
|
}
|
|
|
|
}
|
2014-11-07 22:41:59 +00:00
|
|
|
|
|
|
|
func TestTemplateEmitsVersionedObjects(t *testing.T) {
|
|
|
|
// kind is always blank in memory and set on the wire
|
2014-11-18 18:04:10 +00:00
|
|
|
printer, err := NewTemplatePrinter([]byte(`{{.kind}}`), testapi.Version(), api.Scheme)
|
2014-11-07 22:41:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("tmpl fail: %v", err)
|
|
|
|
}
|
|
|
|
buffer := &bytes.Buffer{}
|
|
|
|
err = printer.PrintObj(&api.Pod{}, buffer)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("print fail: %v", err)
|
|
|
|
}
|
|
|
|
if e, a := "Pod", string(buffer.Bytes()); e != a {
|
|
|
|
t.Errorf("Expected %v, got %v", e, a)
|
|
|
|
}
|
|
|
|
}
|
2014-11-20 22:09:59 +00:00
|
|
|
|
2014-12-23 19:21:38 +00:00
|
|
|
func TestTemplatePanic(t *testing.T) {
|
|
|
|
tmpl := `{{and ((index .currentState.info "update-demo").state.running.startedAt) .currentState.info.net.state.running.startedAt}}`
|
|
|
|
// kind is always blank in memory and set on the wire
|
|
|
|
printer, err := NewTemplatePrinter([]byte(tmpl), testapi.Version(), api.Scheme)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("tmpl fail: %v", err)
|
|
|
|
}
|
|
|
|
buffer := &bytes.Buffer{}
|
|
|
|
err = printer.PrintObj(&api.Pod{}, buffer)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected that template to crash")
|
|
|
|
}
|
|
|
|
if buffer.String() == "" {
|
|
|
|
t.Errorf("no debugging info was printed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-20 22:09:59 +00:00
|
|
|
func TestPrinters(t *testing.T) {
|
|
|
|
om := func(name string) api.ObjectMeta { return api.ObjectMeta{Name: name} }
|
|
|
|
templatePrinter, err := NewTemplatePrinter([]byte("{{.name}}"), testapi.Version(), api.Scheme)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
templatePrinter2, err := NewTemplatePrinter([]byte("{{len .items}}"), testapi.Version(), api.Scheme)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
printers := map[string]ResourcePrinter{
|
|
|
|
"humanReadable": NewHumanReadablePrinter(true),
|
|
|
|
"humanReadableHeaders": NewHumanReadablePrinter(false),
|
|
|
|
"json": &JSONPrinter{testapi.Version(), api.Scheme},
|
|
|
|
"yaml": &YAMLPrinter{testapi.Version(), api.Scheme},
|
|
|
|
"template": templatePrinter,
|
|
|
|
"template2": templatePrinter2,
|
|
|
|
}
|
|
|
|
objects := map[string]runtime.Object{
|
|
|
|
"pod": &api.Pod{ObjectMeta: om("pod")},
|
|
|
|
"emptyPodList": &api.PodList{},
|
|
|
|
"nonEmptyPodList": &api.PodList{Items: []api.Pod{{}}},
|
|
|
|
}
|
|
|
|
// map of printer name to set of objects it should fail on.
|
|
|
|
expectedErrors := map[string]util.StringSet{
|
|
|
|
"template2": util.NewStringSet("pod", "emptyPodList"),
|
|
|
|
}
|
|
|
|
|
|
|
|
for pName, p := range printers {
|
|
|
|
for oName, obj := range objects {
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
if err := p.PrintObj(obj, b); err != nil {
|
|
|
|
if set, found := expectedErrors[pName]; found && set.Has(oName) {
|
|
|
|
// expected error
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.Errorf("printer '%v', object '%v'; error: '%v'", pName, oName, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-16 22:20:51 +00:00
|
|
|
|
|
|
|
func TestPrintEventsResultSorted(t *testing.T) {
|
|
|
|
// Arrange
|
|
|
|
printer := NewHumanReadablePrinter(false /* noHeaders */)
|
|
|
|
|
|
|
|
obj := api.EventList{
|
|
|
|
Items: []api.Event{
|
|
|
|
{
|
|
|
|
Source: "kubelet",
|
|
|
|
Message: "Item 1",
|
|
|
|
Timestamp: util.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Source: "scheduler",
|
|
|
|
Message: "Item 2",
|
|
|
|
Timestamp: util.NewTime(time.Date(1987, time.June, 17, 0, 0, 0, 0, time.UTC)),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Source: "kubelet",
|
|
|
|
Message: "Item 3",
|
|
|
|
Timestamp: util.NewTime(time.Date(2002, time.December, 25, 0, 0, 0, 0, time.UTC)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buffer := &bytes.Buffer{}
|
|
|
|
|
|
|
|
// Act
|
|
|
|
err := printer.PrintObj(&obj, buffer)
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("An error occurred printing the EventList: %#v", err)
|
|
|
|
}
|
|
|
|
out := buffer.String()
|
|
|
|
VerifyDatesInOrder(out, "\n" /* rowDelimiter */, " " /* columnDelimiter */, t)
|
|
|
|
}
|