diff --git a/test/default.nix b/test/default.nix new file mode 100644 index 0000000..72dbdaa --- /dev/null +++ b/test/default.nix @@ -0,0 +1,3 @@ +{ system, inputs, self, pkgs, flake }: + (import ./package { inherit system inputs self pkgs; }) + diff --git a/test/hardware/default.nix b/test/hardware/default.nix new file mode 100644 index 0000000..e69de29 diff --git a/test/hardware/lenovo-ideapad-15arh7.nix b/test/hardware/lenovo-ideapad-15arh7.nix new file mode 100644 index 0000000..330e119 --- /dev/null +++ b/test/hardware/lenovo-ideapad-15arh7.nix @@ -0,0 +1,17 @@ +{ self, inputs, system, ... }: let + mkSys = system: opts: + (inputs.nixpkgs.lib.nixosSystem { + inherit system; + modules = [ + self.nixosModules.hectic + { hectic.hardware.lenovo-ideapad-15arh7 = opts; } + ]; + }); + + cases = { + #enable = { enable = true; }; + #disabled = { enable = false; }; + #customFoo = { enable = true; foo = "bar"; }; + }; +in inputs.nixpkgs.lib.mapAttrs + (name: opts: (mkSys system opts).config.system.build.toplevel) cases diff --git a/test/package/default.nix b/test/package/default.nix new file mode 100644 index 0000000..0436afa --- /dev/null +++ b/test/package/default.nix @@ -0,0 +1 @@ +{ system, inputs, self, pkgs }: (import ./migrator { inherit system inputs self pkgs; }) diff --git a/test/package/migrator/default.nix b/test/package/migrator/default.nix new file mode 100644 index 0000000..18908c2 --- /dev/null +++ b/test/package/migrator/default.nix @@ -0,0 +1,44 @@ +{ inputs, self, pkgs, system, ... }: let + lib = inputs.nixpkgs.lib; + + # turn anything under ./test into a derivation that exposes $out/run.sh + mkTestDrv = name: type: + if type == "directory" then + pkgs.runCommand "test-${name}" {} '' + if ! [ -f ${./test + "/${name}" + /run.sh} ]; then + echo no run.sh in test/${name} + exit 1 + fi + + mkdir -p "$out" + cp -r ${./test + "/${name}"}/* "$out/" + chmod +x "$out/run.sh" + '' + else if lib.hasSuffix ".sh" name then + pkgs.runCommand "test-${lib.removeSuffix ".sh" name}" {} '' + mkdir -p "$out" + install -Dm755 ${./test + "/${name}"} "$out/run.sh" + '' + else + null; + + testDir = builtins.readDir ./test; + + # attrset: testName -> drv with run.sh + testDrvs = + lib.mapAttrs' (n: v: + lib.nameValuePair (lib.removeSuffix ".sh" n) v + ) (lib.filterAttrs (_: v: v != null) + (lib.mapAttrs (n: t: mkTestDrv n t) testDir)); + + migrator = self.packages.${system}.migrator; + mkPgTest = testName: testDrv: pkgs.runCommand "migrator-test-${testName}" + { + nativeBuildInputs = [ pkgs.coreutils pkgs.gnugrep pkgs.gnused ]; + buildInputs = [ migrator pkgs.postgresql ]; + } '' + ${builtins.readFile self.legacyPackages.${system}.helpers.posix-shell.log} + test=${testDrv} + ${builtins.readFile ./lauch.sh} + ''; +in builtins.trace testDir (lib.mapAttrs (name: drv: mkPgTest name drv) testDrvs) diff --git a/test/package/migrator/lauch.sh b/test/package/migrator/lauch.sh new file mode 100644 index 0000000..d187e3e --- /dev/null +++ b/test/package/migrator/lauch.sh @@ -0,0 +1,68 @@ +#!/bin/dash + +# $out - nix derivation output +# $test - test and assertion file + +test_derivation="$(basename "$test")" +test_name="${test_derivation#*-*-}" + +set -eu + +root_dir="$(dirname $0)" + +HECTIC_LOG= + +# save path to pg_ctl in case $PATH will change +PG_CTL="$(command -v pg_ctl)" +cleanup() { + "$PG_CTL" -D "$data" -m fast -w stop +} + +log info 'start test pipeline' + +# temp dirs +wd="$PWD" +data="$wd/data" +sockdir="$wd/sock" +db="testdb" +mkdir -p "$data" "$sockdir" + +# initdb +initdb -D "$data" --no-locale -E UTF8 >/dev/null + +# trust local auth for the test +{ + echo "unix_socket_directories = '$sockdir'" + echo "listen_addresses = ''" +} >> "$data/postgresql.conf" +sed -i "1ilocal all all trust" "$data/pg_hba.conf" + +# start cluster +pg_ctl -D "$data" -o "-F" -w start +trap cleanup EXIT + +user="$(id -un)" + +# bootstrap DB +createdb -h "$sockdir" -U "$user" "$db" + +psql -h "$sockdir" -d testdb -v ON_ERROR_STOP=1 -c 'select 1;' >/dev/null + +export PGHOST="$sockdir" +export PGPORT=5432 +export PGUSER="$user" +export PGDATABASE="$db" + +DATABASE_URL="postgresql://${PGUSER}@/${PGDATABASE}?host=${PGHOST}&port=${PGPORT}" +export DATABASE_URL + +log info "run test ${WHITE}${test_name}${NC}" + +# run test +. "${test}/run.sh" + +log info "finish test pipeline" + +# success marker for Nix +# shellcheck disable=SC2154 +mkdir -p "$out" diff --git a/test/package/migrator/test/mising-psql.sh b/test/package/migrator/test/mising-psql.sh new file mode 100644 index 0000000..aecf016 --- /dev/null +++ b/test/package/migrator/test/mising-psql.sh @@ -0,0 +1,21 @@ +# remove psql from $PATH +dir=$(dirname -- "$(command -v psql)") +PATH=$(printf '%s' "$PATH" | awk -v RS=: -v ORS=: -v d="$dir" '$0!=d{print}') +PATH=${PATH%:} +# clear lookup cache +hash -r 2>/dev/null || true + +# temporary disable break on errors, coz in this test we check error-handling scenario +set +e +# try to run migrator without installed psql +migrator 2>/dev/null +migrator_error_code=$? +set -e + +log debug "migrator error code: $migrator_error_code" + +if [ "$migrator_error_code" -eq 127 ]; then + log notice "test passed" +else + log error "test failed" +fi diff --git a/test/package/migrator/test/placeholder b/test/package/migrator/test/placeholder new file mode 100644 index 0000000..e69de29