From c27da1c44300367f5e86585501439a07c521d2f5 Mon Sep 17 00:00:00 2001 From: Steven Powell Date: Fri, 13 Jan 2023 15:00:27 -0800 Subject: [PATCH] call docker version once and testing --- pkg/minikube/registry/drvs/docker/docker.go | 21 +++++++----------- .../registry/drvs/docker/docker_test.go | 22 ++++++++++++++++++- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/pkg/minikube/registry/drvs/docker/docker.go b/pkg/minikube/registry/drvs/docker/docker.go index bcb40f1d77..45bcc90bfd 100644 --- a/pkg/minikube/registry/drvs/docker/docker.go +++ b/pkg/minikube/registry/drvs/docker/docker.go @@ -102,7 +102,7 @@ func status() (retState registry.State) { ctx, cancel := context.WithTimeout(context.Background(), 6*time.Second) defer cancel() - cmd := exec.CommandContext(ctx, oci.Docker, "version", "--format", "{{.Server.Os}}-{{.Server.Version}}") + cmd := exec.CommandContext(ctx, oci.Docker, "version", "--format", "{{.Server.Os}}-{{.Server.Version}}:{{.Server.Platform.Name}}") o, err := cmd.Output() if err != nil { reason := "" @@ -135,12 +135,15 @@ func status() (retState registry.State) { } }() + versions := strings.Split(string(o), ":") + dockerEngineVersion := versions[0] + dockerPlatformVersion := versions[1] klog.Infof("docker version: %s", o) if !viper.GetBool("force") { - if s := checkDockerDesktopVersion(); s != nil { + if s := checkDockerDesktopVersion(dockerPlatformVersion); s != nil { return *s } - s := checkDockerEngineVersion(strings.TrimSpace(string(o))) // remove '\n' from o at the end + s := checkDockerEngineVersion(strings.TrimSpace(dockerEngineVersion)) // remove '\n' from o at the end if s.Error != nil { return s } @@ -243,16 +246,8 @@ func checkDockerEngineVersion(o string) registry.State { Doc: docURL + "#requirements"} } -func checkDockerDesktopVersion() *registry.State { - ctx, cancel := context.WithTimeout(context.Background(), 6*time.Second) - defer cancel() - - o, err := exec.CommandContext(ctx, oci.Docker, "version", "--format", "{{.Server.Platform.Name}}").Output() - if err != nil { - klog.Warningf("failed to get docker version: %v", err) - return nil - } - fields := strings.Fields(string(o)) +func checkDockerDesktopVersion(version string) *registry.State { + fields := strings.Fields(version) if len(fields) < 3 || fields[0] != "Docker" || fields[1] != "Desktop" { return nil } diff --git a/pkg/minikube/registry/drvs/docker/docker_test.go b/pkg/minikube/registry/drvs/docker/docker_test.go index 2f3bdcf8dc..9951603822 100644 --- a/pkg/minikube/registry/drvs/docker/docker_test.go +++ b/pkg/minikube/registry/drvs/docker/docker_test.go @@ -64,7 +64,7 @@ func stringToIntSlice(t *testing.T, s string) []int { return []int{int(sem.Major), int(sem.Minor), int(sem.Patch)} } -func TestCheckDockerVersion(t *testing.T) { +func TestCheckDockerEngineVersion(t *testing.T) { recParts := stringToIntSlice(t, recommendedDockerVersion) minParts := stringToIntSlice(t, minDockerVersion) @@ -150,3 +150,23 @@ func TestCheckDockerVersion(t *testing.T) { }) } } + +func TestCheckDockerDesktopVersion(t *testing.T) { + tests := []struct { + input string + shouldReturnState bool + }{ + {"Docker Desktop", false}, + {"Cat Desktop 4.16.0", false}, + {"Docker Playground 4.16.0", false}, + {"Docker Desktop 4.15.0", false}, + {"Docker Desktop 4.16.0", true}, + {" Docker Desktop 4.16.0 ", true}, + } + for _, tt := range tests { + state := checkDockerDesktopVersion(tt.input) + if (state == nil && tt.shouldReturnState) || (state != nil && !tt.shouldReturnState) { + t.Errorf("checkDockerDesktopVersion(%q) = %v; expected shouldRetunState = %t", tt.input, state, tt.shouldReturnState) + } + } +}