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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <algorithm>
#include <filesystem>
#include <map>
#include <optional>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "catch2/catch_test_macros.hpp"
#include "gsl/gsl"
#include "src/buildtool/common/artifact_factory.hpp"
#include "src/buildtool/common/repository_config.hpp"
#include "src/buildtool/common/statistics.hpp"
#include "src/buildtool/execution_api/common/execution_api.hpp"
#include "src/buildtool/execution_engine/executor/executor.hpp"
#include "src/buildtool/file_system/file_system_manager.hpp"
#include "src/buildtool/progress_reporting/progress.hpp"
/// \brief Mockup API test config.
struct TestApiConfig {
struct TestArtifactConfig {
bool uploads{};
bool available{};
};
struct TestExecutionConfig {
bool failed{};
std::vector<std::string> outputs{};
};
struct TestResponseConfig {
bool cached{};
int exit_code{};
};
std::unordered_map<std::string, TestArtifactConfig> artifacts{};
TestExecutionConfig execution;
TestResponseConfig response;
};
// forward declarations
class TestApi;
class TestAction;
class TestResponse;
/// \brief Mockup Response, stores only config and action result
class TestResponse : public IExecutionResponse {
friend class TestAction;
public:
[[nodiscard]] auto Status() const noexcept -> StatusCode final {
return StatusCode::Success;
}
[[nodiscard]] auto ExitCode() const noexcept -> int final {
return config_.response.exit_code;
}
[[nodiscard]] auto IsCached() const noexcept -> bool final {
return config_.response.cached;
}
[[nodiscard]] auto HasStdErr() const noexcept -> bool final { return true; }
[[nodiscard]] auto HasStdOut() const noexcept -> bool final { return true; }
[[nodiscard]] auto StdErr() noexcept -> std::string final { return {}; }
[[nodiscard]] auto StdOut() noexcept -> std::string final { return {}; }
[[nodiscard]] auto ActionDigest() const noexcept -> std::string final {
return {};
}
[[nodiscard]] auto Artifacts() noexcept -> ArtifactInfos final {
ArtifactInfos artifacts{};
artifacts.reserve(config_.execution.outputs.size());
// collect files and store them
for (auto const& path : config_.execution.outputs) {
try {
artifacts.emplace(
path,
Artifact::ObjectInfo{
.digest = ArtifactDigest{path, 0, /*is_tree=*/false},
.type = ObjectType::File});
} catch (...) {
return {};
}
}
return artifacts;
}
[[nodiscard]] auto ArtifactsWithDirSymlinks() noexcept
-> std::pair<ArtifactInfos, DirSymlinks> final {
return {};
}
private:
TestApiConfig config_{};
explicit TestResponse(TestApiConfig config) noexcept
: config_{std::move(config)} {}
};
/// \brief Mockup Action, stores only config
class TestAction : public IExecutionAction {
friend class TestApi;
public:
auto Execute(Logger const* /*unused*/) noexcept
-> IExecutionResponse::Ptr final {
if (config_.execution.failed) {
return nullptr;
}
return IExecutionResponse::Ptr{new TestResponse{config_}};
}
void SetCacheFlag(CacheFlag /*unused*/) noexcept final {}
void SetTimeout(std::chrono::milliseconds /*unused*/) noexcept final {}
private:
TestApiConfig config_{};
explicit TestAction(TestApiConfig config) noexcept
: config_{std::move(config)} {}
};
/// \brief Mockup Api, use config to create action and handle artifact upload
class TestApi : public IExecutionApi {
public:
explicit TestApi(TestApiConfig config) noexcept
: config_{std::move(config)} {}
[[nodiscard]] auto CreateAction(
ArtifactDigest const& /*unused*/,
std::vector<std::string> const& /*unused*/,
std::vector<std::string> const& /*unused*/,
std::vector<std::string> const& /*unused*/,
std::map<std::string, std::string> const& /*unused*/,
std::map<std::string, std::string> const& /*unused*/) const noexcept
-> IExecutionAction::Ptr final {
return IExecutionAction::Ptr{new TestAction(config_)};
}
[[nodiscard]] auto RetrieveToPaths(
std::vector<Artifact::ObjectInfo> const& /*unused*/,
std::vector<std::filesystem::path> const& /*unused*/,
IExecutionApi::OptionalPtr const& /* unused */) const noexcept
-> bool final {
return false; // not needed by Executor
}
[[nodiscard]] auto RetrieveToFds(
std::vector<Artifact::ObjectInfo> const& /*unused*/,
std::vector<int> const& /*unused*/,
bool /*unused*/) const noexcept -> bool final {
return false; // not needed by Executor
}
[[nodiscard]] auto RetrieveToCas(
std::vector<Artifact::ObjectInfo> const& unused,
IExecutionApi const& /*unused*/) const noexcept -> bool final {
// Note that a false-positive "free-nonheap-object" warning is thrown by
// gcc 12.2 with GNU libstdc++, if the caller passes a temporary vector
// that is not used by this function. Therefore, we explicitly use this
// vector here to suppress this warning. The actual value returned is
// irrelevant for testing though.
return unused.empty(); // not needed by Executor
}
[[nodiscard]] auto RetrieveToMemory(
Artifact::ObjectInfo const& /*artifact_info*/) const noexcept
-> std::optional<std::string> override {
return std::nullopt; // not needed by Executor
}
[[nodiscard]] auto Upload(ArtifactBlobContainer&& blobs,
bool /*unused*/) const noexcept -> bool final {
auto blob_range = blobs.Blobs();
return std::all_of(
blob_range.begin(), blob_range.end(), [this](auto const& blob) {
// for local artifacts
auto it1 = config_.artifacts.find(*blob.data);
if (it1 != config_.artifacts.end() and it1->second.uploads) {
return true;
}
// for known and action artifacts
auto it2 = config_.artifacts.find(blob.digest.hash());
return it2 != config_.artifacts.end() and it2->second.uploads;
});
}
[[nodiscard]] auto UploadTree(
std::vector<DependencyGraph::NamedArtifactNodePtr> const& /*unused*/)
const noexcept -> std::optional<ArtifactDigest> final {
return ArtifactDigest{}; // not needed by Executor
}
[[nodiscard]] auto IsAvailable(ArtifactDigest const& digest) const noexcept
-> bool final {
try {
return config_.artifacts.at(digest.hash()).available;
} catch (std::exception const& /* unused */) {
return false;
}
}
[[nodiscard]] auto IsAvailable(std::vector<ArtifactDigest> const& digests)
const noexcept -> std::vector<ArtifactDigest> final {
std::vector<ArtifactDigest> result;
try {
for (auto const& digest : digests) {
if (not config_.artifacts.at(digest.hash()).available) {
result.push_back(digest);
}
}
} catch (std::exception const& /* unused */) {
return result;
}
return result;
}
private:
TestApiConfig config_{};
};
[[nodiscard]] auto SetupConfig(std::filesystem::path const& ws)
-> RepositoryConfig {
auto info = RepositoryConfig::RepositoryInfo{FileRoot{ws}};
RepositoryConfig repo_config{};
repo_config.SetInfo("", std::move(info));
return repo_config;
}
[[nodiscard]] static auto CreateTest(gsl::not_null<DependencyGraph*> const& g,
std::filesystem::path const& ws)
-> std::pair<TestApiConfig, RepositoryConfig> {
using path = std::filesystem::path;
auto const local_cpp_desc = ArtifactDescription{path{"local.cpp"}, ""};
auto const known_cpp_desc = ArtifactDescription{
ArtifactDigest{"known.cpp", 0, /*is_tree=*/false}, ObjectType::File};
auto const test_action_desc = ActionDescription{
{"output1.exe", "output2.exe"},
{},
Action{"test_action", {"cmd", "line"}, {}},
{{"local.cpp", local_cpp_desc}, {"known.cpp", known_cpp_desc}}};
CHECK(g->AddAction(test_action_desc));
CHECK(FileSystemManager::WriteFile("local.cpp", ws / "local.cpp"));
TestApiConfig config{};
config.artifacts["local.cpp"].uploads = true;
config.artifacts["known.cpp"].available = true;
config.artifacts["output1.exe"].available = true;
config.artifacts["output2.exe"].available = true;
config.execution.failed = false;
config.execution.outputs = {"output1.exe", "output2.exe"};
config.response.cached = true;
config.response.exit_code = 0;
return std::make_pair(config, SetupConfig(ws));
}
TEST_CASE("Executor: Process artifact", "[executor]") {
std::filesystem::path workspace_path{
"test/buildtool/execution_engine/executor"};
DependencyGraph g;
auto [config, repo_config] = CreateTest(&g, workspace_path);
auto const local_cpp_desc =
ArtifactFactory::DescribeLocalArtifact("local.cpp", "");
auto const local_cpp_id = ArtifactFactory::Identifier(local_cpp_desc);
auto const known_cpp_desc = ArtifactFactory::DescribeKnownArtifact(
"known.cpp", 0, ObjectType::File);
auto const known_cpp_id = ArtifactFactory::Identifier(known_cpp_desc);
SECTION("Processing succeeds for valid config") {
auto api = TestApi::Ptr{new TestApi{config}};
Statistics stats{};
Progress progress{};
Executor runner{
&repo_config, api.get(), api.get(), {}, {}, &stats, &progress};
CHECK(runner.Process(g.ArtifactNodeWithId(local_cpp_id)));
CHECK(runner.Process(g.ArtifactNodeWithId(known_cpp_id)));
}
SECTION("Processing fails if uploading local artifact failed") {
config.artifacts["local.cpp"].uploads = false;
auto api = TestApi::Ptr{new TestApi{config}};
Statistics stats{};
Progress progress{};
Executor runner{
&repo_config, api.get(), api.get(), {}, {}, &stats, &progress};
CHECK(not runner.Process(g.ArtifactNodeWithId(local_cpp_id)));
CHECK(runner.Process(g.ArtifactNodeWithId(known_cpp_id)));
}
SECTION("Processing fails if known artifact is not available") {
config.artifacts["known.cpp"].available = false;
auto api = TestApi::Ptr{new TestApi{config}};
Statistics stats{};
Progress progress{};
Executor runner{
&repo_config, api.get(), api.get(), {}, {}, &stats, &progress};
CHECK(runner.Process(g.ArtifactNodeWithId(local_cpp_id)));
CHECK(not runner.Process(g.ArtifactNodeWithId(known_cpp_id)));
}
}
TEST_CASE("Executor: Process action", "[executor]") {
std::filesystem::path workspace_path{
"test/buildtool/execution_engine/executor"};
DependencyGraph g;
auto [config, repo_config] = CreateTest(&g, workspace_path);
auto const local_cpp_desc =
ArtifactFactory::DescribeLocalArtifact("local.cpp", "");
auto const local_cpp_id = ArtifactFactory::Identifier(local_cpp_desc);
auto const known_cpp_desc = ArtifactFactory::DescribeKnownArtifact(
"known.cpp", 0, ObjectType::File);
auto const known_cpp_id = ArtifactFactory::Identifier(known_cpp_desc);
ActionIdentifier action_id{"test_action"};
auto const output1_desc =
ArtifactFactory::DescribeActionArtifact(action_id, "output1.exe");
auto const output1_id = ArtifactFactory::Identifier(output1_desc);
auto const output2_desc =
ArtifactFactory::DescribeActionArtifact(action_id, "output2.exe");
auto const output2_id = ArtifactFactory::Identifier(output2_desc);
SECTION("Processing succeeds for valid config") {
auto api = TestApi::Ptr{new TestApi{config}};
Statistics stats{};
Progress progress{};
Executor runner{
&repo_config, api.get(), api.get(), {}, {}, &stats, &progress};
CHECK(runner.Process(g.ArtifactNodeWithId(local_cpp_id)));
CHECK(runner.Process(g.ArtifactNodeWithId(known_cpp_id)));
CHECK(runner.Process(g.ActionNodeWithId(action_id)));
CHECK(runner.Process(g.ArtifactNodeWithId(output1_id)));
CHECK(runner.Process(g.ArtifactNodeWithId(output2_id)));
}
SECTION("Processing succeeds even if result was is not cached") {
config.response.cached = false;
auto api = TestApi::Ptr{new TestApi{config}};
Statistics stats{};
Progress progress{};
Executor runner{
&repo_config, api.get(), api.get(), {}, {}, &stats, &progress};
CHECK(runner.Process(g.ArtifactNodeWithId(local_cpp_id)));
CHECK(runner.Process(g.ArtifactNodeWithId(known_cpp_id)));
CHECK(runner.Process(g.ActionNodeWithId(action_id)));
CHECK(runner.Process(g.ArtifactNodeWithId(output1_id)));
CHECK(runner.Process(g.ArtifactNodeWithId(output2_id)));
}
SECTION("Processing succeeds even if output is not available in CAS") {
config.artifacts["output2.exe"].available = false;
auto api = TestApi::Ptr{new TestApi{config}};
Statistics stats{};
Progress progress{};
Executor runner{
&repo_config, api.get(), api.get(), {}, {}, &stats, &progress};
CHECK(runner.Process(g.ArtifactNodeWithId(local_cpp_id)));
CHECK(runner.Process(g.ArtifactNodeWithId(known_cpp_id)));
CHECK(runner.Process(g.ActionNodeWithId(action_id)));
// Note: Both output digests should be created via SaveDigests(),
// but processing output2.exe fails as it is not available in CAS.
CHECK(runner.Process(g.ArtifactNodeWithId(output1_id)));
CHECK(not runner.Process(g.ArtifactNodeWithId(output2_id)));
}
SECTION("Processing fails if execution failed") {
config.execution.failed = true;
auto api = TestApi::Ptr{new TestApi{config}};
Statistics stats{};
Progress progress{};
Executor runner{
&repo_config, api.get(), api.get(), {}, {}, &stats, &progress};
CHECK(runner.Process(g.ArtifactNodeWithId(local_cpp_id)));
CHECK(runner.Process(g.ArtifactNodeWithId(known_cpp_id)));
CHECK(not runner.Process(g.ActionNodeWithId(action_id)));
CHECK(not runner.Process(g.ArtifactNodeWithId(output1_id)));
CHECK(not runner.Process(g.ArtifactNodeWithId(output2_id)));
}
SECTION("Processing fails if exit code is non-zero") {
config.response.exit_code = 1;
auto api = TestApi::Ptr{new TestApi{config}};
Statistics stats{};
Progress progress{};
Executor runner{
&repo_config, api.get(), api.get(), {}, {}, &stats, &progress};
CHECK(runner.Process(g.ArtifactNodeWithId(local_cpp_id)));
CHECK(runner.Process(g.ArtifactNodeWithId(known_cpp_id)));
CHECK(not runner.Process(g.ActionNodeWithId(action_id)));
// Note: Both output digests should be missing as SaveDigests() for
// both is only called if processing action succeeds.
CHECK(not runner.Process(g.ArtifactNodeWithId(output1_id)));
CHECK(not runner.Process(g.ArtifactNodeWithId(output2_id)));
}
SECTION("Processing fails if any output is missing") {
config.execution.outputs = {"output1.exe" /*, "output2.exe"*/};
auto api = TestApi::Ptr{new TestApi{config}};
Statistics stats{};
Progress progress{};
Executor runner{
&repo_config, api.get(), api.get(), {}, {}, &stats, &progress};
CHECK(runner.Process(g.ArtifactNodeWithId(local_cpp_id)));
CHECK(runner.Process(g.ArtifactNodeWithId(known_cpp_id)));
CHECK(not runner.Process(g.ActionNodeWithId(action_id)));
// Note: Both output digests should be missing as SaveDigests() for
// both is only called if processing action succeeds.
CHECK(not runner.Process(g.ArtifactNodeWithId(output1_id)));
CHECK(not runner.Process(g.ArtifactNodeWithId(output2_id)));
}
}
|