Fix a bug in kubectl exec handling.

This commit is contained in:
Brendan Burns
2015-06-02 19:45:51 -07:00
parent 3cb30e1e21
commit 911e3e9594
2 changed files with 40 additions and 22 deletions

View File

@@ -21,6 +21,7 @@ import (
"fmt"
"io"
"net/http"
"reflect"
"testing"
"github.com/spf13/cobra"
@@ -43,58 +44,73 @@ func TestPodAndContainer(t *testing.T) {
tests := []struct {
args []string
p *execParams
name string
expectError bool
expectedPod string
expectedContainer string
expectedArgs []string
}{
{
p: &execParams{},
expectError: true,
name: "empty",
},
{
p: &execParams{podName: "foo"},
expectError: true,
name: "no cmd",
},
{
p: &execParams{podName: "foo", containerName: "bar"},
expectError: true,
name: "no cmd, w/ container",
},
{
p: &execParams{podName: "foo"},
args: []string{"cmd"},
expectedPod: "foo",
p: &execParams{podName: "foo"},
args: []string{"cmd"},
expectedPod: "foo",
expectedArgs: []string{"cmd"},
name: "pod in flags",
},
{
p: &execParams{},
args: []string{"foo"},
expectError: true,
name: "no cmd, w/o flags",
},
{
p: &execParams{},
args: []string{"foo", "cmd"},
expectedPod: "foo",
p: &execParams{},
args: []string{"foo", "cmd"},
expectedPod: "foo",
expectedArgs: []string{"cmd"},
name: "cmd, w/o flags",
},
{
p: &execParams{containerName: "bar"},
args: []string{"foo", "cmd"},
expectedPod: "foo",
expectedContainer: "bar",
expectedArgs: []string{"cmd"},
name: "cmd, container in flag",
},
}
for _, test := range tests {
cmd := &cobra.Command{}
podName, containerName, err := extractPodAndContainer(cmd, test.args, test.p)
podName, containerName, args, err := extractPodAndContainer(cmd, test.args, test.p)
if podName != test.expectedPod {
t.Errorf("expected: %s, got: %s", test.expectedPod, podName)
t.Errorf("expected: %s, got: %s (%s)", test.expectedPod, podName, test.name)
}
if containerName != test.expectedContainer {
t.Errorf("expected: %s, got: %s", test.expectedContainer, containerName)
t.Errorf("expected: %s, got: %s (%s)", test.expectedContainer, containerName, test.name)
}
if test.expectError && err == nil {
t.Error("unexpected non-error")
t.Errorf("unexpected non-error (%s)", test.name)
}
if !test.expectError && err != nil {
t.Errorf("unexpected error: %v", err)
t.Errorf("unexpected error: %v (%s)", err, test.name)
}
if !reflect.DeepEqual(test.expectedArgs, args) {
t.Errorf("expected: %v, got %v (%s)", test.expectedArgs, args, test.name)
}
}
}