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
|
// Copyright Epic Games, Inc. All Rights Reserved.
#if ZEN_WITH_TESTS
# include "zenserver-test.h"
# include <zencore/memoryview.h>
# include <zencore/testing.h>
# include <zencore/testutils.h>
# include <zenhttp/httpclient.h>
# include <zenutil/cloud/s3client.h>
# include <zenutil/zenserverprocess.h>
ZEN_THIRD_PARTY_INCLUDES_START
# include <tsl/robin_set.h>
ZEN_THIRD_PARTY_INCLUDES_END
namespace zen::tests {
using namespace std::literals;
TEST_SUITE_BEGIN("server.objectstore");
TEST_CASE("objectstore.blobs")
{
std::string_view Bucket = "bkt"sv;
std::vector<IoHash> CompressedBlobsHashes;
std::vector<uint64_t> BlobsSizes;
std::vector<uint64_t> CompressedBlobsSizes;
{
ZenServerInstance Instance(TestEnv);
const uint16_t PortNumber = Instance.SpawnServerAndWaitUntilReady(fmt::format("--objectstore-enabled"));
CHECK(PortNumber != 0);
HttpClient Client(Instance.GetBaseUri() + "/obj/");
for (size_t I = 0; I < 5; I++)
{
IoBuffer Blob = CreateSemiRandomBlob(4711 + I * 7);
BlobsSizes.push_back(Blob.GetSize());
CompressedBuffer CompressedBlob = CompressedBuffer::Compress(SharedBuffer(std::move(Blob)));
CompressedBlobsHashes.push_back(CompressedBlob.DecodeRawHash());
CompressedBlobsSizes.push_back(CompressedBlob.GetCompressedSize());
IoBuffer Payload = std::move(CompressedBlob).GetCompressed().Flatten().AsIoBuffer();
Payload.SetContentType(ZenContentType::kCompressedBinary);
std::string ObjectPath = fmt::format("{}/{}.utoc",
CompressedBlobsHashes.back().ToHexString().substr(0, 2),
CompressedBlobsHashes.back().ToHexString());
HttpClient::Response Result = Client.Put(fmt::format("bucket/{}/{}.utoc", Bucket, ObjectPath), Payload);
CHECK(Result);
}
for (size_t I = 0; I < 5; I++)
{
std::string ObjectPath =
fmt::format("{}/{}.utoc", CompressedBlobsHashes[I].ToHexString().substr(0, 2), CompressedBlobsHashes[I].ToHexString());
HttpClient::Response Result = Client.Get(fmt::format("bucket/{}/{}.utoc", Bucket, ObjectPath));
CHECK(Result);
CHECK_EQ(Result.ResponsePayload.GetSize(), CompressedBlobsSizes[I]);
IoHash RawHash;
uint64_t RawSize;
CompressedBuffer Compressed =
CompressedBuffer::FromCompressed(SharedBuffer(std::move(Result.ResponsePayload)), RawHash, RawSize);
CHECK(Compressed);
CHECK_EQ(RawHash, CompressedBlobsHashes[I]);
CHECK_EQ(RawSize, BlobsSizes[I]);
}
}
}
TEST_CASE("objectstore.s3client")
{
ZenServerInstance Instance(TestEnv);
const uint16_t Port = Instance.SpawnServerAndWaitUntilReady("--objectstore-enabled");
CHECK_MESSAGE(Port != 0, Instance.GetLogOutput());
// S3Client in path-style builds paths as /{bucket}/{key}.
// The objectstore routes objects at bucket/{bucket}/{key} relative to its base.
// Point the S3Client endpoint at {server}/obj/bucket so the paths line up.
S3ClientOptions Opts;
Opts.BucketName = "s3test";
Opts.Region = "us-east-1";
Opts.Endpoint = fmt::format("http://localhost:{}/obj/bucket", Port);
Opts.PathStyle = true;
Opts.Credentials.AccessKeyId = "testkey";
Opts.Credentials.SecretAccessKey = "testsecret";
S3Client Client(Opts);
// -- PUT + GET roundtrip --
std::string_view TestData = "hello from s3client via objectstore"sv;
IoBuffer Content = IoBufferBuilder::MakeFromMemory(MakeMemoryView(TestData));
S3Result PutRes = Client.PutObject("test/hello.txt", std::move(Content));
REQUIRE_MESSAGE(PutRes.IsSuccess(), PutRes.Error);
S3GetObjectResult GetRes = Client.GetObject("test/hello.txt");
REQUIRE_MESSAGE(GetRes.IsSuccess(), GetRes.Error);
CHECK(GetRes.AsText() == TestData);
// -- PUT overwrites --
IoBuffer Original = IoBufferBuilder::MakeFromMemory(MakeMemoryView("original"sv));
IoBuffer Overwrite = IoBufferBuilder::MakeFromMemory(MakeMemoryView("overwritten"sv));
REQUIRE(Client.PutObject("overwrite/file.txt", std::move(Original)).IsSuccess());
REQUIRE(Client.PutObject("overwrite/file.txt", std::move(Overwrite)).IsSuccess());
S3GetObjectResult OverwriteGet = Client.GetObject("overwrite/file.txt");
REQUIRE(OverwriteGet.IsSuccess());
CHECK(OverwriteGet.AsText() == "overwritten"sv);
// -- GET not found --
S3GetObjectResult NotFoundGet = Client.GetObject("nonexistent/file.dat");
CHECK_FALSE(NotFoundGet.IsSuccess());
// -- HEAD found --
std::string_view HeadData = "head test data"sv;
IoBuffer HeadContent = IoBufferBuilder::MakeFromMemory(MakeMemoryView(HeadData));
REQUIRE(Client.PutObject("head/meta.txt", std::move(HeadContent)).IsSuccess());
S3HeadObjectResult HeadRes = Client.HeadObject("head/meta.txt");
REQUIRE_MESSAGE(HeadRes.IsSuccess(), HeadRes.Error);
CHECK(HeadRes.Status == HeadObjectResult::Found);
CHECK(HeadRes.Info.Size == HeadData.size());
// -- HEAD not found --
S3HeadObjectResult HeadNotFound = Client.HeadObject("nonexistent/file.dat");
CHECK(HeadNotFound.IsSuccess());
CHECK(HeadNotFound.Status == HeadObjectResult::NotFound);
// -- LIST objects --
for (int i = 0; i < 3; ++i)
{
std::string Key = fmt::format("listing/item-{}.txt", i);
std::string Payload = fmt::format("content-{}", i);
IoBuffer Buf = IoBufferBuilder::MakeFromMemory(MakeMemoryView(Payload));
REQUIRE(Client.PutObject(Key, std::move(Buf)).IsSuccess());
}
S3ListObjectsResult ListRes = Client.ListObjects("listing/");
REQUIRE_MESSAGE(ListRes.IsSuccess(), ListRes.Error);
REQUIRE(ListRes.Objects.size() == 3);
std::vector<std::string> Keys;
for (const S3ObjectInfo& Obj : ListRes.Objects)
{
Keys.push_back(Obj.Key);
CHECK(Obj.Size > 0);
}
std::sort(Keys.begin(), Keys.end());
CHECK(Keys[0] == "listing/item-0.txt");
CHECK(Keys[1] == "listing/item-1.txt");
CHECK(Keys[2] == "listing/item-2.txt");
// -- LIST empty prefix --
S3ListObjectsResult EmptyList = Client.ListObjects("no-such-prefix/");
REQUIRE(EmptyList.IsSuccess());
CHECK(EmptyList.Objects.empty());
}
TEST_CASE("objectstore.range-requests")
{
ZenServerInstance Instance(TestEnv);
const uint16_t Port = Instance.SpawnServerAndWaitUntilReady("--objectstore-enabled");
REQUIRE(Port != 0);
HttpClient Client(Instance.GetBaseUri() + "/obj/");
IoBuffer Blob = CreateRandomBlob(1024);
MemoryView BlobView = Blob.GetView();
std::string ObjectPath = "bucket/bkt/range-test/data.bin";
HttpClient::Response PutResult = Client.Put(ObjectPath, IoBuffer(Blob));
REQUIRE(PutResult);
// Full GET without Range header
{
HttpClient::Response Result = Client.Get(ObjectPath);
CHECK(Result.StatusCode == HttpResponseCode::OK);
CHECK_EQ(Result.ResponsePayload.GetSize(), 1024u);
CHECK(Result.ResponsePayload.GetView().EqualBytes(BlobView));
}
// Single range: bytes 100-199
{
HttpClient::Response Result = Client.Get(ObjectPath, {{"Range", "bytes=100-199"}});
CHECK(Result.StatusCode == HttpResponseCode::PartialContent);
CHECK_EQ(Result.ResponsePayload.GetSize(), 100u);
CHECK(Result.ResponsePayload.GetView().EqualBytes(BlobView.Mid(100, 100)));
}
// Range starting at zero: bytes 0-49
{
HttpClient::Response Result = Client.Get(ObjectPath, {{"Range", "bytes=0-49"}});
CHECK(Result.StatusCode == HttpResponseCode::PartialContent);
CHECK_EQ(Result.ResponsePayload.GetSize(), 50u);
CHECK(Result.ResponsePayload.GetView().EqualBytes(BlobView.Mid(0, 50)));
}
// Range at end of file: bytes 1000-1023
{
HttpClient::Response Result = Client.Get(ObjectPath, {{"Range", "bytes=1000-1023"}});
CHECK(Result.StatusCode == HttpResponseCode::PartialContent);
CHECK_EQ(Result.ResponsePayload.GetSize(), 24u);
CHECK(Result.ResponsePayload.GetView().EqualBytes(BlobView.Mid(1000, 24)));
}
// Multiple ranges: not supported, falls back to 200 with full body per RFC 7233
{
HttpClient::Response Result = Client.Get(ObjectPath, {{"Range", "bytes=0-49,100-149"}});
CHECK(Result.StatusCode == HttpResponseCode::OK);
CHECK_EQ(Result.ResponsePayload.GetSize(), 1024u);
CHECK(Result.ResponsePayload.GetView().EqualBytes(BlobView));
}
// Out-of-bounds range: should return 400
{
HttpClient::Response Result = Client.Get(ObjectPath, {{"Range", "bytes=2000-2099"}});
CHECK(Result.StatusCode == HttpResponseCode::BadRequest);
}
}
TEST_SUITE_END();
} // namespace zen::tests
#endif
|