diff options
author | Maksim Denisov <denisov.maksim@huawei.com> | 2024-12-11 15:27:12 +0100 |
---|---|---|
committer | Maksim Denisov <denisov.maksim@huawei.com> | 2024-12-19 16:37:59 +0100 |
commit | 6a1a70e6882e9ef19843cf7171fc74eee763da44 (patch) | |
tree | caa2d78c3c651f6cfde9dc17caa8473d0a58106a | |
parent | 969abef7af52a43da3040223a6825ebd4dd485a1 (diff) | |
download | justbuild-6a1a70e6882e9ef19843cf7171fc74eee763da44.tar.gz |
Test TreeStructureCache
-rw-r--r-- | test/buildtool/TARGETS | 1 | ||||
-rw-r--r-- | test/buildtool/tree_structure/TARGETS | 32 | ||||
-rw-r--r-- | test/buildtool/tree_structure/tree_structure.test.cpp | 119 |
3 files changed, 152 insertions, 0 deletions
diff --git a/test/buildtool/TARGETS b/test/buildtool/TARGETS index 84673a9d..28351129 100644 --- a/test/buildtool/TARGETS +++ b/test/buildtool/TARGETS @@ -15,6 +15,7 @@ , ["./", "serve_api", "TESTS"] , ["./", "storage", "TESTS"] , ["./", "system", "TESTS"] + , ["./", "tree_structure", "TESTS"] ] } } diff --git a/test/buildtool/tree_structure/TARGETS b/test/buildtool/tree_structure/TARGETS new file mode 100644 index 00000000..c927492f --- /dev/null +++ b/test/buildtool/tree_structure/TARGETS @@ -0,0 +1,32 @@ +{ "tree_structure": + { "type": ["@", "rules", "CC/test", "test"] + , "name": ["tree_structure"] + , "srcs": ["tree_structure.test.cpp"] + , "private-deps": + [ ["@", "catch2", "", "catch2"] + , ["@", "gsl", "", "gsl"] + , ["@", "src", "src/buildtool/common", "common"] + , ["@", "src", "src/buildtool/common", "protocol_traits"] + , ["@", "src", "src/buildtool/crypto", "hash_function"] + , [ "@" + , "src" + , "src/buildtool/execution_api/bazel_msg" + , "bazel_msg_factory" + ] + , ["@", "src", "src/buildtool/storage", "config"] + , ["@", "src", "src/buildtool/storage", "garbage_collector"] + , ["@", "src", "src/buildtool/storage", "storage"] + , ["@", "src", "src/buildtool/tree_structure", "tree_structure_cache"] + , ["@", "src", "src/utils/cpp", "tmp_dir"] + , ["", "catch-main"] + , ["utils", "large_object_utils"] + , ["utils", "test_storage_config"] + ] + , "stage": ["test", "buildtool", "tree_structure"] + } +, "TESTS": + { "type": ["@", "rules", "test", "suite"] + , "stage": ["tree_structure"] + , "deps": ["tree_structure"] + } +} diff --git a/test/buildtool/tree_structure/tree_structure.test.cpp b/test/buildtool/tree_structure/tree_structure.test.cpp new file mode 100644 index 00000000..e0656c69 --- /dev/null +++ b/test/buildtool/tree_structure/tree_structure.test.cpp @@ -0,0 +1,119 @@ +// Copyright 2024 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 <cstdint> +#include <filesystem> +#include <memory> +#include <optional> +#include <string> + +#include "catch2/catch_test_macros.hpp" +#include "gsl/gsl" +#include "src/buildtool/common/artifact_digest.hpp" +#include "src/buildtool/common/protocol_traits.hpp" +#include "src/buildtool/crypto/hash_function.hpp" +#include "src/buildtool/execution_api/bazel_msg/bazel_msg_factory.hpp" +#include "src/buildtool/storage/config.hpp" +#include "src/buildtool/storage/garbage_collector.hpp" +#include "src/buildtool/storage/storage.hpp" +#include "src/buildtool/tree_structure/tree_structure_cache.hpp" +#include "src/utils/cpp/tmp_dir.hpp" +#include "test/utils/hermeticity/test_storage_config.hpp" +#include "test/utils/large_objects/large_object_utils.hpp" + +namespace { +[[nodiscard]] auto CreateFlatTestDirectory(StorageConfig const& storage_config, + Storage const& storage, + std::uintmax_t entries) + -> std::optional<ArtifactDigest>; +} + +TEST_CASE("cache", "[tree_structure]") { + auto const storage_config = TestStorageConfig::Create(); + if (not ProtocolTraits::IsNative( + storage_config.Get().hash_function.GetType())) { + return; + } + + auto const storage = Storage::Create(&storage_config.Get()); + TreeStructureCache const ts_cache(&storage_config.Get()); + + auto const from_dir = + CreateFlatTestDirectory(storage_config.Get(), storage, 128); + REQUIRE(from_dir); + auto const to_dir = + CreateFlatTestDirectory(storage_config.Get(), storage, 128); + REQUIRE(to_dir); + + // Set dependency + REQUIRE(ts_cache.Set(*from_dir, *to_dir)); + + // Obtain value + REQUIRE(ts_cache.Get(*from_dir) == *to_dir); + + // Reseting dependency fails and the entry doesn't get overwritten: + REQUIRE_FALSE(ts_cache.Set(*from_dir, ArtifactDigest{})); + REQUIRE(ts_cache.Get(*from_dir) == *to_dir); + + // Rotate generations + REQUIRE(GarbageCollector::TriggerGarbageCollection(storage_config.Get())); + + auto const youngest = Generation::Create(&storage_config.Get(), 0); + // Check there's no entry in the youngest generation: + CHECK_FALSE(youngest.CAS().TreePath(*from_dir).has_value()); + CHECK_FALSE(youngest.CAS().TreePath(*to_dir).has_value()); + + // Obtain value one more time and check uplinking has happened: + REQUIRE(ts_cache.Get(*from_dir) == *to_dir); + CHECK(youngest.CAS().TreePath(*from_dir).has_value()); + CHECK(youngest.CAS().TreePath(*to_dir).has_value()); +} + +namespace { +[[nodiscard]] auto CreateDirectory(std::filesystem::path const& directory, + Storage const& storage) { + BazelMsgFactory::FileStoreFunc store_file = + [&storage](std::filesystem::path const& path, + bool is_exec) -> std::optional<ArtifactDigest> { + return storage.CAS().StoreBlob(path, is_exec); + }; + + BazelMsgFactory::TreeStoreFunc store_tree = + [&storage]( + std::string const& content) -> std::optional<ArtifactDigest> { + return storage.CAS().StoreTree(content); + }; + + BazelMsgFactory::SymlinkStoreFunc store_symlink = + [&storage]( + std::string const& content) -> std::optional<ArtifactDigest> { + return storage.CAS().StoreBlob(content, /*is_executable=*/false); + }; + + return BazelMsgFactory::CreateGitTreeDigestFromLocalTree( + directory, store_file, store_tree, store_symlink); +} + +[[nodiscard]] auto CreateFlatTestDirectory(StorageConfig const& storage_config, + Storage const& storage, + std::uintmax_t entries) + -> std::optional<ArtifactDigest> { + auto tree = storage_config.CreateTypedTmpDir("tree"); + if (not tree or + not LargeObjectUtils::GenerateDirectory(tree->GetPath(), entries)) { + return std::nullopt; + } + return CreateDirectory(tree->GetPath(), storage); +} +} // namespace |