summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/ext/storage_sources/gcp_store/test/test_gcp_connection.cpp
blob: 0fef6e7e730fbc1eed936eb288d4e0e6e23ef655 (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
/*-
 * Public Domain 2014-present MongoDB, Inc.
 * Public Domain 2008-2014 WiredTiger, Inc.
 *
 * This is free and unencumbered software released into the public domain.
 *
 * Anyone is free to copy, modify, publish, use, compile, sell, or
 * distribute this software, either in source code form or as a compiled
 * binary, for any purpose, commercial or non-commercial, and by any
 * means.
 *
 * In jurisdictions that recognize copyright laws, the author or authors
 * of this software dedicate any and all copyright interest in the
 * software to the public domain. We make this dedication for the benefit
 * of the public at large and to the detriment of our heirs and
 * successors. We intend this dedication to be an overt act of
 * relinquishment in perpetuity of all present and future rights to this
 * software under copyright law.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#define CATCH_CONFIG_MAIN
#include "gcp_connection.h"

#include <catch2/catch.hpp>

namespace gcs = google::cloud::storage;
using namespace gcs;

static std::string
create_file(const std::string file_name, const std::string payload)
{
    std::ofstream file(file_name);
    file << payload;
    file.close();
    return file_name;
}

static auto
upload_file(gcs::Client client, std::string bucket_name, const std::string bucket_prefix,
  const std::string file_name, const std::string object_name)
{
    auto metadata = client.UploadFile("./" + file_name, bucket_name, bucket_prefix + object_name);

    return metadata;
}

static bool
file_exists_in_bucket(gcs::Client client, std::string bucket_name, const std::string bucket_prefix,
  const std::string object_name)
{
    auto metadata = client.GetObjectMetadata(bucket_name, bucket_prefix + object_name);

    // Metadata ok implies that the file is present.
    return metadata.ok();
}

static int
num_objects_in_bucket(gcs::Client client, std::string bucket_name, const std::string bucket_prefix)
{
    auto objects_iterator = client.ListObjects(bucket_name, gcs::Prefix(bucket_prefix));
    return std::distance(objects_iterator.begin(), objects_iterator.end());
}

// Concatenates a random suffix to the prefix being used for the test object keys. Example of
// generated test prefix: "gcptest/unit/2022-31-01-16-34-10/623843294/".
static std::string
generate_test_prefix()
{
    std::string prefix = "gcptest/unit/";
    char time_str[100];
    std::time_t t = std::time(nullptr);

    REQUIRE(std::strftime(time_str, sizeof(time_str), "%F-%H-%M-%S", std::localtime(&t)) != 0);

    prefix += time_str;

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

    prefix += "/" + std::to_string(my_random_engine()) + "/";

    return prefix;
}

TEST_CASE("Testing class gcpConnection", "gcp-connection")
{
    std::string test_bucket_name = "unit_testing_gcp";

    // Set up the test environment.
    std::string test_bucket_prefix = generate_test_prefix();
    gcp_connection conn(test_bucket_name, test_bucket_prefix);

    const std::string object_name = "test_object";
    const std::string file_name = object_name + ".txt";
    const std::string non_existant_object_name = "test_non_exist";
    const std::string non_existant_file_name = non_existant_object_name + ".txt";
    const bool list_single = true;
    std::vector<std::string> objects;

    gcs::Client client = gcs::Client();

    std::string payload = "Test payload :)";
    create_file(file_name, payload);

    SECTION("Simple list test", "[gcp-connection]")
    {
        // No matching objects. Objects list should be empty.
        REQUIRE(conn.list_objects(objects, false) == 0);
        REQUIRE(objects.empty());

        // No matching objects with list_single. Objects list should be empty.
        REQUIRE(conn.list_objects(objects, list_single) == 0);
        REQUIRE(objects.empty());

        // Upload 1 file to the bucket and test list_objects function.
        // List_objects should return an objects list with size 1.
        REQUIRE(
          upload_file(client, test_bucket_name, test_bucket_prefix, file_name, object_name).ok());
        REQUIRE(file_exists_in_bucket(client, test_bucket_name, test_bucket_prefix, object_name));
        REQUIRE(conn.list_objects(objects, false) == 0);
        REQUIRE(objects.size() == 1);
        objects.clear();

        // Delete the object we uploaded and test list_objects function.
        // List_objects should return an empty objects list.
        client.DeleteObject(test_bucket_name, test_bucket_prefix + object_name);
        REQUIRE(conn.list_objects(objects, false) == 0);
        REQUIRE(objects.empty());
        objects.clear();

        // Upload multiple files and test list.
        const int32_t total_objects = 20;
        for (int i = 0; i < total_objects; i++) {
            std::string multi_file_name = object_name + std::to_string(i);
            REQUIRE(
              upload_file(client, test_bucket_name, test_bucket_prefix, file_name, multi_file_name)
                .ok());
        }

        // List objects should return a list with size total_objects.
        REQUIRE(conn.list_objects(objects, false) == 0);
        REQUIRE(objects.size() == total_objects);
        objects.clear();

        // Test if list single correctly returns one object.
        REQUIRE(conn.list_objects(objects, list_single) == 0);
        REQUIRE(objects.size() == 1);
        objects.clear();

        // Delete all object we uploaded.
        for (int i = 0; i < total_objects; i++) {
            client.DeleteObject(
              test_bucket_name, test_bucket_prefix + object_name + std::to_string(i));
        }

        // Bucket should be cleared.
        REQUIRE(conn.list_objects(objects, false) == 0);
        REQUIRE(objects.empty());
    }

    SECTION("Simple put test", "[gcp-connection]")
    {

        // Upload a file that does not exist locally - should fail.
        REQUIRE(conn.put_object(non_existant_object_name, non_existant_file_name) == ENOENT);

        // Check number of files with the given prefix that are currently in the bucket.
        REQUIRE(num_objects_in_bucket(client, test_bucket_name, test_bucket_prefix) == 0);

        // Upload a test file.
        REQUIRE(conn.put_object(object_name, "./" + file_name) == 0);

        // Check the bucket contains the uploaded file.
        REQUIRE(file_exists_in_bucket(client, test_bucket_name, test_bucket_prefix, object_name));

        // Delete the uploaded file.
        client.DeleteObject(test_bucket_name, test_bucket_prefix + object_name);
    }

    SECTION("Simple delete test", "[gcp-connection]")
    {

        // Delete a file that does not exist in the bucket - should fail.
        REQUIRE(conn.delete_object(non_existant_object_name) == ENOENT);

        // Upload a test file.
        REQUIRE(
          upload_file(client, test_bucket_name, test_bucket_prefix, file_name, object_name).ok());
        REQUIRE(file_exists_in_bucket(client, test_bucket_name, test_bucket_prefix, object_name));

        // Delete the uploaded file.
        REQUIRE(conn.delete_object(object_name) == 0);

        // Check that the file has been deleted.
        REQUIRE_FALSE(
          file_exists_in_bucket(client, test_bucket_name, test_bucket_prefix, object_name));
    }

    SECTION("Simple object exists test", "[gcp-connection]")
    {
        bool exists;
        size_t size;

        REQUIRE(conn.object_exists(object_name, exists, size) == 0);
        REQUIRE(exists == false);
        REQUIRE(size == 0);

        // Upload a test file.
        REQUIRE(
          upload_file(client, test_bucket_name, test_bucket_prefix, file_name, object_name).ok());
        REQUIRE(num_objects_in_bucket(client, test_bucket_name, test_bucket_prefix) == 1);

        // Check the bucket contains the uploaded file.
        REQUIRE(conn.object_exists(object_name, exists, size) == 0);
        REQUIRE(exists);
        REQUIRE(size != 0);

        // Delete the uploaded file.
        auto dl_metadata = client.DeleteObject(test_bucket_name, test_bucket_prefix + object_name);
        REQUIRE(dl_metadata.ok());

        // Check that the file has been deleted.
        REQUIRE_FALSE(
          file_exists_in_bucket(client, test_bucket_name, test_bucket_prefix, object_name));

        // Check for a file that is not in the bucket.
        // Object exists should modify the exists variable and set it to false.
        REQUIRE(conn.object_exists(object_name, exists, size) == 0);
        REQUIRE(exists == false);
        REQUIRE(size == 0);
    }

    SECTION("Read tests", "[gcp-connection]")
    {
        // Upload a test file.
        REQUIRE(
          upload_file(client, test_bucket_name, test_bucket_prefix, file_name, object_name).ok());
        REQUIRE(file_exists_in_bucket(client, test_bucket_name, test_bucket_prefix, object_name));

        // Read GCP objects under the test bucket with no offset.
        char buf[1024];

        REQUIRE(conn.read_object(object_name, 0, payload.length(), buf) == 0);
        REQUIRE(payload.compare(buf) == 0);
        memset(buf, 0, 1000);

        // Read GCP objects under the test bucket with offset.
        const int str_len = payload.length() - payload.find(" ");
        REQUIRE(conn.read_object(object_name, payload.find(" "), str_len, buf) == 0);
        REQUIRE(payload.substr(payload.find(" "), str_len).compare(buf) == 0);
        memset(buf, 0, 1000);

        // Read GCP objects under the test bucket with len > file length.
        REQUIRE(conn.read_object(object_name, 0, 100000, buf) == EINVAL);

        // Read GCP objects under the test bucket with offset < 0.
        REQUIRE(conn.read_object(object_name, -5, 15, buf) == EINVAL);

        // Read GCP objects under the test bucket with offset > file length.
        REQUIRE(conn.read_object(object_name, 1000, 15, buf) == EINVAL);
    }

    // Cleanup
    // List and loop through objects with prefix.
    for (auto &&object_metadata :
      client.ListObjects(test_bucket_name, gcs::Prefix(test_bucket_prefix))) {
        // Delete the test file.
        if (object_metadata) {
            auto dl_metadata =
              client.DeleteObject(test_bucket_name, object_metadata.value().name());
            REQUIRE(dl_metadata.ok());
        }
    }
}