call docker version once and testing

pull/15646/head
Steven Powell 2023-01-13 15:00:27 -08:00
parent 4bd288c4bf
commit c27da1c443
2 changed files with 29 additions and 14 deletions

View File

@ -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
}

View File

@ -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)
}
}
}