-
Notifications
You must be signed in to change notification settings - Fork 9
/
s3_test.go
257 lines (232 loc) · 6.92 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
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
/*
Copyright © 2022, 2023 Red Hat, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main_test
// Generated documentation is available at:
// https://pkg.go.dev/github.com/RedHatInsights/insights-results-aggregator-exporter
//
// Documentation in literate-programming-style is available at:
// https://redhatinsights.github.io/insights-results-aggregator-exporter/packages/s3_test.html
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/minio/minio-go/v7"
main "github.com/RedHatInsights/insights-results-aggregator-exporter"
)
// mustConstructMinioClient helper function constructs an instance of Minio
// client or make the test fail
func mustConstructMinioClient(t *testing.T) *minio.Client {
minioClient, err := minio.New("localhost:1234", &minio.Options{})
assert.Nil(t, err)
return minioClient
}
// Test case specification structure for function main.NewS3Connection
type newS3ConnectionTestSpecification struct {
description string
configuration *main.ConfigStruct
shouldFail bool
expectedError string
}
// TestNewS3Connection checks the function/constructor NewS3Connection
func TestNewS3Connection(t *testing.T) {
// all test cases
testCases := []newS3ConnectionTestSpecification{
{
description: "nilConfiguration",
configuration: nil,
shouldFail: true,
expectedError: "Configuration is nil",
},
{
description: "emptyConfiguration",
configuration: &main.ConfigStruct{},
shouldFail: true,
expectedError: "Endpoint: does not follow ip address or domain name standards.",
},
{
description: "wrongConfiguration",
configuration: &main.ConfigStruct{
S3: main.S3Configuration{
Type: "",
EndpointURL: "",
EndpointPort: 1234,
AccessKeyID: "",
SecretAccessKey: "",
UseSSL: false,
Bucket: "",
}},
shouldFail: true,
expectedError: "Endpoint: :1234 does not follow ip address or domain name standards.",
},
{
description: "correctConfiguration",
configuration: &main.ConfigStruct{
S3: main.S3Configuration{
Type: "minio",
EndpointURL: "localhost",
EndpointPort: 1234,
AccessKeyID: "foobar",
SecretAccessKey: "foobar",
UseSSL: false,
Bucket: "test",
}},
shouldFail: false,
},
}
// run all specified test cases
for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
// try to construct Minio client using nil
// configuration
client, _, err := main.NewS3Connection(testCase.configuration)
// check for error
if testCase.shouldFail {
// client should not be constructed and error
// should be returned
assert.Error(t, err)
assert.Contains(t, err.Error(), testCase.expectedError)
assert.Nil(t, client)
} else {
// client should be constructed and error
// should not be returned
assert.NoError(t, err)
assert.NotNil(t, client)
}
})
}
}
// Test case specification structure for function main.s3BucketExists
type s3BucketExistsTestSpecification struct {
description string
minioClient *minio.Client
bucketName string
shouldFail bool
expectedError string
}
// TestS3BucketExists checks the function s3BucketExists
func TestS3BucketExists(t *testing.T) {
ctx := context.Background()
// all test cases
testCases := []s3BucketExistsTestSpecification{
{
description: "NoMinioClient",
minioClient: nil,
bucketName: "",
shouldFail: true,
expectedError: "Minio Client is nil",
},
{
description: "EmptyBucketName",
minioClient: mustConstructMinioClient(t),
bucketName: "",
shouldFail: true,
expectedError: "Bucket name is not set",
},
{
description: "NotAccessibleClient",
minioClient: mustConstructMinioClient(t),
bucketName: "bucket",
shouldFail: true,
expectedError: "connect: connection refused",
}}
// run all specified test cases
for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
_, err := main.S3BucketExists(ctx,
testCase.minioClient, testCase.bucketName)
// check for error
if testCase.shouldFail {
assert.Error(t, err)
assert.Contains(t, err.Error(), testCase.expectedError)
} else {
assert.NoError(t, err)
}
})
}
}
// Test case specification structure for function main.storeTableNames
type storeTableTestSpecification struct {
description string
minioClient *minio.Client
bucketName string
objectName string
tableNames []main.TableName
shouldFail bool
expectedError string
}
// TestStoreTableNamesNoClient checks the function storeTableNames
func TestStoreTable(t *testing.T) {
ctx := context.Background()
// all test cases
testCases := []storeTableTestSpecification{
{
description: "NoMinioClient",
minioClient: nil,
bucketName: "",
objectName: "",
tableNames: []main.TableName{},
shouldFail: true,
expectedError: "Minio Client is nil",
},
{
description: "EmptyBucketName",
minioClient: mustConstructMinioClient(t),
bucketName: "",
objectName: "",
tableNames: []main.TableName{},
shouldFail: true,
expectedError: "Bucket name is not set",
},
{
description: "EmptyObjectName",
minioClient: mustConstructMinioClient(t),
bucketName: "bucket",
objectName: "",
tableNames: []main.TableName{},
shouldFail: true,
expectedError: "Object name is not set",
},
{
description: "NotAccessibleClient",
minioClient: mustConstructMinioClient(t),
bucketName: "bucket",
objectName: "object",
tableNames: []main.TableName{},
shouldFail: true,
expectedError: "connect: connection refused",
},
{
description: "NotAccessibleClient",
minioClient: mustConstructMinioClient(t),
bucketName: "bucket",
objectName: "object",
tableNames: []main.TableName{main.TableName("first"), main.TableName("second")},
shouldFail: true,
expectedError: "connect: connection refused",
}}
// run all specified test cases
for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
err := main.StoreTableNames(ctx, testCase.minioClient,
testCase.bucketName, testCase.objectName,
testCase.tableNames)
// check for error
if testCase.shouldFail {
assert.Error(t, err)
assert.Contains(t, err.Error(), testCase.expectedError)
} else {
assert.NoError(t, err)
}
})
}
}