forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvacation_tar_test.go
234 lines (180 loc) · 7.52 KB
/
vacation_tar_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
package vacation_test
import (
"archive/tar"
"bytes"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/paketo-buildpacks/packit/vacation"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testVacationTar(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
)
context("TarArchive.Decompress", func() {
var (
tempDir string
tarArchive vacation.TarArchive
)
it.Before(func() {
var err error
tempDir, err = os.MkdirTemp("", "vacation")
Expect(err).NotTo(HaveOccurred())
buffer := bytes.NewBuffer(nil)
tw := tar.NewWriter(buffer)
Expect(tw.WriteHeader(&tar.Header{Name: "some-dir", Mode: 0755, Typeflag: tar.TypeDir})).To(Succeed())
_, err = tw.Write(nil)
Expect(err).NotTo(HaveOccurred())
Expect(tw.WriteHeader(&tar.Header{Name: filepath.Join("some-dir", "some-other-dir"), Mode: 0755, Typeflag: tar.TypeDir})).To(Succeed())
_, err = tw.Write(nil)
Expect(err).NotTo(HaveOccurred())
nestedFile := filepath.Join("some-dir", "some-other-dir", "some-file")
Expect(tw.WriteHeader(&tar.Header{Name: nestedFile, Mode: 0755, Size: int64(len(nestedFile))})).To(Succeed())
_, err = tw.Write([]byte(nestedFile))
Expect(err).NotTo(HaveOccurred())
for _, file := range []string{"first", "second", "third"} {
Expect(tw.WriteHeader(&tar.Header{Name: file, Mode: 0755, Size: int64(len(file))})).To(Succeed())
_, err = tw.Write([]byte(file))
Expect(err).NotTo(HaveOccurred())
}
Expect(tw.WriteHeader(&tar.Header{Name: "symlink", Mode: 0755, Size: int64(0), Typeflag: tar.TypeSymlink, Linkname: "first"})).To(Succeed())
_, err = tw.Write([]byte{})
Expect(err).NotTo(HaveOccurred())
Expect(tw.Close()).To(Succeed())
tarArchive = vacation.NewTarArchive(bytes.NewReader(buffer.Bytes()))
})
it.After(func() {
Expect(os.RemoveAll(tempDir)).To(Succeed())
})
it("unpackages the archive into the path", func() {
var err error
err = tarArchive.Decompress(tempDir)
Expect(err).ToNot(HaveOccurred())
files, err := filepath.Glob(fmt.Sprintf("%s/*", tempDir))
Expect(err).NotTo(HaveOccurred())
Expect(files).To(ConsistOf([]string{
filepath.Join(tempDir, "first"),
filepath.Join(tempDir, "second"),
filepath.Join(tempDir, "third"),
filepath.Join(tempDir, "some-dir"),
filepath.Join(tempDir, "symlink"),
}))
info, err := os.Stat(filepath.Join(tempDir, "first"))
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode()).To(Equal(os.FileMode(0755)))
Expect(filepath.Join(tempDir, "some-dir", "some-other-dir")).To(BeADirectory())
Expect(filepath.Join(tempDir, "some-dir", "some-other-dir", "some-file")).To(BeARegularFile())
data, err := os.ReadFile(filepath.Join(tempDir, "symlink"))
Expect(err).NotTo(HaveOccurred())
Expect(data).To(Equal([]byte(`first`)))
})
it("unpackages the archive into the path but also strips the first component", func() {
var err error
err = tarArchive.StripComponents(1).Decompress(tempDir)
Expect(err).ToNot(HaveOccurred())
files, err := filepath.Glob(fmt.Sprintf("%s/*", tempDir))
Expect(err).NotTo(HaveOccurred())
Expect(files).To(ConsistOf([]string{
filepath.Join(tempDir, "some-other-dir"),
}))
Expect(filepath.Join(tempDir, "some-other-dir")).To(BeADirectory())
Expect(filepath.Join(tempDir, "some-other-dir", "some-file")).To(BeARegularFile())
})
context("there is no directory metadata", func() {
it.Before(func() {
var err error
buffer := bytes.NewBuffer(nil)
tw := tar.NewWriter(buffer)
nestedFile := filepath.Join("some-dir", "some-other-dir", "some-file")
Expect(tw.WriteHeader(&tar.Header{Name: nestedFile, Mode: 0755, Size: int64(len(nestedFile))})).To(Succeed())
_, err = tw.Write([]byte(nestedFile))
Expect(err).NotTo(HaveOccurred())
Expect(tw.WriteHeader(&tar.Header{Name: filepath.Join("sym-dir", "symlink"), Mode: 0755, Size: int64(0), Typeflag: tar.TypeSymlink, Linkname: filepath.Join("..", nestedFile)})).To(Succeed())
_, err = tw.Write([]byte{})
Expect(err).NotTo(HaveOccurred())
Expect(tw.Close()).To(Succeed())
tarArchive = vacation.NewTarArchive(bytes.NewReader(buffer.Bytes()))
})
it("unpackages the archive into the path", func() {
err := tarArchive.Decompress(tempDir)
Expect(err).ToNot(HaveOccurred())
Expect(filepath.Join(tempDir, "some-dir", "some-other-dir")).To(BeADirectory())
Expect(filepath.Join(tempDir, "some-dir", "some-other-dir", "some-file")).To(BeARegularFile())
data, err := os.ReadFile(filepath.Join(tempDir, "sym-dir", "symlink"))
Expect(err).NotTo(HaveOccurred())
Expect(data).To(Equal([]byte(filepath.Join("some-dir", "some-other-dir", "some-file"))))
})
})
context("failure cases", func() {
context("when it fails to read the tar response", func() {
it("returns an error", func() {
readyArchive := vacation.NewTarArchive(bytes.NewBuffer([]byte(`something`)))
err := readyArchive.Decompress(tempDir)
Expect(err).To(MatchError(ContainSubstring("failed to read tar response")))
})
})
context("when it is unable to create an archived directory", func() {
it.Before(func() {
Expect(os.Chmod(tempDir, 0000)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(tempDir, os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
err := tarArchive.Decompress(tempDir)
Expect(err).To(MatchError(ContainSubstring("failed to create archived directory")))
})
context("there are no directory headers", func() {
it.Before(func() {
var err error
buffer := bytes.NewBuffer(nil)
tw := tar.NewWriter(buffer)
nestedFile := filepath.Join("some-dir", "some-other-dir", "some-file")
Expect(tw.WriteHeader(&tar.Header{Name: nestedFile, Mode: 0755, Size: int64(len(nestedFile))})).To(Succeed())
_, err = tw.Write([]byte(nestedFile))
Expect(err).NotTo(HaveOccurred())
Expect(tw.Close()).To(Succeed())
tarArchive = vacation.NewTarArchive(bytes.NewReader(buffer.Bytes()))
})
it("returns an error", func() {
err := tarArchive.Decompress(tempDir)
Expect(err).To(MatchError(ContainSubstring("failed to create archived directory from file path")))
})
})
})
context("when it is unable to create an archived file", func() {
it.Before(func() {
Expect(os.MkdirAll(filepath.Join(tempDir, "some-dir", "some-other-dir"), os.ModePerm)).To(Succeed())
Expect(os.Chmod(filepath.Join(tempDir, "some-dir", "some-other-dir"), 0000)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(filepath.Join(tempDir, "some-dir", "some-other-dir"), os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
err := tarArchive.Decompress(tempDir)
Expect(err).To(MatchError(ContainSubstring("failed to create archived file")))
})
})
context("when it tries to decompress a broken symlink", func() {
var brokenSymlinkTar vacation.TarArchive
it.Before(func() {
var err error
buffer := bytes.NewBuffer(nil)
tw := tar.NewWriter(buffer)
Expect(tw.WriteHeader(&tar.Header{Name: "symlink", Mode: 0755, Size: int64(0), Typeflag: tar.TypeSymlink, Linkname: ""})).To(Succeed())
_, err = tw.Write([]byte{})
Expect(err).NotTo(HaveOccurred())
Expect(tw.Close()).To(Succeed())
brokenSymlinkTar = vacation.NewTarArchive(bytes.NewReader(buffer.Bytes()))
})
it("returns an error", func() {
err := brokenSymlinkTar.Decompress(tempDir)
Expect(err).To(MatchError(ContainSubstring("failed to extract symlink")))
})
})
})
})
}