Skip to content

Commit c5e375a

Browse files
committed
Write a basic unit test for the dummy store
Tests the realisation functionality we just added.
1 parent 1cb1e31 commit c5e375a

File tree

5 files changed

+99
-32
lines changed

5 files changed

+99
-32
lines changed

src/libfetchers-tests/git.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ TEST_F(GitTest, submodulePeriodSupport)
183183
auto store = [] {
184184
auto cfg = make_ref<DummyStoreConfig>(StoreReference::Params{});
185185
cfg->readOnly = false;
186-
return cfg->openStore();
186+
return cfg->openDummyStore();
187187
}();
188188

189189
auto settings = fetchers::Settings{};

src/libstore-tests/dummy-store.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <gtest/gtest.h>
2+
3+
#include "nix/store/dummy-store.hh"
4+
#include "nix/store/globals.hh"
5+
#include "nix/store/realisation.hh"
6+
7+
namespace nix {
8+
9+
TEST(DummyStore, realisation_read)
10+
{
11+
initLibStore(/*loadConfig=*/false);
12+
13+
auto store = [] {
14+
auto cfg = make_ref<DummyStoreConfig>(StoreReference::Params{});
15+
cfg->readOnly = false;
16+
return cfg->openDummyStore();
17+
}();
18+
19+
auto drvHash = Hash::parseExplicitFormatUnprefixed(
20+
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", HashAlgorithm::SHA256, HashFormat::Base16);
21+
22+
auto outputName = "foo";
23+
24+
UnkeyedRealisation value{
25+
.outPath = StorePath{"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv"},
26+
};
27+
28+
store->buildTrace.insert({drvHash, {{outputName, make_ref<UnkeyedRealisation>(value)}}});
29+
30+
auto value2 = store->queryRealisation({drvHash, outputName});
31+
32+
ASSERT_TRUE(value2);
33+
EXPECT_EQ(*value2, value);
34+
}
35+
36+
} // namespace nix

src/libstore-tests/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ sources = files(
6161
'derivation.cc',
6262
'derived-path.cc',
6363
'downstream-placeholder.cc',
64+
'dummy-store.cc',
6465
'http-binary-cache-store.cc',
6566
'legacy-ssh-store.cc',
6667
'local-binary-cache-store.cc',

src/libstore/dummy-store.cc

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -109,36 +109,10 @@ class WholeStoreViewAccessor : public SourceAccessor
109109

110110
} // namespace
111111

112-
struct DummyStore : virtual Store
112+
struct DummyStoreImpl : DummyStore
113113
{
114114
using Config = DummyStoreConfig;
115115

116-
ref<const Config> config;
117-
118-
struct PathInfoAndContents
119-
{
120-
UnkeyedValidPathInfo info;
121-
ref<MemorySourceAccessor> contents;
122-
};
123-
124-
/**
125-
* This is map conceptually owns the file system objects for each
126-
* store object.
127-
*/
128-
boost::concurrent_flat_map<StorePath, PathInfoAndContents> contents;
129-
130-
/**
131-
* The build trace maps the pair of a content-addressing (fixed or
132-
* floating) derivations an one of its output to a
133-
* (content-addressed) store object.
134-
*
135-
* It is [curried](https://en.wikipedia.org/wiki/Currying), so we
136-
* instead having a single output with a `DrvOutput` key, we have an
137-
* outer map for the derivation, and inner maps for the outputs of a
138-
* given derivation.
139-
*/
140-
boost::concurrent_flat_map<Hash, std::map<std::string, ref<UnkeyedRealisation>>> buildTrace;
141-
142116
/**
143117
* This view conceptually just borrows the file systems objects of
144118
* each store object from `contents`, and combines them together
@@ -148,9 +122,9 @@ struct DummyStore : virtual Store
148122
*/
149123
ref<WholeStoreViewAccessor> wholeStoreView = make_ref<WholeStoreViewAccessor>();
150124

151-
DummyStore(ref<const Config> config)
125+
DummyStoreImpl(ref<const Config> config)
152126
: Store{*config}
153-
, config(config)
127+
, DummyStore{config}
154128
{
155129
wholeStoreView->setPathDisplay(config->storeDir);
156130
}
@@ -319,9 +293,9 @@ struct DummyStore : virtual Store
319293
}
320294
};
321295

322-
ref<Store> DummyStore::Config::openStore() const
296+
ref<DummyStore> DummyStore::Config::openDummyStore() const
323297
{
324-
return make_ref<DummyStore>(ref{shared_from_this()});
298+
return make_ref<DummyStoreImpl>(ref{shared_from_this()});
325299
}
326300

327301
static RegisterStoreImplementation<DummyStore::Config> regDummyStore;

src/libstore/include/nix/store/dummy-store.hh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33

44
#include "nix/store/store-api.hh"
55

6+
#include <boost/unordered/concurrent_flat_map.hpp>
7+
68
namespace nix {
79

10+
struct DummyStore;
11+
812
struct DummyStoreConfig : public std::enable_shared_from_this<DummyStoreConfig>, virtual StoreConfig
913
{
1014
DummyStoreConfig(const Params & params)
@@ -42,6 +46,11 @@ struct DummyStoreConfig : public std::enable_shared_from_this<DummyStoreConfig>,
4246
return {"dummy"};
4347
}
4448

49+
/**
50+
* Same as `openStore`, just with a more precise return type.
51+
*/
52+
ref<DummyStore> openDummyStore() const;
53+
4554
ref<Store> openStore() const override;
4655

4756
StoreReference getReference() const override
@@ -56,4 +65,51 @@ struct DummyStoreConfig : public std::enable_shared_from_this<DummyStoreConfig>,
5665
}
5766
};
5867

68+
struct MemorySourceAccessor;
69+
70+
/**
71+
* Enough of the Dummy Store exposed for sake of writing unit tests
72+
*/
73+
struct DummyStore : virtual Store
74+
{
75+
using Config = DummyStoreConfig;
76+
77+
ref<const Config> config;
78+
79+
struct PathInfoAndContents
80+
{
81+
UnkeyedValidPathInfo info;
82+
ref<MemorySourceAccessor> contents;
83+
};
84+
85+
/**
86+
* This is map conceptually owns the file system objects for each
87+
* store object.
88+
*/
89+
boost::concurrent_flat_map<StorePath, PathInfoAndContents> contents;
90+
91+
/**
92+
* The build trace maps the pair of a content-addressing (fixed or
93+
* floating) derivations an one of its output to a
94+
* (content-addressed) store object.
95+
*
96+
* It is [curried](https://en.wikipedia.org/wiki/Currying), so we
97+
* instead having a single output with a `DrvOutput` key, we have an
98+
* outer map for the derivation, and inner maps for the outputs of a
99+
* given derivation.
100+
*/
101+
boost::concurrent_flat_map<Hash, std::map<std::string, ref<UnkeyedRealisation>>> buildTrace;
102+
103+
DummyStore(ref<const Config> config)
104+
: Store{*config}
105+
, config(config)
106+
{
107+
}
108+
};
109+
110+
ref<Store> DummyStoreConfig::openStore() const
111+
{
112+
return openDummyStore();
113+
}
114+
59115
} // namespace nix

0 commit comments

Comments
 (0)