forked from concourse/registry-image-resource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
in_test.go
315 lines (251 loc) · 8.76 KB
/
in_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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package resource_test
import (
"bytes"
"encoding/json"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"syscall"
"time"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/tarball"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
resource "github.com/concourse/registry-image-resource"
)
var _ = Describe("In", func() {
var destDir string
var req struct {
Source resource.Source `json:"source"`
Params resource.GetParams `json:"params"`
Version resource.Version `json:"version"`
}
var res struct {
Version resource.Version `json:"version"`
Metadata []resource.MetadataField `json:"metadata"`
}
rootfsPath := func(path ...string) string {
return filepath.Join(append([]string{destDir, "rootfs"}, path...)...)
}
BeforeEach(func() {
var err error
destDir, err = ioutil.TempDir("", "docker-image-in-dir")
Expect(err).ToNot(HaveOccurred())
req.Source = resource.Source{}
req.Params = resource.GetParams{}
req.Version = resource.Version{}
res.Version = resource.Version{}
res.Metadata = nil
})
AfterEach(func() {
Expect(os.RemoveAll(destDir)).To(Succeed())
})
JustBeforeEach(func() {
cmd := exec.Command(bins.In, destDir)
payload, err := json.Marshal(req)
Expect(err).ToNot(HaveOccurred())
outBuf := new(bytes.Buffer)
cmd.Stdin = bytes.NewBuffer(payload)
cmd.Stdout = outBuf
cmd.Stderr = GinkgoWriter
err = cmd.Run()
Expect(err).ToNot(HaveOccurred())
err = json.Unmarshal(outBuf.Bytes(), &res)
Expect(err).ToNot(HaveOccurred())
})
Describe("image metadata", func() {
BeforeEach(func() {
req.Source.Repository = "concourse/test-image-metadata"
req.Version.Digest = latestDigest(req.Source.Repository)
})
It("captures the env and user", func() {
var meta struct {
User string `json:"user"`
Env []string `json:"env"`
}
md, err := os.Open(filepath.Join(destDir, "metadata.json"))
Expect(err).ToNot(HaveOccurred())
defer md.Close()
json.NewDecoder(md).Decode(&meta)
Expect(meta.User).To(Equal("someuser"))
Expect(meta.Env).To(Equal([]string{
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"FOO=1",
}))
})
})
Describe("response metadata", func() {
BeforeEach(func() {
req.Source.Repository = "concourse/test-image-metadata"
req.Version.Digest = latestDigest(req.Source.Repository)
})
It("returns metadata", func() {
Expect(res.Version).To(Equal(req.Version))
Expect(res.Metadata).To(Equal([]resource.MetadataField{
resource.MetadataField{
Name: "repository",
Value: "concourse/test-image-metadata",
},
resource.MetadataField{
Name: "tag",
Value: "latest",
},
}))
})
})
Describe("file attributes", func() {
BeforeEach(func() {
req.Source.Repository = "concourse/test-image-file-perms-mtime"
req.Version.Digest = latestDigest(req.Source.Repository)
})
It("keeps file ownership, permissions, and modified times", func() {
stat, err := os.Stat(rootfsPath("home", "alex", "birthday"))
Expect(err).ToNot(HaveOccurred())
Expect(stat.Mode()).To(Equal(os.FileMode(0603)))
Expect(stat.ModTime()).To(BeTemporally("==", time.Date(1991, 06, 03, 05, 30, 30, 0, time.UTC)))
sys, ok := stat.Sys().(*syscall.Stat_t)
Expect(ok).To(BeTrue())
Expect(sys.Uid).To(Equal(uint32(1000)))
Expect(sys.Gid).To(Equal(uint32(1000)))
})
})
Describe("removed files in layers", func() {
BeforeEach(func() {
req.Source.Repository = "concourse/test-image-whiteout"
req.Version.Digest = latestDigest(req.Source.Repository)
})
It("does not restore files that were removed in later layers", func() {
infos, err := ioutil.ReadDir(rootfsPath("top-dir-1"))
Expect(err).ToNot(HaveOccurred())
Expect(infos).To(HaveLen(2))
stat, err := os.Stat(rootfsPath("top-dir-1", "nested-file"))
Expect(err).ToNot(HaveOccurred())
Expect(stat.IsDir()).To(BeFalse())
stat, err = os.Stat(rootfsPath("top-dir-1", "nested-dir"))
Expect(err).ToNot(HaveOccurred())
Expect(stat.IsDir()).To(BeTrue())
infos, err = ioutil.ReadDir(rootfsPath("top-dir-1", "nested-dir"))
Expect(err).ToNot(HaveOccurred())
Expect(infos).To(HaveLen(3))
stat, err = os.Stat(rootfsPath("top-dir-1", "nested-dir", "file-gone"))
Expect(err).To(HaveOccurred())
stat, err = os.Stat(rootfsPath("top-dir-1", "nested-dir", "file-here"))
Expect(err).ToNot(HaveOccurred())
Expect(stat.IsDir()).To(BeFalse())
stat, err = os.Stat(rootfsPath("top-dir-1", "nested-dir", "file-recreated"))
Expect(err).ToNot(HaveOccurred())
Expect(stat.IsDir()).To(BeFalse())
stat, err = os.Stat(rootfsPath("top-dir-1", "nested-dir", "file-then-dir"))
Expect(err).ToNot(HaveOccurred())
Expect(stat.IsDir()).To(BeTrue())
stat, err = os.Stat(rootfsPath("top-dir-2"))
Expect(err).To(HaveOccurred())
})
})
Describe("a hardlink that is later removed", func() {
BeforeEach(func() {
req.Source.Repository = "concourse/test-image-removed-hardlinks"
req.Version.Digest = latestDigest(req.Source.Repository)
})
It("works", func() {
lstat, err := os.Lstat(rootfsPath("usr", "libexec", "git-core", "git"))
Expect(err).ToNot(HaveOccurred())
Expect(lstat.Mode() & os.ModeSymlink).To(BeZero())
stat, err := os.Stat(rootfsPath("usr", "libexec", "git-core", "git"))
Expect(err).ToNot(HaveOccurred())
Expect(stat.Mode() & os.ModeSymlink).To(BeZero())
})
})
Describe("layers that replace symlinks with regular files", func() {
BeforeEach(func() {
req.Source.Repository = "concourse/test-image-symlinks"
req.Version.Digest = latestDigest(req.Source.Repository)
})
It("removes the symlink and writes to a new file rather than trying to open and write to it (thereby overwriting its target)", func() {
Expect(cat(rootfsPath("a"))).To(Equal("symlinked\n"))
Expect(cat(rootfsPath("b"))).To(Equal("replaced\n"))
})
})
Describe("fetching from a private repository with credentials", func() {
BeforeEach(func() {
req.Source = resource.Source{
Repository: dockerPrivateRepo,
RawTag: "latest",
Username: dockerPrivateUsername,
Password: dockerPrivatePassword,
}
checkDockerPrivateUserConfigured()
req.Version = resource.Version{
Digest: PRIVATE_LATEST_STATIC_DIGEST,
}
})
It("works", func() {
Expect(cat(rootfsPath("Dockerfile"))).To(ContainSubstring("hello!"))
})
})
Describe("fetching in OCI format", func() {
var manifest *v1.Manifest
BeforeEach(func() {
req.Source.Repository = "concourse/test-image-static"
req.Params.RawFormat = "oci"
req.Version.Digest, manifest = latestManifest(req.Source.Repository)
})
It("saves the tagged image as image.tar instead of saving the rootfs", func() {
_, err := os.Stat(filepath.Join(destDir, "rootfs"))
Expect(os.IsNotExist(err)).To(BeTrue())
_, err = os.Stat(filepath.Join(destDir, "manifest.json"))
Expect(os.IsNotExist(err)).To(BeTrue())
tag, err := name.NewTag("concourse/test-image-static:latest", name.WeakValidation)
Expect(err).ToNot(HaveOccurred())
img, err := tarball.ImageFromPath(filepath.Join(destDir, "image.tar"), &tag)
Expect(err).ToNot(HaveOccurred())
fetchedManifest, err := img.Manifest()
Expect(err).ToNot(HaveOccurred())
// cannot assert against digest because the saved image's manifest isn't
// JSON-prettified, so it has a different sha256. so just assert against
// digest within manifest, which is what ends up being the 'image id'
// anyway.
Expect(fetchedManifest.Config.Digest).To(Equal(manifest.Config.Digest))
})
})
Describe("saving the digest", func() {
BeforeEach(func() {
req.Source.Repository = "concourse/test-image-static"
req.Version.Digest = LATEST_STATIC_DIGEST
})
It("saves the digest to a file", func() {
digest, err := ioutil.ReadFile(filepath.Join(destDir, "digest"))
Expect(err).ToNot(HaveOccurred())
Expect(string(digest)).To(Equal(req.Version.Digest))
})
})
Describe("saving the tag", func() {
BeforeEach(func() {
req.Source.Repository = "concourse/test-image-static"
req.Version.Digest = LATEST_STATIC_DIGEST
})
Context("with no tag specified", func() {
BeforeEach(func() {
req.Source.RawTag = ""
})
It("assumes 'latest' and saves the tag to a file", func() {
digest, err := ioutil.ReadFile(filepath.Join(destDir, "tag"))
Expect(err).ToNot(HaveOccurred())
Expect(string(digest)).To(Equal("latest"))
})
})
Context("with a tag specified", func() {
BeforeEach(func() {
req.Source.RawTag = "tagged"
req.Version.Digest = LATEST_TAGGED_STATIC_DIGEST
})
It("saves the tag to a file", func() {
tag, err := ioutil.ReadFile(filepath.Join(destDir, "tag"))
Expect(err).ToNot(HaveOccurred())
Expect(string(tag)).To(Equal("tagged"))
})
})
})
})