forked from gruntwork-io/fetch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_test.go
423 lines (362 loc) · 10.8 KB
/
file_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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
func init() {
if os.Getenv("GITHUB_OAUTH_TOKEN") == "" && os.Getenv("GITHUB_TOKEN") == "" {
fmt.Println("ERROR: Before running these tests, set GITHUB_OAUTH_TOKEN or GITHUB_TOKEN.")
fmt.Println("See the tests cases to see which GitHub repos the oAuth token needs access to.")
os.Exit(1)
}
}
func TestDownloadGitTagZipFile(t *testing.T) {
t.Parallel()
cases := []struct {
repoOwner string
repoName string
gitTag string
apiToken string
}{
{"opsgang", "fetch", "v0.1.1", ""},
{"opsgang", "fetch", "v0.0.2", os.Getenv("GITHUB_OAUTH_TOKEN")},
}
for _, tc := range cases {
c := commit{
Repo: repo{
Api: "https://api.github.com",
Owner: tc.repoOwner,
Name: tc.repoName,
},
GitTag: tc.gitTag,
}
zipFilePath, _, err := getSrcZip(c, tc.apiToken)
defer os.RemoveAll(zipFilePath)
if err != nil {
t.Fatalf("Failed to download file: %s", err)
}
if _, err := os.Stat(zipFilePath); os.IsNotExist(err) {
t.Fatalf("Downloaded file doesn't exist at the expected path of %s", zipFilePath)
}
}
}
func TestDownloadGitBranchZipFile(t *testing.T) {
t.Parallel()
cases := []struct {
repoOwner string
repoName string
branch string
apiToken string
}{
{"opsgang", "fetch", "enable-fetch-to-pull-from-branch", ""},
{"opsgang", "fetch", "enable-fetch-to-pull-from-branch", os.Getenv("GITHUB_OAUTH_TOKEN")},
}
for _, tc := range cases {
c := commit{
Repo: repo{
Api: "https://api.github.com",
Owner: tc.repoOwner,
Name: tc.repoName,
},
branch: tc.branch,
}
zipFilePath, _, err := getSrcZip(c, tc.apiToken)
defer os.RemoveAll(zipFilePath)
if err != nil {
t.Fatalf("Failed to download file: %s", err)
}
if _, err := os.Stat(zipFilePath); os.IsNotExist(err) {
t.Fatalf("Downloaded file doesn't exist at the expected path of %s", zipFilePath)
}
}
}
func TestDownloadBadGitBranchZipFile(t *testing.T) {
t.Parallel()
cases := []struct {
repoOwner string
repoName string
branch string
apiToken string
}{
{"opsgang", "fetch", "branch-that-doesnt-exist", ""},
}
for _, tc := range cases {
c := commit{
Repo: repo{
Api: "https://api.github.com",
Owner: tc.repoOwner,
Name: tc.repoName,
},
branch: tc.branch,
}
zipFilePath, _, err := getSrcZip(c, tc.apiToken)
defer os.RemoveAll(zipFilePath)
if err == nil {
t.Fatalf("Expected that attempt to download repo %s/%s for branch \"%s\" would fail, but received no error.", tc.repoOwner, tc.repoName, tc.branch)
}
}
}
func TestDownloadGitCommitFile(t *testing.T) {
t.Parallel()
cases := []struct {
repoOwner string
repoName string
commitSha string
apiToken string
}{
{"opsgang", "fetch", "f5790b465750498bf781169bae74747a6a7b536e", ""},
{"opsgang", "fetch", "9815bb39119e66c89d5f1c3abeb9d980993ef0a4", ""},
{"opsgang", "fetch", "f5790b465750498bf781169bae74747a6a7b536e", os.Getenv("GITHUB_OAUTH_TOKEN")},
}
for _, tc := range cases {
c := commit{
Repo: repo{
Api: "https://api.github.com",
Owner: tc.repoOwner,
Name: tc.repoName,
},
commitSha: tc.commitSha,
}
zipFilePath, _, err := getSrcZip(c, tc.apiToken)
defer os.RemoveAll(zipFilePath)
if err != nil {
t.Fatalf("Failed to download file: %s", err)
}
if _, err := os.Stat(zipFilePath); os.IsNotExist(err) {
t.Fatalf("Downloaded file doesn't exist at the expected path of %s", zipFilePath)
}
}
}
func TestDownloadBadGitCommitFile(t *testing.T) {
t.Parallel()
cases := []struct {
repoOwner string
repoName string
commitSha string
apiToken string
}{
{"opsgang", "fetch", "hello-world", ""},
{"opsgang", "fetch", "i-am-a-non-existent-commit", ""},
// remove a single letter from the beginning of an otherwise legit commit sha
// interestingly, through testing I found that GitHub will attempt to find the right commit sha if you
// truncate the end of it.
{"opsgang", "fetch", "7752e7f1df0acbd3c1e61545d5c4d0e87699d84", ""},
}
for _, tc := range cases {
c := commit{
Repo: repo{
Api: "https://api.github.com",
Owner: tc.repoOwner,
Name: tc.repoName,
},
commitSha: tc.commitSha,
}
zipFilePath, _, err := getSrcZip(c, tc.apiToken)
defer os.RemoveAll(zipFilePath)
if err == nil {
t.Fatalf("Expected that attempt to download repo %s/%s at commmit sha \"%s\" would fail, but received no error.", tc.repoOwner, tc.repoName, tc.commitSha)
}
}
}
func TestDownloadZipFileWithBadRepoValues(t *testing.T) {
t.Parallel()
cases := []struct {
repoOwner string
repoName string
gitTag string
apiToken string
}{
{"https://github.com/opsgang/fetch/archive/does-not-exist.zip", "MyNameIsWhat", "x.y.z", ""},
}
for _, tc := range cases {
c := commit{
Repo: repo{
Api: "https://api.github.com",
Owner: tc.repoOwner,
Name: tc.repoName,
},
GitTag: tc.gitTag,
}
_, status, err := getSrcZip(c, tc.apiToken)
if err == nil && status != 500 {
t.Fatalf("Expected error for bad repo values: %s/%s:%s", tc.repoOwner, tc.repoName, tc.gitTag)
}
}
}
func TestExtractFiles(t *testing.T) {
t.Parallel()
cases := []struct {
localFilePath string
filePathToExtract string
expectedNumFiles int
nonemptyFiles []string
}{
{"test-fixtures/fetch-test-public-0.0.1.zip", "/", 1, nil},
{"test-fixtures/fetch-test-public-0.0.1.zip", "/README.md", 1, nil}, // single file as --source-path
{"test-fixtures/fetch-test-public-0.0.2.zip", "/", 2, nil},
{"test-fixtures/fetch-test-public-0.0.3.zip", "/", 4, []string{"/README.md"}},
{"test-fixtures/fetch-test-public-0.0.3.zip", "/folder", 2, nil},
}
for _, tc := range cases {
// Create a temp directory
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("Failed to create temp directory: %s", err)
}
defer os.RemoveAll(tempDir)
err = extractFiles(tc.localFilePath, tc.filePathToExtract, tempDir)
if err != nil {
t.Fatalf("Failed to extract files: %s", err)
}
// Count the number of files in the directory
var numFiles int
filepath.Walk(tempDir, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
numFiles++
}
return nil
})
if numFiles != tc.expectedNumFiles {
t.Fatalf("While extracting %s, expected to find %d file(s), but found %d. Local path = %s", tc.localFilePath, tc.expectedNumFiles, numFiles, tempDir)
}
// Ensure that files declared to be non-empty are in fact non-empty
filepath.Walk(tempDir, func(path string, info os.FileInfo, err error) error {
relativeFilename := strings.TrimPrefix(path, tempDir)
if !info.IsDir() && stringInSlice(relativeFilename, tc.nonemptyFiles) {
if info.Size() == 0 {
t.Fatalf("Expected %s in %s to have non-zero file size, but found file size = %d.\n", relativeFilename, tc.localFilePath, info.Size())
}
}
return nil
})
}
}
func TestUnpack(t *testing.T) {
t.Parallel()
fixDir := "test-fixtures"
tmpDirBase := "/tmp/ghfetch-TestUnpack"
allFiles := []string{
"dirA",
"dirA/file",
"dirB",
"dirB/dirC",
"dirB/file.sh",
"symlink",
}
cases := []struct {
sourceFileName string
expectedFiles []string
}{
{"packed.tgz", allFiles},
{"packed.tar.gz", allFiles},
{"packed.tar", allFiles},
{"file.gz", []string{"file"}},
{"dodgygz", []string{"dodgygz"}},
}
os.MkdirAll(tmpDirBase, 0755)
for _, tc := range cases {
tempDir, err := ioutil.TempDir(tmpDirBase, "")
if err != nil {
t.Fatalf("Failed to create temp directory: %s", err)
}
defer os.RemoveAll(tempDir)
sourceFileOriginal := fmt.Sprintf("%s/%s", fixDir, tc.sourceFileName)
sourceFile := fmt.Sprintf("%s/%s", tmpDirBase, tc.sourceFileName)
if err := copyFile(sourceFileOriginal, sourceFile); err != nil {
t.Fatalf("Failed to copy file %s: %s", sourceFileOriginal, err)
}
var o fetchOpts
o.destDir = tempDir
err = o.doUnpack(sourceFile)
if err != nil {
t.Fatalf("Failed to unpack files: %s", err)
}
// Ensure that files declared to be non-empty are in fact non-empty
filepath.Walk(tempDir, func(path string, info os.FileInfo, err error) error {
if path != tempDir {
relativeFilename := strings.TrimPrefix(path, fmt.Sprintf("%s/", tempDir))
if !stringInSlice(relativeFilename, tc.expectedFiles) {
t.Fatalf("Unexpected file %s in pack %s.\n", relativeFilename, tc.sourceFileName)
}
}
return nil
})
}
return
}
// TestUntar - check that untarred contents have expected objects and permissions
func TestUntar(t *testing.T) {
t.Parallel()
fixDir := "test-fixtures"
tmpDirBase := "/tmp/ghfetch-TestUntar"
type fileInfo struct {
isDir bool
isSymLink bool
isRegular bool
filePerms string
}
dirPerms := "drwxr-xr-x"
regPerms := "-rw-r--r--"
exePerms := "-rwxr-xr-x"
symlinkPerms := "Lrwxrwxrwx"
r := make(map[string]fileInfo)
r["dirA"] = fileInfo{isDir: true, filePerms: dirPerms}
r["dirA/file"] = fileInfo{isRegular: true, filePerms: regPerms}
r["dirB"] = fileInfo{isDir: true, filePerms: dirPerms}
r["dirB/dirC"] = fileInfo{isDir: true, filePerms: dirPerms}
r["dirB/file.sh"] = fileInfo{isRegular: true, filePerms: exePerms}
r["symlink"] = fileInfo{isSymLink: true, filePerms: symlinkPerms}
os.MkdirAll(tmpDirBase, 0755)
tempDir, err := ioutil.TempDir(tmpDirBase, "")
if err != nil {
t.Fatalf("Failed to create temp directory: %s", err)
}
defer os.RemoveAll(tempDir)
sourceFileName := "packed.tar"
sourceFileOriginal := fmt.Sprintf("%s/%s", fixDir, sourceFileName)
sourceFile := fmt.Sprintf("%s/%s", tmpDirBase, sourceFileName)
if err := copyFile(sourceFileOriginal, sourceFile); err != nil {
t.Fatalf("Failed to copy file %s: %s", sourceFileOriginal, err)
}
var o fetchOpts
o.destDir = tempDir
err = o.untar(sourceFile)
if err != nil {
t.Fatalf("Failed to untar files: %s", err)
}
// Ensure that files declared to be non-empty are in fact non-empty
filepath.Walk(tempDir, func(path string, info os.FileInfo, err error) error {
if path != tempDir {
relativePath := strings.TrimPrefix(path, fmt.Sprintf("%s/", tempDir))
if i, ok := r[relativePath]; !ok {
t.Fatalf("File %s in tar %s not expected in results!", relativePath, sourceFileName)
} else {
if info.IsDir() && !i.isDir {
t.Fatalf("path %s in tar expected to be a dir", relativePath)
}
if info.Mode().IsRegular() && !i.isRegular {
t.Fatalf("path %s in tar expected to be a regular file", relativePath)
}
if info.Mode()&os.ModeSymlink != 0 && !i.isSymLink {
t.Fatalf("path %s in tar expected to be a symlink", relativePath)
}
if fmt.Sprintf("%s", info.Mode()) != i.filePerms {
t.Fatalf("path %s expected perms %s, not %s", relativePath, i.filePerms, info.Mode())
}
}
}
return nil
})
}
func copyFile(src string, dst string) error {
data, err := ioutil.ReadFile(src)
if err != nil {
return err
}
err = ioutil.WriteFile(dst, data, 0644)
return err
}