2014-10-06 01:24:19 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-10-06 01:24:19 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package kubectl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"reflect"
|
2015-01-15 01:11:34 +00:00
|
|
|
"strings"
|
2014-10-06 01:24:19 +00:00
|
|
|
"testing"
|
2014-12-16 22:20:51 +00:00
|
|
|
"time"
|
2014-10-06 01:24:19 +00:00
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
"k8s.io/kubernetes/pkg/api/testapi"
|
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
|
|
|
"k8s.io/kubernetes/pkg/util"
|
2014-11-11 23:29:01 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-12-31 00:42:55 +00:00
|
|
|
func TestVersionedPrinter(t *testing.T) {
|
|
|
|
original := &testStruct{Key: "value"}
|
|
|
|
p := NewVersionedPrinter(
|
|
|
|
ResourcePrinterFunc(func(obj runtime.Object, w io.Writer) error {
|
|
|
|
if obj == original {
|
|
|
|
t.Fatalf("object should not be identical: %#v", obj)
|
|
|
|
}
|
|
|
|
if obj.(*testStruct).Key != "value" {
|
|
|
|
t.Fatalf("object was not converted: %#v", obj)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}),
|
|
|
|
api.Scheme,
|
|
|
|
testapi.Version(),
|
|
|
|
)
|
|
|
|
if err := p.PrintObj(original, nil); err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-06 01:24:19 +00:00
|
|
|
func TestYAMLPrinter(t *testing.T) {
|
2014-12-31 00:42:55 +00:00
|
|
|
testPrinter(t, &YAMLPrinter{}, yaml.Unmarshal)
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestJSONPrinter(t *testing.T) {
|
2014-12-31 00:42:55 +00:00
|
|
|
testPrinter(t, &JSONPrinter{}, json.Unmarshal)
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
2014-12-31 00:42:55 +00:00
|
|
|
func TestPrintDefault(t *testing.T) {
|
|
|
|
printer, found, err := GetPrinter("", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %#v", err)
|
|
|
|
}
|
|
|
|
if found {
|
|
|
|
t.Errorf("no printer should have been found: %#v / %v", printer, err)
|
|
|
|
}
|
2014-11-18 18:04:10 +00:00
|
|
|
}
|
|
|
|
|
2014-12-31 00:42:55 +00:00
|
|
|
type internalType struct {
|
2014-11-18 18:04:10 +00:00
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2014-12-31 00:42:55 +00:00
|
|
|
func (*internalType) IsAnAPIObject() {
|
2014-11-18 18:04:10 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-12-31 00:42:55 +00:00
|
|
|
func TestPrintJSONForObject(t *testing.T) {
|
2014-11-18 18:04:10 +00:00
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2014-12-31 00:42:55 +00:00
|
|
|
printer, found, err := GetPrinter("json", "")
|
|
|
|
if err != nil || !found {
|
2014-11-18 18:04:10 +00:00
|
|
|
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 TestPrintJSON(t *testing.T) {
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
2014-12-31 00:42:55 +00:00
|
|
|
printer, found, err := GetPrinter("json", "")
|
|
|
|
if err != nil || !found {
|
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 := 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-12-31 00:42:55 +00:00
|
|
|
printer, found, err := GetPrinter("yaml", "")
|
|
|
|
if err != nil || !found {
|
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{})
|
2015-04-16 20:34:42 +00:00
|
|
|
printer, found, err := GetPrinter("template", "{{if .id}}{{.id}}{{end}}{{if .metadata.name}}{{.metadata.name}}{{end}}")
|
2014-12-31 00:42:55 +00:00
|
|
|
if err != nil || !found {
|
2014-11-07 22:41:59 +00:00
|
|
|
t.Fatalf("unexpected error: %#v", err)
|
2014-10-30 02:32:25 +00:00
|
|
|
}
|
2015-04-16 20:34:42 +00:00
|
|
|
unversionedPod := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
|
|
|
|
obj, err := api.Scheme.ConvertToVersion(unversionedPod, testapi.Version())
|
|
|
|
err = printer.PrintObj(obj, buf)
|
2014-11-07 22:41:59 +00:00
|
|
|
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-12-31 00:42:55 +00:00
|
|
|
if _, _, err := GetPrinter("template", ""); err == nil {
|
2014-10-30 02:32:25 +00:00
|
|
|
t.Errorf("unexpected non-error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBadTemplate(t *testing.T) {
|
2014-12-31 00:42:55 +00:00
|
|
|
if _, _, err := GetPrinter("template", "{{ .Name"); err == nil {
|
2014-10-30 02:32:25 +00:00
|
|
|
t.Errorf("unexpected non-error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrintBadTemplateFile(t *testing.T) {
|
2014-12-31 00:42:55 +00:00
|
|
|
if _, _, err := GetPrinter("templatefile", ""); 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{}
|
2015-03-17 03:43:59 +00:00
|
|
|
err = runtime.YAMLDecoder(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{}
|
2015-03-17 03:43:59 +00:00
|
|
|
err = runtime.YAMLDecoder(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() {}
|
|
|
|
|
2015-06-29 18:36:06 +00:00
|
|
|
func PrintCustomType(obj *TestPrintType, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
|
2014-10-06 01:24:19 +00:00
|
|
|
_, err := fmt.Fprintf(w, "%s", obj.Data)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-29 18:36:06 +00:00
|
|
|
func ErrorPrintHandler(obj *TestPrintType, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error {
|
2014-10-06 01:24:19 +00:00
|
|
|
return fmt.Errorf("ErrorPrintHandler error")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCustomTypePrinting(t *testing.T) {
|
|
|
|
columns := []string{"Data"}
|
2015-06-29 18:36:06 +00:00
|
|
|
printer := NewHumanReadablePrinter(false, false, false, []string{})
|
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"}
|
2015-06-29 18:36:06 +00:00
|
|
|
printer := NewHumanReadablePrinter(false, false, false, []string{})
|
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) {
|
2015-06-29 18:36:06 +00:00
|
|
|
printer := NewHumanReadablePrinter(false, false, false, []string{})
|
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-12-31 00:42:55 +00:00
|
|
|
printer, err := NewTemplatePrinter([]byte(`{{.kind}}`))
|
2014-11-07 22:41:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("tmpl fail: %v", err)
|
|
|
|
}
|
2015-04-16 20:34:42 +00:00
|
|
|
obj, err := api.Scheme.ConvertToVersion(&api.Pod{}, testapi.Version())
|
2014-12-31 00:42:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-11-07 22:41:59 +00:00
|
|
|
buffer := &bytes.Buffer{}
|
2014-12-31 00:42:55 +00:00
|
|
|
err = printer.PrintObj(obj, buffer)
|
2014-11-07 22:41:59 +00:00
|
|
|
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) {
|
2015-01-28 18:13:02 +00:00
|
|
|
tmpl := `{{and ((index .currentState.info "foo").state.running.startedAt) .currentState.info.net.state.running.startedAt}}`
|
2014-12-31 00:42:55 +00:00
|
|
|
printer, err := NewTemplatePrinter([]byte(tmpl))
|
2014-12-23 19:21:38 +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("expected that template to crash")
|
|
|
|
}
|
|
|
|
if buffer.String() == "" {
|
|
|
|
t.Errorf("no debugging info was printed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-23 22:05:26 +00:00
|
|
|
func TestTemplateStrings(t *testing.T) {
|
|
|
|
// This unit tests the "exists" function as well as the template from update.sh
|
|
|
|
table := map[string]struct {
|
|
|
|
pod api.Pod
|
|
|
|
expect string
|
|
|
|
}{
|
|
|
|
"nilInfo": {api.Pod{}, "false"},
|
2015-03-25 11:09:35 +00:00
|
|
|
"emptyInfo": {api.Pod{Status: api.PodStatus{ContainerStatuses: []api.ContainerStatus{}}}, "false"},
|
2015-01-28 18:13:02 +00:00
|
|
|
"fooExists": {
|
2014-12-23 22:05:26 +00:00
|
|
|
api.Pod{
|
|
|
|
Status: api.PodStatus{
|
2015-03-25 11:09:35 +00:00
|
|
|
ContainerStatuses: []api.ContainerStatus{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
},
|
2014-12-23 22:05:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"false",
|
|
|
|
},
|
2015-01-28 18:13:02 +00:00
|
|
|
"barExists": {
|
2014-12-23 22:05:26 +00:00
|
|
|
api.Pod{
|
|
|
|
Status: api.PodStatus{
|
2015-03-25 11:09:35 +00:00
|
|
|
ContainerStatuses: []api.ContainerStatus{
|
|
|
|
{
|
|
|
|
Name: "bar",
|
|
|
|
},
|
|
|
|
},
|
2014-12-23 22:05:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"false",
|
|
|
|
},
|
|
|
|
"bothExist": {
|
|
|
|
api.Pod{
|
|
|
|
Status: api.PodStatus{
|
2015-03-25 11:09:35 +00:00
|
|
|
ContainerStatuses: []api.ContainerStatus{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "bar",
|
|
|
|
},
|
2014-12-23 22:05:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"false",
|
|
|
|
},
|
2015-04-08 17:22:33 +00:00
|
|
|
"barValid": {
|
2014-12-23 22:05:26 +00:00
|
|
|
api.Pod{
|
|
|
|
Status: api.PodStatus{
|
2015-03-25 11:09:35 +00:00
|
|
|
ContainerStatuses: []api.ContainerStatus{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "bar",
|
2014-12-23 22:05:26 +00:00
|
|
|
State: api.ContainerState{
|
|
|
|
Running: &api.ContainerStateRunning{
|
|
|
|
StartedAt: util.Time{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"false",
|
|
|
|
},
|
|
|
|
"bothValid": {
|
|
|
|
api.Pod{
|
|
|
|
Status: api.PodStatus{
|
2015-03-25 11:09:35 +00:00
|
|
|
ContainerStatuses: []api.ContainerStatus{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
2014-12-23 22:05:26 +00:00
|
|
|
State: api.ContainerState{
|
|
|
|
Running: &api.ContainerStateRunning{
|
|
|
|
StartedAt: util.Time{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-03-25 11:09:35 +00:00
|
|
|
{
|
|
|
|
Name: "bar",
|
2014-12-23 22:05:26 +00:00
|
|
|
State: api.ContainerState{
|
|
|
|
Running: &api.ContainerStateRunning{
|
|
|
|
StartedAt: util.Time{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"true",
|
|
|
|
},
|
|
|
|
}
|
2015-07-28 14:59:28 +00:00
|
|
|
// The point of this test is to verify that the below template works.
|
2015-06-04 03:09:03 +00:00
|
|
|
tmpl := `{{if (exists . "status" "containerStatuses")}}{{range .status.containerStatuses}}{{if (and (eq .name "foo") (exists . "state" "running"))}}true{{end}}{{end}}{{end}}`
|
2014-12-31 00:42:55 +00:00
|
|
|
p, err := NewTemplatePrinter([]byte(tmpl))
|
2014-12-23 22:05:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("tmpl fail: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-04-08 17:22:33 +00:00
|
|
|
printer := NewVersionedPrinter(p, api.Scheme, testapi.Version())
|
2014-12-31 00:42:55 +00:00
|
|
|
|
2014-12-23 22:05:26 +00:00
|
|
|
for name, item := range table {
|
|
|
|
buffer := &bytes.Buffer{}
|
|
|
|
err = printer.PrintObj(&item.pod, buffer)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v: unexpected err: %v", name, err)
|
|
|
|
continue
|
|
|
|
}
|
2015-04-08 17:22:33 +00:00
|
|
|
actual := buffer.String()
|
|
|
|
if len(actual) == 0 {
|
|
|
|
actual = "false"
|
|
|
|
}
|
|
|
|
if e := item.expect; e != actual {
|
|
|
|
t.Errorf("%v: expected %v, got %v", name, e, actual)
|
2014-12-23 22:05:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-20 22:09:59 +00:00
|
|
|
func TestPrinters(t *testing.T) {
|
|
|
|
om := func(name string) api.ObjectMeta { return api.ObjectMeta{Name: name} }
|
2014-12-31 00:42:55 +00:00
|
|
|
templatePrinter, err := NewTemplatePrinter([]byte("{{.name}}"))
|
2014-11-20 22:09:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-12-31 00:42:55 +00:00
|
|
|
templatePrinter2, err := NewTemplatePrinter([]byte("{{len .items}}"))
|
2014-11-20 22:09:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
printers := map[string]ResourcePrinter{
|
2015-06-29 18:36:06 +00:00
|
|
|
"humanReadable": NewHumanReadablePrinter(true, false, false, []string{}),
|
|
|
|
"humanReadableHeaders": NewHumanReadablePrinter(false, false, false, []string{}),
|
2014-12-31 00:42:55 +00:00
|
|
|
"json": &JSONPrinter{},
|
|
|
|
"yaml": &YAMLPrinter{},
|
2014-11-20 22:09:59 +00:00
|
|
|
"template": templatePrinter,
|
|
|
|
"template2": templatePrinter2,
|
|
|
|
}
|
|
|
|
objects := map[string]runtime.Object{
|
|
|
|
"pod": &api.Pod{ObjectMeta: om("pod")},
|
|
|
|
"emptyPodList": &api.PodList{},
|
|
|
|
"nonEmptyPodList": &api.PodList{Items: []api.Pod{{}}},
|
2015-03-20 21:24:43 +00:00
|
|
|
"endpoints": &api.Endpoints{
|
|
|
|
Subsets: []api.EndpointSubset{{
|
|
|
|
Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}, {IP: "localhost"}},
|
|
|
|
Ports: []api.EndpointPort{{Port: 8080}},
|
|
|
|
}}},
|
2014-11-20 22:09:59 +00:00
|
|
|
}
|
|
|
|
// map of printer name to set of objects it should fail on.
|
|
|
|
expectedErrors := map[string]util.StringSet{
|
2015-02-05 23:45:53 +00:00
|
|
|
"template2": util.NewStringSet("pod", "emptyPodList", "endpoints"),
|
2014-11-20 22:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2015-06-29 18:36:06 +00:00
|
|
|
printer := NewHumanReadablePrinter(false /* noHeaders */, false, false, []string{})
|
2014-12-16 22:20:51 +00:00
|
|
|
|
|
|
|
obj := api.EventList{
|
|
|
|
Items: []api.Event{
|
|
|
|
{
|
2015-02-06 02:21:01 +00:00
|
|
|
Source: api.EventSource{Component: "kubelet"},
|
|
|
|
Message: "Item 1",
|
|
|
|
FirstTimestamp: util.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)),
|
|
|
|
LastTimestamp: util.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)),
|
|
|
|
Count: 1,
|
2014-12-16 22:20:51 +00:00
|
|
|
},
|
|
|
|
{
|
2015-02-06 02:21:01 +00:00
|
|
|
Source: api.EventSource{Component: "scheduler"},
|
|
|
|
Message: "Item 2",
|
|
|
|
FirstTimestamp: util.NewTime(time.Date(1987, time.June, 17, 0, 0, 0, 0, time.UTC)),
|
|
|
|
LastTimestamp: util.NewTime(time.Date(1987, time.June, 17, 0, 0, 0, 0, time.UTC)),
|
|
|
|
Count: 1,
|
2014-12-16 22:20:51 +00:00
|
|
|
},
|
|
|
|
{
|
2015-02-06 02:21:01 +00:00
|
|
|
Source: api.EventSource{Component: "kubelet"},
|
|
|
|
Message: "Item 3",
|
|
|
|
FirstTimestamp: util.NewTime(time.Date(2002, time.December, 25, 0, 0, 0, 0, time.UTC)),
|
|
|
|
LastTimestamp: util.NewTime(time.Date(2002, time.December, 25, 0, 0, 0, 0, time.UTC)),
|
|
|
|
Count: 1,
|
2014-12-16 22:20:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
2015-01-15 01:11:34 +00:00
|
|
|
|
|
|
|
func TestPrintMinionStatus(t *testing.T) {
|
2015-06-29 18:36:06 +00:00
|
|
|
printer := NewHumanReadablePrinter(false, false, false, []string{})
|
2015-01-15 01:11:34 +00:00
|
|
|
table := []struct {
|
|
|
|
minion api.Node
|
|
|
|
status string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
minion: api.Node{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo1"},
|
2015-03-23 18:33:55 +00:00
|
|
|
Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: api.NodeReady, Status: api.ConditionTrue}}},
|
2015-01-15 01:11:34 +00:00
|
|
|
},
|
|
|
|
status: "Ready",
|
|
|
|
},
|
2015-04-16 01:53:03 +00:00
|
|
|
{
|
|
|
|
minion: api.Node{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo2"},
|
|
|
|
Spec: api.NodeSpec{Unschedulable: true},
|
|
|
|
Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: api.NodeReady, Status: api.ConditionTrue}}},
|
|
|
|
},
|
|
|
|
status: "Ready,SchedulingDisabled",
|
|
|
|
},
|
2015-01-15 01:11:34 +00:00
|
|
|
{
|
|
|
|
minion: api.Node{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo3"},
|
|
|
|
Status: api.NodeStatus{Conditions: []api.NodeCondition{
|
2015-03-23 18:33:55 +00:00
|
|
|
{Type: api.NodeReady, Status: api.ConditionTrue},
|
|
|
|
{Type: api.NodeReady, Status: api.ConditionTrue}}},
|
2015-01-15 01:11:34 +00:00
|
|
|
},
|
|
|
|
status: "Ready",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
minion: api.Node{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo4"},
|
2015-03-23 18:33:55 +00:00
|
|
|
Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: api.NodeReady, Status: api.ConditionFalse}}},
|
2015-01-15 01:11:34 +00:00
|
|
|
},
|
|
|
|
status: "NotReady",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
minion: api.Node{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo5"},
|
2015-04-16 01:53:03 +00:00
|
|
|
Spec: api.NodeSpec{Unschedulable: true},
|
|
|
|
Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: api.NodeReady, Status: api.ConditionFalse}}},
|
|
|
|
},
|
|
|
|
status: "NotReady,SchedulingDisabled",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
minion: api.Node{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo6"},
|
2015-03-23 18:33:55 +00:00
|
|
|
Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: "InvalidValue", Status: api.ConditionTrue}}},
|
2015-01-15 01:11:34 +00:00
|
|
|
},
|
|
|
|
status: "Unknown",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
minion: api.Node{
|
2015-04-16 01:53:03 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo7"},
|
2015-01-15 01:11:34 +00:00
|
|
|
Status: api.NodeStatus{Conditions: []api.NodeCondition{{}}},
|
|
|
|
},
|
|
|
|
status: "Unknown",
|
|
|
|
},
|
2015-04-16 01:53:03 +00:00
|
|
|
{
|
|
|
|
minion: api.Node{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo8"},
|
|
|
|
Spec: api.NodeSpec{Unschedulable: true},
|
|
|
|
Status: api.NodeStatus{Conditions: []api.NodeCondition{{Type: "InvalidValue", Status: api.ConditionTrue}}},
|
|
|
|
},
|
|
|
|
status: "Unknown,SchedulingDisabled",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
minion: api.Node{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo9"},
|
|
|
|
Spec: api.NodeSpec{Unschedulable: true},
|
|
|
|
Status: api.NodeStatus{Conditions: []api.NodeCondition{{}}},
|
|
|
|
},
|
|
|
|
status: "Unknown,SchedulingDisabled",
|
|
|
|
},
|
2015-01-15 01:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range table {
|
|
|
|
buffer := &bytes.Buffer{}
|
|
|
|
err := printer.PrintObj(&test.minion, buffer)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("An error occurred printing Minion: %#v", err)
|
|
|
|
}
|
|
|
|
if !contains(strings.Fields(buffer.String()), test.status) {
|
|
|
|
t.Fatalf("Expect printing minion %s with status %#v, got: %#v", test.minion.Name, test.status, buffer.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func contains(fields []string, field string) bool {
|
|
|
|
for _, v := range fields {
|
|
|
|
if v == field {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2015-04-01 21:46:33 +00:00
|
|
|
|
|
|
|
func TestPrintHumanReadableService(t *testing.T) {
|
|
|
|
tests := []api.Service{
|
|
|
|
{
|
|
|
|
Spec: api.ServiceSpec{
|
2015-05-23 20:41:11 +00:00
|
|
|
ClusterIP: "1.2.3.4",
|
2015-08-08 04:08:43 +00:00
|
|
|
Type: "LoadBalancer",
|
2015-04-01 21:46:33 +00:00
|
|
|
Ports: []api.ServicePort{
|
|
|
|
{
|
|
|
|
Port: 80,
|
|
|
|
Protocol: "TCP",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-05-22 21:33:29 +00:00
|
|
|
Status: api.ServiceStatus{
|
|
|
|
LoadBalancer: api.LoadBalancerStatus{
|
|
|
|
Ingress: []api.LoadBalancerIngress{
|
|
|
|
{
|
|
|
|
IP: "2.3.4.5",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IP: "3.4.5.6",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-04-01 21:46:33 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Spec: api.ServiceSpec{
|
2015-05-23 20:41:11 +00:00
|
|
|
ClusterIP: "1.2.3.4",
|
2015-04-01 21:46:33 +00:00
|
|
|
Ports: []api.ServicePort{
|
|
|
|
{
|
|
|
|
Port: 80,
|
|
|
|
Protocol: "TCP",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Port: 8090,
|
|
|
|
Protocol: "UDP",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Port: 8000,
|
|
|
|
Protocol: "TCP",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Spec: api.ServiceSpec{
|
2015-05-23 20:41:11 +00:00
|
|
|
ClusterIP: "1.2.3.4",
|
2015-08-08 04:08:43 +00:00
|
|
|
Type: "LoadBalancer",
|
2015-04-01 21:46:33 +00:00
|
|
|
Ports: []api.ServicePort{
|
|
|
|
{
|
|
|
|
Port: 80,
|
|
|
|
Protocol: "TCP",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Port: 8090,
|
|
|
|
Protocol: "UDP",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Port: 8000,
|
|
|
|
Protocol: "TCP",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-05-22 21:33:29 +00:00
|
|
|
Status: api.ServiceStatus{
|
|
|
|
LoadBalancer: api.LoadBalancerStatus{
|
|
|
|
Ingress: []api.LoadBalancerIngress{
|
|
|
|
{
|
|
|
|
IP: "2.3.4.5",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-04-01 21:46:33 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Spec: api.ServiceSpec{
|
2015-05-23 20:41:11 +00:00
|
|
|
ClusterIP: "1.2.3.4",
|
2015-08-08 04:08:43 +00:00
|
|
|
Type: "LoadBalancer",
|
2015-04-01 21:46:33 +00:00
|
|
|
Ports: []api.ServicePort{
|
|
|
|
{
|
|
|
|
Port: 80,
|
|
|
|
Protocol: "TCP",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Port: 8090,
|
|
|
|
Protocol: "UDP",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Port: 8000,
|
|
|
|
Protocol: "TCP",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-05-22 21:33:29 +00:00
|
|
|
Status: api.ServiceStatus{
|
|
|
|
LoadBalancer: api.LoadBalancerStatus{
|
|
|
|
Ingress: []api.LoadBalancerIngress{
|
|
|
|
{
|
|
|
|
IP: "2.3.4.5",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IP: "3.4.5.6",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IP: "5.6.7.8",
|
|
|
|
Hostname: "host5678",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-04-01 21:46:33 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, svc := range tests {
|
|
|
|
buff := bytes.Buffer{}
|
2015-06-29 18:36:06 +00:00
|
|
|
printService(&svc, &buff, false, false, []string{})
|
2015-04-01 21:46:33 +00:00
|
|
|
output := string(buff.Bytes())
|
2015-05-23 20:41:11 +00:00
|
|
|
ip := svc.Spec.ClusterIP
|
2015-04-01 21:46:33 +00:00
|
|
|
if !strings.Contains(output, ip) {
|
2015-05-23 20:41:11 +00:00
|
|
|
t.Errorf("expected to contain ClusterIP %s, but doesn't: %s", ip, output)
|
2015-04-01 21:46:33 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 21:33:29 +00:00
|
|
|
for _, ingress := range svc.Status.LoadBalancer.Ingress {
|
|
|
|
ip = ingress.IP
|
2015-04-01 21:46:33 +00:00
|
|
|
if !strings.Contains(output, ip) {
|
2015-05-22 21:33:29 +00:00
|
|
|
t.Errorf("expected to contain ingress ip %s, but doesn't: %s", ip, output)
|
2015-04-01 21:46:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, port := range svc.Spec.Ports {
|
|
|
|
portSpec := fmt.Sprintf("%d/%s", port.Port, port.Protocol)
|
|
|
|
if !strings.Contains(output, portSpec) {
|
|
|
|
t.Errorf("expected to contain port: %s, but doesn't: %s", portSpec, output)
|
|
|
|
}
|
|
|
|
}
|
2015-08-08 04:08:43 +00:00
|
|
|
// Each service should print on one line
|
|
|
|
if 1 != strings.Count(output, "\n") {
|
|
|
|
t.Errorf("expected a single newline, found %d", strings.Count(output, "\n"))
|
2015-04-01 21:46:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-15 19:24:41 +00:00
|
|
|
|
2015-05-20 19:51:35 +00:00
|
|
|
func TestPrintHumanReadableWithNamespace(t *testing.T) {
|
|
|
|
namespaceName := "testnamespace"
|
|
|
|
name := "test"
|
|
|
|
table := []struct {
|
2015-07-01 20:41:54 +00:00
|
|
|
obj runtime.Object
|
|
|
|
isNamespaced bool
|
2015-05-20 19:51:35 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
obj: &api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
|
|
|
|
},
|
2015-07-01 20:41:54 +00:00
|
|
|
isNamespaced: true,
|
2015-05-20 19:51:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
obj: &api.ReplicationController{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
|
|
|
|
Spec: api.ReplicationControllerSpec{
|
|
|
|
Replicas: 2,
|
|
|
|
Template: &api.PodTemplateSpec{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Labels: map[string]string{
|
|
|
|
"name": "foo",
|
|
|
|
"type": "production",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Image: "foo/bar",
|
|
|
|
TerminationMessagePath: api.TerminationMessagePathDefault,
|
|
|
|
ImagePullPolicy: api.PullIfNotPresent,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
RestartPolicy: api.RestartPolicyAlways,
|
|
|
|
DNSPolicy: api.DNSDefault,
|
|
|
|
NodeSelector: map[string]string{
|
|
|
|
"baz": "blah",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-07-01 20:41:54 +00:00
|
|
|
isNamespaced: true,
|
2015-05-20 19:51:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
obj: &api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
|
|
|
|
Spec: api.ServiceSpec{
|
2015-05-23 20:41:11 +00:00
|
|
|
ClusterIP: "1.2.3.4",
|
2015-05-20 19:51:35 +00:00
|
|
|
Ports: []api.ServicePort{
|
|
|
|
{
|
|
|
|
Port: 80,
|
|
|
|
Protocol: "TCP",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-05-22 22:12:46 +00:00
|
|
|
Status: api.ServiceStatus{
|
|
|
|
LoadBalancer: api.LoadBalancerStatus{
|
|
|
|
Ingress: []api.LoadBalancerIngress{
|
|
|
|
{
|
|
|
|
IP: "2.3.4.5",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-05-20 19:51:35 +00:00
|
|
|
},
|
2015-07-01 20:41:54 +00:00
|
|
|
isNamespaced: true,
|
2015-05-20 19:51:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
obj: &api.Endpoints{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
|
|
|
|
Subsets: []api.EndpointSubset{{
|
|
|
|
Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}, {IP: "localhost"}},
|
|
|
|
Ports: []api.EndpointPort{{Port: 8080}},
|
|
|
|
},
|
|
|
|
}},
|
2015-07-01 20:41:54 +00:00
|
|
|
isNamespaced: true,
|
2015-05-20 19:51:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
obj: &api.Namespace{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: name},
|
|
|
|
},
|
2015-07-01 20:41:54 +00:00
|
|
|
isNamespaced: false,
|
2015-05-20 19:51:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
obj: &api.Secret{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
|
|
|
|
},
|
2015-07-01 20:41:54 +00:00
|
|
|
isNamespaced: true,
|
2015-05-20 19:51:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
obj: &api.ServiceAccount{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
|
|
|
|
Secrets: []api.ObjectReference{},
|
|
|
|
},
|
2015-07-01 20:41:54 +00:00
|
|
|
isNamespaced: true,
|
2015-05-20 19:51:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
obj: &api.Node{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: name},
|
|
|
|
Status: api.NodeStatus{},
|
|
|
|
},
|
2015-07-01 20:41:54 +00:00
|
|
|
isNamespaced: false,
|
2015-05-20 19:51:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
obj: &api.PersistentVolume{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
|
|
|
|
Spec: api.PersistentVolumeSpec{},
|
|
|
|
},
|
2015-07-01 20:41:54 +00:00
|
|
|
isNamespaced: false,
|
2015-05-20 19:51:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
obj: &api.PersistentVolumeClaim{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
|
|
|
|
Spec: api.PersistentVolumeClaimSpec{},
|
|
|
|
},
|
2015-07-01 20:41:54 +00:00
|
|
|
isNamespaced: true,
|
2015-05-20 19:51:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
obj: &api.Event{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
|
|
|
|
Source: api.EventSource{Component: "kubelet"},
|
|
|
|
Message: "Item 1",
|
|
|
|
FirstTimestamp: util.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)),
|
|
|
|
LastTimestamp: util.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)),
|
|
|
|
Count: 1,
|
|
|
|
},
|
2015-07-01 20:41:54 +00:00
|
|
|
isNamespaced: true,
|
2015-05-20 19:51:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
obj: &api.LimitRange{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
|
|
|
|
},
|
2015-07-01 20:41:54 +00:00
|
|
|
isNamespaced: true,
|
2015-05-20 19:51:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
obj: &api.ResourceQuota{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
|
|
|
|
},
|
2015-07-01 20:41:54 +00:00
|
|
|
isNamespaced: true,
|
2015-05-20 19:51:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
obj: &api.ComponentStatus{
|
|
|
|
Conditions: []api.ComponentCondition{
|
|
|
|
{Type: api.ComponentHealthy, Status: api.ConditionTrue, Message: "ok", Error: ""},
|
|
|
|
},
|
|
|
|
},
|
2015-07-01 20:41:54 +00:00
|
|
|
isNamespaced: false,
|
2015-05-20 19:51:35 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range table {
|
2015-07-01 20:41:54 +00:00
|
|
|
if test.isNamespaced {
|
|
|
|
// Expect output to include namespace when requested.
|
|
|
|
printer := NewHumanReadablePrinter(false, true, false, []string{})
|
|
|
|
buffer := &bytes.Buffer{}
|
|
|
|
err := printer.PrintObj(test.obj, buffer)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("An error occurred printing object: %#v", err)
|
|
|
|
}
|
2015-07-01 20:56:31 +00:00
|
|
|
matched := contains(strings.Fields(buffer.String()), fmt.Sprintf("%s", namespaceName))
|
2015-07-01 20:41:54 +00:00
|
|
|
if !matched {
|
|
|
|
t.Errorf("Expect printing object to contain namespace: %#v", test.obj)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Expect error when trying to get all namespaces for un-namespaced object.
|
|
|
|
printer := NewHumanReadablePrinter(false, true, false, []string{})
|
|
|
|
buffer := &bytes.Buffer{}
|
|
|
|
err := printer.PrintObj(test.obj, buffer)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error when printing un-namespaced type")
|
|
|
|
}
|
2015-05-20 19:51:35 +00:00
|
|
|
}
|
2015-05-15 19:24:41 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-30 01:42:44 +00:00
|
|
|
|
|
|
|
func TestPrintPod(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
pod api.Pod
|
|
|
|
expect string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// Test name, num of containers, restarts, container ready status
|
|
|
|
api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "test1"},
|
|
|
|
Spec: api.PodSpec{Containers: make([]api.Container, 2)},
|
|
|
|
Status: api.PodStatus{
|
|
|
|
Phase: "podPhase",
|
|
|
|
ContainerStatuses: []api.ContainerStatus{
|
|
|
|
{Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
|
|
|
|
{RestartCount: 3},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"test1\t1/2\tpodPhase\t6\t",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Test container error overwrites pod phase
|
|
|
|
api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "test2"},
|
|
|
|
Spec: api.PodSpec{Containers: make([]api.Container, 2)},
|
|
|
|
Status: api.PodStatus{
|
|
|
|
Phase: "podPhase",
|
|
|
|
ContainerStatuses: []api.ContainerStatus{
|
|
|
|
{Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
|
2015-06-09 15:58:16 +00:00
|
|
|
{State: api.ContainerState{Waiting: &api.ContainerStateWaiting{Reason: "ContainerWaitingReason"}}, RestartCount: 3},
|
2015-05-30 01:42:44 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-06-09 15:58:16 +00:00
|
|
|
"test2\t1/2\tContainerWaitingReason\t6\t",
|
2015-05-30 01:42:44 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// Test the same as the above but with Terminated state and the first container overwrites the rest
|
|
|
|
api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "test3"},
|
|
|
|
Spec: api.PodSpec{Containers: make([]api.Container, 2)},
|
|
|
|
Status: api.PodStatus{
|
|
|
|
Phase: "podPhase",
|
|
|
|
ContainerStatuses: []api.ContainerStatus{
|
2015-06-09 15:58:16 +00:00
|
|
|
{State: api.ContainerState{Waiting: &api.ContainerStateWaiting{Reason: "ContainerWaitingReason"}}, RestartCount: 3},
|
|
|
|
{State: api.ContainerState{Terminated: &api.ContainerStateTerminated{Reason: "ContainerTerminatedReason"}}, RestartCount: 3},
|
2015-05-30 01:42:44 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-06-09 15:58:16 +00:00
|
|
|
"test3\t0/2\tContainerWaitingReason\t6\t",
|
2015-05-30 01:42:44 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// Test ready is not enough for reporting running
|
|
|
|
api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "test4"},
|
|
|
|
Spec: api.PodSpec{Containers: make([]api.Container, 2)},
|
|
|
|
Status: api.PodStatus{
|
|
|
|
Phase: "podPhase",
|
|
|
|
ContainerStatuses: []api.ContainerStatus{
|
|
|
|
{Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
|
|
|
|
{Ready: true, RestartCount: 3},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"test4\t1/2\tpodPhase\t6\t",
|
|
|
|
},
|
2015-06-09 15:58:16 +00:00
|
|
|
{
|
|
|
|
// Test ready is not enough for reporting running
|
|
|
|
api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "test5"},
|
|
|
|
Spec: api.PodSpec{Containers: make([]api.Container, 2)},
|
|
|
|
Status: api.PodStatus{
|
|
|
|
Reason: "OutOfDisk",
|
|
|
|
Phase: "podPhase",
|
|
|
|
ContainerStatuses: []api.ContainerStatus{
|
|
|
|
{Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
|
|
|
|
{Ready: true, RestartCount: 3},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"test5\t1/2\tOutOfDisk\t6\t",
|
|
|
|
},
|
2015-05-30 01:42:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
|
|
|
for _, test := range tests {
|
2015-06-29 18:36:06 +00:00
|
|
|
printPod(&test.pod, buf, false, false, []string{})
|
2015-05-30 01:42:44 +00:00
|
|
|
// We ignore time
|
|
|
|
if !strings.HasPrefix(buf.String(), test.expect) {
|
|
|
|
t.Fatalf("Expected: %s, got: %s", test.expect, buf.String())
|
|
|
|
}
|
|
|
|
buf.Reset()
|
|
|
|
}
|
|
|
|
}
|
2015-06-16 16:30:11 +00:00
|
|
|
|
|
|
|
func TestPrintPodWithLabels(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
pod api.Pod
|
|
|
|
labelColumns []string
|
|
|
|
startsWith string
|
|
|
|
endsWith string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// Test name, num of containers, restarts, container ready status
|
|
|
|
api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "test1",
|
|
|
|
Labels: map[string]string{"col1": "asd", "COL2": "zxc"},
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{Containers: make([]api.Container, 2)},
|
|
|
|
Status: api.PodStatus{
|
|
|
|
Phase: "podPhase",
|
|
|
|
ContainerStatuses: []api.ContainerStatus{
|
|
|
|
{Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
|
|
|
|
{RestartCount: 3},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
[]string{"col1", "COL2"},
|
|
|
|
"test1\t1/2\tpodPhase\t6\t",
|
|
|
|
"\tasd\tzxc\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Test name, num of containers, restarts, container ready status
|
|
|
|
api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "test1",
|
|
|
|
Labels: map[string]string{"col1": "asd", "COL2": "zxc"},
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{Containers: make([]api.Container, 2)},
|
|
|
|
Status: api.PodStatus{
|
|
|
|
Phase: "podPhase",
|
|
|
|
ContainerStatuses: []api.ContainerStatus{
|
|
|
|
{Ready: true, RestartCount: 3, State: api.ContainerState{Running: &api.ContainerStateRunning{}}},
|
|
|
|
{RestartCount: 3},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
"test1\t1/2\tpodPhase\t6\t",
|
|
|
|
"\n",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
|
|
|
for _, test := range tests {
|
2015-06-29 18:36:06 +00:00
|
|
|
printPod(&test.pod, buf, false, false, test.labelColumns)
|
2015-06-16 16:30:11 +00:00
|
|
|
// We ignore time
|
|
|
|
if !strings.HasPrefix(buf.String(), test.startsWith) || !strings.HasSuffix(buf.String(), test.endsWith) {
|
|
|
|
t.Fatalf("Expected to start with: %s and end with: %s, but got: %s", test.startsWith, test.endsWith, buf.String())
|
|
|
|
}
|
|
|
|
buf.Reset()
|
|
|
|
}
|
|
|
|
}
|