From d2b3cba75e3220b4e08de04bb67593cfbb9be12c Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Giraudeau Date: Tue, 16 Jun 2020 22:30:43 +0200 Subject: [PATCH] Add nixos service and nixos test. --- default.nix | 4 +++ nix/nixos/smash-service.nix | 53 +++++++++++++++++++++++++++++ nix/nixos/tests/default.nix | 20 +++++++++++ nix/nixos/tests/smash-test.nix | 61 ++++++++++++++++++++++++++++++++++ nix/sources.json | 24 +++++++++---- release.nix | 3 ++ 6 files changed, 159 insertions(+), 6 deletions(-) create mode 100644 nix/nixos/smash-service.nix create mode 100644 nix/nixos/tests/default.nix create mode 100644 nix/nixos/tests/smash-test.nix diff --git a/default.nix b/default.nix index b418d75..c75bd10 100644 --- a/default.nix +++ b/default.nix @@ -35,6 +35,10 @@ let tests = collectChecks haskellPackages; }; + nixosTests = import ./nix/nixos/tests { + inherit pkgs; + }; + shell = import ./shell.nix { inherit pkgs; withHoogle = true; diff --git a/nix/nixos/smash-service.nix b/nix/nixos/smash-service.nix new file mode 100644 index 0000000..3417871 --- /dev/null +++ b/nix/nixos/smash-service.nix @@ -0,0 +1,53 @@ +{ config, lib, pkgs, ... }: + +let + cfg = config.services.smash; +in { + + options = { + services.smash = { + enable = lib.mkEnableOption "enable the smash server"; + script = lib.mkOption { + internal = true; + type = lib.types.package; + }; + package = lib.mkOption { + type = lib.types.package; + default = (import ../. {}).smashHaskellPackages.smash.components.exes.smash-exe; + }; + }; + }; + config = lib.mkIf cfg.enable { + services.smash.script = pkgs.writeShellScript "smash" '' + set -euo pipefail + + cp ${config.services.cardano-db-sync.pgpass} $RUNTIME_DIRECTORY/pgpass + chmod 0600 $RUNTIME_DIRECTORY/pgpass + export PGPASSFILE=$RUNTIME_DIRECTORY/pgpass + + ${cfg.package}/bin/smash-exe run-migrations --mdir ${../../schema} + exec ${cfg.package}/bin/smash-exe run-app + ''; + environment.systemPackages = [ cfg.package config.services.postgresql.package ]; + systemd.services.smash = { + path = [ cfg.package config.services.postgresql.package pkgs.netcat ]; + preStart = '' + for x in {1..10}; do + nc -z localhost ${toString config.services.cardano-db-sync.postgres.port} && break + echo loop $x: waiting for postgresql 2 sec... + sleep 2 + done + sleep 1 + ''; + serviceConfig = { + ExecStart = config.services.smash.script; + DynamicUser = true; + RuntimeDirectory = "smash"; + }; + + wantedBy = [ "multi-user.target" ]; + after = [ "postgres.service" "cardano-db-sync.service" ]; + requires = [ "postgresql.service" ]; + }; + }; +} diff --git a/nix/nixos/tests/default.nix b/nix/nixos/tests/default.nix new file mode 100644 index 0000000..4dc9b93 --- /dev/null +++ b/nix/nixos/tests/default.nix @@ -0,0 +1,20 @@ +{ pkgs +, supportedSystems ? [ "x86_64-linux" ] +}: + +with pkgs; +with pkgs.commonLib; + + let + forAllSystems = genAttrs supportedSystems; + importTest = fn: args: system: let + imported = import fn; + test = import (pkgs.path + "/nixos/tests/make-test.nix") imported; + in test ({ + inherit pkgs system config; + } // args); + callTest = fn: args: forAllSystems (system: hydraJob (importTest fn args system)); +in rec { + + smashTest = callTest ./smash-test.nix {}; +} diff --git a/nix/nixos/tests/smash-test.nix b/nix/nixos/tests/smash-test.nix new file mode 100644 index 0000000..c2e574b --- /dev/null +++ b/nix/nixos/tests/smash-test.nix @@ -0,0 +1,61 @@ +{ pkgs, ... }: +with pkgs; with commonLib; +{ + name = "smash-test"; + nodes = { + machine = { config, ... }: { + nixpkgs.pkgs = pkgs; + environment = { + systemPackages = with pkgs; [ curl jq ]; + variables = { + PGPASSFILE = config.services.cardano-db-sync.pgpass; + }; + }; + imports = [ + ../smash-service.nix + (sources.cardano-db-sync + "/nix/nixos") + ]; + services.cardano-db-sync = { + enable = true; + cluster = "ff"; + }; + services.smash = { + enable = true; + package = smashHaskellPackages.smash.components.exes.smash-exe; + }; + services.postgresql = { + enable = true; + package = postgresql_12; + enableTCPIP = false; + ensureDatabases = [ "${config.services.cardano-db-sync.user}" ]; + ensureUsers = [ + { + name = "${config.services.cardano-db-sync.user}"; + ensurePermissions = { + "DATABASE ${config.services.cardano-db-sync.user}" = "ALL PRIVILEGES"; + }; + } + ]; + identMap = '' + cdbsync-users root ${config.services.cardano-db-sync.user} + cdbsync-users smash ${config.services.cardano-db-sync.user} + cdbsync-users ${config.services.cardano-db-sync.user} ${config.services.cardano-db-sync.user} + cdbsync-users postgres postgres + ''; + authentication = '' + local all all ident map=cdbsync-users + ''; + }; + }; + }; + testScript = '' + startAll + $machine->waitForUnit("postgresql.service"); + $machine->waitForUnit("smash.service"); + $machine->waitForOpenPort(3100); + $machine->succeed("smash-exe insert-pool --filepath ${../../../test_pool.json} --poolhash \"cbdfc4f21feb0a414b2b9471fa56b0ebd312825e63db776d68cc3fa0ca1f5a2f\""); + $machine->succeed("curl -s -i -H \"Accept: application/json\" http://localhost:3100/api/v1/metadata/cbdfc4f21feb0a414b2b9471fa56b0ebd312825e63db776d68cc3fa0ca1f5a2f | systemd-cat --identifier=curl-smash"); + $machine->succeed("curl -s -H \"Accept: application/json\" http://localhost:3100/api/v1/metadata/cbdfc4f21feb0a414b2b9471fa56b0ebd312825e63db776d68cc3fa0ca1f5a2f | jq | systemd-cat --identifier=jq-smash"); + ''; + +} diff --git a/nix/sources.json b/nix/sources.json index 5a90883..c2af853 100644 --- a/nix/sources.json +++ b/nix/sources.json @@ -1,14 +1,26 @@ { + "cardano-db-sync": { + "branch": "master", + "description": "A component that follows the Cardano chain and stores blocks and transactions in PostgreSQL", + "homepage": "", + "owner": "input-output-hk", + "repo": "cardano-db-sync", + "rev": "6d2917212848a7f70e7e84336b9291dd2634ea61", + "sha256": "0bi6v6b9c2i2d3h416lpn0p2qy8pd08w0g0zjjsnldr2sb7ph3s9", + "type": "tarball", + "url": "https://github.com/input-output-hk/cardano-db-sync/archive/6d2917212848a7f70e7e84336b9291dd2634ea61.tar.gz", + "url_template": "https://github.com///archive/.tar.gz" + }, "haskell.nix": { "branch": "master", "description": "Alternative Haskell Infrastructure for Nixpkgs", "homepage": "https://input-output-hk.github.io/haskell.nix", "owner": "input-output-hk", "repo": "haskell.nix", - "rev": "c474dcec4a435698cd7978ef25e6fea25ff6795b", - "sha256": "149n40p56njpzibbkxlqj7rv8f2yvgfqni9m6xr8rvl5yry1lxd0", + "rev": "36836c1f580a021e99e7ad3ad23f4257e7ffda2b", + "sha256": "0jsg3lairnfn9i74dj6r88n53y5ca724wv9lvak67cbhmxgj35df", "type": "tarball", - "url": "https://github.com/input-output-hk/haskell.nix/archive/c474dcec4a435698cd7978ef25e6fea25ff6795b.tar.gz", + "url": "https://github.com/input-output-hk/haskell.nix/archive/36836c1f580a021e99e7ad3ad23f4257e7ffda2b.tar.gz", "url_template": "https://github.com///archive/.tar.gz" }, "iohk-nix": { @@ -17,10 +29,10 @@ "homepage": null, "owner": "input-output-hk", "repo": "iohk-nix", - "rev": "8b1d65ba294708b12d7b15103ac35431d9b60819", - "sha256": "1z23lw28s3wa5bf5yr89i61m413ad299lyhv02i9r36p28wjl94g", + "rev": "a3eae81cbae000bbfdc2bc1880528512747a3a9c", + "sha256": "1jairn2vp43z36j0b1fi2nmnfpadff217dqv5bjjxm5v099g0gsw", "type": "tarball", - "url": "https://github.com/input-output-hk/iohk-nix/archive/8b1d65ba294708b12d7b15103ac35431d9b60819.tar.gz", + "url": "https://github.com/input-output-hk/iohk-nix/archive/a3eae81cbae000bbfdc2bc1880528512747a3a9c.tar.gz", "url_template": "https://github.com///archive/.tar.gz" } } diff --git a/release.nix b/release.nix index b0a736e..b330d51 100644 --- a/release.nix +++ b/release.nix @@ -57,10 +57,13 @@ let jobs = { native = mapTestOn (__trace (__toJSON (packagePlatforms project)) (packagePlatforms project)); + # only build nixos tests on first supported system (linux) + inherit (pkgsFor (builtins.head supportedSystems)) nixosTests; } // (mkRequiredJob (concatLists [ (collectJobs jobs.native.checks) (collectJobs jobs.native.libs) (collectJobs jobs.native.exes) + (collectJobs jobs.nixosTests.smashTest) ])); in jobs