From f283a576c1a2dc2cd75d87ce2d3db7c436daae1c Mon Sep 17 00:00:00 2001 From: tstromberg Date: Thu, 30 Jan 2020 14:36:39 -0800 Subject: [PATCH] Promote status function to one that should be used across tests --- test/integration/helpers.go | 28 ++++++++++++++++++++-- test/integration/start_stop_delete_test.go | 22 +++++------------ 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/test/integration/helpers.go b/test/integration/helpers.go index c525cf99f1..bac683ea87 100644 --- a/test/integration/helpers.go +++ b/test/integration/helpers.go @@ -185,9 +185,9 @@ func CleanupWithLogs(t *testing.T, profile string, cancel context.CancelFunc) { // clusterLogs shows logs for debugging a failed cluster func clusterLogs(t *testing.T, profile string) { - st := Status(context.Background(), t, Target(), profile) + st := Status(context.Background(), t, Target(), profile, "Host") if st != state.Running.String() { - t.Logf("%q is not running, skipping log retrieval (state=%q)", profile, st) + t.Logf("%q host is not running, skipping log retrieval (state=%q)", profile, st) return } @@ -199,6 +199,12 @@ func clusterLogs(t *testing.T, profile string) { } t.Logf("%s logs: %s", t.Name(), rr.Stdout) + st = Status(context.Background(), t, Target(), profile, "APIServer") + if st != state.Running.String() { + t.Logf("%q apiserver is not running, skipping kubectl commands (state=%q)", profile, st) + return + } + rr, rerr := Run(t, exec.Command("kubectl", "--context", profile, "get", "po", "-A", "--show-labels")) if rerr != nil { t.Logf("%s: %v", rr.Command(), rerr) @@ -313,8 +319,26 @@ func PodWait(ctx context.Context, t *testing.T, profile string, ns string, selec return names, fmt.Errorf("%s: %v", fmt.Sprintf("%s within %s", selector, timeout), err) } +// Status returns a minikube component status as a string +func Status(ctx context.Context, t *testing.T, path string, profile string, key string) string { + t.Helper() + // Reminder of useful keys: "Host", "Kubelet", "APIServer" + rr, err := Run(t, exec.CommandContext(ctx, path, "status", fmt.Sprintf("--format={{.%s}}", key), "-p", profile)) + if err != nil { + t.Logf("status error: %v (may be ok)", err) + } + return strings.TrimSpace(rr.Stdout.String()) +} + // showPodLogs logs debug info for pods func showPodLogs(ctx context.Context, t *testing.T, profile string, ns string, names []string) { + t.Helper() + st := Status(context.Background(), t, Target(), profile, "APIServer") + if st != state.Running.String() { + t.Logf("%q apiserver is not running, skipping kubectl commands (state=%q)", profile, st) + return + } + t.Logf("%s: showing logs for failed pods as of %s", t.Name(), time.Now()) for _, name := range names { diff --git a/test/integration/start_stop_delete_test.go b/test/integration/start_stop_delete_test.go index 832e231aba..f71c78e6b2 100644 --- a/test/integration/start_stop_delete_test.go +++ b/test/integration/start_stop_delete_test.go @@ -36,16 +36,6 @@ import ( "k8s.io/minikube/pkg/minikube/constants" ) -// status returns a minikube component status as a string -func status(ctx context.Context, t *testing.T, path string, profile string, key string) string { - t.Helper() - rr, err := Run(t, exec.CommandContext(ctx, path, "status", fmt.Sprintf("--format={{.%s}}", key), "-p", profile)) - if err != nil { - t.Logf("status error: %v (may be ok)", err) - } - return strings.TrimSpace(rr.Stdout.String()) -} - func TestStartStop(t *testing.T) { MaybeParallel(t) @@ -115,7 +105,7 @@ func TestStartStop(t *testing.T) { t.Errorf("%s failed: %v", rr.Args, err) } - got := status(ctx, t, Target(), profile, "Host") + got := Status(ctx, t, Target(), profile, "Host") if got != state.Stopped.String() { t.Errorf("status = %q; want = %q", got, state.Stopped) } @@ -132,7 +122,7 @@ func TestStartStop(t *testing.T) { t.Fatalf("wait: %v", err) } - got = status(ctx, t, Target(), profile, "Host") + got = Status(ctx, t, Target(), profile, "Host") if got != state.Running.String() { t.Errorf("host status = %q; want = %q", got, state.Running) } @@ -235,12 +225,12 @@ func testPause(ctx context.Context, t *testing.T, profile string) { t.Fatalf("%s failed: %v", rr.Args, err) } - got := status(ctx, t, Target(), profile, "APIServer") + got := Status(ctx, t, Target(), profile, "APIServer") if got != state.Paused.String() { t.Errorf("apiserver status = %q; want = %q", got, state.Paused) } - got = status(ctx, t, Target(), profile, "Kubelet") + got = Status(ctx, t, Target(), profile, "Kubelet") if got != state.Stopped.String() { t.Errorf("kubelet status = %q; want = %q", got, state.Stopped) } @@ -250,12 +240,12 @@ func testPause(ctx context.Context, t *testing.T, profile string) { t.Fatalf("%s failed: %v", rr.Args, err) } - got = status(ctx, t, Target(), profile, "APIServer") + got = Status(ctx, t, Target(), profile, "APIServer") if got != state.Running.String() { t.Errorf("apiserver status = %q; want = %q", got, state.Running) } - got = status(ctx, t, Target(), profile, "Kubelet") + got = Status(ctx, t, Target(), profile, "Kubelet") if got != state.Running.String() { t.Errorf("kubelet status = %q; want = %q", got, state.Running) }