11#include " gtest/gtest.h"
2- #include < random>
32#include < s3cpp/s3.h>
43#include < string>
54
@@ -15,9 +14,9 @@ class S3 : public ::testing::Test {
1514 return ;
1615 }
1716
18- // Upload 10k files
17+ // Upload 1001 files
1918 if (client.ListObjects (" my-bucket" )->Contents .empty ()) {
20- for (int i = 1 ; i <= 10'000 ; i++) {
19+ for (int i = 1 ; i <= 1'001 ; i++) {
2120 const std::string key = std::format (" path/to/file_{}.txt" , i);
2221 const std::string body = std::format (" This is test file number {}" , i);
2322 auto putObjRes = client.PutObject (" my-bucket" , key, body);
@@ -30,22 +29,9 @@ class S3 : public ::testing::Test {
3029 }
3130};
3231
33- std::string generateRandomBucketName (const std::string& prefix = " test-bucket" ) {
34- static std::random_device rd;
35- static std::mt19937 gen (rd ());
36- static std::uniform_int_distribution<> dis (0 , 35 );
37-
38- const char * chars = " abcdefghijklmnopqrstuvwxyz0123456789" ;
39- std::string suffix;
40- for (int i = 0 ; i < 8 ; ++i) {
41- suffix += chars[dis (gen)];
42- }
43- return prefix + " -" + suffix;
44- }
45-
4632TEST_F (S3, ListObjectsBucket) {
4733 S3Client client (" minio_access" , " minio_secret" , " 127.0.0.1:9000" , S3AddressingStyle::PathStyle);
48- // Assuming the bucket has the 10K objects
34+ // Assuming the bucket has 1001 objects
4935 // Once we implement PutObject we will do this ourselves with s3cpp
5036 std::expected<ListObjectsResult, Error> res = client.ListObjects (" my-bucket" );
5137 if (!res)
@@ -114,7 +100,7 @@ TEST_F(S3, ListObjectsCheckFields) {
114100
115101TEST_F (S3, ListObjectsCheckLenKeys) {
116102 S3Client client (" minio_access" , " minio_secret" , " 127.0.0.1:9000" , S3AddressingStyle::PathStyle);
117- // has 10K objects - limit is 1000 keys
103+ // has 1001 objects - limit is 1000 keys
118104 std::expected<ListObjectsResult, Error> res = client.ListObjects (" my-bucket" , { .Prefix = " path/to/" });
119105 if (!res)
120106 GTEST_FAIL ();
@@ -123,7 +109,7 @@ TEST_F(S3, ListObjectsCheckLenKeys) {
123109
124110TEST_F (S3, ListObjectsPaginator) {
125111 S3Client client (" minio_access" , " minio_secret" , " 127.0.0.1:9000" , S3AddressingStyle::PathStyle);
126- // has 10K objects - fetch 100 per page
112+ // has 1001 objects - fetch 100 per page
127113 ListObjectsPaginator paginator (client, " my-bucket" , " path/to/" , 100 );
128114
129115 int totalObjects = 0 ;
@@ -144,8 +130,8 @@ TEST_F(S3, ListObjectsPaginator) {
144130 }
145131 }
146132
147- EXPECT_EQ (totalObjects, 10000 );
148- EXPECT_EQ (pageCount, 100 );
133+ EXPECT_EQ (totalObjects, 1001 );
134+ EXPECT_EQ (pageCount, 11 );
149135}
150136
151137TEST_F (S3, GetObjectExists) {
@@ -247,7 +233,7 @@ TEST_F(S3, PutObjectTxt) {
247233
248234TEST_F (S3, CreateBucket) {
249235 S3Client client (" minio_access" , " minio_secret" , " 127.0.0.1:9000" , S3AddressingStyle::PathStyle);
250- std::string bucketName = generateRandomBucketName ( " test-bucket-s3cpp" ) ;
236+ std::string bucketName = " test-bucket-s3cpp" ;
251237 CreateBucketConfiguration config;
252238 CreateBucketInput options;
253239
@@ -270,7 +256,7 @@ TEST_F(S3, CreateBucket) {
270256
271257TEST_F (S3, CreateBucketWithLocationConstraint) {
272258 S3Client client (" minio_access" , " minio_secret" , " 127.0.0.1:9000" , S3AddressingStyle::PathStyle);
273- std::string bucketName = generateRandomBucketName ( " test-bucket-location" ) ;
259+ std::string bucketName = " test-bucket-location" ;
274260 CreateBucketConfiguration config;
275261 config.LocationConstraint = " us-west-2" ;
276262 CreateBucketInput options;
@@ -294,7 +280,7 @@ TEST_F(S3, CreateBucketWithLocationConstraint) {
294280
295281TEST_F (S3, CreateBucketWithTags) {
296282 S3Client client (" minio_access" , " minio_secret" , " 127.0.0.1:9000" , S3AddressingStyle::PathStyle);
297- std::string bucketName = generateRandomBucketName ( " test-bucket-tags" ) ;
283+ std::string bucketName = " test-bucket-tags" ;
298284 CreateBucketConfiguration config;
299285 config.Tags = {
300286 { " Environment" , " Test" },
@@ -415,7 +401,7 @@ TEST_F(S3, DeleteBucketAndElementsWithPaginator) {
415401TEST_F (S3, HeadBucketExists) {
416402 S3Client client (" minio_access" , " minio_secret" , " 127.0.0.1:9000" , S3AddressingStyle::PathStyle);
417403
418- // Premise is that "my-bucket" already exists and contains 10k objects
404+ // Premise is that "my-bucket" already exists and contains 1001 objects
419405 auto res = client.HeadBucket (" my-bucket" );
420406 if (!res)
421407 FAIL () << std::format (" HeadBucket request failed for an existing bucket. Code={}, Message={}" , res.error ().Code , res.error ().Message );
@@ -453,3 +439,19 @@ TEST_F(S3, HeadObjectNotExists) {
453439
454440 EXPECT_EQ (res.error ().Code , " NoSuchKey" );
455441}
442+
443+ TEST_F (S3, ListBuckets) {
444+ S3Client client (" minio_access" , " minio_secret" , " 127.0.0.1:9000" , S3AddressingStyle::PathStyle);
445+
446+ std::expected<ListAllMyBucketsResult, Error> res = client.ListBuckets ();
447+ if (res) {
448+ EXPECT_EQ (res->Buckets .size (), 5 );
449+ std::vector<std::string> bucket_names;
450+ for (const auto & Bucket : res->Buckets ) {
451+ bucket_names.push_back (Bucket.Name );
452+ }
453+ EXPECT_TRUE (std::find (bucket_names.begin (), bucket_names.end (), " my-bucket" ) != bucket_names.end ());
454+ } else {
455+ FAIL () << std::format (" ListBuckets request failed. Code={}, Message={}" , res.error ().Code , res.error ().Message );
456+ }
457+ }
0 commit comments