From 879c55ef8f407dce0717e7be0126bdd9a41eea53 Mon Sep 17 00:00:00 2001 From: Luflosi Date: Tue, 17 Dec 2024 23:59:42 +0100 Subject: [PATCH] nixos/tests/nik4: init --- nixos/tests/all-tests.nix | 1 + nixos/tests/nik4.nix | 69 +++++++++++++++++++ pkgs/by-name/ni/nik4/package.nix | 5 ++ .../op/openstreetmap-carto/package.nix | 2 + 4 files changed, 77 insertions(+) create mode 100644 nixos/tests/nik4.nix diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index 622d43d4080bda..9f0cb278d585b6 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -667,6 +667,7 @@ in { nix-channel = pkgs.callPackage ../modules/config/nix-channel/test.nix { }; nebula = handleTest ./nebula.nix {}; netbird = handleTest ./netbird.nix {}; + nik4 = runTest ./nik4.nix; nimdow = handleTest ./nimdow.nix {}; neo4j = handleTest ./neo4j.nix {}; netdata = handleTest ./netdata.nix {}; diff --git a/nixos/tests/nik4.nix b/nixos/tests/nik4.nix new file mode 100644 index 00000000000000..42b06282e31ddc --- /dev/null +++ b/nixos/tests/nik4.nix @@ -0,0 +1,69 @@ +# This test downloads some example OpenStreetMap data, imports it into a database and then renders a portion of it. The resulting PNG image will be in the output. +# Importing the data takes some time, at least a couple minutes. + +{ lib, pkgs, ... }: +let + # Fetch a tiny example region + andorra = pkgs.fetchurl { + # The OpenStreetMap data from the first of January seems to be kept for at least 10 years + url = "https://download.geofabrik.de/europe/andorra-240101.osm.pbf"; + hash = "sha256-9RylVK1C28e9qnc8Lu1VGcxS3lNlmjPTWcWvJJlmAf4="; + }; +in +{ + name = "nik4"; + meta.maintainers = lib.teams.geospatial.members ++ (with lib.maintainers; [ Luflosi ]); + + nodes.machine = { + virtualisation = { + cores = 2; + diskSize = 8 * 1024; + memorySize = 2 * 1024; + }; + + services.postgresql = { + enable = true; + extensions = ps: with ps; [ postgis ]; + ensureUsers = lib.singleton { + name = "osmuser"; + ensureClauses.createdb = true; + }; + }; + + users.users.osmuser = { + isSystemUser = true; + group = "osmuser"; + }; + users.groups.osmuser = { }; + + environment.systemPackages = with pkgs; [ + osm2pgsql + nik4 + ]; + }; + + testScript = '' + machine.wait_for_unit("multi-user.target") + + with subtest("Create database"): + machine.succeed("sudo -u postgres createdb --encoding=UTF8 --owner=osmuser gis") + machine.succeed("sudo -u postgres psql gis --command='CREATE EXTENSION postgis;'") + machine.succeed("sudo -u postgres psql gis --command='CREATE EXTENSION hstore;'") + + with subtest("Insert data into database"): + machine.succeed("mkdir /osm >&2") + machine.succeed("chown osmuser /osm >&2") + + machine.succeed("sudo -u osmuser '${pkgs.openstreetmap-carto.get_external_data}/bin/get-external-data.py' --config '${pkgs.openstreetmap-carto.get_external_data}/external-data.yml' --data '/osm/get-external-data/'") + machine.succeed("sudo -u osmuser osm2pgsql --output=flex --style='${pkgs.openstreetmap-carto}/openstreetmap-carto-flex.lua' --database=gis --create '${andorra}'") + machine.succeed("sudo -u osmuser psql -d gis -f '${pkgs.openstreetmap-carto}/indexes.sql'") + machine.succeed("sudo -u osmuser psql -d gis -f '${pkgs.openstreetmap-carto}/functions.sql'") + + with subtest("Execute nik4.py"): + machine.succeed("sudo -u osmuser nik4.py --fonts '${pkgs.openstreetmap-carto.passthru.fonts}' --url 'https://www.openstreetmap.org/#map=17/42.506650/1.525828' --ppi 300 -a 4 '${pkgs.openstreetmap-carto}/mapnik.xml' /tmp/map.png >&2") + machine.copy_from_vm("/tmp/map.png") + import os + image_size = os.stat(machine.out_dir / "map.png").st_size + assert image_size >= 2 * 1024 * 1024, f"The rendered map image was too small ({image_size} bytes). This indicates, that the map was not rendered correctly." + ''; +} diff --git a/pkgs/by-name/ni/nik4/package.nix b/pkgs/by-name/ni/nik4/package.nix index d712d1e96da7e6..b6a265fc6c42d1 100644 --- a/pkgs/by-name/ni/nik4/package.nix +++ b/pkgs/by-name/ni/nik4/package.nix @@ -2,6 +2,7 @@ lib, fetchFromGitHub, + nixosTests, python3Packages, }: @@ -26,6 +27,10 @@ python3Packages.buildPythonPackage rec { strictDeps = true; + passthru.tests = { + inherit (nixosTests) nik4; + }; + meta = { changelog = "https://github.com/Zverik/Nik4/releases/tag/v${version}"; description = "Mapnik to image export"; diff --git a/pkgs/by-name/op/openstreetmap-carto/package.nix b/pkgs/by-name/op/openstreetmap-carto/package.nix index c759a784a7df70..5f627bb3a8f0b0 100644 --- a/pkgs/by-name/op/openstreetmap-carto/package.nix +++ b/pkgs/by-name/op/openstreetmap-carto/package.nix @@ -8,6 +8,7 @@ coreutils, gdal, hanazono, + nixosTests, noto-fonts, python3, runCommand, @@ -173,6 +174,7 @@ stdenvNoCC.mkDerivation (finalAttrs: { }; tests = { + inherit (nixosTests) nik4; inherit (finalAttrs.finalPackage.passthru) fonts get-fonts; # Check that we generated the exact same files as the pre-generated ones