Updated dashboard URL detection logic for DashboardCmd test

pull/10509/head
Steven Powell 2021-02-18 13:34:13 -07:00
parent 95dcd96147
commit af4364ba6f
2 changed files with 20 additions and 30 deletions

View File

@ -19,6 +19,7 @@ limitations under the License.
package integration package integration
import ( import (
"bufio"
"bytes" "bytes"
"context" "context"
"encoding/json" "encoding/json"
@ -546,8 +547,11 @@ func validateStatusCmd(ctx context.Context, t *testing.T, profile string) {
func validateDashboardCmd(ctx context.Context, t *testing.T, profile string) { func validateDashboardCmd(ctx context.Context, t *testing.T, profile string) {
defer PostMortemLogs(t, profile) defer PostMortemLogs(t, profile)
mctx, cancel := context.WithTimeout(ctx, Seconds(300))
defer cancel()
args := []string{"dashboard", "--url", "-p", profile, "--alsologtostderr", "-v=1"} args := []string{"dashboard", "--url", "-p", profile, "--alsologtostderr", "-v=1"}
ss, err := Start(t, exec.CommandContext(ctx, Target(), args...)) ss, err := Start(t, exec.CommandContext(mctx, Target(), args...))
if err != nil { if err != nil {
t.Errorf("failed to run minikube dashboard. args %q : %v", args, err) t.Errorf("failed to run minikube dashboard. args %q : %v", args, err)
} }
@ -555,13 +559,12 @@ func validateDashboardCmd(ctx context.Context, t *testing.T, profile string) {
ss.Stop(t) ss.Stop(t)
}() }()
start := time.Now() s, err := dashboardURL(ss.Stdout)
s, err := ReadLineWithTimeout(ss.Stdout, Seconds(300))
if err != nil { if err != nil {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
t.Skipf("failed to read url within %s: %v\noutput: %q\n", time.Since(start), err, s) t.Skip(err)
} }
t.Fatalf("failed to read url within %s: %v\noutput: %q\n", time.Since(start), err, s) t.Fatal(err)
} }
u, err := url.Parse(strings.TrimSpace(s)) u, err := url.Parse(strings.TrimSpace(s))
@ -583,6 +586,18 @@ func validateDashboardCmd(ctx context.Context, t *testing.T, profile string) {
} }
} }
// dashboardURL gets the dashboard URL from the command stdout.
func dashboardURL(b *bufio.Reader) (string, error) {
s := bufio.NewScanner(b)
for s.Scan() {
l := s.Text()
if strings.HasPrefix(l, "http") {
return l, nil
}
}
return "", fmt.Errorf("output didn't produce a URL")
}
// validateDryRun asserts that the dry-run mode quickly exits with the right code // validateDryRun asserts that the dry-run mode quickly exits with the right code
func validateDryRun(ctx context.Context, t *testing.T, profile string) { func validateDryRun(ctx context.Context, t *testing.T, profile string) {
// dry-run mode should always be able to finish quickly (<5s) // dry-run mode should always be able to finish quickly (<5s)

View File

@ -26,31 +26,6 @@ import (
"k8s.io/minikube/pkg/minikube/localpath" "k8s.io/minikube/pkg/minikube/localpath"
) )
// ReadLineWithTimeout reads a line of text from a buffer with a timeout
func ReadLineWithTimeout(b *bufio.Reader, timeout time.Duration) (string, error) {
s := make(chan string)
e := make(chan error)
go func() {
read, err := b.ReadString('\n')
if err != nil {
e <- err
} else {
s <- read
}
close(s)
close(e)
}()
select {
case line := <-s:
return line, nil
case err := <-e:
return "", err
case <-time.After(timeout):
return "", fmt.Errorf("timeout after %s", timeout)
}
}
// UniqueProfileName returns a reasonably unique profile name // UniqueProfileName returns a reasonably unique profile name
func UniqueProfileName(prefix string) string { func UniqueProfileName(prefix string) string {
if *forceProfile != "" { if *forceProfile != "" {