-
Notifications
You must be signed in to change notification settings - Fork 2
/
s3_upload_test.go
126 lines (101 loc) · 3.8 KB
/
s3_upload_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
package main
import (
"flag"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli"
"os"
"os/exec"
"testing"
)
var capturedArgs []string
func mockExecCommand(command string, args ...string) *exec.Cmd {
capturedArgs = append([]string{command}, args...)
cs := []string{"-test.run=TestHelperProcess", "--", command}
cs = append(cs, args...)
cmd := exec.Command(os.Args[0], cs...)
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
return cmd
}
func TestHelperProcess(*testing.T) {
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
return
}
os.Exit(0)
}
func TestRun_GeneratesCorrectArgs(t *testing.T) {
execCommand = mockExecCommand
defer func() { execCommand = exec.Command }()
defer func() { capturedArgs = []string{} }()
app := cli.NewApp()
app.Action = run
set := flag.NewFlagSet("test", 0)
set.String("aws-access-key", "mock-access-key", "AWS Access Key ID")
set.String("aws-secret-key", "mock-secret-key", "AWS Secret Access Key")
set.String("aws-default-region", "ap-south-1", "AWS Default Region")
set.String("aws-bucket", "bfw-hns-test-bucket", "AWS S3 Bucket")
set.String("source", "./test", "Source")
set.String("target-path", "test-target", "Target path")
set.String("artifact-file", "artifact.txt", "Artifact file")
set.String("glob", "", "Include patterns")
context := cli.NewContext(app, set, nil)
err := run(context)
assert.NoError(t, err)
expectedArgs := []string{
"aws", "s3", "cp", "./test",
"s3://bfw-hns-test-bucket/test-target/test",
"--region", "ap-south-1", "--recursive",
}
assert.Equal(t, expectedArgs, capturedArgs)
}
func TestCopyFilesToS3WithGlobIncludes(t *testing.T) {
execCommand = mockExecCommand
defer func() { execCommand = exec.Command }()
defer func() { capturedArgs = []string{} }()
globArgsList := GetGlobArgsList("**/*.html, **/*.css")
var allMatchedFiles []string
for _, pattern := range globArgsList {
tmpFilesList, err := GetMatchedFiles("./test", pattern)
if err != nil {
t.Error("Failed to get matched files")
}
allMatchedFiles = append(allMatchedFiles, tmpFilesList...)
}
expectedFilesMap := map[string]bool{
"s3-copy-test-files/project_root/level1/level2/styles/contact.css": true,
"s3-copy-test-files/project_root/level1/styles/about.css": true,
"s3-copy-test-files/project_root/styles/style.css": true,
"s3-copy-test-files/project_root/index.html": true,
"s3-copy-test-files/project_root/level1/about.html": true,
"s3-copy-test-files/project_root/level1/level2/contact.html": true,
}
gotFilesMap := make(map[string]bool)
for _, file := range allMatchedFiles {
gotFilesMap[file] = true
}
for key := range expectedFilesMap {
assert.True(t, gotFilesMap[key])
}
}
func TestExecArgs(t *testing.T) {
execCommand = mockExecCommand
defer func() { execCommand = exec.Command }()
defer func() { capturedArgs = []string{} }()
app := cli.NewApp()
app.Action = run
set := flag.NewFlagSet("test", 0)
set.String("aws-access-key", "mock-access-key", "AWS Access Key ID")
set.String("aws-secret-key", "mock-secret-key", "AWS Secret Access Key")
set.String("aws-default-region", "ap-south-1", "AWS Default Region")
set.String("aws-bucket", "bfw-hns-test-bucket", "AWS S3 Bucket")
set.String("source", "./test", "Source")
set.String("target-path", "test-target", "Target path")
set.String("artifact-file", "artifact.txt", "Artifact file")
set.String("glob", "**/*.xhtml", "Include patterns")
context := cli.NewContext(app, set, nil)
err := run(context)
assert.NoError(t, err)
expectedArgs := []string{"aws", "s3", "cp", "./test/s3-copy-test-files/project_root/level1/level2/contact.xhtml",
"s3://bfw-hns-test-bucket/test-target/test/s3-copy-test-files/project_root/level1/level2/contact.xhtml",
"--region", "ap-south-1"}
assert.Equal(t, expectedArgs, capturedArgs)
}