forked from fsouza/go-dockerclient
-
Notifications
You must be signed in to change notification settings - Fork 3
/
build_test.go
166 lines (149 loc) · 3.99 KB
/
build_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
// Copyright 2014 go-dockerclient authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build go1.10
package docker
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"reflect"
"testing"
"github.com/docker/docker/pkg/archive"
)
func TestBuildImageMultipleContextsError(t *testing.T) {
t.Parallel()
fakeRT := &FakeRoundTripper{message: "", status: http.StatusOK}
client := newTestClient(fakeRT)
var buf bytes.Buffer
opts := BuildImageOptions{
Name: "testImage",
NoCache: true,
CacheFrom: []string{"a", "b", "c"},
SuppressOutput: true,
RmTmpContainer: true,
ForceRmTmpContainer: true,
InputStream: &buf,
OutputStream: &buf,
ContextDir: "testing/data",
}
err := client.BuildImage(opts)
if err != ErrMultipleContexts {
t.Errorf("BuildImage: providing both InputStream and ContextDir should produce an error")
}
}
func TestBuildImageContextDirDockerignoreParsing(t *testing.T) {
t.Parallel()
fakeRT := &FakeRoundTripper{message: "", status: http.StatusOK}
client := newTestClient(fakeRT)
if err := os.Symlink("doesnotexist", "testing/data/symlink"); err != nil {
t.Errorf("error creating symlink on demand: %s", err)
}
defer func() {
if err := os.Remove("testing/data/symlink"); err != nil {
t.Errorf("error removing symlink on demand: %s", err)
}
}()
workingdir, err := os.Getwd()
var buf bytes.Buffer
opts := BuildImageOptions{
Name: "testImage",
NoCache: true,
CacheFrom: []string{"a", "b", "c"},
SuppressOutput: true,
RmTmpContainer: true,
ForceRmTmpContainer: true,
OutputStream: &buf,
ContextDir: filepath.Join(workingdir, "testing", "data"),
}
err = client.BuildImage(opts)
if err != nil {
t.Fatal(err)
}
reqBody := fakeRT.requests[0].Body
tmpdir, err := unpackBodyTarball(reqBody)
if err != nil {
t.Fatal(err)
}
defer func() {
if err = os.RemoveAll(tmpdir); err != nil {
t.Fatal(err)
}
}()
files, err := ioutil.ReadDir(tmpdir)
if err != nil {
t.Fatal(err)
}
foundFiles := []string{}
for _, file := range files {
foundFiles = append(foundFiles, file.Name())
}
expectedFiles := []string{
".dockerignore",
"Dockerfile",
"barfile",
"ca.pem",
"cert.pem",
"key.pem",
"server.pem",
"serverkey.pem",
"symlink",
}
if !reflect.DeepEqual(expectedFiles, foundFiles) {
t.Errorf(
"BuildImage: incorrect files sent in tarball to docker server\nexpected %+v, found %+v",
expectedFiles, foundFiles,
)
}
}
func TestBuildImageSendXRegistryConfig(t *testing.T) {
t.Parallel()
fakeRT := &FakeRoundTripper{message: "", status: http.StatusOK}
client := newTestClient(fakeRT)
var buf bytes.Buffer
opts := BuildImageOptions{
Name: "testImage",
NoCache: true,
SuppressOutput: true,
RmTmpContainer: true,
ForceRmTmpContainer: true,
OutputStream: &buf,
ContextDir: "testing/data",
AuthConfigs: AuthConfigurations{
Configs: map[string]AuthConfiguration{
"quay.io": {
Username: "foo",
Password: "bar",
Email: "baz",
ServerAddress: "quay.io",
},
},
},
}
encodedConfig := "eyJjb25maWdzIjp7InF1YXkuaW8iOnsidXNlcm5hbWUiOiJmb28iLCJwYXNzd29yZCI6ImJhciIsImVtYWlsIjoiYmF6Iiwic2VydmVyYWRkcmVzcyI6InF1YXkuaW8ifX19"
if err := client.BuildImage(opts); err != nil {
t.Fatal(err)
}
xRegistryConfig := fakeRT.requests[0].Header.Get("X-Registry-Config")
if xRegistryConfig != encodedConfig {
t.Errorf(
"BuildImage: X-Registry-Config not set currectly: expected %q, got %q",
encodedConfig,
xRegistryConfig,
)
}
}
func unpackBodyTarball(req io.ReadCloser) (tmpdir string, err error) {
tmpdir, err = ioutil.TempDir("", "go-dockerclient-test")
if err != nil {
return
}
err = archive.Untar(req, tmpdir, &archive.TarOptions{
Compression: archive.Uncompressed,
NoLchown: true,
})
return
}