summaryrefslogtreecommitdiff
path: root/test/buildtool/graph_traverser/graph_traverser.test.hpp
blob: a983963850b0923f0a6960e6d3b9cf8972f5e16a (plain)
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
// 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.

#ifndef INCLUDED_SRC_TEST_BUILDTOOL_GRAPH_GRAVERSER_GRAPH_TRAVERSER_TEST_HPP
#define INCLUDED_SRC_TEST_BUILDTOOL_GRAPH_GRAVERSER_GRAPH_TRAVERSER_TEST_HPP

#include <chrono>
#include <cstdlib>
#include <filesystem>
#include <optional>
#include <sstream>
#include <string>
#include <thread>
#include <utility>
#include <vector>

#include "catch2/catch_test_macros.hpp"
#include "gsl/gsl"
#include "nlohmann/json.hpp"
#include "src/buildtool/auth/authentication.hpp"
#include "src/buildtool/common/protocol_traits.hpp"
#include "src/buildtool/common/remote/retry_config.hpp"
#include "src/buildtool/common/statistics.hpp"
#include "src/buildtool/execution_api/common/api_bundle.hpp"
#include "src/buildtool/execution_api/local/config.hpp"
#include "src/buildtool/execution_api/local/context.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/file_system/jsonfs.hpp"
#include "src/buildtool/graph_traverser/graph_traverser.hpp"
#include "src/buildtool/logging/log_level.hpp"
#include "src/buildtool/logging/logger.hpp"
#include "src/buildtool/progress_reporting/progress.hpp"
#include "src/buildtool/storage/config.hpp"
#include "src/buildtool/storage/storage.hpp"
#include "src/utils/cpp/json.hpp"
#include "test/utils/hermeticity/test_hash_function_type.hpp"

// NOLINTNEXTLINE(google-build-namespaces)
namespace {

class TestProject {
  public:
    struct CommandLineArguments {
        GraphTraverser::CommandLineArguments gtargs;
        nlohmann::json artifacts;
        std::filesystem::path graph_description;

        explicit CommandLineArguments(
            GraphTraverser::CommandLineArguments gtargs)
            : gtargs{std::move(std::move(gtargs))} {}
    };

    // NOLINTNEXTLINE(modernize-pass-by-value)
    explicit TestProject(std::string const& example_name)
        : example_name_{example_name},
          root_dir_{kWorkspacePrefix / example_name_} {
        SetupConfig();
    }

    explicit TestProject(std::string&& example_name)
        : example_name_{std::move(example_name)},
          root_dir_{kWorkspacePrefix / example_name_} {
        SetupConfig();
    }

    /// \brief Get command line arguments parsing entry points file in
    /// data/<example-name>/<entry_points_filename>, where
    /// <entry_points_filename> takes "_entry_points" as default value
    auto CmdLineArgs(std::string const& entry_points_filename =
                         kDefaultEntryPointsFileName) -> CommandLineArguments {
        auto const entry_points_file = root_dir_ / entry_points_filename;
        if (not FileSystemManager::IsFile(entry_points_file)) {
            Logger::Log(
                LogLevel::Error,
                "file with entry points for graph_traverser tests can not be "
                "found in path {}",
                entry_points_file.string());
            std::exit(EXIT_FAILURE);
        }
        auto const entry_points_json = Json::ReadFile(entry_points_file);
        if (not entry_points_json.has_value()) {
            Logger::Log(LogLevel::Error,
                        "can not read {} for graph_traverser tests",
                        entry_points_file.string());
            std::exit(EXIT_FAILURE);
        }
        return GenerateFromEntryPoints(*entry_points_json);
    }

    auto GetRepoConfig() -> RepositoryConfig* { return &repo_config_; }

  private:
    static inline std::filesystem::path const kOutputDirPrefix =
        FileSystemManager::GetCurrentDirectory() / "./tmp-";
    static inline std::filesystem::path const kWorkspacePrefix =
        FileSystemManager::GetCurrentDirectory() /
        "test/buildtool/graph_traverser/data/";
    static inline std::string const kDefaultEntryPointsFileName =
        "_entry_points";
    std::string example_name_;
    std::filesystem::path root_dir_;
    RepositoryConfig repo_config_{};

    void SetupConfig() {
        auto info = RepositoryConfig::RepositoryInfo{FileRoot{root_dir_}};
        repo_config_.SetInfo("", std::move(info));
    }

    auto GenerateFromEntryPoints(nlohmann::json const& entry_points)
        -> CommandLineArguments {
        static int id{};

        GraphTraverser::CommandLineArguments gtargs{0, {}, {}, {}};

        CommandLineArguments clargs{gtargs};
        clargs.artifacts = entry_points;
        auto const comp_graph = root_dir_ / "graph_description_compatible";
        if (not ProtocolTraits::IsNative(
                TestHashType::ReadFromEnvironment()) and
            FileSystemManager::Exists(comp_graph)) {
            clargs.graph_description = comp_graph;
        }
        else {
            clargs.graph_description = root_dir_ / "graph_description";
        }
        clargs.gtargs.jobs = std::max(1U, std::thread::hardware_concurrency());
        clargs.gtargs.stage = StageArguments{
            kOutputDirPrefix / (example_name_ + std::to_string(id++))};
        return clargs;
    }
};

[[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

[[maybe_unused]] static void TestHelloWorldCopyMessage(
    StorageConfig const& storage_config,
    Storage const& storage,
    gsl::not_null<Auth const*> const& auth,
    gsl::not_null<RemoteExecutionConfig const*> const& remote_config,
    bool is_hermetic = true) {
    TestProject p("hello_world_copy_message");

    auto const local_exec_config = CreateLocalExecConfig();
    auto const clargs = p.CmdLineArgs();

    Statistics stats{};
    Progress progress{};

    // pack the local context instances to be passed to ApiBundle
    LocalContext const local_context{.exec_config = &local_exec_config,
                                     .storage_config = &storage_config,
                                     .storage = &storage};

    RetryConfig retry_config{};  // default retry config

    // pack the remote context instances to be passed to ApiBundle
    RemoteContext const remote_context{.auth = auth,
                                       .retry_config = &retry_config,
                                       .exec_config = remote_config};

    auto const apis =
        ApiBundle::Create(&local_context, &remote_context, p.GetRepoConfig());

    ExecutionContext const exec_context{.repo_config = p.GetRepoConfig(),
                                        .apis = &apis,
                                        .remote_context = &remote_context,
                                        .statistics = &stats,
                                        .progress = &progress};

    GraphTraverser const gt{
        clargs.gtargs, &exec_context, [](auto done, auto cv) {}};
    auto const result =
        gt.BuildAndStage(clargs.graph_description, clargs.artifacts);

    REQUIRE(result);
    REQUIRE(result->output_paths.size() == 1);
    CHECK(FileSystemManager::IsFile(result->output_paths.at(0)));
    auto const contents =
        FileSystemManager::ReadFile(result->output_paths.at(0));
    CHECK(contents.has_value());
    CHECK(contents == "Hello, World!\n");

    if (is_hermetic) {
        CHECK(stats.ActionsQueuedCounter() == 2);
        CHECK(stats.ActionsCachedCounter() == 0);
    }

    SECTION("Executable is retrieved as executable") {
        auto const clargs_exec = p.CmdLineArgs("_entry_points_get_executable");
        GraphTraverser const gt_get_exec{
            clargs_exec.gtargs, &exec_context, [](auto done, auto cv) {}};
        auto const exec_result = gt_get_exec.BuildAndStage(
            clargs_exec.graph_description, clargs_exec.artifacts);

        REQUIRE(exec_result);
        REQUIRE(exec_result->output_paths.size() == 1);
        auto const exec_path = exec_result->output_paths.at(0);
        CHECK(FileSystemManager::IsFile(exec_path));
        CHECK(FileSystemManager::IsExecutable(exec_path));
        CHECK(FileSystemManager::Type(exec_path) == ObjectType::Executable);

        if (is_hermetic) {
            CHECK(stats.ActionsQueuedCounter() == 3);  // One more action queued
            CHECK(stats.ActionsCachedCounter() ==
                  1);  // But that action was cached
        }
    }
}

[[maybe_unused]] static void TestCopyLocalFile(
    StorageConfig const& storage_config,
    Storage const& storage,
    gsl::not_null<Auth const*> const& auth,
    gsl::not_null<RemoteExecutionConfig const*> const& remote_config,
    bool is_hermetic = true) {
    TestProject p("copy_local_file");

    auto const local_exec_config = CreateLocalExecConfig();
    auto const clargs = p.CmdLineArgs();

    Statistics stats{};
    Progress progress{};

    // pack the local context instances to be passed to ApiBundle
    LocalContext const local_context{.exec_config = &local_exec_config,
                                     .storage_config = &storage_config,
                                     .storage = &storage};

    RetryConfig retry_config{};  // default retry config

    // pack the remote context instances to be passed to ApiBundle
    RemoteContext const remote_context{.auth = auth,
                                       .retry_config = &retry_config,
                                       .exec_config = remote_config};

    auto const apis =
        ApiBundle::Create(&local_context, &remote_context, p.GetRepoConfig());

    ExecutionContext const exec_context{.repo_config = p.GetRepoConfig(),
                                        .apis = &apis,
                                        .remote_context = &remote_context,
                                        .statistics = &stats,
                                        .progress = &progress};

    GraphTraverser const gt{
        clargs.gtargs, &exec_context, [](auto done, auto cv) {}};
    auto const result =
        gt.BuildAndStage(clargs.graph_description, clargs.artifacts);

    REQUIRE(result);
    REQUIRE(result->output_paths.size() == 1);
    CHECK(FileSystemManager::IsFile(result->output_paths.at(0)));

    if (is_hermetic) {
        CHECK(stats.ActionsQueuedCounter() == 0);
        CHECK(stats.ActionsCachedCounter() == 0);
    }
}

[[maybe_unused]] static void TestSequencePrinterBuildLibraryOnly(
    StorageConfig const& storage_config,
    Storage const& storage,
    gsl::not_null<Auth const*> const& auth,
    gsl::not_null<RemoteExecutionConfig const*> const& remote_config,
    bool is_hermetic = true) {
    TestProject p("sequence_printer_build_library_only");

    auto const local_exec_config = CreateLocalExecConfig();
    auto const clargs = p.CmdLineArgs();

    Statistics stats{};
    Progress progress{};

    // pack the local context instances to be passed to ApiBundle
    LocalContext const local_context{.exec_config = &local_exec_config,
                                     .storage_config = &storage_config,
                                     .storage = &storage};

    RetryConfig retry_config{};  // default retry config

    // pack the remote context instances to be passed to ApiBundle
    RemoteContext const remote_context{.auth = auth,
                                       .retry_config = &retry_config,
                                       .exec_config = remote_config};

    auto const apis =
        ApiBundle::Create(&local_context, &remote_context, p.GetRepoConfig());

    ExecutionContext const exec_context{.repo_config = p.GetRepoConfig(),
                                        .apis = &apis,
                                        .remote_context = &remote_context,
                                        .statistics = &stats,
                                        .progress = &progress};

    GraphTraverser const gt{
        clargs.gtargs, &exec_context, [](auto done, auto cv) {}};
    auto const result =
        gt.BuildAndStage(clargs.graph_description, clargs.artifacts);

    REQUIRE(result);
    REQUIRE(result->output_paths.size() == 1);
    CHECK(FileSystemManager::IsFile(result->output_paths.at(0)));

    auto const clargs_full_build = p.CmdLineArgs("_entry_points_full_build");
    GraphTraverser const gt_full_build{
        clargs_full_build.gtargs, &exec_context, [](auto done, auto cv) {}};
    auto const full_build_result = gt_full_build.BuildAndStage(
        clargs_full_build.graph_description, clargs_full_build.artifacts);

    REQUIRE(full_build_result);
    REQUIRE(full_build_result->output_paths.size() == 1);
    CHECK(FileSystemManager::IsFile(full_build_result->output_paths.at(0)));

    if (is_hermetic) {
        CHECK(stats.ActionsQueuedCounter() == 8);
        CHECK(stats.ActionsCachedCounter() == 3);
    }
    else {
        CHECK(stats.ActionsCachedCounter() > 0);
    }
}

[[maybe_unused]] static void TestHelloWorldWithKnownSource(
    StorageConfig const& storage_config,
    Storage const& storage,
    gsl::not_null<Auth const*> const& auth,
    gsl::not_null<RemoteExecutionConfig const*> const& remote_config,
    bool is_hermetic = true) {
    TestProject full_hello_world("hello_world_copy_message");

    auto const local_exec_config = CreateLocalExecConfig();
    auto const clargs_update_cpp =
        full_hello_world.CmdLineArgs("_entry_points_upload_source");

    Statistics stats{};
    Progress progress{};

    // pack the local context instances to be passed to ApiBundle
    LocalContext const local_context{.exec_config = &local_exec_config,
                                     .storage_config = &storage_config,
                                     .storage = &storage};

    RetryConfig retry_config{};  // default retry config

    // pack the remote context instances to be passed to ApiBundle
    RemoteContext const remote_context{.auth = auth,
                                       .retry_config = &retry_config,
                                       .exec_config = remote_config};

    auto const full_apis = ApiBundle::Create(
        &local_context, &remote_context, full_hello_world.GetRepoConfig());

    ExecutionContext const full_context{
        .repo_config = full_hello_world.GetRepoConfig(),
        .apis = &full_apis,
        .remote_context = &remote_context,
        .statistics = &stats,
        .progress = &progress};

    GraphTraverser const gt_upload{
        clargs_update_cpp.gtargs, &full_context, [](auto done, auto cv) {}};
    auto const cpp_result = gt_upload.BuildAndStage(
        clargs_update_cpp.graph_description, clargs_update_cpp.artifacts);

    REQUIRE(cpp_result);
    REQUIRE(cpp_result->output_paths.size() == 1);

    CHECK(FileSystemManager::IsFile(cpp_result->output_paths.at(0)));

    if (is_hermetic) {
        CHECK(stats.ActionsQueuedCounter() == 0);
        CHECK(stats.ActionsCachedCounter() == 0);
    }
    TestProject hello_world_known_cpp("hello_world_known_source");

    auto const clargs = hello_world_known_cpp.CmdLineArgs();

    auto const apis_known = ApiBundle::Create(
        &local_context, &remote_context, hello_world_known_cpp.GetRepoConfig());

    ExecutionContext const context_known{
        .repo_config = hello_world_known_cpp.GetRepoConfig(),
        .apis = &apis_known,
        .remote_context = &remote_context,
        .statistics = &stats,
        .progress = &progress};
    GraphTraverser const gt{
        clargs.gtargs, &context_known, [](auto done, auto cv) {}};
    auto const result =
        gt.BuildAndStage(clargs.graph_description, clargs.artifacts);

    REQUIRE(result);
    REQUIRE(result->output_paths.size() == 1);
    CHECK(FileSystemManager::IsFile(result->output_paths.at(0)));

    if (is_hermetic) {
        CHECK(stats.ActionsQueuedCounter() == 2);
        CHECK(stats.ActionsCachedCounter() == 0);
    }
    else {
        CHECK(stats.ActionsQueuedCounter() >= 2);
    }
}

static void TestBlobsUploadedAndUsed(
    StorageConfig const& storage_config,
    Storage const& storage,
    gsl::not_null<Auth const*> const& auth,
    gsl::not_null<RemoteExecutionConfig const*> const& remote_config,
    bool is_hermetic = true) {
    TestProject p("use_uploaded_blobs");
    auto const clargs = p.CmdLineArgs();

    Statistics stats{};
    Progress progress{};

    auto const local_exec_config = CreateLocalExecConfig();

    // pack the local context instances to be passed to ApiBundle
    LocalContext const local_context{.exec_config = &local_exec_config,
                                     .storage_config = &storage_config,
                                     .storage = &storage};

    RetryConfig retry_config{};  // default retry config

    // pack the remote context instances to be passed to ApiBundle
    RemoteContext const remote_context{.auth = auth,
                                       .retry_config = &retry_config,
                                       .exec_config = remote_config};

    auto const apis =
        ApiBundle::Create(&local_context, &remote_context, p.GetRepoConfig());

    ExecutionContext const exec_context{.repo_config = p.GetRepoConfig(),
                                        .apis = &apis,
                                        .remote_context = &remote_context,
                                        .statistics = &stats,
                                        .progress = &progress};

    GraphTraverser gt{clargs.gtargs, &exec_context, [](auto done, auto cv) {}};
    auto const result =
        gt.BuildAndStage(clargs.graph_description, clargs.artifacts);

    REQUIRE(result);
    REQUIRE(result->output_paths.size() == 1);
    CHECK(FileSystemManager::IsFile(result->output_paths.at(0)));

    auto const contents =
        FileSystemManager::ReadFile(result->output_paths.at(0));
    CHECK(contents.has_value());
    CHECK(contents == "this is a test to check if blobs are uploaded");

    if (is_hermetic) {
        CHECK(stats.ActionsQueuedCounter() == 1);
        CHECK(stats.ActionsCachedCounter() == 0);
    }
    else {
        CHECK(stats.ActionsQueuedCounter() >= 1);
    }
}

static void TestEnvironmentVariablesSetAndUsed(
    StorageConfig const& storage_config,
    Storage const& storage,
    gsl::not_null<Auth const*> const& auth,
    gsl::not_null<RemoteExecutionConfig const*> const& remote_config,
    bool is_hermetic = true) {
    TestProject p("use_env_variables");
    auto const clargs = p.CmdLineArgs();

    Statistics stats{};
    Progress progress{};

    auto const local_exec_config = CreateLocalExecConfig();

    // pack the local context instances to be passed to ApiBundle
    LocalContext const local_context{.exec_config = &local_exec_config,
                                     .storage_config = &storage_config,
                                     .storage = &storage};

    RetryConfig retry_config{};  // default retry config

    // pack the remote context instances to be passed to ApiBundle
    RemoteContext const remote_context{.auth = auth,
                                       .retry_config = &retry_config,
                                       .exec_config = remote_config};

    auto const apis =
        ApiBundle::Create(&local_context, &remote_context, p.GetRepoConfig());

    ExecutionContext const exec_context{.repo_config = p.GetRepoConfig(),
                                        .apis = &apis,
                                        .remote_context = &remote_context,
                                        .statistics = &stats,
                                        .progress = &progress};

    GraphTraverser gt{clargs.gtargs, &exec_context, [](auto done, auto cv) {}};
    auto const result =
        gt.BuildAndStage(clargs.graph_description, clargs.artifacts);

    REQUIRE(result);
    REQUIRE(result->output_paths.size() == 1);
    CHECK(FileSystemManager::IsFile(result->output_paths.at(0)));

    auto const contents =
        FileSystemManager::ReadFile(result->output_paths.at(0));
    CHECK(contents.has_value());
    CHECK(contents == "content from environment variable");

    if (is_hermetic) {
        CHECK(stats.ActionsQueuedCounter() == 1);
        CHECK(stats.ActionsCachedCounter() == 0);
    }
    else {
        CHECK(stats.ActionsQueuedCounter() >= 1);
    }
}

static void TestTreesUsed(
    StorageConfig const& storage_config,
    Storage const& storage,
    gsl::not_null<Auth const*> const& auth,
    gsl::not_null<RemoteExecutionConfig const*> const& remote_config,
    bool is_hermetic = true) {
    TestProject p("use_trees");
    auto const clargs = p.CmdLineArgs();

    Statistics stats{};
    Progress progress{};

    auto const local_exec_config = CreateLocalExecConfig();

    // pack the local context instances to be passed to ApiBundle
    LocalContext const local_context{.exec_config = &local_exec_config,
                                     .storage_config = &storage_config,
                                     .storage = &storage};

    RetryConfig retry_config{};  // default retry config

    // pack the remote context instances to be passed to ApiBundle
    RemoteContext const remote_context{.auth = auth,
                                       .retry_config = &retry_config,
                                       .exec_config = remote_config};

    auto const apis =
        ApiBundle::Create(&local_context, &remote_context, p.GetRepoConfig());

    ExecutionContext const exec_context{.repo_config = p.GetRepoConfig(),
                                        .apis = &apis,
                                        .remote_context = &remote_context,
                                        .statistics = &stats,
                                        .progress = &progress};

    GraphTraverser gt{clargs.gtargs, &exec_context, [](auto done, auto cv) {}};
    auto const result =
        gt.BuildAndStage(clargs.graph_description, clargs.artifacts);

    REQUIRE(result);
    REQUIRE(result->output_paths.size() == 1);
    CHECK(FileSystemManager::IsFile(result->output_paths.at(0)));

    auto const contents =
        FileSystemManager::ReadFile(result->output_paths.at(0));
    CHECK(contents.has_value());
    CHECK(contents == "this is a test to check if blobs are uploaded");

    if (is_hermetic) {
        CHECK(stats.ActionsQueuedCounter() == 2);
        CHECK(stats.ActionsCachedCounter() == 0);
    }
    else {
        CHECK(stats.ActionsQueuedCounter() >= 2);
    }
}

static void TestNestedTreesUsed(
    StorageConfig const& storage_config,
    Storage const& storage,
    gsl::not_null<Auth const*> const& auth,
    gsl::not_null<RemoteExecutionConfig const*> const& remote_config,
    bool is_hermetic = true) {
    TestProject p("use_nested_trees");
    auto const clargs = p.CmdLineArgs();

    Statistics stats{};
    Progress progress{};

    auto const local_exec_config = CreateLocalExecConfig();

    // pack the local context instances to be passed to ApiBundle
    LocalContext const local_context{.exec_config = &local_exec_config,
                                     .storage_config = &storage_config,
                                     .storage = &storage};

    RetryConfig retry_config{};  // default retry config

    // pack the remote context instances to be passed to ApiBundle
    RemoteContext const remote_context{.auth = auth,
                                       .retry_config = &retry_config,
                                       .exec_config = remote_config};

    auto const apis =
        ApiBundle::Create(&local_context, &remote_context, p.GetRepoConfig());

    ExecutionContext const exec_context{.repo_config = p.GetRepoConfig(),
                                        .apis = &apis,
                                        .remote_context = &remote_context,
                                        .statistics = &stats,
                                        .progress = &progress};

    GraphTraverser gt{clargs.gtargs, &exec_context, [](auto done, auto cv) {}};
    auto const result =
        gt.BuildAndStage(clargs.graph_description, clargs.artifacts);

    REQUIRE(result);
    REQUIRE(result->output_paths.size() == 1);
    CHECK(FileSystemManager::IsFile(result->output_paths.at(0)));

    auto const contents =
        FileSystemManager::ReadFile(result->output_paths.at(0));
    CHECK(contents.has_value());
    CHECK(contents == "this is a test to check if blobs are uploaded");

    if (is_hermetic) {
        CHECK(stats.ActionsQueuedCounter() == 1);
        CHECK(stats.ActionsCachedCounter() == 0);
    }
    else {
        CHECK(stats.ActionsQueuedCounter() >= 1);
    }
}

static void TestFlakyHelloWorldDetected(
    StorageConfig const& storage_config,
    Storage const& storage,
    gsl::not_null<Auth const*> const& auth,
    gsl::not_null<RemoteExecutionConfig const*> const& remote_config,
    bool /*is_hermetic*/ = true) {
    TestProject p("flaky_hello_world");

    Statistics stats{};
    Progress progress{};

    auto const local_exec_config = CreateLocalExecConfig();

    // pack the local context instances to be passed to ApiBundle
    LocalContext const local_context{.exec_config = &local_exec_config,
                                     .storage_config = &storage_config,
                                     .storage = &storage};

    RetryConfig retry_config{};  // default retry config

    // pack the remote context instances to be passed to ApiBundle
    RemoteContext const remote_context{.auth = auth,
                                       .retry_config = &retry_config,
                                       .exec_config = remote_config};

    auto const apis =
        ApiBundle::Create(&local_context, &remote_context, p.GetRepoConfig());

    ExecutionContext const exec_context{.repo_config = p.GetRepoConfig(),
                                        .apis = &apis,
                                        .remote_context = &remote_context,
                                        .statistics = &stats,
                                        .progress = &progress};

    {
        auto clargs = p.CmdLineArgs("_entry_points_ctimes");
        GraphTraverser const gt{
            clargs.gtargs, &exec_context, [](auto done, auto cv) {}};
        auto const result =
            gt.BuildAndStage(clargs.graph_description, clargs.artifacts);

        REQUIRE(result);
        REQUIRE(result->output_paths.size() == 1);
    }

    using namespace std::chrono_literals;
    std::this_thread::sleep_for(1s);

    // make_exe[flaky]->make_output[miss]
    auto clargs_output = p.CmdLineArgs();
    clargs_output.gtargs.rebuild = RebuildArguments{};
    GraphTraverser const gt_output{
        clargs_output.gtargs, &exec_context, [](auto done, auto cv) {}};
    REQUIRE(gt_output.BuildAndStage(clargs_output.graph_description,
                                    clargs_output.artifacts));
    CHECK(stats.ActionsFlakyCounter() == 1);
    CHECK(stats.RebuiltActionComparedCounter() == 1);
    CHECK(stats.RebuiltActionMissingCounter() == 1);
    stats.Reset();

    // make_exe[flaky]->make_output[miss]->strip_time [miss]
    auto clargs_stripped = p.CmdLineArgs("_entry_points_stripped");
    clargs_stripped.gtargs.rebuild = RebuildArguments{};
    GraphTraverser const gt_stripped{
        clargs_stripped.gtargs, &exec_context, [](auto done, auto cv) {}};
    REQUIRE(gt_stripped.BuildAndStage(clargs_stripped.graph_description,
                                      clargs_stripped.artifacts));
    CHECK(stats.ActionsFlakyCounter() == 1);
    CHECK(stats.RebuiltActionComparedCounter() == 1);
    CHECK(stats.RebuiltActionMissingCounter() == 2);
    stats.Reset();

    // make_exe[flaky]->make_output[miss]->strip_time[miss]->list_ctimes [flaky]
    auto clargs_ctimes = p.CmdLineArgs("_entry_points_ctimes");
    clargs_ctimes.gtargs.rebuild = RebuildArguments{};
    GraphTraverser const gt_ctimes{
        clargs_ctimes.gtargs, &exec_context, [](auto done, auto cv) {}};
    REQUIRE(gt_ctimes.BuildAndStage(clargs_ctimes.graph_description,
                                    clargs_ctimes.artifacts));
    CHECK(stats.ActionsFlakyCounter() == 2);
    CHECK(stats.RebuiltActionComparedCounter() == 2);
    CHECK(stats.RebuiltActionMissingCounter() == 2);
}

#endif  // INCLUDED_SRC_TEST_BUILDTOOL_GRAPH_GRAVERSER_GRAPH_TRAVERSER_TEST_HPP