From d50939b91aa8bb377b086879d867785486d12969 Mon Sep 17 00:00:00 2001 From: iamhopaul123 Date: Tue, 6 Feb 2024 11:13:10 -0800 Subject: [PATCH 1/4] fix: check if docker is running should work for windows --- internal/pkg/docker/dockerengine/dockerengine.go | 2 +- internal/pkg/docker/dockerengine/dockerengine_test.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/pkg/docker/dockerengine/dockerengine.go b/internal/pkg/docker/dockerengine/dockerengine.go index 5f216a584b6..7d587e66101 100644 --- a/internal/pkg/docker/dockerengine/dockerengine.go +++ b/internal/pkg/docker/dockerengine/dockerengine.go @@ -477,7 +477,7 @@ 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) } diff --git a/internal/pkg/docker/dockerengine/dockerengine_test.go b/internal/pkg/docker/dockerengine/dockerengine_test.go index a2c6d5609de..419a9fb493e 100644 --- a/internal/pkg/docker/dockerengine/dockerengine_test.go +++ b/internal/pkg/docker/dockerengine/dockerengine_test.go @@ -425,7 +425,7 @@ 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"), @@ -433,7 +433,7 @@ func TestDockerCommand_CheckDockerEngineRunning(t *testing.T) { "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) @@ -448,7 +448,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) From e7b7cf2b6862fd1e1efb6645cdd6a97ce1873761 Mon Sep 17 00:00:00 2001 From: iamhopaul123 Date: Tue, 6 Feb 2024 13:35:12 -0800 Subject: [PATCH 2/4] Use regex to trim --- internal/pkg/docker/dockerengine/dockerengine.go | 12 ++++++++---- .../pkg/docker/dockerengine/dockerengine_test.go | 3 ++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/internal/pkg/docker/dockerengine/dockerengine.go b/internal/pkg/docker/dockerengine/dockerengine.go index 7d587e66101..1b187ce5c6e 100644 --- a/internal/pkg/docker/dockerengine/dockerengine.go +++ b/internal/pkg/docker/dockerengine/dockerengine.go @@ -15,6 +15,7 @@ import ( "os" osexec "os/exec" "path/filepath" + "regexp" "sort" "strings" "sync" @@ -481,14 +482,17 @@ func (c DockerCmdClient) CheckDockerEngineRunning() error { 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()), "'"), "'") + // Trim redundant prefix and suffix. For example: '...{"ServerErrors":["Cannot connect...}...' returns + // '"ServerErrors":["Cannot connect..."]' + match := regexp.MustCompile(`"ServerErrors"\s*:\s*\[.*?\]`).FindString(buf.String()) + if len(match) == 0 { + return nil + } type dockerEngineNotRunningMsg struct { ServerErrors []string `json:"ServerErrors"` } var msg dockerEngineNotRunningMsg - if err := json.Unmarshal([]byte(out), &msg); err != nil { + if err := json.Unmarshal([]byte("{"+match+"}"), &msg); err != nil { return fmt.Errorf("unmarshal docker info message: %w", err) } if len(msg.ServerErrors) == 0 { diff --git a/internal/pkg/docker/dockerengine/dockerengine_test.go b/internal/pkg/docker/dockerengine/dockerengine_test.go index 419a9fb493e..b444ec3bdc6 100644 --- a/internal/pkg/docker/dockerengine/dockerengine_test.go +++ b/internal/pkg/docker/dockerengine/dockerengine_test.go @@ -437,7 +437,8 @@ func TestDockerCommand_CheckDockerEngineRunning(t *testing.T) { 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) }, From b7b13ffa7b49a56cb47c2e516c7ee5fde2ff1908 Mon Sep 17 00:00:00 2001 From: iamhopaul123 Date: Fri, 9 Feb 2024 15:08:37 -0800 Subject: [PATCH 3/4] Add more trimming --- .../pkg/docker/dockerengine/dockerengine.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/internal/pkg/docker/dockerengine/dockerengine.go b/internal/pkg/docker/dockerengine/dockerengine.go index 1b187ce5c6e..de7599130c2 100644 --- a/internal/pkg/docker/dockerengine/dockerengine.go +++ b/internal/pkg/docker/dockerengine/dockerengine.go @@ -438,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(`{.*}`).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 @@ -482,17 +484,13 @@ func (c DockerCmdClient) CheckDockerEngineRunning() error { if err != nil { return fmt.Errorf("get docker info: %w", err) } - // Trim redundant prefix and suffix. For example: '...{"ServerErrors":["Cannot connect...}...' returns - // '"ServerErrors":["Cannot connect..."]' - match := regexp.MustCompile(`"ServerErrors"\s*:\s*\[.*?\]`).FindString(buf.String()) - if len(match) == 0 { - return nil - } + // Make sure we unmarshal a valid json string. + out := regexp.MustCompile(`{.*}`).FindString(buf.String()) type dockerEngineNotRunningMsg struct { ServerErrors []string `json:"ServerErrors"` } var msg dockerEngineNotRunningMsg - if err := json.Unmarshal([]byte("{"+match+"}"), &msg); err != nil { + if err := json.Unmarshal([]byte(out), &msg); err != nil { return fmt.Errorf("unmarshal docker info message: %w", err) } if len(msg.ServerErrors) == 0 { @@ -514,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(`{.*}`).FindString(buf.String()) type dockerServer struct { OS string `json:"Os"` Arch string `json:"Arch"` From 7ce99505ae6c87e5e3980aeed1b1c1ef4e10101c Mon Sep 17 00:00:00 2001 From: iamhopaul123 Date: Fri, 9 Feb 2024 15:20:55 -0800 Subject: [PATCH 4/4] Include line break --- internal/pkg/docker/dockerengine/dockerengine.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/pkg/docker/dockerengine/dockerengine.go b/internal/pkg/docker/dockerengine/dockerengine.go index de7599130c2..7bc919a80fb 100644 --- a/internal/pkg/docker/dockerengine/dockerengine.go +++ b/internal/pkg/docker/dockerengine/dockerengine.go @@ -439,7 +439,7 @@ func (d *DockerCmdClient) containerState(ctx context.Context, containerName stri return ContainerState{}, fmt.Errorf("run docker inspect: %w", err) } // Make sure we unmarshal a valid json string. - out := regexp.MustCompile(`{.*}`).FindString(buf.String()) + out := regexp.MustCompile(`{(.|\n)*}`).FindString(buf.String()) var containerState ContainerState if err := json.Unmarshal([]byte(out), &containerState); err != nil { return ContainerState{}, fmt.Errorf("unmarshal state of container %q:%w", containerName, err) @@ -485,7 +485,7 @@ func (c DockerCmdClient) CheckDockerEngineRunning() error { return fmt.Errorf("get docker info: %w", err) } // Make sure we unmarshal a valid json string. - out := regexp.MustCompile(`{.*}`).FindString(buf.String()) + out := regexp.MustCompile(`{(.|\n)*}`).FindString(buf.String()) type dockerEngineNotRunningMsg struct { ServerErrors []string `json:"ServerErrors"` } @@ -513,7 +513,7 @@ func (c DockerCmdClient) GetPlatform() (os, arch string, err error) { } // Make sure we unmarshal a valid json string. - out := regexp.MustCompile(`{.*}`).FindString(buf.String()) + out := regexp.MustCompile(`{(.|\n)*}`).FindString(buf.String()) type dockerServer struct { OS string `json:"Os"` Arch string `json:"Arch"`