forked from hashicorp/go-getter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_test.go
188 lines (157 loc) · 3.55 KB
/
get_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
package getter
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestGet_badSchema(t *testing.T) {
dst := tempDir(t)
u := testModule("basic")
u = strings.Replace(u, "file", "nope", -1)
if err := Get(dst, u); err == nil {
t.Fatal("should error")
}
}
func TestGet_file(t *testing.T) {
dst := tempDir(t)
u := testModule("basic")
if err := Get(dst, u); err != nil {
t.Fatalf("err: %s", err)
}
mainPath := filepath.Join(dst, "main.tf")
if _, err := os.Stat(mainPath); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestGet_fileDetect(t *testing.T) {
dst := tempDir(t)
u := filepath.Join("./test-fixtures", "basic")
pwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
client := &Client{
Src: u,
Dst: dst,
Pwd: pwd,
Dir: true,
}
if err := client.Get(); err != nil {
t.Fatalf("err: %s", err)
}
mainPath := filepath.Join(dst, "main.tf")
if _, err := os.Stat(mainPath); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestGet_fileForced(t *testing.T) {
dst := tempDir(t)
u := testModule("basic")
u = "file::" + u
if err := Get(dst, u); err != nil {
t.Fatalf("err: %s", err)
}
mainPath := filepath.Join(dst, "main.tf")
if _, err := os.Stat(mainPath); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestGet_fileSubdir(t *testing.T) {
dst := tempDir(t)
u := testModule("basic//subdir")
if err := Get(dst, u); err != nil {
t.Fatalf("err: %s", err)
}
mainPath := filepath.Join(dst, "sub.tf")
if _, err := os.Stat(mainPath); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestGetFile(t *testing.T) {
dst := tempFile(t)
u := testModule("basic-file/foo.txt")
if err := GetFile(dst, u); err != nil {
t.Fatalf("err: %s", err)
}
// Verify the main file exists
assertContents(t, dst, "Hello\n")
}
func TestGetFile_checksum(t *testing.T) {
cases := []struct {
Append string
Err bool
}{
{
"",
false,
},
// MD5
{
"?checksum=md5:09f7e02f1290be211da707a266f153b3",
false,
},
{
"?checksum=md5:09f7e02f1290be211da707a266f153b4",
true,
},
// SHA1
{
"?checksum=sha1:1d229271928d3f9e2bb0375bd6ce5db6c6d348d9",
false,
},
{
"?checksum=sha1:1d229271928d3f9e2bb0375bd6ce5db6c6d348d0",
true,
},
// SHA256
{
"?checksum=sha256:66a045b452102c59d840ec097d59d9467e13a3f34f6494e539ffd32c1bb35f18",
false,
},
{
"?checksum=sha256:66a045b452102c59d840ec097d59d9467e13a3f34f6494e539ffd32c1bb35f19",
true,
},
// SHA512
{
"?checksum=sha512:c2bad2223811194582af4d1508ac02cd69eeeeedeeb98d54fcae4dcefb13cc882e7640328206603d3fb9cd5f949a9be0db054dd34fbfa190c498a5fe09750cef",
false,
},
{
"?checksum=sha512:c2bad2223811194582af4d1508ac02cd69eeeeedeeb98d54fcae4dcefb13cc882e7640328206603d3fb9cd5f949a9be0db054dd34fbfa190c498a5fe09750ced",
true,
},
}
for _, tc := range cases {
u := testModule("basic-file/foo.txt") + tc.Append
func() {
dst := tempFile(t)
defer os.Remove(dst)
if err := GetFile(dst, u); (err != nil) != tc.Err {
t.Fatalf("append: %s\n\nerr: %s", tc.Append, err)
}
// Verify the main file exists
assertContents(t, dst, "Hello\n")
}()
}
}
func TestGetFile_checksumURL(t *testing.T) {
dst := tempFile(t)
u := testModule("basic-file/foo.txt") + "?checksum=md5:09f7e02f1290be211da707a266f153b3"
getter := &MockGetter{Proxy: new(FileGetter)}
client := &Client{
Src: u,
Dst: dst,
Dir: false,
Getters: map[string]Getter{
"file": getter,
},
}
if err := client.Get(); err != nil {
t.Fatalf("err: %s", err)
}
if v := getter.GetFileURL.Query().Get("checksum"); v != "" {
t.Fatalf("bad: %s", v)
}
}