mirror of
https://github.com/NixOS/nix.git
synced 2025-12-22 17:01:08 +01:00
Create basic substitution unit tests
- substitute single store object - substitute single store object with single dep
This commit is contained in:
parent
3cfac9b079
commit
bb74677b08
6 changed files with 343 additions and 0 deletions
|
|
@ -0,0 +1,31 @@
|
||||||
|
{
|
||||||
|
"buildTrace": {},
|
||||||
|
"config": {
|
||||||
|
"store": "/nix/store"
|
||||||
|
},
|
||||||
|
"contents": {
|
||||||
|
"axqic2q30v0sqvcpiqxs139q8w6zd4n8-hello": {
|
||||||
|
"contents": {
|
||||||
|
"contents": "Hello, world!",
|
||||||
|
"executable": false,
|
||||||
|
"type": "regular"
|
||||||
|
},
|
||||||
|
"info": {
|
||||||
|
"ca": {
|
||||||
|
"hash": "sha256-KShIJtIwWG1gMSpvPMt5drppc1h5WMwHWzVpNJiVqGI=",
|
||||||
|
"method": "nar"
|
||||||
|
},
|
||||||
|
"deriver": null,
|
||||||
|
"narHash": "sha256-KShIJtIwWG1gMSpvPMt5drppc1h5WMwHWzVpNJiVqGI=",
|
||||||
|
"narSize": 128,
|
||||||
|
"references": [],
|
||||||
|
"registrationTime": null,
|
||||||
|
"signatures": [],
|
||||||
|
"storeDir": "/nix/store",
|
||||||
|
"ultimate": false,
|
||||||
|
"version": 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"derivations": {}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,55 @@
|
||||||
|
{
|
||||||
|
"buildTrace": {},
|
||||||
|
"config": {
|
||||||
|
"store": "/nix/store"
|
||||||
|
},
|
||||||
|
"contents": {
|
||||||
|
"4k79i02avcckr96r97lqnswn75fi1gv7-dependency": {
|
||||||
|
"contents": {
|
||||||
|
"contents": "I am a dependency",
|
||||||
|
"executable": false,
|
||||||
|
"type": "regular"
|
||||||
|
},
|
||||||
|
"info": {
|
||||||
|
"ca": {
|
||||||
|
"hash": "sha256-miJnClL0Ai/HAmX1G/pz7P2TIaeFjP5D/VN1rhYf354=",
|
||||||
|
"method": "nar"
|
||||||
|
},
|
||||||
|
"deriver": null,
|
||||||
|
"narHash": "sha256-miJnClL0Ai/HAmX1G/pz7P2TIaeFjP5D/VN1rhYf354=",
|
||||||
|
"narSize": 136,
|
||||||
|
"references": [],
|
||||||
|
"registrationTime": null,
|
||||||
|
"signatures": [],
|
||||||
|
"storeDir": "/nix/store",
|
||||||
|
"ultimate": false,
|
||||||
|
"version": 2
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"k09ldq9fvxb6vfwq0cmv6j1jgqx08y1n-main": {
|
||||||
|
"contents": {
|
||||||
|
"contents": "I depend on /nix/store/4k79i02avcckr96r97lqnswn75fi1gv7-dependency",
|
||||||
|
"executable": false,
|
||||||
|
"type": "regular"
|
||||||
|
},
|
||||||
|
"info": {
|
||||||
|
"ca": {
|
||||||
|
"hash": "sha256-CBfMK3HkqiXjpI8HNL1spWD/US4RnQHwI67Ojl50XoQ=",
|
||||||
|
"method": "nar"
|
||||||
|
},
|
||||||
|
"deriver": null,
|
||||||
|
"narHash": "sha256-CBfMK3HkqiXjpI8HNL1spWD/US4RnQHwI67Ojl50XoQ=",
|
||||||
|
"narSize": 184,
|
||||||
|
"references": [
|
||||||
|
"4k79i02avcckr96r97lqnswn75fi1gv7-dependency"
|
||||||
|
],
|
||||||
|
"registrationTime": null,
|
||||||
|
"signatures": [],
|
||||||
|
"storeDir": "/nix/store",
|
||||||
|
"ultimate": false,
|
||||||
|
"version": 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"derivations": {}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,55 @@
|
||||||
|
{
|
||||||
|
"buildTrace": {},
|
||||||
|
"config": {
|
||||||
|
"store": "/nix/store"
|
||||||
|
},
|
||||||
|
"contents": {
|
||||||
|
"4k79i02avcckr96r97lqnswn75fi1gv7-dependency": {
|
||||||
|
"contents": {
|
||||||
|
"contents": "I am a dependency",
|
||||||
|
"executable": false,
|
||||||
|
"type": "regular"
|
||||||
|
},
|
||||||
|
"info": {
|
||||||
|
"ca": {
|
||||||
|
"hash": "sha256-miJnClL0Ai/HAmX1G/pz7P2TIaeFjP5D/VN1rhYf354=",
|
||||||
|
"method": "nar"
|
||||||
|
},
|
||||||
|
"deriver": null,
|
||||||
|
"narHash": "sha256-miJnClL0Ai/HAmX1G/pz7P2TIaeFjP5D/VN1rhYf354=",
|
||||||
|
"narSize": 136,
|
||||||
|
"references": [],
|
||||||
|
"registrationTime": null,
|
||||||
|
"signatures": [],
|
||||||
|
"storeDir": "/nix/store",
|
||||||
|
"ultimate": false,
|
||||||
|
"version": 2
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"k09ldq9fvxb6vfwq0cmv6j1jgqx08y1n-main": {
|
||||||
|
"contents": {
|
||||||
|
"contents": "I depend on /nix/store/4k79i02avcckr96r97lqnswn75fi1gv7-dependency",
|
||||||
|
"executable": false,
|
||||||
|
"type": "regular"
|
||||||
|
},
|
||||||
|
"info": {
|
||||||
|
"ca": {
|
||||||
|
"hash": "sha256-CBfMK3HkqiXjpI8HNL1spWD/US4RnQHwI67Ojl50XoQ=",
|
||||||
|
"method": "nar"
|
||||||
|
},
|
||||||
|
"deriver": null,
|
||||||
|
"narHash": "sha256-CBfMK3HkqiXjpI8HNL1spWD/US4RnQHwI67Ojl50XoQ=",
|
||||||
|
"narSize": 184,
|
||||||
|
"references": [
|
||||||
|
"4k79i02avcckr96r97lqnswn75fi1gv7-dependency"
|
||||||
|
],
|
||||||
|
"registrationTime": null,
|
||||||
|
"signatures": [],
|
||||||
|
"storeDir": "/nix/store",
|
||||||
|
"ultimate": false,
|
||||||
|
"version": 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"derivations": {}
|
||||||
|
}
|
||||||
|
|
@ -85,6 +85,7 @@ sources = files(
|
||||||
'store-reference.cc',
|
'store-reference.cc',
|
||||||
'uds-remote-store.cc',
|
'uds-remote-store.cc',
|
||||||
'worker-protocol.cc',
|
'worker-protocol.cc',
|
||||||
|
'worker-substitution.cc',
|
||||||
'write-derivation.cc',
|
'write-derivation.cc',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
176
src/libstore-tests/worker-substitution.cc
Normal file
176
src/libstore-tests/worker-substitution.cc
Normal file
|
|
@ -0,0 +1,176 @@
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
#include "nix/store/build/worker.hh"
|
||||||
|
#include "nix/store/dummy-store-impl.hh"
|
||||||
|
#include "nix/store/globals.hh"
|
||||||
|
#include "nix/util/memory-source-accessor.hh"
|
||||||
|
|
||||||
|
#include "nix/store/tests/libstore.hh"
|
||||||
|
#include "nix/util/tests/json-characterization.hh"
|
||||||
|
|
||||||
|
namespace nix {
|
||||||
|
|
||||||
|
class WorkerSubstitutionTest : public LibStoreTest, public JsonCharacterizationTest<ref<DummyStore>>
|
||||||
|
{
|
||||||
|
std::filesystem::path unitTestData = getUnitTestData() / "worker-substitution";
|
||||||
|
|
||||||
|
protected:
|
||||||
|
ref<DummyStore> dummyStore;
|
||||||
|
ref<DummyStore> substituter;
|
||||||
|
|
||||||
|
WorkerSubstitutionTest()
|
||||||
|
: LibStoreTest([] {
|
||||||
|
auto config = make_ref<DummyStoreConfig>(DummyStoreConfig::Params{});
|
||||||
|
config->readOnly = false;
|
||||||
|
return config->openDummyStore();
|
||||||
|
}())
|
||||||
|
, dummyStore(store.dynamic_pointer_cast<DummyStore>())
|
||||||
|
, substituter([] {
|
||||||
|
auto config = make_ref<DummyStoreConfig>(DummyStoreConfig::Params{});
|
||||||
|
config->readOnly = false;
|
||||||
|
config->isTrusted = true;
|
||||||
|
return config->openDummyStore();
|
||||||
|
}())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
std::filesystem::path goldenMaster(std::string_view testStem) const override
|
||||||
|
{
|
||||||
|
return unitTestData / testStem;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void SetUpTestSuite()
|
||||||
|
{
|
||||||
|
initLibStore(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_F(WorkerSubstitutionTest, singleStoreObject)
|
||||||
|
{
|
||||||
|
// Add a store path to the substituter
|
||||||
|
auto pathInSubstituter = substituter->addToStore(
|
||||||
|
"hello",
|
||||||
|
SourcePath{
|
||||||
|
[] {
|
||||||
|
auto sc = make_ref<MemorySourceAccessor>();
|
||||||
|
sc->root = MemorySourceAccessor::File{MemorySourceAccessor::File::Regular{
|
||||||
|
.executable = false,
|
||||||
|
.contents = "Hello, world!",
|
||||||
|
}};
|
||||||
|
return sc;
|
||||||
|
}(),
|
||||||
|
},
|
||||||
|
ContentAddressMethod::Raw::NixArchive,
|
||||||
|
HashAlgorithm::SHA256);
|
||||||
|
|
||||||
|
// Snapshot the substituter (has one store object)
|
||||||
|
checkpointJson("single/substituter", substituter);
|
||||||
|
|
||||||
|
// Snapshot the destination store before (should be empty)
|
||||||
|
checkpointJson("../dummy-store/empty", dummyStore);
|
||||||
|
|
||||||
|
// The path should not exist in the destination store yet
|
||||||
|
ASSERT_FALSE(dummyStore->isValidPath(pathInSubstituter));
|
||||||
|
|
||||||
|
// Create a worker with our custom substituter
|
||||||
|
Worker worker{*dummyStore, *dummyStore};
|
||||||
|
|
||||||
|
// Override the substituters to use our dummy store substituter
|
||||||
|
ref<Store> substituerAsStore = substituter;
|
||||||
|
worker.getSubstituters = [substituerAsStore]() -> std::list<ref<Store>> { return {substituerAsStore}; };
|
||||||
|
|
||||||
|
// Create a substitution goal for the path
|
||||||
|
auto goal = worker.makePathSubstitutionGoal(pathInSubstituter);
|
||||||
|
|
||||||
|
// Run the worker with -j0 semantics (no local builds, only substitution)
|
||||||
|
// The worker.run() takes a set of goals
|
||||||
|
Goals goals;
|
||||||
|
goals.insert(upcast_goal(goal));
|
||||||
|
worker.run(goals);
|
||||||
|
|
||||||
|
// Snapshot the destination store after (should match the substituter)
|
||||||
|
checkpointJson("single/substituter", dummyStore);
|
||||||
|
|
||||||
|
// The path should now exist in the destination store
|
||||||
|
ASSERT_TRUE(dummyStore->isValidPath(pathInSubstituter));
|
||||||
|
|
||||||
|
// Verify the goal succeeded
|
||||||
|
ASSERT_EQ(goal->exitCode, Goal::ecSuccess);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(WorkerSubstitutionTest, singleRootStoreObjectWithSingleDepStoreObject)
|
||||||
|
{
|
||||||
|
// First, add a dependency store path to the substituter
|
||||||
|
auto dependencyPath = substituter->addToStore(
|
||||||
|
"dependency",
|
||||||
|
SourcePath{
|
||||||
|
[] {
|
||||||
|
auto sc = make_ref<MemorySourceAccessor>();
|
||||||
|
sc->root = MemorySourceAccessor::File{MemorySourceAccessor::File::Regular{
|
||||||
|
.executable = false,
|
||||||
|
.contents = "I am a dependency",
|
||||||
|
}};
|
||||||
|
return sc;
|
||||||
|
}(),
|
||||||
|
},
|
||||||
|
ContentAddressMethod::Raw::NixArchive,
|
||||||
|
HashAlgorithm::SHA256);
|
||||||
|
|
||||||
|
// Now add a store path that references the dependency
|
||||||
|
auto mainPath = substituter->addToStore(
|
||||||
|
"main",
|
||||||
|
SourcePath{
|
||||||
|
[&] {
|
||||||
|
auto sc = make_ref<MemorySourceAccessor>();
|
||||||
|
// Include a reference to the dependency path in the contents
|
||||||
|
sc->root = MemorySourceAccessor::File{MemorySourceAccessor::File::Regular{
|
||||||
|
.executable = false,
|
||||||
|
.contents = "I depend on " + substituter->printStorePath(dependencyPath),
|
||||||
|
}};
|
||||||
|
return sc;
|
||||||
|
}(),
|
||||||
|
},
|
||||||
|
ContentAddressMethod::Raw::NixArchive,
|
||||||
|
HashAlgorithm::SHA256,
|
||||||
|
StorePathSet{dependencyPath});
|
||||||
|
|
||||||
|
// Snapshot the substituter (has two store objects)
|
||||||
|
checkpointJson("with-dep/substituter", substituter);
|
||||||
|
|
||||||
|
// Snapshot the destination store before (should be empty)
|
||||||
|
checkpointJson("../dummy-store/empty", dummyStore);
|
||||||
|
|
||||||
|
// Neither path should exist in the destination store yet
|
||||||
|
ASSERT_FALSE(dummyStore->isValidPath(dependencyPath));
|
||||||
|
ASSERT_FALSE(dummyStore->isValidPath(mainPath));
|
||||||
|
|
||||||
|
// Create a worker with our custom substituter
|
||||||
|
Worker worker{*dummyStore, *dummyStore};
|
||||||
|
|
||||||
|
// Override the substituters to use our dummy store substituter
|
||||||
|
ref<Store> substituterAsStore = substituter;
|
||||||
|
worker.getSubstituters = [substituterAsStore]() -> std::list<ref<Store>> { return {substituterAsStore}; };
|
||||||
|
|
||||||
|
// Create a substitution goal for the main path only
|
||||||
|
// The worker should automatically substitute the dependency as well
|
||||||
|
auto goal = worker.makePathSubstitutionGoal(mainPath);
|
||||||
|
|
||||||
|
// Run the worker
|
||||||
|
Goals goals;
|
||||||
|
goals.insert(upcast_goal(goal));
|
||||||
|
worker.run(goals);
|
||||||
|
|
||||||
|
// Snapshot the destination store after (should match the substituter)
|
||||||
|
checkpointJson("with-dep/substituter", dummyStore);
|
||||||
|
|
||||||
|
// Both paths should now exist in the destination store
|
||||||
|
ASSERT_TRUE(dummyStore->isValidPath(dependencyPath));
|
||||||
|
ASSERT_TRUE(dummyStore->isValidPath(mainPath));
|
||||||
|
|
||||||
|
// Verify the goal succeeded
|
||||||
|
ASSERT_EQ(goal->exitCode, Goal::ecSuccess);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace nix
|
||||||
|
|
@ -83,6 +83,31 @@ void checkpointJson(CharacterizationTest & test, PathView testStem, const T & go
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specialization for when we need to do "JSON -> `ref<T>`" in one
|
||||||
|
* direction, but "`const T &` -> JSON" in the other direction.
|
||||||
|
*/
|
||||||
|
template<typename T>
|
||||||
|
void checkpointJson(CharacterizationTest & test, PathView testStem, const ref<T> & got)
|
||||||
|
{
|
||||||
|
using namespace nlohmann;
|
||||||
|
|
||||||
|
auto file = test.goldenMaster(Path{testStem} + ".json");
|
||||||
|
|
||||||
|
json gotJson = static_cast<json>(*got);
|
||||||
|
|
||||||
|
if (testAccept()) {
|
||||||
|
std::filesystem::create_directories(file.parent_path());
|
||||||
|
writeFile(file, gotJson.dump(2) + "\n");
|
||||||
|
ADD_FAILURE() << "Updating golden master " << file;
|
||||||
|
} else {
|
||||||
|
json expectedJson = json::parse(readFile(file));
|
||||||
|
ASSERT_EQ(gotJson, expectedJson);
|
||||||
|
ref<T> expected = adl_serializer<ref<T>>::from_json(expectedJson);
|
||||||
|
ASSERT_EQ(*got, *expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mixin class for writing characterization tests for `nlohmann::json`
|
* Mixin class for writing characterization tests for `nlohmann::json`
|
||||||
* conversions for a given type.
|
* conversions for a given type.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue