Skip to content

Commit

Permalink
fix: check if docker is running should work for windows (#5660)
Browse files Browse the repository at this point in the history
Fixes #5656 


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the Apache 2.0 License.
  • Loading branch information
iamhopaul123 authored Feb 12, 2024
1 parent d3b36ac commit 5ee9a70
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
15 changes: 9 additions & 6 deletions internal/pkg/docker/dockerengine/dockerengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"os"
osexec "os/exec"
"path/filepath"
"regexp"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -437,8 +438,10 @@ func (d *DockerCmdClient) containerState(ctx context.Context, containerName stri
if err := d.runner.RunWithContext(ctx, "docker", []string{"inspect", "--format", "{{json .State}}", containerID}, exec.Stdout(buf)); err != nil {
return ContainerState{}, fmt.Errorf("run docker inspect: %w", err)
}
// Make sure we unmarshal a valid json string.
out := regexp.MustCompile(`{(.|\n)*}`).FindString(buf.String())
var containerState ContainerState
if err := json.Unmarshal([]byte(strings.TrimSpace(buf.String())), &containerState); err != nil {
if err := json.Unmarshal([]byte(out), &containerState); err != nil {
return ContainerState{}, fmt.Errorf("unmarshal state of container %q:%w", containerName, err)
}
return containerState, nil
Expand Down Expand Up @@ -477,13 +480,12 @@ func (c DockerCmdClient) CheckDockerEngineRunning() error {
return ErrDockerCommandNotFound
}
buf := &bytes.Buffer{}
err := c.runner.Run("docker", []string{"info", "-f", "'{{json .}}'"}, exec.Stdout(buf))
err := c.runner.Run("docker", []string{"info", "-f", "json"}, exec.Stdout(buf))
if err != nil {
return fmt.Errorf("get docker info: %w", err)
}
// Trim redundant prefix and suffix. For example: '{"ServerErrors":["Cannot connect...}'\n returns
// {"ServerErrors":["Cannot connect...}
out := strings.TrimSuffix(strings.TrimPrefix(strings.TrimSpace(buf.String()), "'"), "'")
// Make sure we unmarshal a valid json string.
out := regexp.MustCompile(`{(.|\n)*}`).FindString(buf.String())
type dockerEngineNotRunningMsg struct {
ServerErrors []string `json:"ServerErrors"`
}
Expand All @@ -510,7 +512,8 @@ func (c DockerCmdClient) GetPlatform() (os, arch string, err error) {
return "", "", fmt.Errorf("run docker version: %w", err)
}

out := strings.TrimSuffix(strings.TrimPrefix(strings.TrimSpace(buf.String()), "'"), "'")
// Make sure we unmarshal a valid json string.
out := regexp.MustCompile(`{(.|\n)*}`).FindString(buf.String())
type dockerServer struct {
OS string `json:"Os"`
Arch string `json:"Arch"`
Expand Down
9 changes: 5 additions & 4 deletions internal/pkg/docker/dockerengine/dockerengine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,19 +425,20 @@ func TestDockerCommand_CheckDockerEngineRunning(t *testing.T) {
"error running docker info": {
setupMocks: func(controller *gomock.Controller) {
mockCmd = NewMockCmd(controller)
mockCmd.EXPECT().Run("docker", []string{"info", "-f", "'{{json .}}'"}, gomock.Any()).Return(mockError)
mockCmd.EXPECT().Run("docker", []string{"info", "-f", "json"}, gomock.Any()).Return(mockError)
},

wantedErr: fmt.Errorf("get docker info: some error"),
},
"return when docker engine is not started": {
setupMocks: func(controller *gomock.Controller) {
mockCmd = NewMockCmd(controller)
mockCmd.EXPECT().Run("docker", []string{"info", "-f", "'{{json .}}'"}, gomock.Any()).
mockCmd.EXPECT().Run("docker", []string{"info", "-f", "json"}, gomock.Any()).
Do(func(_ string, _ []string, opt exec.CmdOption) {
cmd := &osexec.Cmd{}
opt(cmd)
_, _ = cmd.Stdout.Write([]byte(`'{"ServerErrors":["Cannot connect to the Docker daemon at unix:///var/run/docker.sock.", "Is the docker daemon running?"]}'`))
_, _ = cmd.Stdout.Write([]byte(`Cannot connect to the Docker daemon at unix:///Users/penghaoh/.docker/run/docker.sock. Is the docker daemon running?
'{"ID":"","Containers":0,"ContainersRunning":0,"ServerErrors":["Cannot connect to the Docker daemon at unix:///var/run/docker.sock.", "Is the docker daemon running?"]}'`))
}).Return(nil)
},

Expand All @@ -448,7 +449,7 @@ func TestDockerCommand_CheckDockerEngineRunning(t *testing.T) {
"success": {
setupMocks: func(controller *gomock.Controller) {
mockCmd = NewMockCmd(controller)
mockCmd.EXPECT().Run("docker", []string{"info", "-f", "'{{json .}}'"}, gomock.Any()).
mockCmd.EXPECT().Run("docker", []string{"info", "-f", "json"}, gomock.Any()).
Do(func(_ string, _ []string, opt exec.CmdOption) {
cmd := &osexec.Cmd{}
opt(cmd)
Expand Down

0 comments on commit 5ee9a70

Please sign in to comment.