s3downloader_test.cpp 8.1 KB
Newer Older
1
#include "s3downloader.cpp"
2
#include "gtest/gtest.h"
3

4 5
volatile bool QueryCancelPending = false;

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
TEST(OffsetMgr, simple) {
    OffsetMgr *o = new OffsetMgr(4096, 1000);
    Range r = o->NextOffset();
    EXPECT_EQ(r.offset, 0);
    EXPECT_EQ(r.len, 1000);

    r = o->NextOffset();
    EXPECT_EQ(r.offset, 1000);
    EXPECT_EQ(r.len, 1000);

    r = o->NextOffset();
    EXPECT_EQ(r.offset, 2000);
    EXPECT_EQ(r.len, 1000);

    r = o->NextOffset();
    EXPECT_EQ(r.offset, 3000);
    EXPECT_EQ(r.len, 1000);

    r = o->NextOffset();
    EXPECT_EQ(r.offset, 4000);
    EXPECT_EQ(r.len, 96);
    delete o;
}

TEST(OffsetMgr, reset) {
    OffsetMgr *o = new OffsetMgr(1024, 100);

    o->NextOffset();
    o->NextOffset();
    o->Reset(333);
    Range r = o->NextOffset();

    EXPECT_EQ(r.offset, 333);
    EXPECT_EQ(r.len, 100);
    delete o;
}

43
#ifdef FAKE_TEST
44 45 46 47 48 49
#define HOSTSTR "localhost"
#define BUCKETSTR "metro.pivotal.io"
TEST(ListBucket, fake) {
    ListBucketResult *r = ListBucket_FakeHTTP(HOSTSTR, BUCKETSTR);

    ASSERT_NE(r, (void *)NULL);
50 51
    EXPECT_EQ(16, r->contents.size());

52 53
    delete r;
}
54
#endif  // FAKE_TEST
55

A
Adam Lee 已提交
56
#define S3REGION "us-west-2"
57 58 59
#define S3BUCKET "s3test.pivotal.io"
#define S3PREFIX "dataset1/small17"

60
#ifdef AWS_TEST
61 62 63 64 65 66 67

TEST(ListBucket, s3) {
    InitConfig("test/s3.conf", "default");

    S3Credential g_cred = {s3ext_accessid, s3ext_secret};

    ListBucketResult *r =
A
Adam Lee 已提交
68
        ListBucket("https", S3REGION, S3BUCKET, S3PREFIX, g_cred);
69 70

    ASSERT_NE(r, (void *)NULL);
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
    EXPECT_EQ(16, r->contents.size());

    // leave this snippet here, for future debugging
    /*
     * char urlbuf[256];
     * vector<BucketContent *>::iterator i;
     * for (i = r->contents.begin(); i != r->contents.end(); i++) {
     *     BucketContent *p = *i;
     *     sprintf(urlbuf, "https://s3-%s.amazonaws.com/%s/%s", S3REGION,
     * S3BUCKET,
     *             p->Key().c_str());
     *     printf("%s, %d\n", urlbuf, p->Size());
     * }
     */

    delete r;
}

TEST(ListBucket, s3_1024) {
    InitConfig("test/s3.conf", "default");

    S3Credential g_cred = {s3ext_accessid, s3ext_secret};

    ListBucketResult *r =
        ListBucket("https", S3REGION, S3BUCKET, "1024files", g_cred);

    ASSERT_NE(r, (void *)NULL);
    EXPECT_EQ(1024, r->contents.size());

    delete r;
}

TEST(ListBucket, s3_5120) {
    InitConfig("test/s3.conf", "default");

    S3Credential g_cred = {s3ext_accessid, s3ext_secret};

    ListBucketResult *r =
        ListBucket("https", S3REGION, S3BUCKET, "5120files", g_cred);

    ASSERT_NE(r, (void *)NULL);
    EXPECT_EQ(5120, r->contents.size());
113 114 115 116

    delete r;
}

117
#endif  // AWS_TEST
118

A
Adam Lee 已提交
119 120 121
void DownloadTest(const char *url, const char *region, uint64_t file_size,
                  const char *md5_str, uint8_t thread_num, uint64_t chunk_size,
                  uint64_t buffer_size, bool use_credential) {
122 123 124 125 126 127 128 129 130 131 132 133 134
    InitConfig("test/s3.conf", "default");

    S3Credential g_cred = {s3ext_accessid, s3ext_secret};

    uint64_t buf_len = buffer_size;
    char *buf = (char *)malloc(buffer_size);
    Downloader *d = new Downloader(thread_num);
    MD5Calc m;
    bool result = false;

    ASSERT_NE((void *)NULL, buf);

    if (use_credential) {
A
Adam Lee 已提交
135
        result = d->init(url, region, file_size, chunk_size, &g_cred);
136
    } else {
A
Adam Lee 已提交
137
        result = d->init(url, "", file_size, chunk_size, NULL);
138 139 140 141 142 143 144 145 146 147 148 149 150 151
    }

    ASSERT_TRUE(result);

    while (1) {
        ASSERT_TRUE(d->get(buf, buf_len));
        if (buf_len == 0) {
            break;
        }
        m.Update(buf, buf_len);
        buf_len = buffer_size;
    }

    ASSERT_STREQ(md5_str, m.Get());
152 153

    d->destroy();
154 155 156 157
    delete d;
    free(buf);
}

158
#ifdef FAKE_TEST
159 160 161
void HTTPDownloaderTest(const char *url, uint64_t file_size,
                        const char *md5_str, uint8_t thread_num,
                        uint64_t chunk_size, uint64_t buffer_size) {
A
Adam Lee 已提交
162
    return DownloadTest(url, NULL, file_size, md5_str, thread_num, chunk_size,
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
                        buffer_size, false);
}

TEST(HTTPDownloader, divisible) {
    HTTPDownloaderTest("http://localhost/testdata/8M", 8388608,
                       "22129b81bf8f96a06a8b7f3d2a683588", 4, 4 * 1024,
                       16 * 1024);
}

TEST(HTTPDownloader, equal) {
    HTTPDownloaderTest("http://localhost/testdata/8M", 8388608,
                       "22129b81bf8f96a06a8b7f3d2a683588", 4, 16 * 1024,
                       16 * 1024);
}

TEST(HTTPDownloader, one_byte) {
    HTTPDownloaderTest("http://localhost/testdata/8K", 8192,
                       "f92ad4ae5f16a8d04f87e801af5115dc", 4, 4 * 1024, 1);
}

TEST(HTTPDownloader, over_flow) {
    HTTPDownloaderTest("http://localhost/testdata/8M", 8388608,
                       "22129b81bf8f96a06a8b7f3d2a683588", 4, 7 * 1024,
                       15 * 1024);
}

TEST(HTTPDownloader, multi_thread) {
    HTTPDownloaderTest("http://localhost/testdata/1M", 1048576,
                       "06427456b575a3880936b4ae43448082", 64, 4 * 1024,
                       511 * 1023);
}

TEST(HTTPDownloader, single_thread) {
    HTTPDownloaderTest("http://localhost/testdata/1M", 1048576,
                       "06427456b575a3880936b4ae43448082", 1, 4 * 1024,
                       511 * 1023);
}

TEST(HTTPDownloader, random_parameters_1M) {
    HTTPDownloaderTest("http://localhost/testdata/1M", 1048576,
                       "06427456b575a3880936b4ae43448082", 3, 3 * 29,
                       571 * 1023);
}

207
#ifdef BIG_FILE_TEST
208 209 210 211 212
TEST(HTTPDownloader, random_parameters_1G) {
    HTTPDownloaderTest("http://localhost/testdata/1G", 1073741824,
                       "a2cb2399eb8bc97084aed673e5d09f4d", 9, 42 * 1023,
                       513 * 1025 * 37);
}
213
#endif
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

TEST(HTTPDownloader, random_parameters_8M) {
    HTTPDownloaderTest("http://localhost/testdata/8M", 8388608,
                       "22129b81bf8f96a06a8b7f3d2a683588", 77, 7 * 10211,
                       777 * 1025);
}

TEST(HTTPDownloader, random_parameters_64M) {
    HTTPDownloaderTest("http://localhost/testdata/64M", 67108864,
                       "0871a7464a7ff85564f4f95b9ac77321", 51, 7 * 1053,
                       77 * 87);
}

TEST(HTTPDownloader, random_parameters_256M) {
    HTTPDownloaderTest("http://localhost/testdata/256M", 268435456,
                       "9cb7c287fdd5a44378798d3e75d2d2a6", 3, 1 * 10523,
                       77 * 879);
}
232
#endif  // FAKE_TEST
233

234
#ifdef AWS_TEST
A
Adam Lee 已提交
235 236 237
void S3DwonloadTest(const char *url, const char *region, uint64_t file_size,
                    const char *md5_str, uint8_t thread_num,
                    uint64_t chunk_size, uint64_t buffer_size) {
A
Adam Lee 已提交
238 239
    InitConfig("test/s3.conf", "default");

A
Adam Lee 已提交
240
    return DownloadTest(url, region, file_size, md5_str, thread_num, chunk_size,
A
Adam Lee 已提交
241 242 243
                        buffer_size, true);
}

244 245 246 247 248
TEST(S3Downloader, simple) {
    printf("Try downloading data0014\n");
    S3DwonloadTest(
        "http://s3-us-west-2.amazonaws.com/s3test.pivotal.io/dataset1/small17/"
        "data0014",
A
Adam Lee 已提交
249 250
        "us-west-2", 4420346, "68c4a63b721e7af0ae945ce109ca87ad", 4,
        1024 * 1024, 65536);
251 252 253 254 255

    printf("Try downloading data0016\n");
    S3DwonloadTest(
        "http://s3-us-west-2.amazonaws.com/s3test.pivotal.io/dataset1/small17/"
        "data0016",
A
Adam Lee 已提交
256 257
        "us-west-2", 2536018, "0fd502a303eb8f138f5916ec357721b1", 4,
        1024 * 1024, 65536);
258 259 260 261 262 263 264
}

TEST(S3Downloader, httpssimple) {
    printf("Try downloading data0014\n");
    S3DwonloadTest(
        "http://s3-us-west-2.amazonaws.com/s3test.pivotal.io/dataset1/small17/"
        "data0014",
A
Adam Lee 已提交
265 266
        "us-west-2", 4420346, "68c4a63b721e7af0ae945ce109ca87ad", 4,
        1024 * 1024, 65536);
267 268 269 270 271

    printf("Try downloading data0016\n");
    S3DwonloadTest(
        "http://s3-us-west-2.amazonaws.com/s3test.pivotal.io/dataset1/small17/"
        "data0016",
A
Adam Lee 已提交
272 273
        "us-west-2", 2536018, "0fd502a303eb8f138f5916ec357721b1", 4,
        1024 * 1024, 65536);
274 275
}

276
#ifdef BIG_FILE_TEST
277 278 279 280 281 282 283
TEST(S3Downloader, bigfile) {
    // InitLog();
    // s3ext_loglevel = EXT_DEBUG;
    // s3ext_logtype = STDERR_LOG;
    printf("Try downloading big file 1\n");
    S3DwonloadTest(
        "http://s3-us-west-2.amazonaws.com/s3test.pivotal.io/dataset2/hugefile/"
284 285 286
        "airlinedata1.csv",
        "us-west-2", 4664425994, "f5811ad92c994f1d6913d5338575fe38", 4,
        64 * 1024 * 1024, 65536);
287
}
288 289 290
#endif

#endif  // AWS_TEST