From 655d7341387da382f768e5ce442388a731af8c76 Mon Sep 17 00:00:00 2001 From: YuxiJin-tobeyjin Date: Wed, 1 Nov 2017 10:35:50 +0800 Subject: [PATCH] Add complementary unittest for kubectl logs --- pkg/kubectl/cmd/logs_test.go | 61 ++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/pkg/kubectl/cmd/logs_test.go b/pkg/kubectl/cmd/logs_test.go index 65c06bcbdc8..b8005755c00 100644 --- a/pkg/kubectl/cmd/logs_test.go +++ b/pkg/kubectl/cmd/logs_test.go @@ -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) + } + } +}