forked from wal-g/wal-g
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_test.go
192 lines (167 loc) · 4.51 KB
/
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
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
package walg_test
import (
"os"
"testing"
"github.com/wal-g/wal-g"
)
// Sets WAL-G needed environment variables to empty strings.
func setEmpty(t *testing.T) {
err := os.Setenv("WALE_S3_PREFIX", "")
if err != nil {
t.Log(err)
}
err = os.Setenv("AWS_REGION", "")
if err != nil {
t.Log(err)
}
err = os.Setenv("AWS_ACCESS_KEY_ID", "")
if err != nil {
t.Log(err)
}
err = os.Setenv("AWS_SECRET_ACCESS_KEY", "")
if err != nil {
t.Log(err)
}
err = os.Setenv("AWS_SECURITY_TOKEN", "")
if err != nil {
t.Log(err)
}
}
// Sets fake environment variables.
func setFake(t *testing.T) {
err := os.Setenv("WALE_S3_PREFIX", "wale_s3_prefix")
if err != nil {
t.Log(err)
}
err = os.Setenv("AWS_REGION", "aws_region")
if err != nil {
t.Log(err)
}
err = os.Setenv("AWS_ACCESS_KEY_ID", "aws_access_key_id")
if err != nil {
t.Log(err)
}
err = os.Setenv("AWS_SECRET_ACCESS_KEY", "aws_secret_access_key")
if err != nil {
t.Log(err)
}
err = os.Setenv("AWS_SECURITY_TOKEN", "aws_security_token")
if err != nil {
t.Log(err)
}
}
func TestConfigure(t *testing.T) {
bucketPath := "s3://bucket/server"
doConfigureWithBuсketPath(t, bucketPath, "server")
}
func TestConfigureBucketRoot(t *testing.T) {
bucketPath := "s3://bucket/"
doConfigureWithBuсketPath(t, bucketPath, "")
}
func TestConfigureBucketRoot2(t *testing.T) {
bucketPath := "s3://bucket"
doConfigureWithBuсketPath(t, bucketPath, "")
}
func TestConfigureDeepBucket(t *testing.T) {
bucketPath := "s3://bucket/subdir/server"
doConfigureWithBuсketPath(t, bucketPath, "subdir/server")
}
func doConfigureWithBuсketPath(t *testing.T, bucketPath string, expectedServer string) {
//Test empty environment variables
setEmpty(t)
tu, pre, err := walg.Configure()
if _, ok := err.(*walg.UnsetEnvVarError); !ok {
t.Errorf("upload: Expected error 'UnsetEnvVarError' but got %s", err)
}
if tu != nil || pre != nil {
t.Errorf("upload: Expected empty uploader and prefix but got TU:%v and PREFIX:%v", tu, pre)
}
setFake(t)
//Test invalid url
err = os.Setenv("WALE_S3_PREFIX", "test_fail:")
if err != nil {
t.Log(err)
}
_, _, err = walg.Configure()
if err == nil {
t.Errorf("upload: Expected to fail on fake url")
}
//Test created uploader and prefix
err = os.Setenv("WALE_S3_PREFIX", bucketPath)
if err != nil {
t.Log(err)
}
tu, pre, err = walg.Configure()
if err != nil {
t.Errorf("upload: unexpected error %v", err)
}
if *pre.Bucket != "bucket" {
t.Errorf("upload: Prefix field 'Bucket' expected %s but got %s", "bucket", *pre.Bucket)
}
if *pre.Server != expectedServer {
t.Errorf("upload: Prefix field 'Server' expected %s but got %s", "server", *pre.Server)
}
if tu == nil {
t.Errorf("upload: did not create an uploader")
}
if tu.StorageClass != "STANDARD" {
t.Errorf("upload: TarUploader field 'StorageClass' expected %s but got %s", "STANDARD", tu.StorageClass)
}
if err != nil {
t.Errorf("upload: expected error to be '<nil>' but got %s", err)
}
//Test STANDARD_IA storage class
err = os.Setenv("WALG_S3_STORAGE_CLASS", "STANDARD_IA")
defer os.Unsetenv("WALG_S3_STORAGE_CLASS")
if err != nil {
t.Log(err)
}
tu, pre, err = walg.Configure()
if tu.StorageClass != "STANDARD_IA" {
t.Errorf("upload: TarUploader field 'StorageClass' expected %s but got %s", "STANDARD_IA", tu.StorageClass)
}
}
func TestValidUploader(t *testing.T) {
mockSvc := &mockS3Client{}
tu := walg.NewTarUploader(mockSvc, "bucket", "server", "region", 1, float64(1))
if tu == nil {
t.Errorf("upload: Did not create a new tar uploader")
}
upl := walg.CreateUploader(mockSvc, 100, 3)
if upl == nil {
t.Errorf("upload: Did not create a new S3 UploadManager")
}
}
func TestUploadError(t *testing.T) {
mockClient := &mockS3Client{}
mockUploader := &mockS3Uploader{
err: true,
}
tu := walg.NewTarUploader(mockClient, "bucket", "server", "region", 2, float64(2))
tu.Upl = mockUploader
maker := &walg.S3TarBallMaker{
BaseDir: "tmp",
Trim: "/usr/local",
BkupName: "test",
Tu: tu,
}
tarBall := maker.Make(true)
tarBall.SetUp(walg.MockArmedCrypter())
tarBall.Finish(&walg.S3TarBallSentinelDto{})
if tu.Success {
t.Errorf("upload: expected to fail to upload successfully")
}
tu.Upl = &mockS3Uploader{
multierr: true,
}
tarBall = maker.Make(true)
tarBall.SetUp(walg.MockArmedCrypter())
tarBall.Finish(&walg.S3TarBallSentinelDto{})
if tu.Success {
t.Errorf("upload: expected to fail to upload successfully")
}
_, err := tu.UploadWal("fake path")
if err == nil {
t.Errorf("upload: UploadWal expected error but got `<nil>`")
}
}