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.
This commit is contained in:
Eric Tune 2015-07-01 13:41:54 -07:00
parent 8045357c5c
commit 21bbde46d4
2 changed files with 48 additions and 40 deletions

View File

@ -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 { 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 { if _, err := fmt.Fprintf(w, "%s\t%s\t%s\n", item.Name, formatLabels(item.Labels), item.Status.Phase); err != nil {
return err 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 { 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) conditionMap := make(map[api.NodeConditionType]*api.NodeCondition)
NodeAllConditions := []api.NodeConditionType{api.NodeReady} NodeAllConditions := []api.NodeConditionType{api.NodeReady}
for i := range node.Status.Conditions { 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 { 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( if _, err := fmt.Fprintf(
w, "%s\t%s\t%d\t%s\t%s\t%s\t%s\t%s\t%s", w, "%s\t%s\t%d\t%s\t%s\t%s\t%s\t%s\t%s",
event.FirstTimestamp.Time.Format(time.RFC1123Z), 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 { 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" status := "Unknown"
message := "" message := ""
error := "" error := ""

View File

@ -774,14 +774,14 @@ func TestPrintHumanReadableWithNamespace(t *testing.T) {
namespaceName := "testnamespace" namespaceName := "testnamespace"
name := "test" name := "test"
table := []struct { table := []struct {
obj runtime.Object obj runtime.Object
printNamespace bool isNamespaced bool
}{ }{
{ {
obj: &api.Pod{ obj: &api.Pod{
ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName}, ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
}, },
printNamespace: true, isNamespaced: true,
}, },
{ {
obj: &api.ReplicationController{ obj: &api.ReplicationController{
@ -812,7 +812,7 @@ func TestPrintHumanReadableWithNamespace(t *testing.T) {
}, },
}, },
}, },
printNamespace: true, isNamespaced: true,
}, },
{ {
obj: &api.Service{ obj: &api.Service{
@ -836,7 +836,7 @@ func TestPrintHumanReadableWithNamespace(t *testing.T) {
}, },
}, },
}, },
printNamespace: true, isNamespaced: true,
}, },
{ {
obj: &api.Endpoints{ obj: &api.Endpoints{
@ -846,47 +846,47 @@ func TestPrintHumanReadableWithNamespace(t *testing.T) {
Ports: []api.EndpointPort{{Port: 8080}}, Ports: []api.EndpointPort{{Port: 8080}},
}, },
}}, }},
printNamespace: true, isNamespaced: true,
}, },
{ {
obj: &api.Namespace{ obj: &api.Namespace{
ObjectMeta: api.ObjectMeta{Name: name}, ObjectMeta: api.ObjectMeta{Name: name},
}, },
printNamespace: false, isNamespaced: false,
}, },
{ {
obj: &api.Secret{ obj: &api.Secret{
ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName}, ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
}, },
printNamespace: true, isNamespaced: true,
}, },
{ {
obj: &api.ServiceAccount{ obj: &api.ServiceAccount{
ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName}, ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
Secrets: []api.ObjectReference{}, Secrets: []api.ObjectReference{},
}, },
printNamespace: true, isNamespaced: true,
}, },
{ {
obj: &api.Node{ obj: &api.Node{
ObjectMeta: api.ObjectMeta{Name: name}, ObjectMeta: api.ObjectMeta{Name: name},
Status: api.NodeStatus{}, Status: api.NodeStatus{},
}, },
printNamespace: false, isNamespaced: false,
}, },
{ {
obj: &api.PersistentVolume{ obj: &api.PersistentVolume{
ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName}, ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
Spec: api.PersistentVolumeSpec{}, Spec: api.PersistentVolumeSpec{},
}, },
printNamespace: true, isNamespaced: false,
}, },
{ {
obj: &api.PersistentVolumeClaim{ obj: &api.PersistentVolumeClaim{
ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName}, ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
Spec: api.PersistentVolumeClaimSpec{}, Spec: api.PersistentVolumeClaimSpec{},
}, },
printNamespace: true, isNamespaced: true,
}, },
{ {
obj: &api.Event{ 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)), LastTimestamp: util.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)),
Count: 1, Count: 1,
}, },
printNamespace: false, isNamespaced: true,
}, },
{ {
obj: &api.LimitRange{ obj: &api.LimitRange{
ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName}, ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
}, },
printNamespace: true, isNamespaced: true,
}, },
{ {
obj: &api.ResourceQuota{ obj: &api.ResourceQuota{
ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName}, ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespaceName},
}, },
printNamespace: true, isNamespaced: true,
}, },
{ {
obj: &api.ComponentStatus{ obj: &api.ComponentStatus{
@ -917,35 +917,31 @@ func TestPrintHumanReadableWithNamespace(t *testing.T) {
{Type: api.ComponentHealthy, Status: api.ConditionTrue, Message: "ok", Error: ""}, {Type: api.ComponentHealthy, Status: api.ConditionTrue, Message: "ok", Error: ""},
}, },
}, },
printNamespace: false, isNamespaced: false,
}, },
} }
printer := NewHumanReadablePrinter(false, false, false, []string{})
for _, test := range table { for _, test := range table {
buffer := &bytes.Buffer{} if test.isNamespaced {
err := printer.PrintObj(test.obj, buffer) // Expect output to include namespace when requested.
if err != nil { printer := NewHumanReadablePrinter(false, true, false, []string{})
t.Fatalf("An error occurred printing object: %#v", err) buffer := &bytes.Buffer{}
} err := printer.PrintObj(test.obj, buffer)
matched := contains(strings.Fields(buffer.String()), fmt.Sprintf("%s/%s", namespaceName, name)) if err != nil {
if matched { t.Fatalf("An error occurred printing object: %#v", err)
t.Errorf("Expect printing object not to contain namespace: %v", test.obj) }
} 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)
printer = NewHumanReadablePrinter(false, true, false, []string{}) }
for _, test := range table { } else {
buffer := &bytes.Buffer{} // Expect error when trying to get all namespaces for un-namespaced object.
err := printer.PrintObj(test.obj, buffer) printer := NewHumanReadablePrinter(false, true, false, []string{})
if err != nil { buffer := &bytes.Buffer{}
t.Fatalf("An error occurred printing object: %#v", err) err := printer.PrintObj(test.obj, buffer)
} if err == nil {
matched := contains(strings.Fields(buffer.String()), fmt.Sprintf("%s/%s", namespaceName, name)) t.Errorf("Expected error when printing un-namespaced type")
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)
} }
} }
} }