summaryrefslogtreecommitdiff
path: root/util/s3/s3Helper_test.go
blob: 3f7eeb5fbd46d18f8a12d2c29d0388fe643ed56a (plain)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright (C) 2019-2024 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 (
	"fmt"
	"os"
	"reflect"
	"testing"

	"github.com/algorand/go-algorand/test/partitiontest"
	"github.com/stretchr/testify/require"
)

func TestGetS3UploadBucket(t *testing.T) {
	partitiontest.PartitionTest(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) {
	partitiontest.PartitionTest(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) {
	partitiontest.PartitionTest(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) {
	partitiontest.PartitionTest(t)

	const bucket1 = "test-bucket"
	const publicUploadBucket = "algorand-uploads"
	const emptyBucket = ""
	type args struct {
		awsBucket string
	}
	tests := []struct {
		name       string
		args       args
		wantHelper Helper
		wantErr    bool
	}{
		{name: "test1", args: args{awsBucket: bucket1}, wantHelper: Helper{bucket: bucket1}, wantErr: false},
		{name: "test2", args: args{awsBucket: emptyBucket}, wantHelper: Helper{bucket: emptyBucket}, wantErr: true},
		{name: "test6", args: args{awsBucket: publicUploadBucket}, wantHelper: Helper{bucket: publicUploadBucket}, wantErr: false},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			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) {
	partitiontest.PartitionTest(t)

	const bucket1 = "test-bucket"
	const publicReleaseBucket = "algorand-releases"
	const emptyBucket = ""
	type args struct {
		awsBucket string
	}
	tests := []struct {
		name       string
		args       args
		wantHelper Helper
		wantErr    bool
	}{
		{name: "test1", args: args{awsBucket: bucket1}, wantHelper: Helper{bucket: bucket1}, wantErr: false},
		{name: "test2", args: args{awsBucket: emptyBucket}, wantHelper: Helper{bucket: emptyBucket}, wantErr: true},
		{name: "test6", args: args{awsBucket: publicReleaseBucket}, wantHelper: Helper{bucket: publicReleaseBucket}, wantErr: false},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			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) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	type args struct {
		name     string
		version  string
		expected uint64
	}
	tests := []args{
		{name: "test 1 (major)", version: "_1.0.0", expected: 1 * 1 << 40},
		{name: "test 2 (major)", version: "_2.0.0", expected: 2 * 1 << 40},
		{name: "test 3 (minor)", version: "_1.1.0", expected: 1*1<<40 + 1*1<<24},
		{name: "test 4 (minor)", version: "_1.2.0", expected: 1*1<<40 + 2*1<<24},
		{name: "test 5 (patch)", version: "_1.0.1", expected: 1*1<<40 + 1},
		{name: "test 6 (patch)", version: "_1.0.2", expected: 1*1<<40 + 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 TestGetVersionFromNameCompare(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	name1 := "config_3.13.170018.tar.gz"
	name2 := "config_3.15.157.tar.gz"

	ver1, err := GetVersionFromName(name1)
	require.NoError(t, err)
	ver2, err := GetVersionFromName(name2)
	require.NoError(t, err)

	require.Less(t, ver1, ver2)
}

func TestGetPartsFromVersion(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	type args struct {
		name     string
		version  uint64
		expMajor uint64
		expMinor uint64
		expPatch uint64
	}
	tests := []args{
		{name: "test 1 (major)", version: 1 * 1 << 40, expMajor: 1, expMinor: 0, expPatch: 0},
		{name: "test 2 (major)", version: 2 * 1 << 40, expMajor: 2, expMinor: 0, expPatch: 0},
		{name: "test 3 (minor)", version: 1*1<<40 + 1*1<<24, expMajor: 1, expMinor: 1, expPatch: 0},
		{name: "test 4 (minor)", version: 1*1<<40 + 2*1<<24, expMajor: 1, expMinor: 2, expPatch: 0},
		{name: "test 5 (patch)", version: 1*1<<40 + 1, expMajor: 1, expMinor: 0, expPatch: 1},
		{name: "test 6 (patch)", version: 1*1<<40 + 2, expMajor: 1, expMinor: 0, expPatch: 2},
		{name: "test 6 (patch)", version: 3298803318784, expMajor: 3, expMinor: 16, expPatch: 0},
	}

	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<<40 - 1)
	require.Error(t, err, "Versions less than 1.0.0 should not be parsed.")
}

func TestGetPartsFromVersionEndToEnd(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	type args struct {
		major uint64
		minor uint64
		patch uint64
	}
	tests := []args{
		{major: 1, minor: 0, patch: 0},
		{major: 3, minor: 13, patch: 170018},
		{major: 3, minor: 15, patch: 157},
	}

	for _, test := range tests {
		name := fmt.Sprintf("config_%d.%d.%d.tar.gz", test.major, test.minor, test.patch)
		t.Run(name, func(t *testing.T) {
			ver, err := GetVersionFromName(name)
			require.NoError(t, err)
			actualMajor, actualMinor, actualPatch, err := GetVersionPartsFromVersion(ver)
			require.NoError(t, err)
			require.Equal(t, test.major, actualMajor)
			require.Equal(t, test.minor, actualMinor)
			require.Equal(t, test.patch, actualPatch)
		})
	}
}