From 6ac63179198d39249067c975caac1f88f9253dce Mon Sep 17 00:00:00 2001 From: Tomasz Zaremba Date: Fri, 6 Oct 2023 10:17:22 +0200 Subject: [PATCH] chore: code coverage improvements (push.go) --- src/cli/build_test.go | 44 ++-------------------------- src/cli/docker_client_test.go | 45 ++++++++++++++++++++++++++++ src/cli/push_test.go | 55 +++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 42 deletions(-) create mode 100644 src/cli/docker_client_test.go create mode 100644 src/cli/push_test.go diff --git a/src/cli/build_test.go b/src/cli/build_test.go index ac4c276..4dd6e41 100644 --- a/src/cli/build_test.go +++ b/src/cli/build_test.go @@ -1,26 +1,15 @@ package cli import ( - "bytes" "fmt" - "github.com/docker/docker/client" "github.com/nearform/initium-cli/src/services/docker" "github.com/nearform/initium-cli/src/services/project" "github.com/nearform/initium-cli/src/utils/defaults" - "io" - "net/http" "os" "path" - "strings" "testing" ) -type transportFunc func(*http.Request) (*http.Response, error) - -func (tf transportFunc) RoundTrip(req *http.Request) (*http.Response, error) { - return tf(req) -} - func TestShouldSendBuildRequestToDockerDaemon(t *testing.T) { proj := project.Project{ Name: "initium-cli", @@ -35,7 +24,8 @@ func TestShouldSendBuildRequestToDockerDaemon(t *testing.T) { Tag: "v1.1.0", } - dockerClient, err := getMockDockerClient(t) + var apiRequests []dockerApiRequest + dockerClient, err := newAlwaysOkMockDockerClient(&apiRequests) if err != nil { t.Fatalf(fmt.Sprintf("Error: %s", err)) } @@ -50,33 +40,3 @@ func TestShouldSendBuildRequestToDockerDaemon(t *testing.T) { t.Fatalf(fmt.Sprintf("Error: %s", err)) } } - -func getMockDockerClient(t *testing.T) (client.Client, error) { - handler := func(request *http.Request) (*http.Response, error) { - if request.Method != http.MethodPost { - t.Fatalf("POST method call expected") - } - - if !strings.HasSuffix(request.URL.Path, "/build") { - t.Fatalf("/build URL call expected") - } - - header := http.Header{} - header.Set("Content-Type", "application/json") - - return &http.Response{ - StatusCode: 200, - Body: io.NopCloser(bytes.NewReader([]byte("{}"))), - Header: header, - }, nil - } - - mockClient, err := client.NewClientWithOpts(client.WithHTTPClient(&http.Client{ - Transport: transportFunc(handler), - })) - if err != nil { - return client.Client{}, err - } - - return *mockClient, nil -} diff --git a/src/cli/docker_client_test.go b/src/cli/docker_client_test.go new file mode 100644 index 0000000..096ef1f --- /dev/null +++ b/src/cli/docker_client_test.go @@ -0,0 +1,45 @@ +package cli + +import ( + "bytes" + "github.com/docker/docker/client" + "io" + "net/http" +) + +type dockerApiRequest struct { + httpMethod string + url string +} + +type transportFunc func(*http.Request) (*http.Response, error) + +func (tf transportFunc) RoundTrip(req *http.Request) (*http.Response, error) { + return tf(req) +} + +func newAlwaysOkMockDockerClient(apiRequests *[]dockerApiRequest) (client.Client, error) { + handler := func(request *http.Request) (*http.Response, error) { + *apiRequests = append(*apiRequests, dockerApiRequest{ + httpMethod: request.Method, + url: request.URL.Path, + }) + + header := http.Header{} + header.Set("Content-Type", "application/json") + + return &http.Response{ + StatusCode: 200, + Body: io.NopCloser(bytes.NewReader([]byte("{}"))), + Header: header, + }, nil + } + mockClient, err := client.NewClientWithOpts(client.WithHTTPClient(&http.Client{ + Transport: transportFunc(handler), + })) + if err != nil { + return client.Client{}, err + } + + return *mockClient, nil +} diff --git a/src/cli/push_test.go b/src/cli/push_test.go new file mode 100644 index 0000000..2fb1257 --- /dev/null +++ b/src/cli/push_test.go @@ -0,0 +1,55 @@ +package cli + +import ( + "fmt" + "github.com/nearform/initium-cli/src/services/docker" + "github.com/nearform/initium-cli/src/services/project" + "github.com/nearform/initium-cli/src/utils/defaults" + "gotest.tools/v3/assert" + "net/http" + "os" + "path" + "strings" + "testing" +) + +func TestShouldSendImagePushRequestToDockerDaemon(t *testing.T) { + proj := project.Project{ + Name: "initium-cli", + Directory: path.Join("../../", "."), + Resources: os.DirFS("../../"), + } + + dockerImage := docker.DockerImage{ + Registry: "example.org", + Directory: defaults.ProjectDirectory, + Name: "test", + Tag: "v1.1.0", + } + + var apiRequests []dockerApiRequest + dockerClient, err := newAlwaysOkMockDockerClient(&apiRequests) + if err != nil { + t.Fatalf(fmt.Sprintf("Error: %s", err)) + } + + ds, err := docker.NewWithDockerClient(proj, dockerImage, defaults.GeneratedDockerFile, &dockerClient) + if err != nil { + t.Fatalf(fmt.Sprintf("Error: %s", err)) + } + + err = ds.Push() + if err != nil { + t.Fatalf(fmt.Sprintf("Error: %s", err)) + } + + assert.Check(t, len(apiRequests) == 2, "Expected 2 requests to be sent to Docker API") + + imageTagRequest := apiRequests[0] + assert.Assert(t, imageTagRequest.httpMethod == http.MethodPost, "Expected POST method to be called") + assert.Assert(t, strings.HasSuffix(imageTagRequest.url, "/images/test:v1.1.0/tag"), "Expected URL suffix to be /images/test:v1.1.0/tag") + + imagePushRequest := apiRequests[1] + assert.Assert(t, imagePushRequest.httpMethod == http.MethodPost, "Expected POST method to be called") + assert.Assert(t, strings.HasSuffix(imagePushRequest.url, "/images/example.org/test/push"), "Expected URL suffix to be /images/test:v1.1.0/tag") +}