forked from algorand/go-algorand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3Helper_test.go
235 lines (222 loc) · 9.32 KB
/
s3Helper_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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright (C) 2019 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
package s3
import (
"os"
"reflect"
"testing"
"github.com/stretchr/testify/require"
)
func TestGetS3UploadBucket(t *testing.T) {
tests := []struct {
name string
getDefault bool
wantBucketName string
}{
{name: "test1", wantBucketName: "test-bucket"},
{name: "test2", wantBucketName: "anotherbucket"},
{name: "test3", wantBucketName: ""},
{name: "test4", getDefault: true, wantBucketName: "algorand-uploads"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.getDefault {
os.Unsetenv("S3_UPLOAD_BUCKET")
} else {
os.Setenv("S3_UPLOAD_BUCKET", tt.wantBucketName)
}
if gotBucketName := GetS3UploadBucket(); gotBucketName != tt.wantBucketName {
t.Errorf("GetS3UploadBucket() = %v, want %v", gotBucketName, tt.wantBucketName)
}
})
}
}
func TestGetS3ReleaseBucket(t *testing.T) {
tests := []struct {
name string
getDefault bool
wantBucketName string
}{
{name: "test1", wantBucketName: "test-bucket"},
{name: "test2", wantBucketName: "anotherbucket"},
{name: "test3", wantBucketName: ""},
{name: "test4", getDefault: true, wantBucketName: "algorand-releases"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.getDefault {
os.Unsetenv("S3_RELEASE_BUCKET")
} else {
os.Setenv("S3_RELEASE_BUCKET", tt.wantBucketName)
}
if gotBucketName := GetS3ReleaseBucket(); gotBucketName != tt.wantBucketName {
t.Errorf("GetS3ReleaseBucket() = %v, want %v", gotBucketName, tt.wantBucketName)
}
})
}
}
func Test_getS3Region(t *testing.T) {
tests := []struct {
name string
getDefault bool
wantRegion string
}{
{name: "test1", wantRegion: "us-east1"},
{name: "test2", wantRegion: "us-west2"},
{name: "test3", wantRegion: ""},
{name: "test3", getDefault: true, wantRegion: "us-east-1"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.getDefault {
os.Unsetenv("S3_REGION")
} else {
os.Setenv("S3_REGION", tt.wantRegion)
}
if gotRegion := getS3Region(); gotRegion != tt.wantRegion {
t.Errorf("getS3Region() = %v, want %v", gotRegion, tt.wantRegion)
}
})
}
}
func TestMakeS3SessionForUploadWithBucket(t *testing.T) {
const bucket1 = "test-bucket"
const publicUploadBucket = "algorand-uploads"
const emptyBucket = ""
type args struct {
awsBucket string
awsID string
awsSecret string
}
tests := []struct {
name string
args args
wantHelper Helper
wantErr bool
}{
{name: "test1", args: args{awsBucket: bucket1, awsID: "AWS_ID", awsSecret: "AWS_SECRET"}, wantHelper: Helper{bucket: bucket1}, wantErr: false},
{name: "test2", args: args{awsBucket: emptyBucket, awsID: "AWS_ID", awsSecret: "AWS_SECRET"}, wantHelper: Helper{bucket: emptyBucket}, wantErr: true},
{name: "test3", args: args{awsBucket: bucket1, awsID: "", awsSecret: "AWS_SECRET"}, wantHelper: Helper{bucket: bucket1}, wantErr: true},
{name: "test4", args: args{awsBucket: bucket1, awsID: "AWS_ID", awsSecret: ""}, wantHelper: Helper{bucket: bucket1}, wantErr: true},
{name: "test5", args: args{awsBucket: bucket1, awsID: "", awsSecret: ""}, wantHelper: Helper{bucket: bucket1}, wantErr: true},
// public upload bucket requires AWS credentials for uploads
{name: "test6", args: args{awsBucket: publicUploadBucket, awsID: "AWS_ID", awsSecret: "AWS_SECRET"}, wantHelper: Helper{bucket: publicUploadBucket}, wantErr: false},
{name: "test7", args: args{awsBucket: publicUploadBucket, awsID: "", awsSecret: "AWS_SECRET"}, wantHelper: Helper{bucket: publicUploadBucket}, wantErr: true},
{name: "test8", args: args{awsBucket: publicUploadBucket, awsID: "AWS_ID", awsSecret: ""}, wantHelper: Helper{bucket: publicUploadBucket}, wantErr: true},
{name: "test9", args: args{awsBucket: publicUploadBucket, awsID: "", awsSecret: ""}, wantHelper: Helper{bucket: publicUploadBucket}, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Setenv("AWS_ACCESS_KEY_ID", tt.args.awsID)
os.Setenv("AWS_SECRET_ACCESS_KEY", tt.args.awsSecret)
gotHelper, err := MakeS3SessionForUploadWithBucket(tt.args.awsBucket)
if (err != nil) != tt.wantErr {
t.Errorf("MakeS3SessionForUploadWithBucket() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err == nil && !reflect.DeepEqual(gotHelper.bucket, tt.wantHelper.bucket) {
t.Errorf("MakeS3SessionForUploadWithBucket() = %v, want %v", gotHelper, tt.wantHelper)
}
})
}
}
func TestMakeS3SessionForDownloadWithBucket(t *testing.T) {
const bucket1 = "test-bucket"
const publicReleaseBucket = "algorand-releases"
const emptyBucket = ""
type args struct {
awsBucket string
awsID string
awsSecret string
}
tests := []struct {
name string
args args
wantHelper Helper
wantErr bool
}{
{name: "test1", args: args{awsBucket: bucket1, awsID: "AWS_ID", awsSecret: "AWS_SECRET"}, wantHelper: Helper{bucket: bucket1}, wantErr: false},
{name: "test2", args: args{awsBucket: emptyBucket, awsID: "AWS_ID", awsSecret: "AWS_SECRET"}, wantHelper: Helper{bucket: emptyBucket}, wantErr: true},
{name: "test3", args: args{awsBucket: bucket1, awsID: "", awsSecret: "AWS_SECRET"}, wantHelper: Helper{bucket: bucket1}, wantErr: true},
{name: "test4", args: args{awsBucket: bucket1, awsID: "AWS_ID", awsSecret: ""}, wantHelper: Helper{bucket: bucket1}, wantErr: true},
{name: "test5", args: args{awsBucket: bucket1, awsID: "", awsSecret: ""}, wantHelper: Helper{bucket: bucket1}, wantErr: true},
// public release bucket does not require AWS credentials for downloads
{name: "test6", args: args{awsBucket: publicReleaseBucket, awsID: "AWS_ID", awsSecret: "AWS_SECRET"}, wantHelper: Helper{bucket: publicReleaseBucket}, wantErr: false},
{name: "test7", args: args{awsBucket: publicReleaseBucket, awsID: "", awsSecret: "AWS_SECRET"}, wantHelper: Helper{bucket: publicReleaseBucket}, wantErr: false},
{name: "test8", args: args{awsBucket: publicReleaseBucket, awsID: "AWS_ID", awsSecret: ""}, wantHelper: Helper{bucket: publicReleaseBucket}, wantErr: false},
{name: "test9", args: args{awsBucket: publicReleaseBucket, awsID: "", awsSecret: ""}, wantHelper: Helper{bucket: publicReleaseBucket}, wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Setenv("AWS_ACCESS_KEY_ID", tt.args.awsID)
os.Setenv("AWS_SECRET_ACCESS_KEY", tt.args.awsSecret)
gotHelper, err := MakeS3SessionForDownloadWithBucket(tt.args.awsBucket)
if (err != nil) != tt.wantErr {
t.Errorf("MakeS3SessionForDownloadWithBucket() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err == nil && !reflect.DeepEqual(gotHelper.bucket, tt.wantHelper.bucket) {
t.Errorf("MakeS3SessionForDownloadWithBucket() = %v, want %v", gotHelper, tt.wantHelper)
}
})
}
}
func TestGetVersionFromName(t *testing.T) {
type args struct {
name string
version string
expected uint64
}
tests := []args{
{name: "test 1 (major)", version: "_1.0.0", expected: 1 * 1 << 32},
{name: "test 2 (major)", version: "_2.0.0", expected: 2 * 1 << 32},
{name: "test 3 (minor)", version: "_1.1.0", expected: 1*1<<32 + 1*1<<16},
{name: "test 4 (minor)", version: "_1.2.0", expected: 1*1<<32 + 2*1<<16},
{name: "test 5 (patch)", version: "_1.0.1", expected: 1*1<<32 + 1},
{name: "test 6 (patch)", version: "_1.0.2", expected: 1*1<<32 + 2},
}
for _, test := range tests {
actual, err := GetVersionFromName(test.version)
require.NoError(t, err, test.name)
require.Equal(t, test.expected, actual, test.name)
}
}
func TestGetPartsFromVersion(t *testing.T) {
type args struct {
name string
version uint64
expMajor uint64
expMinor uint64
expPatch uint64
}
tests := []args{
{name: "test 1 (major)", version: 1 * 1 << 32, expMajor: 1, expMinor: 0, expPatch: 0},
{name: "test 2 (major)", version: 2 * 1 << 32, expMajor: 2, expMinor: 0, expPatch: 0},
{name: "test 3 (minor)", version: 1*1<<32 + 1*1<<16, expMajor: 1, expMinor: 1, expPatch: 0},
{name: "test 4 (minor)", version: 1*1<<32 + 2*1<<16, expMajor: 1, expMinor: 2, expPatch: 0},
{name: "test 5 (patch)", version: 1*1<<32 + 1, expMajor: 1, expMinor: 0, expPatch: 1},
{name: "test 6 (patch)", version: 1*1<<32 + 2, expMajor: 1, expMinor: 0, expPatch: 2},
}
for _, test := range tests {
actualMajor, actualMinor, actualPatch, err := GetVersionPartsFromVersion(test.version)
require.NoError(t, err, test.name)
require.Equal(t, test.expMajor, actualMajor, test.name)
require.Equal(t, test.expMinor, actualMinor, test.name)
require.Equal(t, test.expPatch, actualPatch, test.name)
}
_, _, _, err := GetVersionPartsFromVersion(1<<32 - 1)
require.Error(t, err, "Versions less than 1.0.0 should not be parsed.")
}