1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-14 22:42:41 +01:00

Expose some core implementation details and write a basic unit test for the dummy store

This test currently doesn't use the new-exposed functionality, but with
future changes the tests will be expanded and they will be used.
This commit is contained in:
John Ericson 2025-09-27 16:52:36 -04:00
parent d76dc2406f
commit 9ac306c4df
6 changed files with 87 additions and 20 deletions

View file

@ -0,0 +1,27 @@
#include <gtest/gtest.h>
#include "nix/store/dummy-store.hh"
#include "nix/store/globals.hh"
#include "nix/store/realisation.hh"
namespace nix {
TEST(DummyStore, realisation_read)
{
initLibStore(/*loadConfig=*/false);
auto store = [] {
auto cfg = make_ref<DummyStoreConfig>(StoreReference::Params{});
cfg->readOnly = false;
return cfg->openStore();
}();
auto drvHash = Hash::parseExplicitFormatUnprefixed(
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", HashAlgorithm::SHA256, HashFormat::Base16);
auto outputName = "foo";
EXPECT_EQ(store->queryRealisation({drvHash, outputName}), nullptr);
}
} // namespace nix

View file

@ -61,6 +61,7 @@ sources = files(
'derivation.cc',
'derived-path.cc',
'downstream-placeholder.cc',
'dummy-store.cc',
'http-binary-cache-store.cc',
'legacy-ssh-store.cc',
'local-binary-cache-store.cc',

View file

@ -2,7 +2,7 @@
#include "nix/util/archive.hh"
#include "nix/util/callback.hh"
#include "nix/util/memory-source-accessor.hh"
#include "nix/store/dummy-store.hh"
#include "nix/store/dummy-store-impl.hh"
#include <boost/unordered/concurrent_flat_map.hpp>
@ -108,24 +108,15 @@ public:
} // namespace
struct DummyStore : virtual Store
ref<Store> DummyStoreConfig::openStore() const
{
return openDummyStore();
}
struct DummyStoreImpl : DummyStore
{
using Config = DummyStoreConfig;
ref<const Config> config;
struct PathInfoAndContents
{
UnkeyedValidPathInfo info;
ref<MemorySourceAccessor> contents;
};
/**
* This is map conceptually owns the file system objects for each
* store object.
*/
boost::concurrent_flat_map<StorePath, PathInfoAndContents> contents;
/**
* This view conceptually just borrows the file systems objects of
* each store object from `contents`, and combines them together
@ -135,9 +126,9 @@ struct DummyStore : virtual Store
*/
ref<WholeStoreViewAccessor> wholeStoreView = make_ref<WholeStoreViewAccessor>();
DummyStore(ref<const Config> config)
DummyStoreImpl(ref<const Config> config)
: Store{*config}
, config(config)
, DummyStore{config}
{
wholeStoreView->setPathDisplay(config->storeDir);
}
@ -289,9 +280,9 @@ struct DummyStore : virtual Store
}
};
ref<Store> DummyStore::Config::openStore() const
ref<DummyStore> DummyStore::Config::openDummyStore() const
{
return make_ref<DummyStore>(ref{shared_from_this()});
return make_ref<DummyStoreImpl>(ref{shared_from_this()});
}
static RegisterStoreImplementation<DummyStore::Config> regDummyStore;

View file

@ -0,0 +1,40 @@
#pragma once
///@file
#include "nix/store/dummy-store.hh"
#include <boost/unordered/concurrent_flat_map.hpp>
namespace nix {
struct MemorySourceAccessor;
/**
* Enough of the Dummy Store exposed for sake of writing unit tests
*/
struct DummyStore : virtual Store
{
using Config = DummyStoreConfig;
ref<const Config> config;
struct PathInfoAndContents
{
UnkeyedValidPathInfo info;
ref<MemorySourceAccessor> contents;
};
/**
* This is map conceptually owns the file system objects for each
* store object.
*/
boost::concurrent_flat_map<StorePath, PathInfoAndContents> contents;
DummyStore(ref<const Config> config)
: Store{*config}
, config(config)
{
}
};
} // namespace nix

View file

@ -5,6 +5,8 @@
namespace nix {
struct DummyStore;
struct DummyStoreConfig : public std::enable_shared_from_this<DummyStoreConfig>, virtual StoreConfig
{
DummyStoreConfig(const Params & params)
@ -42,6 +44,11 @@ struct DummyStoreConfig : public std::enable_shared_from_this<DummyStoreConfig>,
return {"dummy"};
}
/**
* Same as `openStore`, just with a more precise return type.
*/
ref<DummyStore> openDummyStore() const;
ref<Store> openStore() const override;
StoreReference getReference() const override

View file

@ -34,6 +34,7 @@ headers = [ config_pub_h ] + files(
'derived-path-map.hh',
'derived-path.hh',
'downstream-placeholder.hh',
'dummy-store-impl.hh',
'dummy-store.hh',
'export-import.hh',
'filetransfer.hh',