Add complementary unittest for kubectl logs

This commit is contained in:
YuxiJin-tobeyjin
2017-11-01 10:35:50 +08:00
parent c0a519be1f
commit 655d734138

View File

@@ -111,6 +111,11 @@ func TestValidateLogFlags(t *testing.T) {
flags: map[string]string{"since": "1h", "since-time": "2006-01-02T15:04:05Z"},
expected: "at most one of `sinceTime` or `sinceSeconds` may be specified",
},
{
name: "negative since-time",
flags: map[string]string{"since": "-1s"},
expected: "must be greater than 0",
},
{
name: "negative limit-bytes",
flags: map[string]string{"limit-bytes": "-100"},
@@ -142,3 +147,59 @@ func TestValidateLogFlags(t *testing.T) {
}
}
}
func TestLogComplete(t *testing.T) {
f, _, _, _ := cmdtesting.NewAPIFactory()
tests := []struct {
name string
args []string
flags map[string]string
expected string
}{
{
name: "No args case",
flags: map[string]string{"selector": ""},
expected: "'logs (POD | TYPE/NAME) [CONTAINER_NAME]'.\nPOD or TYPE/NAME is a required argument for the logs command",
},
{
name: "One args case",
args: []string{"foo"},
flags: map[string]string{"selector": "foo"},
expected: "only a selector (-l) or a POD name is allowed",
},
{
name: "Two args case",
args: []string{"foo", "foo1"},
flags: map[string]string{"container": "foo1"},
expected: "only one of -c or an inline [CONTAINER] arg is allowed",
},
{
name: "More than two args case",
args: []string{"foo", "foo1", "foo2"},
flags: map[string]string{"tail": "1"},
expected: "'logs (POD | TYPE/NAME) [CONTAINER_NAME]'.\nPOD or TYPE/NAME is a required argument for the logs command",
},
{
name: "follow and selecter conflict",
flags: map[string]string{"selector": "foo", "follow": "true"},
expected: "only one of follow (-f) or selector (-l) is allowed",
},
}
for _, test := range tests {
cmd := NewCmdLogs(f, bytes.NewBuffer([]byte{}))
var err error
out := ""
for flag, value := range test.flags {
cmd.Flags().Set(flag, value)
}
// checkErr breaks tests in case of errors, plus we just
// need to check errors returned by the command validation
o := &LogsOptions{}
err = o.Complete(f, os.Stdout, cmd, test.args)
out = err.Error()
if !strings.Contains(out, test.expected) {
t.Errorf("%s: expected to find:\n\t%s\nfound:\n\t%s\n", test.name, test.expected, out)
}
}
}