Skip to content

Commit cd19c51

Browse files
authored
Merge pull request #5790 from tstromberg/service
service: fix --url mode, add integration tests
2 parents 0e2544b + c34e865 commit cd19c51

File tree

3 files changed

+90
-24
lines changed

3 files changed

+90
-24
lines changed

Diff for: cmd/minikube/cmd/service.go

+19-13
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@ package cmd
1818

1919
import (
2020
"fmt"
21+
"net/url"
2122
"os"
2223
"text/template"
2324

25+
"github.com/golang/glog"
2426
"github.com/pkg/browser"
25-
26-
"k8s.io/minikube/pkg/minikube/out"
27-
2827
"github.com/spf13/cobra"
28+
2929
"k8s.io/minikube/pkg/minikube/cluster"
3030
"k8s.io/minikube/pkg/minikube/exit"
3131
"k8s.io/minikube/pkg/minikube/machine"
32+
"k8s.io/minikube/pkg/minikube/out"
3233
"k8s.io/minikube/pkg/minikube/service"
3334
)
3435

@@ -74,21 +75,26 @@ var serviceCmd = &cobra.Command{
7475
os.Exit(1)
7576
}
7677

77-
var urlString []string
78-
79-
urlString, err = service.WaitForService(api, namespace, svc,
80-
serviceURLTemplate, serviceURLMode, https, wait, interval)
78+
urls, err := service.WaitForService(api, namespace, svc, serviceURLTemplate, serviceURLMode, https, wait, interval)
8179
if err != nil {
8280
exit.WithError("Error opening service", err)
8381
}
8482

85-
if len(urlString) != 0 {
86-
out.T(out.Celebrate, "Opening kubernetes service {{.namespace_name}}/{{.service_name}} in default browser...", out.V{"namespace_name": namespace, "service_name": svc})
83+
for _, u := range urls {
84+
_, err := url.Parse(u)
85+
if err != nil {
86+
glog.Warningf("failed to parse url %q: %v (will not open)", u, err)
87+
out.String(fmt.Sprintf("%s\n", u))
88+
continue
89+
}
8790

88-
for _, url := range urlString {
89-
if err := browser.OpenURL(url); err != nil {
90-
exit.WithError(fmt.Sprintf("browser failed to open url %s", url), err)
91-
}
91+
if serviceURLMode {
92+
out.String(fmt.Sprintf("%s\n", u))
93+
continue
94+
}
95+
out.T(out.Celebrate, "Opening service {{.namespace_name}}/{{.service_name}} in default browser...", out.V{"namespace_name": namespace, "service_name": svc})
96+
if err := browser.OpenURL(u); err != nil {
97+
exit.WithError(fmt.Sprintf("open url failed: %s", u), err)
9298
}
9399
}
94100
},

Diff for: pkg/minikube/service/service.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,8 @@ func WaitForService(api libmachine.API, namespace string, service string, urlTem
300300
}
301301

302302
for _, bareURLString := range serviceURL.URLs {
303-
url, isHTTPSchemedURL := OptionallyHTTPSFormattedURLString(bareURLString, https)
304-
305-
if urlMode || !isHTTPSchemedURL {
306-
out.T(out.Empty, url)
307-
urlList = append(urlList, url)
308-
}
303+
url, _ := OptionallyHTTPSFormattedURLString(bareURLString, https)
304+
urlList = append(urlList, url)
309305
}
310306
return urlList, nil
311307
}

Diff for: test/integration/functional_test.go

+69-5
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func TestFunctional(t *testing.T) {
8989
{"LogsCmd", validateLogsCmd},
9090
{"MountCmd", validateMountCmd},
9191
{"ProfileCmd", validateProfileCmd},
92-
{"ServicesCmd", validateServicesCmd},
92+
{"ServiceCmd", validateServiceCmd},
9393
{"AddonsCmd", validateAddonsCmd},
9494
{"PersistentVolumeClaim", validatePersistentVolumeClaim},
9595
{"TunnelCmd", validateTunnelCmd},
@@ -397,13 +397,77 @@ func validateProfileCmd(ctx context.Context, t *testing.T, profile string) {
397397
}
398398

399399
// validateServiceCmd asserts basic "service" command functionality
400-
func validateServicesCmd(ctx context.Context, t *testing.T, profile string) {
401-
rr, err := Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "service", "list"))
400+
func validateServiceCmd(ctx context.Context, t *testing.T, profile string) {
401+
rr, err := Run(t, exec.CommandContext(ctx, "kubectl", "--context", profile, "create", "deployment", "hello-node", "--image=gcr.io/hello-minikube-zero-install/hello-node"))
402+
if err != nil {
403+
t.Logf("%s failed: %v (may not be an error)", rr.Args, err)
404+
}
405+
rr, err = Run(t, exec.CommandContext(ctx, "kubectl", "--context", profile, "expose", "deployment", "hello-node", "--type=NodePort", "--port=8080"))
406+
if err != nil {
407+
t.Logf("%s failed: %v (may not be an error)", rr.Args, err)
408+
}
409+
410+
if _, err := PodWait(ctx, t, profile, "default", "app=hello-node", 4*time.Minute); err != nil {
411+
t.Fatalf("wait: %v", err)
412+
}
413+
414+
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "service", "list"))
402415
if err != nil {
403416
t.Errorf("%s failed: %v", rr.Args, err)
404417
}
405-
if !strings.Contains(rr.Stdout.String(), "kubernetes") {
406-
t.Errorf("service list got %q, wanted *kubernetes*", rr.Stdout.String())
418+
if !strings.Contains(rr.Stdout.String(), "hello-node") {
419+
t.Errorf("service list got %q, wanted *hello-node*", rr.Stdout.String())
420+
}
421+
422+
// Test --https --url mode
423+
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "service", "--namespace=default", "--https", "--url", "hello-node"))
424+
if err != nil {
425+
t.Fatalf("%s failed: %v", rr.Args, err)
426+
}
427+
if rr.Stderr.String() != "" {
428+
t.Errorf("unexpected stderr output: %s", rr.Stderr)
429+
}
430+
431+
endpoint := strings.TrimSpace(rr.Stdout.String())
432+
u, err := url.Parse(endpoint)
433+
if err != nil {
434+
t.Fatalf("failed to parse %q: %v", endpoint, err)
435+
}
436+
if u.Scheme != "https" {
437+
t.Errorf("got scheme: %q, expected: %q", u.Scheme, "https")
438+
}
439+
440+
// Test --format=IP
441+
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "service", "hello-node", "--url", "--format={{.IP}}"))
442+
if err != nil {
443+
t.Errorf("%s failed: %v", rr.Args, err)
444+
}
445+
if strings.TrimSpace(rr.Stdout.String()) != u.Hostname() {
446+
t.Errorf("%s = %q, wanted %q", rr.Args, rr.Stdout.String(), u.Hostname())
447+
}
448+
449+
// Test a regular URLminikube
450+
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "service", "hello-node", "--url"))
451+
if err != nil {
452+
t.Errorf("%s failed: %v", rr.Args, err)
453+
}
454+
455+
endpoint = strings.TrimSpace(rr.Stdout.String())
456+
u, err = url.Parse(endpoint)
457+
if err != nil {
458+
t.Fatalf("failed to parse %q: %v", endpoint, err)
459+
}
460+
if u.Scheme != "http" {
461+
t.Fatalf("got scheme: %q, expected: %q", u.Scheme, "http")
462+
}
463+
464+
t.Logf("url: %s", endpoint)
465+
resp, err := retryablehttp.Get(endpoint)
466+
if err != nil {
467+
t.Fatalf("get failed: %v\nresp: %v", err, resp)
468+
}
469+
if resp.StatusCode != http.StatusOK {
470+
t.Fatalf("%s = status code %d, want %d", u, resp.StatusCode, http.StatusOK)
407471
}
408472
}
409473

0 commit comments

Comments
 (0)