Be able to specify the timeout to wait for pod for kubectl logs/attach

This commit is contained in:
shiywang
2017-02-22 00:19:13 +08:00
parent b0ce93f9be
commit 52e4be2578
11 changed files with 97 additions and 29 deletions

View File

@@ -24,6 +24,7 @@ import (
"net/url"
"strings"
"testing"
"time"
"github.com/spf13/cobra"
@@ -34,6 +35,7 @@ import (
"k8s.io/client-go/rest/fake"
"k8s.io/kubernetes/pkg/api"
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/util/term"
)
@@ -57,18 +59,21 @@ func TestPodAndContainerAttach(t *testing.T) {
expectError bool
expectedPod string
expectedContainer string
timeout time.Duration
obj runtime.Object
}{
{
p: &AttachOptions{},
expectError: true,
name: "empty",
timeout: 1,
},
{
p: &AttachOptions{},
args: []string{"one", "two", "three"},
expectError: true,
name: "too many args",
timeout: 2,
},
{
p: &AttachOptions{},
@@ -76,6 +81,7 @@ func TestPodAndContainerAttach(t *testing.T) {
expectedPod: "foo",
name: "no container, no flags",
obj: attachPod(),
timeout: defaultPodLogsTimeout,
},
{
p: &AttachOptions{StreamOptions: StreamOptions{ContainerName: "bar"}},
@@ -84,6 +90,7 @@ func TestPodAndContainerAttach(t *testing.T) {
expectedContainer: "bar",
name: "container in flag",
obj: attachPod(),
timeout: 10000000,
},
{
p: &AttachOptions{StreamOptions: StreamOptions{ContainerName: "initfoo"}},
@@ -92,6 +99,7 @@ func TestPodAndContainerAttach(t *testing.T) {
expectedContainer: "initfoo",
name: "init container in flag",
obj: attachPod(),
timeout: 30,
},
{
p: &AttachOptions{StreamOptions: StreamOptions{ContainerName: "bar"}},
@@ -99,6 +107,7 @@ func TestPodAndContainerAttach(t *testing.T) {
expectError: true,
name: "non-existing container in flag",
obj: attachPod(),
timeout: 10,
},
{
p: &AttachOptions{},
@@ -106,6 +115,7 @@ func TestPodAndContainerAttach(t *testing.T) {
expectedPod: "foo",
name: "no container, no flags, pods and name",
obj: attachPod(),
timeout: 10000,
},
{
p: &AttachOptions{},
@@ -113,6 +123,16 @@ func TestPodAndContainerAttach(t *testing.T) {
expectedPod: "foo",
name: "no container, no flags, pod/name",
obj: attachPod(),
timeout: 1,
},
{
p: &AttachOptions{},
args: []string{"pod/foo"},
expectedPod: "foo",
name: "invalid get pod timeout value",
obj: attachPod(),
expectError: true,
timeout: 0,
},
}
@@ -133,6 +153,8 @@ func TestPodAndContainerAttach(t *testing.T) {
cmd := &cobra.Command{}
options := test.p
cmdutil.AddPodRunningTimeoutFlag(cmd, test.timeout)
err := options.Complete(f, cmd, test.args)
if test.expectError && err == nil {
t.Errorf("unexpected non-error (%s)", test.name)
@@ -227,9 +249,11 @@ func TestAttach(t *testing.T) {
Out: bufOut,
Err: bufErr,
},
Attach: remoteAttach,
Attach: remoteAttach,
GetPodTimeout: 1000,
}
cmd := &cobra.Command{}
cmdutil.AddPodRunningTimeoutFlag(cmd, 1000)
if err := params.Complete(f, cmd, []string{"foo"}); err != nil {
t.Fatal(err)
}
@@ -310,9 +334,11 @@ func TestAttachWarnings(t *testing.T) {
Stdin: test.stdin,
TTY: test.tty,
},
Attach: ex,
Attach: ex,
GetPodTimeout: 1000,
}
cmd := &cobra.Command{}
cmdutil.AddPodRunningTimeoutFlag(cmd, 1000)
if err := params.Complete(f, cmd, []string{"foo"}); err != nil {
t.Fatal(err)
}