@@ -4,11 +4,20 @@ package radosgwadmin
4
4
5
5
import (
6
6
"context"
7
+ "io"
7
8
"io/ioutil"
9
+ "math/rand"
8
10
"os"
9
11
"strconv"
10
12
"testing"
11
13
14
+ "github.com/aws/aws-sdk-go/aws"
15
+ "github.com/aws/aws-sdk-go/aws/awserr"
16
+ "github.com/aws/aws-sdk-go/aws/credentials"
17
+ "github.com/aws/aws-sdk-go/aws/session"
18
+ "github.com/aws/aws-sdk-go/service/s3"
19
+ "github.com/aws/aws-sdk-go/service/s3/s3manager"
20
+
12
21
"github.com/BurntSushi/toml"
13
22
"github.com/davecgh/go-spew/spew"
14
23
"github.com/stretchr/testify/suite"
@@ -19,7 +28,7 @@ type IntegrationsSuite struct {
19
28
aa * AdminAPI
20
29
randFilePath string
21
30
lf * os.File
22
- i * Integration
31
+ ic * IntegrationConfig
23
32
}
24
33
25
34
type IntegrationConfig struct {
@@ -61,8 +70,9 @@ func (is *IntegrationsSuite) SetupSuite() {
61
70
is .T ().Logf ("cannot parse config file at location '%s' : %s" , cfgFile , err )
62
71
os .Exit (1 )
63
72
}
73
+
74
+ is .ic = cfg
64
75
is .aa , err = NewAdminAPI (cfg .RGW )
65
- is .i = cfg .Integration
66
76
if err != nil {
67
77
is .T ().Logf ("Error initializing AdminAPI: %s" , err )
68
78
os .Exit (1 )
@@ -80,7 +90,7 @@ func (is *IntegrationsSuite) Test01Usage() {
80
90
usage , err := is .aa .Usage (context .Background (), nil )
81
91
is .NoError (err , "Got error getting Usage" )
82
92
is .T ().Logf ("usage: %#v" , usage )
83
- err = is .aa .UsageTrim (context .Background (), & TrimUsageRequest {UID : is .i .TestUID })
93
+ err = is .aa .UsageTrim (context .Background (), & TrimUsageRequest {UID : is .ic . Integration .TestUID })
84
94
is .NoError (err , "Got error trimming usage" )
85
95
}
86
96
@@ -92,24 +102,24 @@ func (is *IntegrationsSuite) Test02Metadata() {
92
102
93
103
func (is * IntegrationsSuite ) Test03UserCreate () {
94
104
ur := new (UserCreateRequest )
95
- ur .UID = is .i .TestUID
96
- ur .Email = is .i .TestEmail
97
- ur .DisplayName = is .i .TestDisplayName
105
+ ur .UID = is .ic . Integration .TestUID
106
+ ur .Email = is .ic . Integration .TestEmail
107
+ ur .DisplayName = is .ic . Integration .TestDisplayName
98
108
ur .UserCaps = []UserCap {{"users" , "*" }, {"metadata" , "*" }, {"buckets" , "read" }}
99
109
100
110
resp , err := is .aa .UserCreate (context .Background (), ur )
101
111
is .NoError (err , "Got error running UserCreate" )
102
112
is .T ().Logf ("%#v" , resp )
103
113
sur := new (SubUserCreateModifyRequest )
104
- sur .UID = is .i .TestUID
114
+ sur .UID = is .ic . Integration .TestUID
105
115
sur .Access = "full"
106
116
sur .KeyType = "s3"
107
- sur .SubUser = is .i .TestSubUser
117
+ sur .SubUser = is .ic . Integration .TestSubUser
108
118
sur .GenerateSecret = true
109
119
nresp , err := is .aa .SubUserCreate (context .Background (), sur )
110
120
is .NoError (err )
111
121
is .T ().Logf ("%#v" , nresp )
112
- sur .SubUser = is .i .TesTSubUser2
122
+ sur .SubUser = is .ic . Integration .TesTSubUser2
113
123
sur .Access = "read"
114
124
nresp , err = is .aa .SubUserCreate (context .Background (), sur )
115
125
is .NoError (err )
@@ -120,21 +130,37 @@ func (is *IntegrationsSuite) Test04Quota() {
120
130
qsr := new (QuotaSetRequest )
121
131
qsr .Enabled = true
122
132
qsr .MaximumObjects = - 1 // unlimited
123
- qsr .MaximumSizeKb = 8192
133
+ qsr .MaximumSizeKb = 61440
124
134
qsr .QuotaType = "user"
125
- qsr .UID = is .i .TestUID
135
+ qsr .UID = is .ic . Integration .TestUID
126
136
err := is .aa .QuotaSet (context .Background (), qsr )
127
137
is .NoError (err , "Got error running SetQuota" )
128
138
// read it back
129
- qresp , err := is .aa .QuotaUser (context .Background (), is .i .TestUID )
139
+ qresp , err := is .aa .QuotaUser (context .Background (), is .ic . Integration .TestUID )
130
140
is .T ().Logf ("%#v" , qresp )
131
141
is .NoError (err , "Got error fetching user quota" )
132
142
is .True (qresp .Enabled == true , "quota not enabled" )
133
143
is .Equal (qresp .MaxObjects , int64 (- 1 ), "MaxObjects not -1" )
134
- is .Equal (qresp .MaxSizeKb , int64 (8192 ), "MaxSizeKb not 8192 " )
144
+ is .Equal (qresp .MaxSizeKb , int64 (61440 ), "MaxSizeKb not 61440 " )
135
145
}
136
146
137
147
func (is * IntegrationsSuite ) Test05Bucket () {
148
+ // Write 50mb to it.
149
+ is .writeRandomFile ("bigrandomfile.bin" , 1024 * 10 )
150
+ is .aa .BucketIndex (context .Background (), & BucketIndexRequest {
151
+ Bucket : is .ic .Integration .TestBucket ,
152
+ CheckObjects : true ,
153
+ Fix : true ,
154
+ })
155
+
156
+ // see if we can now get stats.
157
+
158
+ ui , err := is .aa .UserInfo (context .Background (), is .ic .Integration .TestUID , true )
159
+
160
+ is .NoError (err )
161
+ is .NotNil (ui .Stats )
162
+
163
+ is .T ().Logf ("%#v" , ui )
138
164
bucketnames , err := is .aa .BucketList (context .Background (), "" )
139
165
is .NoError (err , "Got error fetching bucket names" )
140
166
is .T ().Logf ("bucket names: %#v\n " , bucketnames )
@@ -144,26 +170,25 @@ func (is *IntegrationsSuite) Test05Bucket() {
144
170
is .T ().Log (spew .Sdump (bucketstats ))
145
171
146
172
// bucketstats with bucket filter
147
- bucketstatsf , err := is .aa .BucketStats (context .Background (), "" , is .i .TestBucket )
173
+ bucketstatsf , err := is .aa .BucketStats (context .Background (), "" , is .ic . Integration .TestBucket )
148
174
is .NoError (err , "got error fetching bucket stats filtered by bucket" )
149
175
is .T ().Log (spew .Sdump (bucketstatsf ))
150
176
151
177
// TODO: - make code that creates a bucket and does stuff to test
152
178
// bucket index code. -- for now, do one I know already exists
153
179
154
180
bireq := & BucketIndexRequest {}
155
- bireq .Bucket = is .i .TestBucket
181
+ bireq .Bucket = is .ic . Integration .TestBucket
156
182
bireq .CheckObjects = true
157
183
bireq .Fix = true
158
184
bucketindresp , err := is .aa .BucketIndex (context .Background (), bireq )
159
185
is .NoError (err , "Got error from BucketIndex()" )
160
186
is .T ().Logf (spew .Sdump (bucketindresp ))
161
-
162
187
}
163
188
164
189
func (is * IntegrationsSuite ) Test06Caps () {
165
190
ucr := & UserCapsRequest {
166
- UID : is .i .TestUID ,
191
+ UID : is .ic . Integration .TestUID ,
167
192
UserCaps : []UserCap {{"usage" , "read" }},
168
193
}
169
194
newcaps , err := is .aa .CapsAdd (context .Background (), ucr )
@@ -202,15 +227,14 @@ func (is *IntegrationsSuite) Test06Caps() {
202
227
}
203
228
}
204
229
is .Equal (goodct , 2 , "not expected removal of perms" )
205
-
206
230
}
207
231
208
232
func (is * IntegrationsSuite ) Test07Keys () {
209
233
accessKey := "TESTACCESSKEY"
210
234
secretKey := "TESTSECRETKEY"
211
235
generateKey := false
212
236
kc := & KeyCreateRequest {
213
- UID : is .i .TestUID ,
237
+ UID : is .ic . Integration .TestUID ,
214
238
AccessKey : accessKey ,
215
239
SecretKey : secretKey ,
216
240
GenerateKey : & generateKey ,
@@ -236,7 +260,7 @@ func (is *IntegrationsSuite) Test07Keys() {
236
260
is .NoError (err1 , "Unexpected error deleting key" )
237
261
238
262
// check if the key was really deleted
239
- userInfo , err2 := is .aa .UserInfo (context .Background (), is .i . TestUID )
263
+ userInfo , err2 := is .aa .UserInfo (context .Background (), is .ic . Integration . TestUID , false )
240
264
is .NoError (err2 , "Unexpected error reading user info" )
241
265
found = false
242
266
for i := range userInfo .Keys {
@@ -257,20 +281,111 @@ func (is *IntegrationsSuite) Test08RmUser() {
257
281
return
258
282
}
259
283
}
260
- err := is .aa .UserRm (context .Background (), is .i .TestUID , true )
284
+ err := is .aa .UserRm (context .Background (), is .ic . Integration .TestUID , true )
261
285
is .NoError (err , "got error removing user" )
262
286
users , err := is .aa .MListUsers (context .Background ())
263
287
is .NoError (err , "got error listing users" )
264
288
found := false
265
289
for _ , user := range users {
266
- if user == is .i .TestUID {
290
+ if user == is .ic . Integration .TestUID {
267
291
found = true
268
292
break
269
293
}
270
294
}
271
295
is .False (found , "user not successfully deleted" )
272
296
}
273
297
298
+ func (is * IntegrationsSuite ) writeRandomFile (path string , sizekb int ) {
299
+ var (
300
+ s3c * s3.S3
301
+ err error
302
+ ui * UserInfoResponse
303
+ creds * credentials.Credentials
304
+ )
305
+
306
+ // grab creds for subuser s3 user
307
+ ui , err = is .aa .UserInfo (context .Background (), is .ic .Integration .TestUID , false )
308
+
309
+ is .NoError (err )
310
+ for _ , k := range ui .Keys {
311
+ if k .User == is .ic .Integration .TestUID {
312
+ creds = credentials .NewStaticCredentials (k .AccessKey , k .SecretKey , "" )
313
+ break
314
+ }
315
+ }
316
+
317
+ is .NotNil (creds , "credentials not found" )
318
+ if creds == nil {
319
+ return
320
+ }
321
+ cfg := aws .NewConfig ()
322
+ cfg .Region = aws .String ("us-east" )
323
+ cfg .Endpoint = aws .String (is .ic .RGW .ServerURL )
324
+ cfg .WithS3ForcePathStyle (true )
325
+
326
+ cfg .Credentials = creds
327
+ sess := session .Must (session .NewSession (cfg ))
328
+ s3c = s3 .New (sess )
329
+
330
+ _ , err = s3c .CreateBucket (& s3.CreateBucketInput {
331
+ Bucket : aws .String (is .ic .Integration .TestBucket ),
332
+ CreateBucketConfiguration : & s3.CreateBucketConfiguration {
333
+ LocationConstraint : aws .String ("" ),
334
+ },
335
+ })
336
+ if err != nil {
337
+
338
+ // ignore non-fatal creation errors
339
+ if awsErr , ok := err .(awserr.Error ); ok {
340
+ if awsErr .Code () != s3 .ErrCodeBucketAlreadyExists &&
341
+ awsErr .Code () != s3 .ErrCodeBucketAlreadyOwnedByYou {
342
+ is .FailNow (err .Error ())
343
+ }
344
+ } else {
345
+ is .FailNow (err .Error ())
346
+ }
347
+ }
348
+
349
+ rr := NewRandoReader (0 , int64 (1024 * sizekb ))
350
+
351
+ uploader := s3manager .NewUploader (sess )
352
+
353
+ _ , err = uploader .Upload (& s3manager.UploadInput {
354
+ Bucket : aws .String (is .ic .Integration .TestBucket ),
355
+ Key : aws .String (path ),
356
+ Body : rr ,
357
+ })
358
+ if err != nil {
359
+ is .Fail (err .Error ())
360
+ }
361
+
362
+ }
363
+
364
+ type RandoReader struct {
365
+ r * rand.Rand
366
+ max int64
367
+ read int64
368
+ }
369
+
370
+ func NewRandoReader (seed int64 , bytes int64 ) * RandoReader {
371
+ return & RandoReader {
372
+ r : rand .New (rand .NewSource (seed )),
373
+ max : bytes ,
374
+ read : 0 ,
375
+ }
376
+ }
377
+
378
+ func (rr * RandoReader ) Read (p []byte ) (n int , err error ) {
379
+ if rr .read >= rr .max {
380
+ return 0 , io .EOF
381
+ } else if rr .max < (rr .read + int64 (len (p ))) {
382
+ p = p [:(rr .max - rr .read )]
383
+ }
384
+ c , err := rr .r .Read (p )
385
+ rr .read += int64 (c )
386
+ return c , err
387
+ }
388
+
274
389
func TestIntegrations (t * testing.T ) {
275
390
suite .Run (t , new (IntegrationsSuite ))
276
391
}
0 commit comments