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
|
// 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 <cstdlib>
#include <filesystem>
#include <functional>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include "catch2/catch_test_macros.hpp"
#include "fmt/core.h"
#include "gsl/gsl"
#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/crypto/hash_function.hpp"
#include "src/buildtool/execution_api/common/artifact_blob.hpp"
#include "src/buildtool/execution_api/common/execution_action.hpp"
#include "src/buildtool/execution_api/common/execution_response.hpp"
#include "src/buildtool/execution_api/local/config.hpp"
#include "src/buildtool/execution_api/local/context.hpp"
#include "src/buildtool/execution_api/local/local_api.hpp"
#include "src/buildtool/execution_engine/dag/dag.hpp"
#include "src/buildtool/file_system/file_system_manager.hpp"
#include "src/buildtool/file_system/object_type.hpp"
#include "src/buildtool/logging/log_level.hpp"
#include "src/buildtool/logging/logger.hpp"
#include "src/buildtool/storage/config.hpp"
#include "src/buildtool/storage/storage.hpp"
#include "src/utils/cpp/expected.hpp"
#include "test/utils/hermeticity/test_storage_config.hpp"
namespace {
[[nodiscard]] auto GetTestDir() -> std::filesystem::path {
auto* tmp_dir = std::getenv("TEST_TMPDIR");
if (tmp_dir != nullptr) {
return tmp_dir;
}
return FileSystemManager::GetCurrentDirectory() /
"test/buildtool/execution_api/local";
}
[[nodiscard]] inline auto CreateLocalExecConfig() noexcept
-> LocalExecutionConfig {
std::vector<std::string> launcher{"env"};
auto* env_path = std::getenv("PATH");
if (env_path != nullptr) {
launcher.emplace_back(std::string{"PATH="} + std::string{env_path});
}
else {
launcher.emplace_back("PATH=/bin:/usr/bin");
}
LocalExecutionConfig::Builder builder;
if (auto config = builder.SetLauncher(std::move(launcher)).Build()) {
return *std::move(config);
}
Logger::Log(LogLevel::Error, "Failure setting the local launcher.");
std::exit(EXIT_FAILURE);
}
} // namespace
TEST_CASE("LocalExecution: No input, no output", "[execution_api]") {
auto const storage_config = TestStorageConfig::Create();
auto const storage = Storage::Create(&storage_config.Get());
auto const local_exec_config = CreateLocalExecConfig();
// pack the local context instances to be passed to LocalApi
LocalContext const local_context{.exec_config = &local_exec_config,
.storage_config = &storage_config.Get(),
.storage = &storage};
RepositoryConfig repo_config{};
auto api = LocalApi(&local_context, &repo_config);
std::string test_content("test");
std::vector<std::string> const cmdline = {"echo", "-n", test_content};
auto action =
api.CreateAction(*api.UploadTree({}), cmdline, "", {}, {}, {}, {});
REQUIRE(action);
SECTION("Cache execution result in action cache") {
// run execution
action->SetCacheFlag(IExecutionAction::CacheFlag::CacheOutput);
auto output = action->Execute(nullptr);
REQUIRE(output);
// verify result
CHECK_FALSE(output->IsCached());
CHECK(output->StdOut() == test_content);
output = action->Execute(nullptr);
REQUIRE(output);
CHECK(output->IsCached());
}
SECTION("Do not cache execution result in action cache") {
// run execution
action->SetCacheFlag(IExecutionAction::CacheFlag::DoNotCacheOutput);
auto output = action->Execute(nullptr);
REQUIRE(output);
// verify result
CHECK_FALSE(output->IsCached());
CHECK(output->StdOut() == test_content);
// ensure result IS STILL NOT in cache
output = action->Execute(nullptr);
REQUIRE(output);
CHECK_FALSE(output->IsCached());
}
}
TEST_CASE("LocalExecution: No input, no output, env variables used",
"[execution_api]") {
auto const storage_config = TestStorageConfig::Create();
auto const storage = Storage::Create(&storage_config.Get());
auto const local_exec_config = CreateLocalExecConfig();
// pack the local context instances to be passed to LocalApi
LocalContext const local_context{.exec_config = &local_exec_config,
.storage_config = &storage_config.Get(),
.storage = &storage};
RepositoryConfig repo_config{};
auto api = LocalApi(&local_context, &repo_config);
std::string test_content("test from env var");
std::vector<std::string> const cmdline = {
"/bin/sh", "-c", "set -e\necho -n ${MYCONTENT}"};
auto action = api.CreateAction(*api.UploadTree({}),
cmdline,
"",
{},
{},
{{"MYCONTENT", test_content}},
{});
REQUIRE(action);
SECTION("Cache execution result in action cache") {
// run execution
action->SetCacheFlag(IExecutionAction::CacheFlag::CacheOutput);
auto output = action->Execute(nullptr);
REQUIRE(output);
// verify result
CHECK_FALSE(output->IsCached());
CHECK(output->StdOut() == test_content);
// ensure result IS in cache
output = action->Execute(nullptr);
REQUIRE(output);
CHECK(output->IsCached());
}
SECTION("Do not cache execution result in action cache") {
// run execution
action->SetCacheFlag(IExecutionAction::CacheFlag::DoNotCacheOutput);
auto output = action->Execute(nullptr);
REQUIRE(output);
// verify result
CHECK_FALSE(output->IsCached());
CHECK(output->StdOut() == test_content);
// ensure result IS STILL NOT in cache
output = action->Execute(nullptr);
REQUIRE(output);
CHECK_FALSE(output->IsCached());
}
}
TEST_CASE("LocalExecution: No input, create output", "[execution_api]") {
auto const storage_config = TestStorageConfig::Create();
auto const storage = Storage::Create(&storage_config.Get());
auto const local_exec_config = CreateLocalExecConfig();
// pack the local context instances to be passed to LocalApi
LocalContext const local_context{.exec_config = &local_exec_config,
.storage_config = &storage_config.Get(),
.storage = &storage};
RepositoryConfig repo_config{};
auto api = LocalApi(&local_context, &repo_config);
std::string test_content("test");
auto test_digest = ArtifactDigestFactory::HashDataAs<ObjectType::File>(
storage_config.Get().hash_function, test_content);
std::string output_path{"output_file"};
std::vector<std::string> const cmdline = {
"/bin/sh",
"-c",
"set -e\necho -n " + test_content + " > " + output_path};
auto action = api.CreateAction(
*api.UploadTree({}), cmdline, "", {output_path}, {}, {}, {});
REQUIRE(action);
SECTION("Cache execution result in action cache") {
// run execution
action->SetCacheFlag(IExecutionAction::CacheFlag::CacheOutput);
auto output = action->Execute(nullptr);
REQUIRE(output);
// verify result
CHECK_FALSE(output->IsCached());
auto const artifacts = output->Artifacts();
REQUIRE(artifacts.has_value());
REQUIRE(artifacts.value()->contains(output_path));
CHECK(artifacts.value()->at(output_path).digest == test_digest);
// ensure result IS in cache
output = action->Execute(nullptr);
REQUIRE(output);
CHECK(output->IsCached());
}
SECTION("Do not cache execution result in action cache") {
// run execution
action->SetCacheFlag(IExecutionAction::CacheFlag::DoNotCacheOutput);
auto output = action->Execute(nullptr);
REQUIRE(output);
// verify result
CHECK_FALSE(output->IsCached());
auto const artifacts = output->Artifacts();
REQUIRE(artifacts.has_value());
REQUIRE(artifacts.value()->contains(output_path));
CHECK(artifacts.value()->at(output_path).digest == test_digest);
// ensure result IS STILL NOT in cache
output = action->Execute(nullptr);
REQUIRE(output);
CHECK_FALSE(output->IsCached());
}
}
TEST_CASE("LocalExecution: One input copied to output", "[execution_api]") {
auto const storage_config = TestStorageConfig::Create();
auto const storage = Storage::Create(&storage_config.Get());
auto const local_exec_config = CreateLocalExecConfig();
// pack the local context instances to be passed to LocalApi
LocalContext const local_context{.exec_config = &local_exec_config,
.storage_config = &storage_config.Get(),
.storage = &storage};
RepositoryConfig repo_config{};
auto api = LocalApi(&local_context, &repo_config);
std::string test_content("test");
auto test_digest = ArtifactDigestFactory::HashDataAs<ObjectType::File>(
storage_config.Get().hash_function, test_content);
REQUIRE(api.Upload(
{ArtifactBlob{test_digest, test_content, /*is_exec=*/false}}, false));
std::string input_path{"dir/subdir/input"};
std::string output_path{"output_file"};
std::vector<std::string> const cmdline = {"cp", input_path, output_path};
auto local_artifact_opt =
ArtifactDescription::CreateKnown(test_digest, ObjectType::File)
.ToArtifact();
auto local_artifact =
DependencyGraph::ArtifactNode{std::move(local_artifact_opt)};
auto action =
api.CreateAction(*api.UploadTree({{input_path, &local_artifact}}),
cmdline,
"",
{output_path},
{},
{},
{});
REQUIRE(action);
SECTION("Cache execution result in action cache") {
// run execution
action->SetCacheFlag(IExecutionAction::CacheFlag::CacheOutput);
auto output = action->Execute(nullptr);
REQUIRE(output);
// verify result
CHECK_FALSE(output->IsCached());
auto const artifacts = output->Artifacts();
REQUIRE(artifacts.has_value());
REQUIRE(artifacts.value()->contains(output_path));
CHECK(artifacts.value()->at(output_path).digest == test_digest);
// ensure result IS in cache
output = action->Execute(nullptr);
REQUIRE(output);
CHECK(output->IsCached());
}
SECTION("Do not cache execution result in action cache") {
// run execution
action->SetCacheFlag(IExecutionAction::CacheFlag::DoNotCacheOutput);
auto output = action->Execute(nullptr);
REQUIRE(output);
// verify result
CHECK_FALSE(output->IsCached());
auto const artifacts = output->Artifacts();
REQUIRE(artifacts.has_value());
REQUIRE(artifacts.value()->contains(output_path));
CHECK(artifacts.value()->at(output_path).digest == test_digest);
// ensure result IS STILL NOT in cache
output = action->Execute(nullptr);
REQUIRE(output);
CHECK_FALSE(output->IsCached());
}
}
TEST_CASE("LocalExecution: Cache failed action's result", "[execution_api]") {
auto const storage_config = TestStorageConfig::Create();
auto const storage = Storage::Create(&storage_config.Get());
auto const local_exec_config = CreateLocalExecConfig();
// pack the local context instances to be passed to LocalApi
LocalContext const local_context{.exec_config = &local_exec_config,
.storage_config = &storage_config.Get(),
.storage = &storage};
RepositoryConfig repo_config{};
auto api = LocalApi(&local_context, &repo_config);
auto flag = GetTestDir() / "flag";
std::vector<std::string> const cmdline = {
"sh", "-c", fmt::format("[ -f '{}' ]", flag.string())};
auto action =
api.CreateAction(*api.UploadTree({}), cmdline, "", {}, {}, {}, {});
REQUIRE(action);
action->SetCacheFlag(IExecutionAction::CacheFlag::CacheOutput);
// run failed action
auto failed = action->Execute(nullptr);
REQUIRE(failed);
CHECK_FALSE(failed->IsCached());
CHECK(failed->ExitCode() != 0);
REQUIRE(FileSystemManager::CreateFile(flag));
// run success action (should rerun and overwrite)
auto success = action->Execute(nullptr);
REQUIRE(success);
CHECK_FALSE(success->IsCached());
CHECK(success->ExitCode() == 0);
// rerun success action (should be served from cache)
auto cached = action->Execute(nullptr);
REQUIRE(cached);
CHECK(cached->IsCached());
CHECK(cached->ExitCode() == 0);
CHECK(FileSystemManager::RemoveFile(flag));
}
|