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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
|
// 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 "src/buildtool/execution_engine/executor/executor.hpp"
#include <algorithm>
#include <filesystem>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "catch2/catch_test_macros.hpp"
#include "gsl/gsl"
#include "src/buildtool/auth/authentication.hpp"
#include "src/buildtool/common/artifact_description.hpp"
#include "src/buildtool/common/artifact_digest.hpp"
#include "src/buildtool/common/artifact_digest_factory.hpp"
#include "src/buildtool/common/repository_config.hpp"
#include "src/buildtool/common/statistics.hpp"
#include "src/buildtool/crypto/hash_function.hpp"
#include "src/buildtool/execution_api/common/execution_api.hpp"
#include "src/buildtool/execution_api/remote/config.hpp"
#include "src/buildtool/execution_api/remote/context.hpp"
#include "src/buildtool/execution_engine/executor/context.hpp"
#include "src/buildtool/file_system/file_system_manager.hpp"
#include "src/buildtool/progress_reporting/progress.hpp"
#include "src/utils/cpp/expected.hpp"
#include "test/utils/executor/test_api_bundle.hpp"
#include "test/utils/hermeticity/test_hash_function_type.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;
};
static auto NamedDigest(std::string const& str) -> ArtifactDigest {
HashFunction const hash_function{TestHashType::ReadFromEnvironment()};
return ArtifactDigestFactory::HashDataAs<ObjectType::File>(hash_function,
str);
}
// forward declarations
class TestApi;
class TestAction;
class TestResponse;
/// \brief Mockup Response, stores only config and action result
class TestResponse : public IExecutionResponse {
public:
explicit TestResponse(TestApiConfig config) noexcept
: config_{std::move(config)} {}
[[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 const& final {
static const std::string kEmptyHash;
return kEmptyHash;
}
[[nodiscard]] auto Artifacts() noexcept
-> expected<gsl::not_null<ArtifactInfos const*>, std::string> final {
if (not populated_) {
Populate();
}
return gsl::not_null<ArtifactInfos const*>(&artifacts_);
}
[[nodiscard]] auto DirectorySymlinks() noexcept
-> expected<gsl::not_null<DirSymlinks const*>, std::string> final {
static const DirSymlinks kEmptySymlinks{};
return gsl::not_null<DirSymlinks const*>(&kEmptySymlinks);
}
private:
TestApiConfig config_{};
ArtifactInfos artifacts_;
bool populated_ = false;
void Populate() noexcept {
if (populated_) {
return;
}
populated_ = true;
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 = NamedDigest(path),
.type = ObjectType::File});
} catch (...) {
return;
}
}
artifacts_ = std::move(artifacts);
}
};
/// \brief Mockup Action, stores only config
class TestAction : public IExecutionAction {
public:
explicit TestAction(TestApiConfig config) noexcept
: config_{std::move(config)} {}
auto Execute(Logger const* /*unused*/) noexcept
-> IExecutionResponse::Ptr final {
if (config_.execution.failed) {
return nullptr;
}
return std::make_unique<TestResponse>(config_);
}
void SetCacheFlag(CacheFlag /*unused*/) noexcept final {}
void SetTimeout(std::chrono::milliseconds /*unused*/) noexcept final {}
private:
TestApiConfig 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::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 std::make_unique<TestAction>(config_);
}
[[nodiscard]] auto RetrieveToPaths(
std::vector<Artifact::ObjectInfo> const& /*unused*/,
std::vector<std::filesystem::path> const& /*unused*/,
IExecutionApi 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::CreateLocal(path{"local.cpp"}, "");
auto const known_digest = NamedDigest("known.cpp");
auto const known_cpp_desc =
ArtifactDescription::CreateKnown(known_digest, ObjectType::File);
auto const test_action_desc = ActionDescription{
{"output1.exe", "output2.exe"},
{},
Action{"test_action", {"cmd", "line"}, {}},
{{"local.cpp", local_cpp_desc}, {known_digest.hash(), known_cpp_desc}}};
CHECK(g->AddAction(test_action_desc));
CHECK(FileSystemManager::WriteFile("local.cpp", ws / "local.cpp"));
TestApiConfig config{};
config.artifacts[NamedDigest("local.cpp").hash()].uploads = true;
config.artifacts[NamedDigest("known.cpp").hash()].available = true;
config.artifacts[NamedDigest("output1.exe").hash()].available = true;
config.artifacts[NamedDigest("output2.exe").hash()].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);
HashFunction const hash_function{TestHashType::ReadFromEnvironment()};
auto const local_cpp_id =
ArtifactDescription::CreateLocal("local.cpp", "").Id();
auto const known_cpp_id = ArtifactDescription::CreateKnown(
NamedDigest("known.cpp"), ObjectType::File)
.Id();
Auth auth{};
RetryConfig retry_config{}; // default retry config
RemoteExecutionConfig remote_config{}; // default remote config
RemoteContext const remote_context{.auth = &auth,
.retry_config = &retry_config,
.exec_config = &remote_config};
SECTION("Processing succeeds for valid config") {
auto api = std::make_shared<TestApi>(config);
Statistics stats{};
Progress progress{};
auto const apis = CreateTestApiBundle(&hash_function, api);
ExecutionContext const exec_context{.repo_config = &repo_config,
.apis = &apis,
.remote_context = &remote_context,
.statistics = &stats,
.progress = &progress};
Executor runner{&exec_context};
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[NamedDigest("local.cpp").hash()].uploads = false;
auto api = std::make_shared<TestApi>(config);
Statistics stats{};
Progress progress{};
auto const apis = CreateTestApiBundle(&hash_function, api);
ExecutionContext const exec_context{.repo_config = &repo_config,
.apis = &apis,
.remote_context = &remote_context,
.statistics = &stats,
.progress = &progress};
Executor runner{&exec_context};
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[NamedDigest("known.cpp").hash()].available = false;
auto api = std::make_shared<TestApi>(config);
Statistics stats{};
Progress progress{};
auto const apis = CreateTestApiBundle(&hash_function, api);
ExecutionContext const exec_context{.repo_config = &repo_config,
.apis = &apis,
.remote_context = &remote_context,
.statistics = &stats,
.progress = &progress};
Executor runner{&exec_context};
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);
HashFunction const hash_function{TestHashType::ReadFromEnvironment()};
auto const local_cpp_id =
ArtifactDescription::CreateLocal("local.cpp", "").Id();
auto const known_cpp_id = ArtifactDescription::CreateKnown(
NamedDigest("known.cpp"), ObjectType::File)
.Id();
ActionIdentifier const action_id{"test_action"};
auto const output1_id =
ArtifactDescription::CreateAction(action_id, "output1.exe").Id();
auto const output2_id =
ArtifactDescription::CreateAction(action_id, "output2.exe").Id();
Auth auth{};
RetryConfig retry_config{}; // default retry config
RemoteExecutionConfig remote_config{}; // default remote config
RemoteContext const remote_context{.auth = &auth,
.retry_config = &retry_config,
.exec_config = &remote_config};
SECTION("Processing succeeds for valid config") {
auto api = std::make_shared<TestApi>(config);
Statistics stats{};
Progress progress{};
auto const apis = CreateTestApiBundle(&hash_function, api);
ExecutionContext const exec_context{.repo_config = &repo_config,
.apis = &apis,
.remote_context = &remote_context,
.statistics = &stats,
.progress = &progress};
Executor runner{&exec_context};
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 = std::make_shared<TestApi>(config);
Statistics stats{};
Progress progress{};
auto const apis = CreateTestApiBundle(&hash_function, api);
ExecutionContext const exec_context{.repo_config = &repo_config,
.apis = &apis,
.remote_context = &remote_context,
.statistics = &stats,
.progress = &progress};
Executor runner{&exec_context};
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[NamedDigest("output2.exe").hash()].available = false;
auto api = std::make_shared<TestApi>(config);
Statistics stats{};
Progress progress{};
auto const apis = CreateTestApiBundle(&hash_function, api);
ExecutionContext const exec_context{.repo_config = &repo_config,
.apis = &apis,
.remote_context = &remote_context,
.statistics = &stats,
.progress = &progress};
Executor runner{&exec_context};
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 = std::make_shared<TestApi>(config);
Statistics stats{};
Progress progress{};
auto const apis = CreateTestApiBundle(&hash_function, api);
ExecutionContext const exec_context{.repo_config = &repo_config,
.apis = &apis,
.remote_context = &remote_context,
.statistics = &stats,
.progress = &progress};
Executor runner{&exec_context};
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 = std::make_shared<TestApi>(config);
Statistics stats{};
Progress progress{};
auto const apis = CreateTestApiBundle(&hash_function, api);
ExecutionContext const exec_context{.repo_config = &repo_config,
.apis = &apis,
.remote_context = &remote_context,
.statistics = &stats,
.progress = &progress};
Executor runner{&exec_context};
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 = std::make_shared<TestApi>(config);
Statistics stats{};
Progress progress{};
auto const apis = CreateTestApiBundle(&hash_function, api);
ExecutionContext const exec_context{.repo_config = &repo_config,
.apis = &apis,
.remote_context = &remote_context,
.statistics = &stats,
.progress = &progress};
Executor runner{&exec_context};
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)));
}
}
|