service: fix --url mode, add integration tests

pull/5790/head
tstromberg 2019-10-30 10:44:54 -07:00
parent ba8aeaae71
commit f60764b07f
3 changed files with 71 additions and 24 deletions

View File

@ -18,17 +18,18 @@ package cmd
import ( import (
"fmt" "fmt"
"net/url"
"os" "os"
"text/template" "text/template"
"github.com/golang/glog"
"github.com/pkg/browser" "github.com/pkg/browser"
"k8s.io/minikube/pkg/minikube/out"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/minikube/pkg/minikube/cluster" "k8s.io/minikube/pkg/minikube/cluster"
"k8s.io/minikube/pkg/minikube/exit" "k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/machine" "k8s.io/minikube/pkg/minikube/machine"
"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/minikube/service" "k8s.io/minikube/pkg/minikube/service"
) )
@ -74,21 +75,26 @@ var serviceCmd = &cobra.Command{
os.Exit(1) os.Exit(1)
} }
var urlString []string urls, err := service.WaitForService(api, namespace, svc, serviceURLTemplate, serviceURLMode, https, wait, interval)
urlString, err = service.WaitForService(api, namespace, svc,
serviceURLTemplate, serviceURLMode, https, wait, interval)
if err != nil { if err != nil {
exit.WithError("Error opening service", err) exit.WithError("Error opening service", err)
} }
if len(urlString) != 0 { for _, u := range urls {
out.T(out.Celebrate, "Opening kubernetes service {{.namespace_name}}/{{.service_name}} in default browser...", out.V{"namespace_name": namespace, "service_name": svc}) _, err := url.Parse(u)
if err != nil {
for _, url := range urlString { glog.Warning("could not parse %v (will not open)", u, err)
if err := browser.OpenURL(url); err != nil { out.String(u)
exit.WithError(fmt.Sprintf("browser failed to open url %s", url), err) continue
} }
if serviceURLMode {
out.String(u)
continue
}
out.T(out.Celebrate, "Opening service {{.namespace_name}}/{{.service_name}} in default browser...", out.V{"namespace_name": namespace, "service_name": svc})
if err := browser.OpenURL(u); err != nil {
exit.WithError(fmt.Sprintf("open url failed: %s", u), err)
} }
} }
}, },

View File

@ -300,13 +300,9 @@ func WaitForService(api libmachine.API, namespace string, service string, urlTem
} }
for _, bareURLString := range serviceURL.URLs { for _, bareURLString := range serviceURL.URLs {
url, isHTTPSchemedURL := OptionallyHTTPSFormattedURLString(bareURLString, https) url, _ := OptionallyHTTPSFormattedURLString(bareURLString, https)
if urlMode || !isHTTPSchemedURL {
out.T(out.Empty, url)
urlList = append(urlList, url) urlList = append(urlList, url)
} }
}
return urlList, nil return urlList, nil
} }

View File

@ -89,7 +89,7 @@ func TestFunctional(t *testing.T) {
{"LogsCmd", validateLogsCmd}, {"LogsCmd", validateLogsCmd},
{"MountCmd", validateMountCmd}, {"MountCmd", validateMountCmd},
{"ProfileCmd", validateProfileCmd}, {"ProfileCmd", validateProfileCmd},
{"ServicesCmd", validateServicesCmd}, {"ServiceCmd", validateServiceCmd},
{"AddonsCmd", validateAddonsCmd}, {"AddonsCmd", validateAddonsCmd},
{"PersistentVolumeClaim", validatePersistentVolumeClaim}, {"PersistentVolumeClaim", validatePersistentVolumeClaim},
{"TunnelCmd", validateTunnelCmd}, {"TunnelCmd", validateTunnelCmd},
@ -397,13 +397,58 @@ func validateProfileCmd(ctx context.Context, t *testing.T, profile string) {
} }
// validateServiceCmd asserts basic "service" command functionality // validateServiceCmd asserts basic "service" command functionality
func validateServicesCmd(ctx context.Context, t *testing.T, profile string) { func validateServiceCmd(ctx context.Context, t *testing.T, profile string) {
rr, err := Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "service", "list")) rr, err := Run(t, exec.CommandContext(ctx, "kubectl", "--context", profile, "run", "hello-minikube", "--restart=Never", "--image=gcr.io/google_containers/echoserver:1.4", "--port=8080"))
if err != nil {
t.Logf("%s failed: %v (may not be an error)", rr.Args, err)
}
rr, err = Run(t, exec.CommandContext(ctx, "kubectl", "--context", profile, "expose", "deployment", "hello-minikube", "--type=NodePort"))
if err != nil {
t.Logf("%s failed: %v (may not be an error)", rr.Args, err)
}
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "service", "list"))
if err != nil { if err != nil {
t.Errorf("%s failed: %v", rr.Args, err) t.Errorf("%s failed: %v", rr.Args, err)
} }
if !strings.Contains(rr.Stdout.String(), "kubernetes") { if !strings.Contains(rr.Stdout.String(), "hello-minikube") {
t.Errorf("service list got %q, wanted *kubernetes*", rr.Stdout.String()) t.Errorf("service list got %q, wanted *hello-minikube*", rr.Stdout.String())
}
// Test --https --url mode
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "service", "--namespace=default", "--https", "--url", "hello-minikube"))
if err != nil {
t.Errorf("%s failed: %v", rr.Args, err)
}
endpoint := rr.Stdout.String()
u, err := url.Parse(endpoint)
if err != nil {
t.Fatalf("failed to parse %q: %v", endpoint, err)
}
if u.Scheme != "https" {
t.Errorf("got scheme: %q, expected: %q", u.Scheme, "https")
}
// Test --format=IP
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "service", "hello-minikube", "--url", "--format={{.IP}}"))
if err != nil {
t.Errorf("%s failed: %v", rr.Args, err)
}
if rr.Stdout.String() != u.Hostname() {
t.Errorf("%s = %q, wanted %q", rr.Args, rr.Stdout.String(), u.Hostname())
}
// Test a regular URL
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "service", "hello-minikube", "--url"))
if err != nil {
t.Errorf("%s failed: %v", rr.Args, err)
}
resp, err := retryablehttp.Get(rr.Stdout.String())
if err != nil {
t.Errorf("get failed: %v\nresp: %v", err, resp)
}
if resp.StatusCode != http.StatusOK {
t.Errorf("%s = status code %d, want %d", u, resp.StatusCode, http.StatusOK)
} }
} }