nixosTests.anki-sync-server: migrate to runTest

Part Of #386873
This commit is contained in:
Martin Weinelt 2025-03-13 23:24:58 +01:00
parent e045203e0e
commit 8e3f874502
No known key found for this signature in database
GPG key ID: 87C1E9888F856759
2 changed files with 62 additions and 66 deletions

View file

@ -167,7 +167,7 @@ in {
amazon-ssm-agent = runTest ./amazon-ssm-agent.nix; amazon-ssm-agent = runTest ./amazon-ssm-agent.nix;
amd-sev = runTest ./amd-sev.nix; amd-sev = runTest ./amd-sev.nix;
angie-api = runTest ./angie-api.nix; angie-api = runTest ./angie-api.nix;
anki-sync-server = handleTest ./anki-sync-server.nix {}; anki-sync-server = runTest ./anki-sync-server.nix;
anuko-time-tracker = handleTest ./anuko-time-tracker.nix {}; anuko-time-tracker = handleTest ./anuko-time-tracker.nix {};
apcupsd = handleTest ./apcupsd.nix {}; apcupsd = handleTest ./apcupsd.nix {};
apfs = runTest ./apfs.nix; apfs = runTest ./apfs.nix;

View file

@ -1,75 +1,71 @@
import ./make-test-python.nix ( { pkgs, ... }:
{ pkgs, ... }: let
let ankiSyncTest = pkgs.writeScript "anki-sync-test.py" ''
ankiSyncTest = pkgs.writeScript "anki-sync-test.py" '' #!${pkgs.python3}/bin/python
#!${pkgs.python3}/bin/python
import sys import sys
# get site paths from anki itself # get site paths from anki itself
from runpy import run_path from runpy import run_path
run_path("${pkgs.anki}/bin/.anki-wrapped") run_path("${pkgs.anki}/bin/.anki-wrapped")
import anki import anki
col = anki.collection.Collection('test_collection') col = anki.collection.Collection('test_collection')
endpoint = 'http://localhost:27701' endpoint = 'http://localhost:27701'
# Sanity check: verify bad login fails # Sanity check: verify bad login fails
try: try:
col.sync_login('baduser', 'badpass', endpoint) col.sync_login('baduser', 'badpass', endpoint)
print("bad user login worked?!") print("bad user login worked?!")
sys.exit(1) sys.exit(1)
except anki.errors.SyncError: except anki.errors.SyncError:
pass pass
# test logging in to users # test logging in to users
col.sync_login('user', 'password', endpoint) col.sync_login('user', 'password', endpoint)
col.sync_login('passfileuser', 'passfilepassword', endpoint) col.sync_login('passfileuser', 'passfilepassword', endpoint)
# Test actual sync. login apparently doesn't remember the endpoint... # Test actual sync. login apparently doesn't remember the endpoint...
login = col.sync_login('user', 'password', endpoint) login = col.sync_login('user', 'password', endpoint)
login.endpoint = endpoint login.endpoint = endpoint
sync = col.sync_collection(login, False) sync = col.sync_collection(login, False)
assert sync.required == sync.NO_CHANGES assert sync.required == sync.NO_CHANGES
# TODO: create an archive with server content including a test card # TODO: create an archive with server content including a test card
# and check we got it? # and check we got it?
''; '';
testPasswordFile = pkgs.writeText "anki-password" "passfilepassword"; testPasswordFile = pkgs.writeText "anki-password" "passfilepassword";
in in
{ {
name = "anki-sync-server"; name = "anki-sync-server";
meta = with pkgs.lib.maintainers; { meta = with pkgs.lib.maintainers; {
maintainers = [ martinetd ]; maintainers = [ martinetd ];
};
nodes.machine = {
services.anki-sync-server = {
enable = true;
users = [
{
username = "user";
password = "password";
}
{
username = "passfileuser";
passwordFile = testPasswordFile;
}
];
}; };
};
nodes.machine = testScript = ''
{ pkgs, ... }: start_all()
{
services.anki-sync-server = {
enable = true;
users = [
{
username = "user";
password = "password";
}
{
username = "passfileuser";
passwordFile = testPasswordFile;
}
];
};
};
testScript = '' with subtest("Server starts successfully"):
start_all() # service won't start without users
machine.wait_for_unit("anki-sync-server.service")
machine.wait_for_open_port(27701)
with subtest("Server starts successfully"): with subtest("Can sync"):
# service won't start without users machine.succeed("${ankiSyncTest}")
machine.wait_for_unit("anki-sync-server.service") '';
machine.wait_for_open_port(27701) }
with subtest("Can sync"):
machine.succeed("${ankiSyncTest}")
'';
}
)