From 21bbde46d46abfde5e507f924b54cb56bff8fa77 Mon Sep 17 00:00:00 2001 From: Eric Tune Date: Wed, 1 Jul 2015 13:41:54 -0700 Subject: [PATCH] Error: kubectl --all-namespaces get {node|ev|cs} Report an error if someone asks for --all-namespaces when getting a thing that is not namespaced. This is in preparation for a subsequent commit which prints namespace as its own column. Restructured test to expect an error for non-namespaced things. Dropped the part where it was trying to test that not printing namespace didn't contain namespace. Some other test can cover that. --- pkg/kubectl/resource_printer.go | 12 +++++ pkg/kubectl/resource_printer_test.go | 76 +++++++++++++--------------- 2 files changed, 48 insertions(+), 40 deletions(-) diff --git a/pkg/kubectl/resource_printer.go b/pkg/kubectl/resource_printer.go index eb116b8aa07..cad216e5719 100644 --- a/pkg/kubectl/resource_printer.go +++ b/pkg/kubectl/resource_printer.go @@ -616,6 +616,9 @@ func printEndpointsList(list *api.EndpointsList, w io.Writer, withNamespace bool } func printNamespace(item *api.Namespace, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error { + if withNamespace { + return fmt.Errorf("namespace is not namespaced") + } if _, err := fmt.Fprintf(w, "%s\t%s\t%s\n", item.Name, formatLabels(item.Labels), item.Status.Phase); err != nil { return err } @@ -683,6 +686,9 @@ func printServiceAccountList(list *api.ServiceAccountList, w io.Writer, withName } func printNode(node *api.Node, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error { + if withNamespace { + return fmt.Errorf("node is not namespaced") + } conditionMap := make(map[api.NodeConditionType]*api.NodeCondition) NodeAllConditions := []api.NodeConditionType{api.NodeReady} for i := range node.Status.Conditions { @@ -783,6 +789,9 @@ func printPersistentVolumeClaimList(list *api.PersistentVolumeClaimList, w io.Wr } func printEvent(event *api.Event, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error { + if withNamespace { + return fmt.Errorf("event is not namespaced") + } if _, err := fmt.Fprintf( w, "%s\t%s\t%d\t%s\t%s\t%s\t%s\t%s\t%s", event.FirstTimestamp.Time.Format(time.RFC1123Z), @@ -863,6 +872,9 @@ func printResourceQuotaList(list *api.ResourceQuotaList, w io.Writer, withNamesp } func printComponentStatus(item *api.ComponentStatus, w io.Writer, withNamespace bool, wide bool, columnLabels []string) error { + if withNamespace { + return fmt.Errorf("componentStatus is not namespaced") + } status := "Unknown" message := "" error := "" diff --git a/pkg/kubectl/resource_printer_test.go b/pkg/kubectl/resource_printer_test.go index 931ac560601..9dfc1435e6f 100644 --- a/pkg/kubectl/resource_printer_test.go +++ b/pkg/kubectl/resource_printer_test.go @@ -774,14 +774,14 @@ func TestPrintHumanReadableWithNamespace(t *testing.T) { namespaceName := "testnamespace" name := "test" table := []struct { - obj runtime.Object - printNamespace bool + obj runtime.Object + isNamespaced bool }{ { obj: &api.Pod{ ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName}, }, - printNamespace: true, + isNamespaced: true, }, { obj: &api.ReplicationController{ @@ -812,7 +812,7 @@ func TestPrintHumanReadableWithNamespace(t *testing.T) { }, }, }, - printNamespace: true, + isNamespaced: true, }, { obj: &api.Service{ @@ -836,7 +836,7 @@ func TestPrintHumanReadableWithNamespace(t *testing.T) { }, }, }, - printNamespace: true, + isNamespaced: true, }, { obj: &api.Endpoints{ @@ -846,47 +846,47 @@ func TestPrintHumanReadableWithNamespace(t *testing.T) { Ports: []api.EndpointPort{{Port: 8080}}, }, }}, - printNamespace: true, + isNamespaced: true, }, { obj: &api.Namespace{ ObjectMeta: api.ObjectMeta{Name: name}, }, - printNamespace: false, + isNamespaced: false, }, { obj: &api.Secret{ ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName}, }, - printNamespace: true, + isNamespaced: true, }, { obj: &api.ServiceAccount{ ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName}, Secrets: []api.ObjectReference{}, }, - printNamespace: true, + isNamespaced: true, }, { obj: &api.Node{ ObjectMeta: api.ObjectMeta{Name: name}, Status: api.NodeStatus{}, }, - printNamespace: false, + isNamespaced: false, }, { obj: &api.PersistentVolume{ ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName}, Spec: api.PersistentVolumeSpec{}, }, - printNamespace: true, + isNamespaced: false, }, { obj: &api.PersistentVolumeClaim{ ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName}, Spec: api.PersistentVolumeClaimSpec{}, }, - printNamespace: true, + isNamespaced: true, }, { obj: &api.Event{ @@ -897,19 +897,19 @@ func TestPrintHumanReadableWithNamespace(t *testing.T) { LastTimestamp: util.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)), Count: 1, }, - printNamespace: false, + isNamespaced: true, }, { obj: &api.LimitRange{ ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName}, }, - printNamespace: true, + isNamespaced: true, }, { obj: &api.ResourceQuota{ ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName}, }, - printNamespace: true, + isNamespaced: true, }, { obj: &api.ComponentStatus{ @@ -917,35 +917,31 @@ func TestPrintHumanReadableWithNamespace(t *testing.T) { {Type: api.ComponentHealthy, Status: api.ConditionTrue, Message: "ok", Error: ""}, }, }, - printNamespace: false, + isNamespaced: false, }, } - printer := NewHumanReadablePrinter(false, false, false, []string{}) for _, test := range table { - buffer := &bytes.Buffer{} - err := printer.PrintObj(test.obj, buffer) - if err != nil { - t.Fatalf("An error occurred printing object: %#v", err) - } - matched := contains(strings.Fields(buffer.String()), fmt.Sprintf("%s/%s", namespaceName, name)) - if matched { - t.Errorf("Expect printing object not to contain namespace: %v", test.obj) - } - } - - printer = NewHumanReadablePrinter(false, true, false, []string{}) - for _, test := range table { - buffer := &bytes.Buffer{} - err := printer.PrintObj(test.obj, buffer) - if err != nil { - t.Fatalf("An error occurred printing object: %#v", err) - } - matched := contains(strings.Fields(buffer.String()), fmt.Sprintf("%s/%s", namespaceName, name)) - if test.printNamespace && !matched { - t.Errorf("Expect printing object to contain namespace: %v", test.obj) - } else if !test.printNamespace && matched { - t.Errorf("Expect printing object not to contain namespace: %v", test.obj) + 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) + } + matched := contains(strings.Fields(buffer.String()), fmt.Sprintf("%s/%s", namespaceName, name)) + 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") + } } } }