2024-06-09 14:39:18 +02:00
|
|
|
let
|
|
|
|
sources = import ./npins;
|
|
|
|
inherit (sources) nixpkgs;
|
|
|
|
pkgs = import nixpkgs { };
|
|
|
|
|
|
|
|
django = pkgs.python3.withPackages (p: [ p.django ]);
|
|
|
|
settings = {
|
|
|
|
SMOKE_TEST = "Hello settings";
|
|
|
|
JSON_SMOKE_TEST = {
|
|
|
|
"hello" = "json";
|
|
|
|
};
|
|
|
|
|
|
|
|
DATABASES = {
|
|
|
|
"default" = {
|
|
|
|
"ENGINE" = "django.db.backends.sqlite3";
|
2024-06-09 20:15:08 +02:00
|
|
|
"NAME" = "/var/lib/django-smoke-test/db.sqlite3";
|
2024-06-09 14:39:18 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
secretFile = "${./test_assets/secret}";
|
|
|
|
|
|
|
|
secrets = {
|
|
|
|
SECRETS_SMOKE_TEST = secretFile;
|
|
|
|
};
|
|
|
|
|
2024-06-09 20:15:08 +02:00
|
|
|
project = pkgs.runCommandNoCC "smoke-test" { } ''
|
2024-06-09 14:39:18 +02:00
|
|
|
mkdir -p $out
|
2024-06-09 20:15:08 +02:00
|
|
|
${django}/bin/django-admin startproject smoke_test $out
|
|
|
|
cp ${./test_assets/views.py} $out/smoke_test/views.py
|
|
|
|
cp ${./test_assets/urls.py} $out/smoke_test/urls.py
|
2024-06-09 14:39:18 +02:00
|
|
|
'';
|
|
|
|
in
|
|
|
|
pkgs.testers.runNixOSTest (
|
|
|
|
{ lib, ... }:
|
|
|
|
{
|
|
|
|
name = "djangonix smoke test";
|
|
|
|
nodes.machine =
|
|
|
|
{ pkgs, config, ... }:
|
|
|
|
{
|
|
|
|
imports = [ ./module.nix ];
|
|
|
|
|
2024-06-09 20:15:08 +02:00
|
|
|
services.django.smoke-test = {
|
2024-06-09 14:39:18 +02:00
|
|
|
src = project;
|
2024-06-09 20:15:08 +02:00
|
|
|
mainModule = "smoke_test";
|
2024-06-09 14:39:18 +02:00
|
|
|
inherit settings secrets;
|
|
|
|
port = 8000;
|
|
|
|
};
|
|
|
|
services.nginx = {
|
|
|
|
enable = true;
|
|
|
|
recommendedProxySettings = true;
|
|
|
|
virtualHosts.netbox = {
|
|
|
|
default = true;
|
2024-06-09 20:15:08 +02:00
|
|
|
locations."/".proxyPass = "http://localhost:${toString config.services.django.smoke-test.port}";
|
|
|
|
locations."/static/".alias = config.services.django.smoke-test.staticAssets;
|
2024-06-09 14:39:18 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
testScript = ''
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import json
|
|
|
|
start_all()
|
2024-06-09 20:15:08 +02:00
|
|
|
machine.wait_for_unit("django-smoke-test.service")
|
2024-06-09 14:39:18 +02:00
|
|
|
machine.wait_for_unit("nginx.service")
|
|
|
|
time.sleep(1)
|
|
|
|
with subtest("Test settings"):
|
|
|
|
status, out = machine.execute("curl http://127.0.0.1/test_settings/")
|
|
|
|
print(status, repr(out))
|
|
|
|
if status != 0 or out != "${settings.SMOKE_TEST}":
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
with subtest("Test secrets"):
|
|
|
|
status, out = machine.execute("curl http://127.0.0.1/test_secrets/")
|
|
|
|
print(status, repr(out))
|
|
|
|
if status != 0 or out != json.load(open("${secretFile}")):
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
with subtest("Test json"):
|
|
|
|
status, out = machine.execute("curl http://127.0.0.1:8000/test_json/")
|
|
|
|
print(status, repr(out))
|
|
|
|
if status != 0 or json.loads(out) != json.loads('${builtins.toJSON settings.JSON_SMOKE_TEST}'):
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
)
|