-
Notifications
You must be signed in to change notification settings - Fork 13
/
s3_test.go
122 lines (107 loc) · 5.51 KB
/
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"errors"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestGetS3ConfigStaticCredentials(t *testing.T) {
conf, err := getS3Config("exampleaccessID", "examplesecretkey", "", "exampleprefix", "", "examplebucket", "exampleregion", "", "", "", "", "", "")
if err != nil {
t.Fatalf("failed test %#v", err)
}
assert.Equal(t, "examplebucket", *conf.bucket, "Specify bucket name")
assert.Equal(t, "exampleprefix", *conf.s3prefix, "Specify s3prefix name")
assert.NotNil(t, conf.credentials, "credentials not to be nil")
assert.Equal(t, "exampleregion", *conf.region, "Specify s3prefix name")
assert.Equal(t, plainTextFormat, conf.compress, "Specify compression method")
assert.Equal(t, false, conf.autoCreateBucket, "Specify true/false")
}
func TestGetS3ConfigSharedCredentials(t *testing.T) {
s3Creds = &testS3Credential{}
conf, err := getS3Config("", "", "examplecredentials", "exampleprefix", "", "examplebucket", "exampleregion", "", "", "", "", "", "")
if err != nil {
t.Fatalf("failed test %#v", err)
}
assert.Equal(t, "examplebucket", *conf.bucket, "Specify bucket name")
assert.Equal(t, "exampleprefix", *conf.s3prefix, "Specify s3prefix name")
assert.NotNil(t, conf.credentials, "credentials not to be nil")
assert.Equal(t, "exampleregion", *conf.region, "Specify s3prefix name")
assert.Equal(t, plainTextFormat, conf.compress, "Specify compression method")
assert.Equal(t, false, conf.autoCreateBucket, "Specify true/false")
}
func TestGetS3ConfigCompression(t *testing.T) {
s3Creds = &testS3Credential{}
conf, err := getS3Config("", "", "examplecredentials", "exampleprefix", "", "examplebucket", "exampleregion", "gzip", "", "", "", "", "")
if err != nil {
t.Fatalf("failed test %#v", err)
}
assert.Equal(t, "examplebucket", *conf.bucket, "Specify bucket name")
assert.Equal(t, "exampleprefix", *conf.s3prefix, "Specify s3prefix name")
assert.NotNil(t, conf.credentials, "credentials not to be nil")
assert.Equal(t, "exampleregion", *conf.region, "Specify s3prefix name")
assert.Equal(t, gzipFormat, conf.compress, "Specify compression method")
assert.Equal(t, false, conf.autoCreateBucket, "Specify true/false")
}
func TestGetS3ConfigEndpoint(t *testing.T) {
s3Creds = &testS3Credential{}
conf, err := getS3Config("", "", "examplecredentials", "exampleprefix", "", "examplebucket", "exampleregion", "gzip", "http://localhost:9000", "false", "", "", "")
if err != nil {
t.Fatalf("failed test %#v", err)
}
assert.Equal(t, "examplebucket", *conf.bucket, "Specify bucket name")
assert.Equal(t, "exampleprefix", *conf.s3prefix, "Specify s3prefix name")
assert.NotNil(t, conf.credentials, "credentials not to be nil")
assert.Equal(t, "exampleregion", *conf.region, "Specify s3prefix name")
assert.Equal(t, gzipFormat, conf.compress, "Specify compression method")
assert.Equal(t, "http://localhost:9000", conf.endpoint, "Specify correct endpoint")
assert.Equal(t, false, conf.autoCreateBucket, "Specify true/false")
}
func TestGetS3ConfigInvalidEndpoint(t *testing.T) {
s3Creds = &testS3Credential{}
_, err := getS3Config("", "", "examplecredentials", "exampleprefix", "", "examplebucket", "exampleregion", "gzip", "https://your-bucketname.s3.amazonaws.com", "false", "", "", "")
if err != nil {
expected := errors.New("Endpoint is not supported for AWS S3. This parameter is intended for S3 compatible services. Use Region instead.")
assert.Equal(t, expected, err)
}
}
func TestGetS3ConfigTimeFormat(t *testing.T) {
s3Creds = &testS3Credential{}
conf, err := getS3Config("", "", "examplecredentials", "exampleprefix", "", "examplebucket", "exampleregion", "gzip", "", "", "", "dt=2006-01-02", "Asia/Tokyo")
if err != nil {
t.Fatalf("failed test %#v", err)
}
assert.Equal(t, "examplebucket", *conf.bucket, "Specify bucket name")
assert.Equal(t, "exampleprefix", *conf.s3prefix, "Specify s3prefix name")
assert.NotNil(t, conf.credentials, "credentials not to be nil")
assert.Equal(t, "exampleregion", *conf.region, "Specify s3prefix name")
assert.Equal(t, gzipFormat, conf.compress, "Specify compression method")
assert.Equal(t, false, conf.autoCreateBucket, "Specify true/false")
assert.Equal(t, "dt=2006-01-02", conf.timeFormat, "Specify time format")
loc, _ := time.LoadLocation("Asia/Tokyo")
assert.Equal(t, loc, conf.location, "Specify valid TimeZone")
}
func TestGetS3ConfigTimeZone(t *testing.T) {
s3Creds = &testS3Credential{}
conf, err := getS3Config("", "", "examplecredentials", "exampleprefix", "", "examplebucket", "exampleregion", "gzip", "", "", "", "", "Asia/Tokyo")
if err != nil {
t.Fatalf("failed test %#v", err)
}
assert.Equal(t, "examplebucket", *conf.bucket, "Specify bucket name")
assert.Equal(t, "exampleprefix", *conf.s3prefix, "Specify s3prefix name")
assert.NotNil(t, conf.credentials, "credentials not to be nil")
assert.Equal(t, "exampleregion", *conf.region, "Specify s3prefix name")
assert.Equal(t, gzipFormat, conf.compress, "Specify compression method")
assert.Equal(t, false, conf.autoCreateBucket, "Specify true/false")
assert.Equal(t, "20060102/15", conf.timeFormat, "Specify time format")
loc, _ := time.LoadLocation("Asia/Tokyo")
assert.Equal(t, loc, conf.location, "Specify valid TimeZone")
}
func TestGetS3ConfigInvalidTimeZone(t *testing.T) {
s3Creds = &testS3Credential{}
_, err := getS3Config("", "", "examplecredentials", "exampleprefix", "", "examplebucket", "exampleregion", "gzip", "", "", "", "", "Asia/Nonexistent")
if err != nil {
expected := errors.New("invalid timeZone: unknown time zone Asia/Nonexistent")
assert.Equal(t, expected, err)
}
}