From 1982c1ba86c94efa2d5822f52ad217a32a03afbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Boros?= Date: Fri, 8 Mar 2024 19:18:25 +0000 Subject: [PATCH 1/5] add basic cardano-node module --- checks/testing.nix | 4 +++- modules/cardano-cli/default.nix | 4 ++-- modules/cardano-node/default.nix | 24 ++++++++++++++++++++++ modules/default.nix | 25 ++++++++++++++++++++--- tests/cardano-cli.nix | 8 +++----- tests/cardano-node.nix | 34 ++++++++++++++++++++++++++++++++ tests/default.nix | 1 + 7 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 modules/cardano-node/default.nix create mode 100644 tests/cardano-node.nix diff --git a/checks/testing.nix b/checks/testing.nix index 1efe54e..baca1c7 100644 --- a/checks/testing.nix +++ b/checks/testing.nix @@ -66,9 +66,11 @@ in { inherit (test) specialArgs; }; - # import all of our flake nixos modules by default defaults.imports = [ + # import all of our flake nixos modules by default nixosModules.default + # fix missing pkgs.system in tests + {nixpkgs.overlays = [(_: _: {inherit system;})];} ]; # import the test module diff --git a/modules/cardano-cli/default.nix b/modules/cardano-cli/default.nix index cfe941c..6728546 100644 --- a/modules/cardano-cli/default.nix +++ b/modules/cardano-cli/default.nix @@ -4,11 +4,11 @@ pkgs, ... }: { - options.cardanoNix.cardano-cli.enable = lib.mkEnableOption "Install cardano CLI tools and scripts"; + options.cardanoNix.cardano-cli.enable = lib.mkEnableOption "cardano-cli in systemPackages"; config = lib.mkIf config.cardanoNix.cardano-cli.enable { environment.systemPackages = [ - pkgs.git # TODO: replace with `cardano-cli` (milestone 2) + pkgs.cardano-cli ]; }; } diff --git a/modules/cardano-node/default.nix b/modules/cardano-node/default.nix new file mode 100644 index 0000000..f44e21a --- /dev/null +++ b/modules/cardano-node/default.nix @@ -0,0 +1,24 @@ +{ + config, + lib, + ... +}: let + cfg = config.cardanoNix.cardano-node; +in { + options.cardanoNix.cardano-node = { + enable = lib.mkEnableOption "cardano-node service"; + }; + + config = lib.mkIf cfg.enable { + environment.variables = { + CARDANO_NODE_SOCKET_PATH = config.services.cardano-node.socketPath 0; + }; + + services.cardano-node = { + enable = true; + hostAddr = "0.0.0.0"; + + environment = config.cardanoNix.globals.network; + }; + }; +} diff --git a/modules/default.nix b/modules/default.nix index 33c93b4..f95cbe3 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -1,7 +1,26 @@ -{config, ...}: { - flake.nixosModules = { +{ + inputs, + config, + ... +}: { + flake.nixosModules = rec { globals = ./globals; - cardano-cli = ./cardano-cli; + cardano-node = { + imports = [ + inputs.cardano-node.nixosModules.cardano-node + cardano-overlay + ./cardano-node + ]; + }; + cardano-cli = { + imports = [ + cardano-overlay + ./cardano-cli + ]; + }; + cardano-overlay = { + nixpkgs.overlays = [config.flake.overlays.default]; + }; # the default module imports all modules default = { imports = with builtins; attrValues (removeAttrs config.flake.nixosModules ["default"]); diff --git a/tests/cardano-cli.nix b/tests/cardano-cli.nix index 1c09bad..7bbdaa6 100644 --- a/tests/cardano-cli.nix +++ b/tests/cardano-cli.nix @@ -1,25 +1,23 @@ { cardanoNix.tests = { - dummy = { + cardano-cli = { systems = ["x86_64-linux"]; module = { - name = "cli-test"; + name = "cardano-cli-test"; nodes = { machine = { virtualisation = { cores = 2; memorySize = 1024; - writableStore = true; }; cardanoNix.cardano-cli.enable = true; }; }; - # TODO `git` will be replaced by `cardano-cli` (milestone 2) testScript = '' - machine.succeed("git --version") + machine.succeed("cardano-cli --version") ''; }; }; diff --git a/tests/cardano-node.nix b/tests/cardano-node.nix new file mode 100644 index 0000000..4212d00 --- /dev/null +++ b/tests/cardano-node.nix @@ -0,0 +1,34 @@ +{ + cardanoNix.tests = { + cardano-node = { + systems = ["x86_64-linux"]; + + module = { + name = "cardano-node-test"; + + nodes = { + machine = {pkgs, ...}: { + virtualisation = { + cores = 2; + memorySize = 1024; + }; + cardanoNix = { + globals.network = "preview"; + cardano-cli.enable = true; + cardano-node = { + enable = true; + }; + }; + + environment.systemPackages = with pkgs; [jq bc]; + }; + }; + + testScript = '' + machine.wait_for_unit("cardano-node") + machine.wait_until_succeeds("""[[ $(echo "$(cardano-cli query tip --testnet-magic 2 | jq '.syncProgress' --raw-output) > 1" | bc) == "1" ]]""") + ''; + }; + }; + }; +} diff --git a/tests/default.nix b/tests/default.nix index 837ced2..b1e0354 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -2,6 +2,7 @@ perSystem = _: { imports = [ ./cardano-cli.nix + ./cardano-node.nix ]; }; } From 83d8f74af896a5530aa28c4c4ae8fe33197554dc Mon Sep 17 00:00:00 2001 From: Andrea Ciceri Date: Fri, 8 Mar 2024 20:28:09 +0100 Subject: [PATCH 2/5] fix: exclude `cardano-overlay` from autogenerated docs --- docs/default.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/default.nix b/docs/default.nix index ace7303..529abeb 100644 --- a/docs/default.nix +++ b/docs/default.nix @@ -32,7 +32,7 @@ chmod +x $out/bin/mkdocs ''; - eachOptions = removeAttrs rootConfig.flake.nixosModules ["default"]; + eachOptions = removeAttrs rootConfig.flake.nixosModules ["default" "cardano-overlay"]; eachOptionsDoc = lib.mapAttrs' ( From f35cc13e499ac03a880c3c81d20e768425d8d256 Mon Sep 17 00:00:00 2001 From: Andrea Ciceri Date: Fri, 8 Mar 2024 20:37:55 +0100 Subject: [PATCH 3/5] fix: do not add impure tests to flake checks --- checks/testing.nix | 6 +++++- tests/cardano-node.nix | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/checks/testing.nix b/checks/testing.nix index baca1c7..86d803f 100644 --- a/checks/testing.nix +++ b/checks/testing.nix @@ -39,6 +39,10 @@ in { type = types.attrsOf types.anything; default = {}; }; + impure = mkOption { + type = types.bool; + default = false; + }; }; })); }; @@ -86,7 +90,7 @@ in { mapAttrs' (name: test: nameValuePair "testing-${test.name}" (cfg._mkCheckFromTest test)) (lib.filterAttrs - (_: v: lib.elem system v.systems) + (_: v: lib.elem system v.systems && !v.impure) cfg.tests); apps.run-test.program = lib.getExe cfg.runTestScript; diff --git a/tests/cardano-node.nix b/tests/cardano-node.nix index 4212d00..fb4b5c5 100644 --- a/tests/cardano-node.nix +++ b/tests/cardano-node.nix @@ -2,6 +2,7 @@ cardanoNix.tests = { cardano-node = { systems = ["x86_64-linux"]; + impure = true; module = { name = "cardano-node-test"; From c0d02bc9638ac4657783c09bfdbd0c37020ad07c Mon Sep 17 00:00:00 2001 From: Andrea Ciceri Date: Fri, 8 Mar 2024 20:54:38 +0100 Subject: [PATCH 4/5] feat: run impure tests as Hercules CI effects --- ci/default.nix | 1 + ci/impure-tests.nix | 19 +++++++++++++++++++ tests/cardano-node.nix | 4 ++-- 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 ci/impure-tests.nix diff --git a/ci/default.nix b/ci/default.nix index 95bb972..da9211c 100644 --- a/ci/default.nix +++ b/ci/default.nix @@ -7,6 +7,7 @@ imports = [ inputs.hercules-ci-effects.flakeModule "${inputs.hercules-ci-effects}/effects/push-cache/default.nix" + ./impure-tests.nix ]; config = { hercules-ci.github-pages.branch = "master"; diff --git a/ci/impure-tests.nix b/ci/impure-tests.nix new file mode 100644 index 0000000..1f2c934 --- /dev/null +++ b/ci/impure-tests.nix @@ -0,0 +1,19 @@ +{ + withSystem, + config, + ... +}: { + # TODO map over all tests and add impure oens as effects + + herculesCI.onPush.default.outputs.effects.testing-cardano-node = withSystem config.defaultEffectSystem ({ + hci-effects, + config, + ... + }: + hci-effects.modularEffect { + extraAttributes.__hci_effect_mounts."/dev/kvm" = "kvm"; + effectScript = '' + ${(config.cardanoNix._mkCheckFromTest config.cardanoNix.tests.cardano-node).driver}/bin/nixos-test-driver + ''; + }); +} diff --git a/tests/cardano-node.nix b/tests/cardano-node.nix index fb4b5c5..27b001a 100644 --- a/tests/cardano-node.nix +++ b/tests/cardano-node.nix @@ -10,7 +10,7 @@ nodes = { machine = {pkgs, ...}: { virtualisation = { - cores = 2; + cores = 1; memorySize = 1024; }; cardanoNix = { @@ -27,7 +27,7 @@ testScript = '' machine.wait_for_unit("cardano-node") - machine.wait_until_succeeds("""[[ $(echo "$(cardano-cli query tip --testnet-magic 2 | jq '.syncProgress' --raw-output) > 1" | bc) == "1" ]]""") + machine.wait_until_succeeds("""[[ $(echo "$(cardano-cli query tip --testnet-magic 2 | jq '.syncProgress' --raw-output) > 0.01" | bc) == "1" ]]""") ''; }; }; From 6e3d03a8e00bb74c4ed2da644c9bfada14eeb2cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Boros?= Date: Tue, 12 Mar 2024 15:26:40 +0000 Subject: [PATCH 5/5] refactor tests --- checks/default.nix | 2 +- checks/{testing.nix => nixosTests.nix} | 45 +++++++++++++++++++++--- ci/default.nix | 1 - ci/impure-tests.nix | 19 ---------- flake.lock | 18 +++++----- tests/cardano-cli.nix | 30 +++++++--------- tests/cardano-node.nix | 48 ++++++++++++-------------- 7 files changed, 85 insertions(+), 78 deletions(-) rename checks/{testing.nix => nixosTests.nix} (57%) delete mode 100644 ci/impure-tests.nix diff --git a/checks/default.nix b/checks/default.nix index 0e3bebb..bec963f 100644 --- a/checks/default.nix +++ b/checks/default.nix @@ -1,6 +1,6 @@ {inputs, ...}: { imports = [ - ./testing.nix + ./nixosTests.nix ./licenses.nix ]; perSystem = { diff --git a/checks/testing.nix b/checks/nixosTests.nix similarity index 57% rename from checks/testing.nix rename to checks/nixosTests.nix index 86d803f..bbaa9b2 100644 --- a/checks/testing.nix +++ b/checks/nixosTests.nix @@ -2,6 +2,7 @@ lib, inputs, config, + withSystem, ... }: let inherit (lib) mkOption types mapAttrs' nameValuePair; @@ -14,35 +15,53 @@ in { pkgs, ... }: let - cfg = config.cardanoNix; + cfg = config.nixosTests; in { - options.cardanoNix = { + options.nixosTests = { tests = mkOption { + description = "NixOS tests as modules."; type = types.lazyAttrsOf (types.submodule ({config, ...}: { options = { name = mkOption { + description = "The name of the test."; type = types.str; default = config._module.args.name; internal = true; }; systems = mkOption { + description = "The systems to run the tests on."; type = types.listOf types.str; + default = ["x86_64-linux"]; }; module = mkOption { + description = "The test module. Required."; type = types.deferredModule; }; documentation = mkOption { + description = "Wether to generate documentation for the testnixos configuraion. False by default to speed up builds."; type = types.bool; default = false; }; specialArgs = mkOption { + description = "The specialArgs to pass to the test node."; type = types.attrsOf types.anything; default = {}; }; impure = mkOption { + description = "Wether the test requires internet access and should be run as an effect instead of a nix build."; type = types.bool; default = false; }; + check = mkOption { + description = "The test derivation composed with _mkCheckFromTest from the module."; + type = types.package; + default = cfg._mkCheckFromTest config; + }; + checkEffect = mkOption { + description = "The test hercules-ci-effect composed with _mkEffectFromTest from the module."; + type = types.package; + default = cfg._mkEffectFromTest config; + }; }; })); }; @@ -63,7 +82,6 @@ in { (cfg._nixosLib.runTest { hostPkgs = pkgs; - # false by default, it speeds up evaluation by skipping docs generation defaults.documentation.enable = test.documentation; node = { @@ -83,12 +101,24 @@ in { .config .result; }; + _mkEffectFromTest = mkOption { + type = types.functionTo types.package; + internal = true; + default = test: + withSystem system ({hci-effects, ...}: + hci-effects.modularEffect { + mounts."/dev/kvm" = "kvm"; + effectScript = '' + ${test.check.driver}/bin/nixos-test-driver + ''; + }); + }; }; config = { checks = mapAttrs' - (name: test: nameValuePair "testing-${test.name}" (cfg._mkCheckFromTest test)) + (name: test: nameValuePair "nixosTests-${test.name}" test.check) (lib.filterAttrs (_: v: lib.elem system v.systems && !v.impure) cfg.tests); @@ -105,4 +135,11 @@ in { ]; }; }; + + herculesCI.onPush.default.outputs.effects = + mapAttrs' + (name: test: nameValuePair "nixosTests-${test.name}" test.checkEffect) + (lib.filterAttrs + (_: v: lib.elem config.defaultEffectSystem v.systems && v.impure) + (config.perSystem config.defaultEffectSystem).nixosTests.tests); } diff --git a/ci/default.nix b/ci/default.nix index da9211c..95bb972 100644 --- a/ci/default.nix +++ b/ci/default.nix @@ -7,7 +7,6 @@ imports = [ inputs.hercules-ci-effects.flakeModule "${inputs.hercules-ci-effects}/effects/push-cache/default.nix" - ./impure-tests.nix ]; config = { hercules-ci.github-pages.branch = "master"; diff --git a/ci/impure-tests.nix b/ci/impure-tests.nix deleted file mode 100644 index 1f2c934..0000000 --- a/ci/impure-tests.nix +++ /dev/null @@ -1,19 +0,0 @@ -{ - withSystem, - config, - ... -}: { - # TODO map over all tests and add impure oens as effects - - herculesCI.onPush.default.outputs.effects.testing-cardano-node = withSystem config.defaultEffectSystem ({ - hci-effects, - config, - ... - }: - hci-effects.modularEffect { - extraAttributes.__hci_effect_mounts."/dev/kvm" = "kvm"; - effectScript = '' - ${(config.cardanoNix._mkCheckFromTest config.cardanoNix.tests.cardano-node).driver}/bin/nixos-test-driver - ''; - }); -} diff --git a/flake.lock b/flake.lock index aee7f12..9c99974 100644 --- a/flake.lock +++ b/flake.lock @@ -650,11 +650,11 @@ ] }, "locked": { - "lastModified": 1701473968, - "narHash": "sha256-YcVE5emp1qQ8ieHUnxt1wCZCC3ZfAS+SRRWZ2TMda7E=", + "lastModified": 1709336216, + "narHash": "sha256-Dt/wOWeW6Sqm11Yh+2+t0dfEWxoMxGBvv3JpIocFl9E=", "owner": "hercules-ci", "repo": "flake-parts", - "rev": "34fed993f1674c8d06d58b37ce1e0fe5eebcb9f5", + "rev": "f7b3c975cf067e56e7cda6cb098ebe3fb4d74ca2", "type": "github" }, "original": { @@ -986,11 +986,11 @@ "nixpkgs": "nixpkgs_10" }, "locked": { - "lastModified": 1707187737, - "narHash": "sha256-1vdTyh8dclFK/fVoxFnJmzQis370IteOKERRExn9wXU=", + "lastModified": 1710396488, + "narHash": "sha256-yniBB5i1un44uzR4+luTWvZ6uGvsHSYIBiDZ8Xox4nQ=", "owner": "mlabs-haskell", "repo": "hercules-ci-effects", - "rev": "832a4a30d646bf0a6fbbd7a0d88aa3f748584af8", + "rev": "f5ed263ab0585dfb7b067301419fb80d64e8c021", "type": "github" }, "original": { @@ -1634,11 +1634,11 @@ }, "nixpkgs_10": { "locked": { - "lastModified": 1703637592, - "narHash": "sha256-8MXjxU0RfFfzl57Zy3OfXCITS0qWDNLzlBAdwxGZwfY=", + "lastModified": 1709961763, + "narHash": "sha256-6H95HGJHhEZtyYA3rIQpvamMKAGoa8Yh2rFV29QnuGw=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "cfc3698c31b1fb9cdcf10f36c9643460264d0ca8", + "rev": "3030f185ba6a4bf4f18b87f345f104e6a6961f34", "type": "github" }, "original": { diff --git a/tests/cardano-cli.nix b/tests/cardano-cli.nix index 7bbdaa6..10ee63d 100644 --- a/tests/cardano-cli.nix +++ b/tests/cardano-cli.nix @@ -1,25 +1,19 @@ { - cardanoNix.tests = { - cardano-cli = { - systems = ["x86_64-linux"]; + nixosTests.tests.cardano-cli.module = { + name = "cardano-cli-test"; - module = { - name = "cardano-cli-test"; - - nodes = { - machine = { - virtualisation = { - cores = 2; - memorySize = 1024; - }; - cardanoNix.cardano-cli.enable = true; - }; + nodes = { + machine = { + virtualisation = { + cores = 2; + memorySize = 1024; }; - - testScript = '' - machine.succeed("cardano-cli --version") - ''; + cardanoNix.cardano-cli.enable = true; }; }; + + testScript = '' + machine.succeed("cardano-cli --version") + ''; }; } diff --git a/tests/cardano-node.nix b/tests/cardano-node.nix index 27b001a..f675268 100644 --- a/tests/cardano-node.nix +++ b/tests/cardano-node.nix @@ -1,35 +1,31 @@ { - cardanoNix.tests = { - cardano-node = { - systems = ["x86_64-linux"]; - impure = true; + nixosTests.tests.cardano-node = { + impure = true; + module = { + name = "cardano-node-test"; - module = { - name = "cardano-node-test"; - - nodes = { - machine = {pkgs, ...}: { - virtualisation = { - cores = 1; - memorySize = 1024; - }; - cardanoNix = { - globals.network = "preview"; - cardano-cli.enable = true; - cardano-node = { - enable = true; - }; + nodes = { + machine = {pkgs, ...}: { + virtualisation = { + cores = 1; + memorySize = 1024; + }; + cardanoNix = { + globals.network = "preview"; + cardano-cli.enable = true; + cardano-node = { + enable = true; }; - - environment.systemPackages = with pkgs; [jq bc]; }; - }; - testScript = '' - machine.wait_for_unit("cardano-node") - machine.wait_until_succeeds("""[[ $(echo "$(cardano-cli query tip --testnet-magic 2 | jq '.syncProgress' --raw-output) > 0.01" | bc) == "1" ]]""") - ''; + environment.systemPackages = with pkgs; [jq bc]; + }; }; + + testScript = '' + machine.wait_for_unit("cardano-node") + machine.wait_until_succeeds("""[[ $(echo "$(cardano-cli query tip --testnet-magic 2 | jq '.syncProgress' --raw-output) > 0.01" | bc) == "1" ]]""") + ''; }; }; }