summaryrefslogtreecommitdiff
path: root/test/buildtool/execution_api/bazel/bazel_network.test.cpp
blob: 9125fbc4af9bdcd22cac76fac5bbd2d56b06446d (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
// 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 <cstddef>
#include <string>
#include <vector>

#include "catch2/catch_test_macros.hpp"
#include "src/buildtool/common/artifact_digest.hpp"
#include "src/buildtool/compatibility/compatibility.hpp"
#include "src/buildtool/execution_api/bazel_msg/bazel_blob_container.hpp"
#include "src/buildtool/execution_api/remote/bazel/bazel_execution_client.hpp"
#include "src/buildtool/execution_api/remote/bazel/bazel_network.hpp"
#include "src/buildtool/execution_api/remote/config.hpp"
#include "src/buildtool/file_system/object_type.hpp"

constexpr std::size_t kLargeSize = GRPC_DEFAULT_MAX_RECV_MESSAGE_LENGTH + 1;

TEST_CASE("Bazel network: write/read blobs", "[execution_api]") {
    auto const& info = RemoteExecutionConfig::RemoteAddress();
    std::string instance_name{"remote-execution"};
    auto network = BazelNetwork{instance_name, info->host, info->port, {}};

    std::string content_foo("foo");
    std::string content_bar("bar");
    std::string content_baz(kLargeSize, 'x');  // single larger blob

    BazelBlob foo{ArtifactDigest::Create<ObjectType::File>(content_foo),
                  content_foo,
                  /*is_exec=*/false};
    BazelBlob bar{ArtifactDigest::Create<ObjectType::File>(content_bar),
                  content_bar,
                  /*is_exec=*/false};
    BazelBlob baz{ArtifactDigest::Create<ObjectType::File>(content_baz),
                  content_baz,
                  /*is_exec=*/false};

    // Search blobs via digest
    REQUIRE(network.UploadBlobs(BazelBlobContainer{{foo, bar, baz}}));

    // Read blobs in order
    auto reader = network.ReadBlobs(
        {foo.digest, bar.digest, baz.digest, bar.digest, foo.digest});
    std::vector<BazelBlob> blobs{};
    while (true) {
        auto next = reader.Next();
        if (next.empty()) {
            break;
        }
        blobs.insert(blobs.end(), next.begin(), next.end());
    }

    // Check order maintained
    REQUIRE(blobs.size() == 5);
    CHECK(blobs[0].data == content_foo);
    CHECK(blobs[1].data == content_bar);
    CHECK(blobs[2].data == content_baz);
    CHECK(blobs[3].data == content_bar);
    CHECK(blobs[4].data == content_foo);
}

TEST_CASE("Bazel network: read blobs with unknown size", "[execution_api]") {
    if (Compatibility::IsCompatible()) {
        // only supported in native mode
        return;
    }

    auto const& info = RemoteExecutionConfig::RemoteAddress();
    std::string instance_name{"remote-execution"};
    auto network = BazelNetwork{instance_name, info->host, info->port, {}};

    std::string content_foo("foo");
    std::string content_bar(kLargeSize, 'x');  // single larger blob

    BazelBlob foo{ArtifactDigest::Create<ObjectType::File>(content_foo),
                  content_foo,
                  /*is_exec=*/false};
    BazelBlob bar{ArtifactDigest::Create<ObjectType::File>(content_bar),
                  content_bar,
                  /*is_exec=*/false};

    // Upload blobs
    REQUIRE(network.UploadBlobs(BazelBlobContainer{{foo, bar}}));

    // Set size to unknown
    foo.digest.set_size_bytes(0);
    bar.digest.set_size_bytes(0);

    // Read blobs
    auto reader = network.ReadBlobs({foo.digest, bar.digest});
    std::vector<BazelBlob> blobs{};
    while (true) {
        auto next = reader.Next();
        if (next.empty()) {
            break;
        }
        blobs.insert(blobs.end(), next.begin(), next.end());
    }

    // Check order maintained
    REQUIRE(blobs.size() == 2);
    CHECK(blobs[0].data == content_foo);
    CHECK(blobs[1].data == content_bar);
}