summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/ext/storage_sources/s3_store/test/test_s3_connection.cpp
blob: a87cc2b88f2c9ad4e040f76d1981a10c4e90948a (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include <s3_connection.h>
#include <fstream>
#include <random>

/* Default config settings for the Test environment. */
namespace TestDefaults {
const Aws::String region = Aws::Region::AP_SOUTHEAST_2;
const double throughputTargetGbps = 5;
const uint64_t partSize = 8 * 1024 * 1024;  /* 8 MB. */
static std::string bucketName("s3testext"); // Can be overridden with environment variables.
static std::string objPrefix("s3test_artefacts/unit_"); // To be concatenated with a random string.
} // namespace TestDefaults

#define TEST_SUCCESS 0
#define TEST_FAILURE 1

int TestListObjects(const Aws::S3Crt::ClientConfiguration &config);
int TestGetObject(const Aws::S3Crt::ClientConfiguration &config);
int TestObjectExists(const Aws::S3Crt::ClientConfiguration &config);

/* Wrapper for unit test functions. */
#define TEST(func, config)                            \
    do {                                              \
        int __ret;                                    \
        if ((__ret = (func(config))) != TEST_SUCCESS) \
            return (__ret);                           \
    } while (0)

/*
 * randomizeTestPrefix --
 *     Concatenates a random suffix to the prefix being used for the test object keys. Example of
 *     generated test prefix: "s3test_artefacts/unit_" 2022-31-01-16-34-10_623843294/"
 */
static int
randomizeTestPrefix()
{
    char timeStr[100];
    std::time_t t = std::time(nullptr);

    if (std::strftime(timeStr, sizeof(timeStr), "%F-%H-%M-%S", std::localtime(&t)) == 0)
        return (TEST_FAILURE);

    TestDefaults::objPrefix += timeStr;

    /* Create a random device and use it to generate a random seed to initialize the generator. */
    std::random_device myRandomDevice;
    unsigned seed = myRandomDevice();
    std::default_random_engine myRandomEngine(seed);

    TestDefaults::objPrefix += '_' + std::to_string(myRandomEngine());
    TestDefaults::objPrefix += '/';

    return (TEST_SUCCESS);
}

/*
 * setupTestDefaults --
 *     Override the defaults with the ones specific for this test instance.
 */
static int
setupTestDefaults()
{
    /* Prefer to use the bucket provided through the environment variable. */
    const char *envBucket = std::getenv("WT_S3_EXT_BUCKET");
    if (envBucket != NULL)
        TestDefaults::bucketName = envBucket;
    std::cerr << "Bucket to be used for testing: " << TestDefaults::bucketName << std::endl;

    /* Append the prefix to be used for object names by a unique string. */
    if (randomizeTestPrefix() != 0)
        return (TEST_FAILURE);
    std::cerr << "Generated prefix: " << TestDefaults::objPrefix << std::endl;

    return (TEST_SUCCESS);
}

static int
CleanupTestListObjects(S3Connection &conn, const int totalObjects, const std::string &prefix,
  const std::string &fileName)
{
    /* Delete objects and file at end of test. */
    int ret = 0;
    for (int i = 0; i < totalObjects; i++) {
        if ((ret = conn.DeleteObject(prefix + std::to_string(i) + ".txt")) != 0)
            std::cerr << "Error in CleanupTestListBuckets: failed to remove "
                      << TestDefaults::objPrefix + prefix << std::to_string(i) << ".txt from "
                      << TestDefaults::bucketName << std::endl;
    }
    std::remove(fileName.c_str());

    return (ret);
}

/*
 * TestListObjects --
 *     Unit test for listing S3 objects under the test bucket.
 */
/* Todo: Remove code duplication in this function. */
int
TestListObjects(const Aws::S3Crt::ClientConfiguration &config)
{
    S3Connection conn(config, TestDefaults::bucketName, TestDefaults::objPrefix);
    std::vector<std::string> objects;

    /* Name of file to insert in the test. */
    const std::string fileName = "test_list_objects.txt";
    /* Total objects to insert in the test. */
    const int32_t totalObjects = 20;
    /* Prefix for objects in this test. */
    const std::string prefix = "test_list_objects_";
    /* Parameter for getting single object. */
    const bool listSingle = true;
    /* Number of objects to access per iteration of AWS. */
    int32_t batchSize = 1;
    /* Expected number of matches. */
    int32_t expectedResult = 0;

    int ret;
    /* No matching objects. */
    if ((ret = conn.ListObjects(prefix, objects)) != 0)
        return (ret);
    if (objects.size() != expectedResult)
        return (TEST_FAILURE);

    /* No matching objects with listSingle. */
    if ((ret = conn.ListObjects(prefix, objects, batchSize, listSingle)) != 0)
        return (ret);
    if (objects.size() != expectedResult)
        return (TEST_FAILURE);

    /* Create file to prepare for test. */
    if (!static_cast<bool>(std::ofstream(fileName).put('.'))) {
        std::cerr << "Error creating file." << std::endl;
        return (TEST_FAILURE);
    }

    /* Put objects to prepare for test. */
    for (int i = 0; i < totalObjects; i++) {
        if ((ret = conn.PutObject(prefix + std::to_string(i) + ".txt", fileName)) != 0) {
            CleanupTestListObjects(conn, i, prefix, fileName);
            return (ret);
        }
    }

    /* List all objects. */
    expectedResult = totalObjects;
    if ((ret = conn.ListObjects(prefix, objects)) != 0) {
        CleanupTestListObjects(conn, totalObjects, prefix, fileName);
        return (ret);
    }
    if (objects.size() != expectedResult) {
        CleanupTestListObjects(conn, totalObjects, prefix, fileName);
        return (TEST_FAILURE);
    }

    /* List single. */
    objects.clear();
    expectedResult = 1;
    if ((ret = conn.ListObjects(prefix, objects, batchSize, listSingle)) != 0) {
        CleanupTestListObjects(conn, totalObjects, prefix, fileName);
        return (ret);
    }
    if (objects.size() != expectedResult) {
        CleanupTestListObjects(conn, totalObjects, prefix, fileName);
        return (TEST_FAILURE);
    }

    /* Expected number of matches with test_list_objects_1 prefix. */
    objects.clear();
    expectedResult = 11;
    if ((ret = conn.ListObjects(prefix + "1", objects)) != 0) {
        CleanupTestListObjects(conn, totalObjects, prefix, fileName);
        return (ret);
    }
    if (objects.size() != expectedResult) {
        CleanupTestListObjects(conn, totalObjects, prefix, fileName);
        return (TEST_FAILURE);
    }

    /* List with 5 objects per AWS request. */
    objects.clear();
    batchSize = 5;
    expectedResult = totalObjects;
    if ((ret = conn.ListObjects(prefix, objects, batchSize)) != 0) {
        CleanupTestListObjects(conn, totalObjects, prefix, fileName);
        return (ret);
    }
    if (objects.size() != expectedResult) {
        CleanupTestListObjects(conn, totalObjects, prefix, fileName);
        return (TEST_FAILURE);
    }

    /* ListSingle with 8 objects per AWS request. */
    objects.clear();
    expectedResult = 1;
    if ((ret = conn.ListObjects(prefix, objects, batchSize, listSingle)) != 0) {
        CleanupTestListObjects(conn, totalObjects, prefix, fileName);
        return (ret);
    }
    if (objects.size() != expectedResult) {
        CleanupTestListObjects(conn, totalObjects, prefix, fileName);
        return (TEST_FAILURE);
    }

    /* List with 8 objects per AWS request. */
    objects.clear();
    batchSize = 8;
    expectedResult = totalObjects;
    if ((ret = conn.ListObjects(prefix, objects, batchSize)) != 0) {
        CleanupTestListObjects(conn, totalObjects, prefix, fileName);
        return (ret);
    }
    if (objects.size() != expectedResult) {
        CleanupTestListObjects(conn, totalObjects, prefix, fileName);
        return (TEST_FAILURE);
    }

    /* ListSingle with 8 objects per AWS request. */
    objects.clear();
    expectedResult = 1;
    if ((ret = conn.ListObjects(prefix, objects, batchSize, listSingle)) != 0) {
        CleanupTestListObjects(conn, totalObjects, prefix, fileName);
        return (ret);
    }
    if (objects.size() != expectedResult) {
        CleanupTestListObjects(conn, totalObjects, prefix, fileName);
        return (TEST_FAILURE);
    }

    // CleanupTestListObjects(conn, totalObjects, prefix, fileName);
    std::cout << "TestListObjects(): succeeded." << std::endl;
    return (TEST_SUCCESS);
}

/*
 * TestGetObject --
 *     Unit test to get an object from an S3 Bucket.
 */
int
TestGetObject(const Aws::S3Crt::ClientConfiguration &config)
{
    S3Connection conn(config, TestDefaults::bucketName, TestDefaults::objPrefix);
    int ret = TEST_FAILURE;

    const std::string objectName = "permanent_object";
    const std::string path = "./" + objectName;

    /* Create a file and upload to the bucket. */
    std::ofstream File(objectName);
    File << "Test payload";
    File.close();
    if ((ret = conn.PutObject(objectName, objectName)) != 0)
        return (ret);

    /* Delete the local copy of the file. */
    if (std::remove(path.c_str()) != 0)
        return (TEST_FAILURE);

    /* Download the file from S3 */
    if ((ret = conn.GetObject(objectName, path)) != 0) {
        std::cerr << "TestGetObject: call to S3Connection:GetObject has failed." << std::endl;
        return (ret);
    }

    /* The file should now be in the current directory. */
    std::ifstream f(path);
    if (!f.good()) {
        std::cerr << "TestGetObject: target " << objectName
                  << " has not been successfully downloaded." << std::endl;
        return (TEST_FAILURE);
    }

    /* Clean up test artifacts. */
    if (std::remove(path.c_str()) != 0)
        return (TEST_FAILURE);

    if ((ret = conn.DeleteObject(objectName)) != 0)
        return (ret);

    std::cout << "TestGetObject() succeeded." << std::endl;
    return (TEST_SUCCESS);
}
/*
 * TestObjectExists --
 *     Unit test to check if an object exists in an AWS bucket and size of the object is correct.
 */
int
TestObjectExists(const Aws::S3Crt::ClientConfiguration &config)
{
    S3Connection conn(config, TestDefaults::bucketName, TestDefaults::objPrefix);
    bool exists = false;
    int ret = TEST_FAILURE;
    size_t objectSize;

    const std::string objectName = "test_object";
    const std::string fileName = "test_object.txt";

    /* Create a file to upload to the bucket.*/
    std::ofstream File(fileName);
    std::string payload = "Test payload";
    File << payload;
    File.close();

    if ((ret = conn.ObjectExists(objectName, exists, objectSize)) != 0)
        return (ret);
    if (exists || objectSize != 0)
        return (TEST_FAILURE);

    if ((ret = conn.PutObject(objectName, fileName)) != 0)
        return (ret);

    if ((ret = conn.ObjectExists(objectName, exists, objectSize)) != 0)
        return (ret);
    if (!exists)
        return (TEST_FAILURE);

    if (objectSize != payload.length()) {
        std::cerr << "TestObjectExist().objectSize failed." << std::endl;
        return (TEST_FAILURE);
    }

    if ((ret = conn.DeleteObject(objectName)) != 0)
        return (ret);
    std::cout << "TestObjectExists() succeeded." << std::endl;
    return (TEST_SUCCESS);
}

/*
 * main --
 *     Set up configs and call unit tests.
 */
int
main()
{
    /* Setup the test environment. */
    if (setupTestDefaults() != 0)
        return (TEST_FAILURE);

    /* Set up the config to use the defaults specified. */
    Aws::S3Crt::ClientConfiguration awsConfig;
    awsConfig.region = TestDefaults::region;
    awsConfig.throughputTargetGbps = TestDefaults::throughputTargetGbps;
    awsConfig.partSize = TestDefaults::partSize;

    /* Set the SDK options and initialize the API. */
    Aws::SDKOptions options;
    Aws::InitAPI(options);

    TEST(TestObjectExists, awsConfig);
    TEST(TestListObjects, awsConfig);
    TEST(TestGetObject, awsConfig);

    /* Shutdown the API at end of tests. */
    Aws::ShutdownAPI(options);
    return (TEST_SUCCESS);
}