forked from hashicorp/go-getter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_s3_test.go
108 lines (90 loc) · 2.54 KB
/
get_s3_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
package getter
import (
"os"
"path/filepath"
"testing"
"github.com/aws/aws-sdk-go/aws/awserr"
)
func init() {
// These are well known restricted IAM keys to a HashiCorp-managed bucket
// in a private AWS account that only has access to the open source test
// resources.
//
// We do the string concat below to avoid AWS autodetection of a key. This
// key is locked down an IAM policy that is read-only so we're purposely
// exposing it.
os.Setenv("AWS_ACCESS_KEY", "AKIAJCTNQ" + "IOBWAYXKGZA")
os.Setenv("AWS_SECRET_KEY", "jcQOTYdXNzU5MO" + "5ExqbE1U995dIfKCKQtiVobMvr")
}
func TestS3Getter_impl(t *testing.T) {
var _ Getter = new(S3Getter)
}
func TestS3Getter(t *testing.T) {
g := new(S3Getter)
dst := tempDir(t)
// With a dir that doesn't exist
err := g.Get(
dst, testURL("https://s3.amazonaws.com/hc-oss-test/go-getter/folder"))
if err != nil {
t.Fatalf("err: %s", err)
}
// Verify the main file exists
mainPath := filepath.Join(dst, "main.tf")
if _, err := os.Stat(mainPath); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestS3Getter_subdir(t *testing.T) {
g := new(S3Getter)
dst := tempDir(t)
// With a dir that doesn't exist
err := g.Get(
dst, testURL("https://s3.amazonaws.com/hc-oss-test/go-getter/folder/subfolder"))
if err != nil {
t.Fatalf("err: %s", err)
}
// Verify the main file exists
subPath := filepath.Join(dst, "sub.tf")
if _, err := os.Stat(subPath); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestS3Getter_GetFile(t *testing.T) {
g := new(S3Getter)
dst := tempFile(t)
// Download
err := g.GetFile(
dst, testURL("https://s3.amazonaws.com/hc-oss-test/go-getter/folder/main.tf"))
if err != nil {
t.Fatalf("err: %s", err)
}
// Verify the main file exists
if _, err := os.Stat(dst); err != nil {
t.Fatalf("err: %s", err)
}
assertContents(t, dst, "# Main\n")
}
func TestS3Getter_GetFile_badParams(t *testing.T) {
g := new(S3Getter)
dst := tempFile(t)
// Download
err := g.GetFile(
dst,
testURL("https://s3.amazonaws.com/hc-oss-test/go-getter/folder/main.tf?aws_access_key_id=foo&aws_access_key_secret=bar&aws_access_token=baz"))
if err == nil {
t.Fatalf("expected error, got none")
}
if reqerr, ok := err.(awserr.RequestFailure); !ok || reqerr.StatusCode() != 403 {
t.Fatalf("expected InvalidAccessKeyId error")
}
}
func TestS3Getter_GetFile_notfound(t *testing.T) {
g := new(S3Getter)
dst := tempFile(t)
// Download
err := g.GetFile(
dst, testURL("https://s3.amazonaws.com/hc-oss-test/go-getter/folder/404.tf"))
if err == nil {
t.Fatalf("expected error, got none")
}
}