forked from d2iq-archive/cake-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_client_test.go
227 lines (189 loc) · 6.05 KB
/
docker_client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
import (
"context"
"io"
"io/ioutil"
"os"
"path"
"reflect"
"sort"
"strings"
"testing"
"github.com/docker/docker/api/types"
)
type MockDockerClient struct {
MockTagsResponse []string
MockImageBuildResponse types.ImageBuildResponse
ImageBuildOptions types.ImageBuildOptions
ImagePushOptions types.ImagePushOptions
ImagePushTags []string
}
func (client *MockDockerClient) Tags(repository string, imageName string) (tags []string, err error) {
return client.MockTagsResponse, nil
}
func (client *MockDockerClient) ImageBuild(ctx context.Context, buildContext io.Reader, options types.ImageBuildOptions) (types.ImageBuildResponse, error) {
client.ImageBuildOptions = options
response := types.ImageBuildResponse{
Body: ioutil.NopCloser(strings.NewReader(`{"message": "image built"}`)),
}
//setting input options here to verify them in test
client.ImageBuildOptions = options
return response, nil
}
func (client *MockDockerClient) ImagePush(ctx context.Context, image string, options types.ImagePushOptions) (io.ReadCloser, error) {
client.ImagePushOptions = options
//this method called multiple times so we need to collect all the pushed tags
client.ImagePushTags = append(client.ImagePushTags, image)
return ioutil.NopCloser(strings.NewReader(`{"message": "image pushed"}`)), nil
}
func TestImageExists(t *testing.T) {
testTag := "test_tag"
client := new(MockDockerClient)
client.MockTagsResponse = []string{testTag}
image := Image{ImageConfig: ImageConfig{Repository: "repository", Name: "image-name"}}
config := BuildConfig{
ReleaseTag: testTag,
}
exists, err := imageExists(client, &image, config)
if err != nil {
t.Error(err)
}
if !exists {
t.Errorf("Expected to find the tag but 'imageExists' returned false")
}
config.ReleaseTag = "nonexistent"
exists, err = imageExists(client, &image, config)
if err != nil {
t.Error(err)
}
if exists {
t.Errorf("Expected the tag to be absent but 'imageExists' returned true")
}
}
func TestImageBuild(t *testing.T) {
baseDir, err := ioutil.TempDir("", "base") //Docker build context root
if err != nil {
t.Errorf("Failed to create temporary directory: %v", err)
}
_, err = os.Create(path.Join(baseDir, "script.sh"))
if err != nil {
t.Errorf("Failed to create file: %v", err)
}
buildConfig := BuildConfig{
BaseDir: baseDir,
ReleaseTag: "1.0",
AuthConfig: AuthConfig{
DockerRegistryUrl: "https://index.docker.io",
Username: "user",
Password: "password",
},
}
imageConfig := ImageConfig{
Repository: "repository",
Name: "image-name",
}
image := Image{
Dockerfile: "base/Dockerfile",
ImageConfig: imageConfig,
Checksum: "12w21ew",
}
expectedTags := []string{
"repository/image-name:latest",
"repository/image-name:" + buildConfig.ReleaseTag,
"repository/image-name:" + image.Checksum,
}
sort.Strings(expectedTags)
dockerClient := new(MockDockerClient)
err = buildImage(dockerClient, &image, buildConfig)
if err != nil {
t.Error(err)
}
buildOptions := dockerClient.ImageBuildOptions
if buildOptions.Dockerfile != image.Dockerfile {
t.Errorf("Expected Dockerfile %s but found %s in ImageBuildOptions", image.Dockerfile, buildOptions.Dockerfile)
}
sort.Strings(buildOptions.Tags)
if !reflect.DeepEqual(expectedTags, buildOptions.Tags) {
t.Errorf("Tags in ImageBuildOptions differ from the expected.\nExpected:\n%s\nFound:\n%s", expectedTags, buildOptions.Tags)
}
if len(buildOptions.AuthConfigs) != 1 {
t.Errorf("Expected single entry in AuthConfigs but found: %s", buildOptions.AuthConfigs)
}
authConfig, found := buildOptions.AuthConfigs[buildConfig.AuthConfig.DockerRegistryUrl]
if !found {
t.Errorf("Expected config is not present in AuthConfigs for registry: %s", buildConfig.AuthConfig.DockerRegistryUrl)
}
base64Auth, err := base64Auth(buildConfig)
if err != nil {
t.Error(err)
}
if authConfig.Auth != base64Auth {
t.Errorf("Base64 Auth differs from expected in ImageBuildOptions.AuthConfigs:\nExpected:\n%s\nFound:\n%s", base64Auth, authConfig.Auth)
}
}
func TestPushImage(t *testing.T) {
baseDir, err := ioutil.TempDir("", "base") //Docker build context root
if err != nil {
t.Errorf("Failed to create temporary directory: %v", err)
}
_, err = os.Create(path.Join(baseDir, "script.sh"))
if err != nil {
t.Errorf("Failed to create file: %v", err)
}
buildConfig := BuildConfig{
ReleaseTag: "1.0",
AuthConfig: AuthConfig{
DockerRegistryUrl: "https://index.docker.io",
Username: "user",
Password: "password",
},
}
imageConfig := ImageConfig{
Repository: "repository",
Name: "image-name",
}
image := Image{
ImageConfig: imageConfig,
Checksum: "12w21ew",
}
expectedTags := []string{
"repository/image-name:latest",
"repository/image-name:" + buildConfig.ReleaseTag,
"repository/image-name:" + image.Checksum,
}
sort.Strings(expectedTags)
dockerClient := new(MockDockerClient)
err = pushImage(dockerClient, &image, buildConfig)
if err != nil {
t.Error(err)
}
pushOptions := dockerClient.ImagePushOptions
base64Auth, err := base64Auth(buildConfig)
if err != nil {
t.Error(err)
}
if pushOptions.RegistryAuth != base64Auth {
t.Errorf("Base64 Auth differs from expected in ImagePushOptions.RegistryAuth:\nExpected:\n%s\nFound:\n%s", base64Auth, pushOptions.RegistryAuth)
}
sort.Strings(dockerClient.ImagePushTags)
if !reflect.DeepEqual(expectedTags, dockerClient.ImagePushTags) {
t.Errorf("Tags in ImagePushTags differ from the expected.\nExpected:\n%s\nFound:\n%s", expectedTags, dockerClient.ImagePushTags)
}
}
func TestBase64Auth(t *testing.T) {
buildConfig := BuildConfig{
AuthConfig: AuthConfig{
Username: "user",
Password: "password",
},
}
//Base64 for the following JSON: {"username":"user","password":"password"}
base64 := "eyJ1c2VybmFtZSI6InVzZXIiLCJwYXNzd29yZCI6InBhc3N3b3JkIn0="
base64Auth, err := base64Auth(buildConfig)
if err != nil {
t.Error(err)
}
if base64 != base64Auth {
t.Errorf("Base64-encoded Auth object differs from expected.\nExpected:\n%s\nFound:\n%s", base64, base64Auth)
}
}