flake reorg, nix integration testing

This commit is contained in:
Robin Appelman 2024-12-24 16:25:26 +01:00
commit 67465fc22a
14 changed files with 1558 additions and 689 deletions

27
nix/demostf-api.nix Normal file
View file

@ -0,0 +1,27 @@
{
php,
fetchFromGitHub,
}: let
phpWithExtensions = php.withExtensions ({
enabled,
all,
}:
enabled ++ (with all; [pdo apcu]));
in
phpWithExtensions.buildComposerProject (finalAttrs: {
pname = "demostf-api";
version = "0.1.0";
src = fetchFromGitHub {
owner = "demostf";
repo = "api";
rev = "1a8380360b993226ae1c6fcc226011e03a6c1467";
hash = "sha256-JcBRU1N44tt0QDLnj6z9MCT3V2s2dkf+JbpWb1rmXnY=";
};
vendorHash = "sha256-EYWCR2aJAoyWvEX+SML4Fb3F3KGcUtwCgqhAGT6ZjZ4=";
composerStrictValidation = false;
doCheck = false;
})

23
nix/demostf-parser.nix Normal file
View file

@ -0,0 +1,23 @@
{
rustPlatform,
fetchFromGitHub,
}:
rustPlatform.buildRustPackage {
pname = "demostf-parser";
version = "0.5.1";
src = fetchFromGitHub {
owner = "demostf";
repo = "parser";
rev = "0cd87a8a40e2a6af637d831b272c2758cebd2f9c";
hash = "sha256-bKcc0hWTkdYUDMI/DjUh45abuBeQEvkn6TsuAz02H5Y=";
};
cargoBuildFlags = ''
--bin parse_demo
'';
doCheck = false;
cargoHash = "sha256-/Fnw6l2fznrBK780E4q1PKFOkT0eiL+dE+UuhFA+V9M=";
}

5
nix/overlay.nix Normal file
View file

@ -0,0 +1,5 @@
prev: final: {
demostf-api = final.callPackage ./demostf-api.nix {};
demostf-parser = final.callPackage ./demostf-parser.nix {};
demostf-api-test = final.callPackage ./package.nix {};
}

28
nix/package.nix Normal file
View file

@ -0,0 +1,28 @@
{
rustPlatform,
lib,
pkg-config,
openssl,
}: let
inherit (lib.sources) sourceByRegex;
inherit (builtins) fromTOML readFile;
src = sourceByRegex ../. ["Cargo.*" "(src|data)(/.*)?"];
cargoPackage = (fromTOML (readFile ../Cargo.toml)).package;
in
rustPlatform.buildRustPackage {
pname = cargoPackage.name;
inherit (cargoPackage) version;
inherit src;
buildInputs = [openssl];
nativeBuildInputs = [pkg-config];
doCheck = false;
cargoLock = {
lockFile = ../Cargo.lock;
};
meta.mainProgram = "api-test";
}

126
nix/test.nix Normal file
View file

@ -0,0 +1,126 @@
{
pkgs,
lib,
...
}: {
name = "demostf-api-client-test";
nodes.machine = {config, ...}: let
fpmCfg = config.services.phpfpm.pools.demostf-api;
in {
config = {
users.groups.demostf = {};
users.users.demostf = {
group = "demostf";
isSystemUser = true;
};
services.postgresql = {
enable = true;
ensureDatabases = ["demostf"];
ensureUsers = [
{
name = "demostf";
ensureDBOwnership = true;
}
];
initialScript = pkgs.writeText "init-sql-script" ''
CREATE EXTENSION IF NOT EXISTS pg_trgm WITH SCHEMA public;
'';
};
services.nginx = {
enable = true;
virtualHosts."localhost" = {
root = "/var/empty";
extraConfig = ''
try_files $uri /index.php?$query_string /index.php;
'';
locations = {
"~ ^(.+?\\.php)(/.*)?$" = {
extraConfig = ''
fastcgi_param PATH_INFO $2;
fastcgi_pass unix:${fpmCfg.socket};
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME ${pkgs.demostf-api}/share/php/demostf-api/src/public/index.php;
include ${pkgs.nginx}/conf/fastcgi_params;
client_max_body_size 250m;
'';
};
"= /upload" = {
extraConfig = ''
fastcgi_pass unix:${fpmCfg.socket};
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME ${pkgs.demostf-api}/share/php/demostf-api/src/public/upload.php;
include ${pkgs.nginx}/conf/fastcgi_params;
client_max_body_size 250m;
'';
};
"/static/" = {
alias = "/demos/";
};
};
};
};
services.phpfpm.pools.demostf-api = {
phpPackage = pkgs.php.buildEnv {
extensions = {
enabled,
all,
}:
enabled ++ (with all; [pdo apcu]);
extraConfig = ''
post_max_size = 150M
upload_max_filesize = 150M
'';
};
settings = {
"clear_env" = "no";
"pm" = "dynamic";
"pm.max_children" = "25";
"pm.start_servers" = "5";
"pm.min_spare_servers" = "5";
"pm.max_spare_servers" = "15";
"catch_workers_output" = "yes";
"listen.owner" = "nginx";
"listen.group" = "nginx";
};
phpEnv = {
BASE_HOST = "demos.tf";
DEMO_ROOT = "/demos";
DEMO_HOST = "localhost";
DB_TYPE = "pgsql";
DB_HOST = "/run/postgresql";
DB_PORT = "5432";
DB_DATABASE = "demostf";
DB_USERNAME = "demostf";
APP_ROOT = "http://localhost";
EDIT_SECRET = "edit";
PARSER_PATH = "${pkgs.demostf-parser}/bin/parse_demo";
};
user = "demostf";
group = "demostf";
};
};
};
testScript = let
testBinary = lib.getExe pkgs.demostf-api-test;
initSql = pkgs.fetchurl {
url = "https://github.com/demostf/db/raw/refs/heads/master/schema.sql";
hash = "sha256-AwXN9mh9CRk6HWdvyUR+YdBkpmExNIDOIeDMz6XqjEQ=";
};
in ''
machine.succeed("mkdir /demos && chmod 0777 /demos");
machine.wait_for_unit("postgresql")
machine.succeed("sudo -u demostf psql demostf demostf < ${initSql}");
machine.succeed("sudo -u postgres psql postgres postgres -c \"alter user demostf with password 'test';\"");
machine.wait_for_unit("phpfpm-demostf-api")
machine.wait_for_unit("nginx")
machine.wait_until_succeeds("curl http://127.0.0.1", timeout=45)
machine.succeed("DB_URL='postgres://demostf:test@localhost/demostf'\
BASE_URL='http://localhost/'\
EDIT_KEY='edit'\
${testBinary}", timeout=180)
'';
}