mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 13:37:30 +00:00
Merge pull request #54902 from YuxiJin-tobeyjin/addunitforlogs
Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Supplement unit test for kubectl logs **What this PR does / why we need it**: Supplement unit test for kubectl logs **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*: Fixes # **Special notes for your reviewer**: **Release note**: ```release-note NONE ```
This commit is contained in:
commit
b9eee8fd6b
@ -107,6 +107,11 @@ func TestValidateLogFlags(t *testing.T) {
|
|||||||
flags: map[string]string{"since": "1h", "since-time": "2006-01-02T15:04:05Z"},
|
flags: map[string]string{"since": "1h", "since-time": "2006-01-02T15:04:05Z"},
|
||||||
expected: "at most one of `sinceTime` or `sinceSeconds` may be specified",
|
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",
|
name: "negative limit-bytes",
|
||||||
flags: map[string]string{"limit-bytes": "-100"},
|
flags: map[string]string{"limit-bytes": "-100"},
|
||||||
@ -138,3 +143,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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user