diff --git a/src/libstore-tests/data/realisation/simple.json b/src/libstore-tests/data/realisation/simple.json new file mode 100644 index 000000000..2ccb1e721 --- /dev/null +++ b/src/libstore-tests/data/realisation/simple.json @@ -0,0 +1,6 @@ +{ + "dependentRealisations": {}, + "id": "sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad!foo", + "outPath": "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv", + "signatures": [] +} diff --git a/src/libstore-tests/data/realisation/with-dependent-realisations.json b/src/libstore-tests/data/realisation/with-dependent-realisations.json new file mode 100644 index 000000000..a58e0d7fe --- /dev/null +++ b/src/libstore-tests/data/realisation/with-dependent-realisations.json @@ -0,0 +1,8 @@ +{ + "dependentRealisations": { + "sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad!foo": "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv" + }, + "id": "sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad!foo", + "outPath": "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv", + "signatures": [] +} diff --git a/src/libstore-tests/data/realisation/with-signature.json b/src/libstore-tests/data/realisation/with-signature.json new file mode 100644 index 000000000..a28848cb0 --- /dev/null +++ b/src/libstore-tests/data/realisation/with-signature.json @@ -0,0 +1,8 @@ +{ + "dependentRealisations": {}, + "id": "sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad!foo", + "outPath": "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv", + "signatures": [ + "asdfasdfasdf" + ] +} diff --git a/src/libstore-tests/meson.build b/src/libstore-tests/meson.build index e3984d62f..915c10a38 100644 --- a/src/libstore-tests/meson.build +++ b/src/libstore-tests/meson.build @@ -74,6 +74,7 @@ sources = files( 'outputs-spec.cc', 'path-info.cc', 'path.cc', + 'realisation.cc', 'references.cc', 's3-binary-cache-store.cc', 's3.cc', diff --git a/src/libstore-tests/realisation.cc b/src/libstore-tests/realisation.cc new file mode 100644 index 000000000..2e4d592dc --- /dev/null +++ b/src/libstore-tests/realisation.cc @@ -0,0 +1,105 @@ +#include + +#include +#include +#include + +#include "nix/store/store-api.hh" + +#include "nix/util/tests/characterization.hh" +#include "nix/store/tests/libstore.hh" + +namespace nix { + +class RealisationTest : public CharacterizationTest, public LibStoreTest +{ + std::filesystem::path unitTestData = getUnitTestData() / "realisation"; + +public: + + std::filesystem::path goldenMaster(std::string_view testStem) const override + { + return unitTestData / testStem; + } +}; + +/* ---------------------------------------------------------------------------- + * JSON + * --------------------------------------------------------------------------*/ + +using nlohmann::json; + +struct RealisationJsonTest : RealisationTest, ::testing::WithParamInterface> +{}; + +TEST_P(RealisationJsonTest, from_json) +{ + auto [name, expected] = GetParam(); + readTest(name + ".json", [&](const auto & encoded_) { + auto encoded = json::parse(encoded_); + Realisation got = static_cast(encoded); + ASSERT_EQ(got, expected); + }); +} + +TEST_P(RealisationJsonTest, to_json) +{ + auto [name, value] = GetParam(); + writeTest( + name + ".json", + [&]() -> json { return static_cast(value); }, + [](const auto & file) { return json::parse(readFile(file)); }, + [](const auto & file, const auto & got) { return writeFile(file, got.dump(2) + "\n"); }); +} + +INSTANTIATE_TEST_SUITE_P( + RealisationJSON, + RealisationJsonTest, + ([] { + Realisation simple{ + + .id = + { + .drvHash = Hash::parseExplicitFormatUnprefixed( + "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", + HashAlgorithm::SHA256, + HashFormat::Base16), + .outputName = "foo", + }, + .outPath = StorePath{"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv"}, + }; + return ::testing::Values( + std::pair{ + "simple", + simple, + }, + std::pair{ + "with-signature", + [&] { + auto r = simple; + // FIXME actually sign properly + r.signatures = {"asdfasdfasdf"}; + return r; + }()}, + std::pair{ + "with-dependent-realisations", + [&] { + auto r = simple; + r.dependentRealisations = {{ + { + .drvHash = Hash::parseExplicitFormatUnprefixed( + "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", + HashAlgorithm::SHA256, + HashFormat::Base16), + .outputName = "foo", + }, + StorePath{"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv"}, + }}; + return r; + }(), + }); + } + + ())); + +} // namespace nix