Skip to content

Commit

Permalink
chore: code coverage improvements (push.go)
Browse files Browse the repository at this point in the history
  • Loading branch information
zaremba-tomasz committed Oct 6, 2023
1 parent aa5f12e commit 6ac6317
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 42 deletions.
44 changes: 2 additions & 42 deletions src/cli/build_test.go
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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))
}
Expand All @@ -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
}
45 changes: 45 additions & 0 deletions src/cli/docker_client_test.go
Original file line number Diff line number Diff line change
@@ -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
}
55 changes: 55 additions & 0 deletions src/cli/push_test.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit 6ac6317

Please sign in to comment.