-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: code coverage improvements (push.go)
- Loading branch information
1 parent
aa5f12e
commit 6ac6317
Showing
3 changed files
with
102 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |