From ffa8e7a9986260280b5a3f415f73b47a53ba25c3 Mon Sep 17 00:00:00 2001 From: Kane York Date: Mon, 3 Aug 2020 06:43:22 -0700 Subject: feat(3p/nix): add tests for BinaryCacheStore These tests are in preparation for factoring the Store away from libutil's Callback to absl::StatusOr. They use the newly added MockBinaryCacheStore. Updates: #25 Change-Id: I30c207589ec38254806ebc9a983f35668e353ae9 Reviewed-on: https://cl.tvl.fyi/c/depot/+/1595 Tested-by: BuildkiteCI Reviewed-by: glittershark --- third_party/nix/src/tests/CMakeLists.txt | 9 +++ third_party/nix/src/tests/store_tests.cc | 97 ++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 third_party/nix/src/tests/store_tests.cc (limited to 'third_party/nix/src') diff --git a/third_party/nix/src/tests/CMakeLists.txt b/third_party/nix/src/tests/CMakeLists.txt index 33f4f774a61b..929acc5ea671 100644 --- a/third_party/nix/src/tests/CMakeLists.txt +++ b/third_party/nix/src/tests/CMakeLists.txt @@ -30,6 +30,15 @@ target_link_libraries(hash_test gtest_discover_tests(hash_test) +add_executable(store_test store_tests.cc) +target_link_libraries(store_test + nixstore + nixstoremock + GTest::gtest_main +) + +gtest_discover_tests(store_test) + add_executable(value-to-json value-to-json.cc) target_link_libraries(value-to-json nixexpr diff --git a/third_party/nix/src/tests/store_tests.cc b/third_party/nix/src/tests/store_tests.cc new file mode 100644 index 000000000000..1aea33bfd18b --- /dev/null +++ b/third_party/nix/src/tests/store_tests.cc @@ -0,0 +1,97 @@ +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "libstore/binary-cache-store.hh" +#include "libstore/mock-binary-cache-store.hh" + +using ::testing::HasSubstr; + +namespace nix { + +MakeError(InjectedError, Error); + +class StoreTest : public ::testing::Test { + public: + static void SetUpTestSuite() { + google::InitGoogleLogging("--logtostderr=false"); + } +}; + +class BinaryCacheStoreTest : public StoreTest {}; + +constexpr absl::string_view kXZHeader = "7zXZ"; + +constexpr absl::string_view kRootFileName = "myRootFile"; +constexpr absl::string_view kDep1FileName = "dep1"; +constexpr absl::string_view kDep1FileContents = "==dep1 contents=="; +constexpr absl::string_view kDep1NarCache = + "nar/0hfdc95cy6mxi4c15pp0frdf97r7yvd8c141qzvpms2f8x17p2ig.nar.xz"; +constexpr absl::string_view kBogusPath = + "/nix/store/g1ghizdg18k0d00000000000000z3v32-doesNotExist"; + +struct TestTree { + Path rootPath; + Path dep1Path; +}; + +TestTree AddTestTreeToStore(Store& store) { + TestTree results; + results.rootPath = + store.addTextToStore(std::string(kRootFileName), "1", PathSet()); + + PathSet onlyRoot; + onlyRoot.insert(results.rootPath); + results.dep1Path = store.addTextToStore( + std::string(kDep1FileName), std::string(kDep1FileContents), onlyRoot); + + return results; +} + +TEST_F(BinaryCacheStoreTest, BasicStorage) { + MockBinaryCacheStore::Params params; + MockBinaryCacheStore store(params); + + store.init(); + + auto tree = AddTestTreeToStore(store); + + EXPECT_TRUE(store.isValidPath(tree.rootPath)); + EXPECT_TRUE(store.isValidPath(tree.dep1Path)); + + StringSink sink; + store.narFromPath(tree.dep1Path, sink); + EXPECT_THAT(*sink.s, HasSubstr(kDep1FileContents)); + + EXPECT_THAT(*store.BinaryCacheStore::getFile(Path(kDep1NarCache)), + HasSubstr(kXZHeader)); +} + +TEST_F(BinaryCacheStoreTest, BasicErrors) { + MockBinaryCacheStore::Params params; + MockBinaryCacheStore store(params); + + store.init(); + + auto tree = AddTestTreeToStore(store); + store.PrepareErrorInjection(std::string(kDep1NarCache), + []() { throw InjectedError("injected"); }); + + { + StringSink sink; + EXPECT_THROW(store.narFromPath(tree.dep1Path, sink), InjectedError); + } + { + StringSink sink; + EXPECT_THROW(store.narFromPath(std::string(kBogusPath), sink), + NoSuchBinaryCacheFile); + } +} + +} // namespace nix -- cgit 1.4.1