|
| 1 | +//===- llvm/unittest/CAS/PluginCASTest.cpp --------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "llvm/CAS/ActionCache.h" |
| 10 | +#include "llvm/CAS/ObjectStore.h" |
| 11 | +#include "llvm/Config/config.h" |
| 12 | +#include "llvm/Support/Path.h" |
| 13 | +#include "llvm/Testing/Support/Error.h" |
| 14 | +#include "llvm/Testing/Support/SupportHelpers.h" |
| 15 | +#include "gtest/gtest.h" |
| 16 | + |
| 17 | +#if LLVM_ENABLE_ONDISK_CAS |
| 18 | + |
| 19 | +using namespace llvm; |
| 20 | +using namespace llvm::cas; |
| 21 | + |
| 22 | +// See llvm/utils/unittest/UnitTestMain/TestMain.cpp |
| 23 | +extern const char *TestMainArgv0; |
| 24 | + |
| 25 | +// Just a reachable symbol to ease resolving of the executable's path. |
| 26 | +static std::string TestStringArg1("plugincas-test-string-arg1"); |
| 27 | + |
| 28 | +static std::string getCASPluginPath() { |
| 29 | + std::string Executable = |
| 30 | + sys::fs::getMainExecutable(TestMainArgv0, &TestStringArg1); |
| 31 | + llvm::SmallString<256> PathBuf(sys::path::parent_path( |
| 32 | + sys::path::parent_path(sys::path::parent_path(Executable)))); |
| 33 | + std::string LibName = "libCASPluginTest"; |
| 34 | + sys::path::append(PathBuf, "lib", LibName + LLVM_PLUGIN_EXT); |
| 35 | + return std::string(PathBuf); |
| 36 | +} |
| 37 | + |
| 38 | +TEST(PluginCASTest, isMaterialized) { |
| 39 | + unittest::TempDir Temp("plugin-cas", /*Unique=*/true); |
| 40 | + std::string UpDir(Temp.path("up")); |
| 41 | + std::string DownDir(Temp.path("down")); |
| 42 | + std::pair<std::string, std::string> PluginOpts[] = { |
| 43 | + {"upstream-path", std::string(UpDir)}}; |
| 44 | + |
| 45 | + { |
| 46 | + std::optional< |
| 47 | + std::pair<std::shared_ptr<ObjectStore>, std::shared_ptr<ActionCache>>> |
| 48 | + DBs; |
| 49 | + ASSERT_THAT_ERROR( |
| 50 | + createPluginCASDatabases(getCASPluginPath(), DownDir, PluginOpts) |
| 51 | + .moveInto(DBs), |
| 52 | + Succeeded()); |
| 53 | + std::shared_ptr<ObjectStore> CAS; |
| 54 | + std::shared_ptr<ActionCache> AC; |
| 55 | + std::tie(CAS, AC) = std::move(*DBs); |
| 56 | + |
| 57 | + Optional<CASID> ID1, ID2; |
| 58 | + ASSERT_THAT_ERROR(CAS->createProxy(std::nullopt, "1").moveInto(ID1), |
| 59 | + Succeeded()); |
| 60 | + ASSERT_THAT_ERROR(CAS->createProxy(std::nullopt, "2").moveInto(ID2), |
| 61 | + Succeeded()); |
| 62 | + Optional<ObjectRef> ID2Ref = CAS->getReference(*ID2); |
| 63 | + ASSERT_TRUE(ID2Ref); |
| 64 | + bool IsMaterialized = false; |
| 65 | + ASSERT_THAT_ERROR(CAS->isMaterialized(*ID2Ref).moveInto(IsMaterialized), |
| 66 | + Succeeded()); |
| 67 | + EXPECT_TRUE(IsMaterialized); |
| 68 | + ASSERT_THAT_ERROR(AC->put(*ID1, *ID2, /*Globally=*/true), Succeeded()); |
| 69 | + } |
| 70 | + |
| 71 | + // Clear "local" cache. |
| 72 | + sys::fs::remove_directories(DownDir); |
| 73 | + |
| 74 | + { |
| 75 | + std::optional< |
| 76 | + std::pair<std::shared_ptr<ObjectStore>, std::shared_ptr<ActionCache>>> |
| 77 | + DBs; |
| 78 | + ASSERT_THAT_ERROR( |
| 79 | + createPluginCASDatabases(getCASPluginPath(), DownDir, PluginOpts) |
| 80 | + .moveInto(DBs), |
| 81 | + Succeeded()); |
| 82 | + std::shared_ptr<ObjectStore> CAS; |
| 83 | + std::shared_ptr<ActionCache> AC; |
| 84 | + std::tie(CAS, AC) = std::move(*DBs); |
| 85 | + |
| 86 | + Optional<CASID> ID1, ID2; |
| 87 | + ASSERT_THAT_ERROR(CAS->createProxy(std::nullopt, "1").moveInto(ID1), |
| 88 | + Succeeded()); |
| 89 | + ASSERT_THAT_ERROR(AC->get(*ID1, /*Globally=*/true).moveInto(ID2), |
| 90 | + Succeeded()); |
| 91 | + Optional<ObjectRef> ID2Ref = CAS->getReference(*ID2); |
| 92 | + ASSERT_TRUE(ID2Ref); |
| 93 | + bool IsMaterialized = false; |
| 94 | + ASSERT_THAT_ERROR(CAS->isMaterialized(*ID2Ref).moveInto(IsMaterialized), |
| 95 | + Succeeded()); |
| 96 | + EXPECT_FALSE(IsMaterialized); |
| 97 | + |
| 98 | + Optional<ObjectProxy> Obj; |
| 99 | + ASSERT_THAT_ERROR(CAS->getProxy(*ID2Ref).moveInto(Obj), Succeeded()); |
| 100 | + ASSERT_THAT_ERROR(CAS->isMaterialized(*ID2Ref).moveInto(IsMaterialized), |
| 101 | + Succeeded()); |
| 102 | + EXPECT_TRUE(IsMaterialized); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +#endif // LLVM_ENABLE_ONDISK_CAS |
0 commit comments