2023-12-29 10:15:16 +02:00
|
|
|
{ lib, config, extendModules, ... }:
|
|
|
|
let
|
|
|
|
inherit (lib)
|
|
|
|
mkOption
|
|
|
|
types
|
|
|
|
;
|
|
|
|
|
|
|
|
indent = lib.replaceStrings ["\n"] ["\n "];
|
|
|
|
|
|
|
|
execTestCase = testCase: ''
|
|
|
|
|
|
|
|
### TEST ${testCase.name}: ${testCase.description} ###
|
|
|
|
|
|
|
|
with subtest("${testCase.description}"):
|
|
|
|
repo = Repo("${testCase.name}")
|
|
|
|
${indent testCase.script}
|
|
|
|
'';
|
|
|
|
in
|
|
|
|
{
|
|
|
|
|
|
|
|
options = {
|
|
|
|
setupScript = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
description = ''
|
|
|
|
Python code that runs before the main test.
|
|
|
|
|
|
|
|
Variables defined by this code will be available in the test.
|
|
|
|
'';
|
|
|
|
default = "";
|
|
|
|
};
|
|
|
|
testCases = mkOption {
|
|
|
|
description = ''
|
|
|
|
The test cases. See `testScript`.
|
|
|
|
'';
|
|
|
|
type = types.listOf (types.submodule {
|
|
|
|
options.name = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = ''
|
|
|
|
The name of the test case.
|
|
|
|
|
|
|
|
A repository with that name will be set up on the gitea server and locally.
|
|
|
|
|
|
|
|
This name can also be used to execute only a single test case via:
|
|
|
|
`nix build .#hydraJobs.fetch-git.{test-case-name}`
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
options.description = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = ''
|
|
|
|
A description of the test case.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
options.script = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
description = ''
|
|
|
|
Python code that runs the test.
|
|
|
|
|
|
|
|
Variables defined by `setupScript` will be available here.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
|
|
|
nodes.client = {
|
|
|
|
environment.variables = {
|
|
|
|
_NIX_FORCE_HTTP = "1";
|
|
|
|
};
|
|
|
|
nix.settings.experimental-features = ["nix-command" "flakes"];
|
|
|
|
};
|
|
|
|
setupScript = ''
|
|
|
|
class Repo:
|
|
|
|
"""
|
|
|
|
A class to create a git repository on the gitea server and locally.
|
|
|
|
"""
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
self.path = "/tmp/repos/" + name
|
|
|
|
self.remote = "http://gitea:3000/test/" + name
|
2024-01-11 09:41:35 +02:00
|
|
|
self.remote_ssh = "ssh://gitea/root/" + name
|
2023-12-29 10:15:16 +02:00
|
|
|
self.git = f"git -C {self.path}"
|
|
|
|
self.create()
|
|
|
|
|
|
|
|
def create(self):
|
2024-01-11 09:41:35 +02:00
|
|
|
# create ssh remote repo
|
|
|
|
gitea.succeed(f"""
|
|
|
|
git init --bare -b main /root/{self.name}
|
|
|
|
""")
|
|
|
|
# create http remote repo
|
2023-12-29 10:15:16 +02:00
|
|
|
gitea.succeed(f"""
|
|
|
|
curl --fail -X POST http://{gitea_admin}:{gitea_admin_password}@gitea:3000/api/v1/user/repos \
|
|
|
|
-H 'Accept: application/json' -H 'Content-Type: application/json' \
|
|
|
|
-d {shlex.quote( f'{{"name":"{self.name}", "default_branch": "main"}}' )}
|
|
|
|
""")
|
2024-01-11 09:41:35 +02:00
|
|
|
# setup git remotes on client
|
2023-12-29 10:15:16 +02:00
|
|
|
client.succeed(f"""
|
|
|
|
mkdir -p {self.path} \
|
|
|
|
&& git init -b main {self.path} \
|
2024-01-11 09:41:35 +02:00
|
|
|
&& {self.git} remote add origin {self.remote} \
|
|
|
|
&& {self.git} remote add origin-ssh root@gitea:{self.name}
|
2023-12-29 10:15:16 +02:00
|
|
|
""")
|
|
|
|
'';
|
|
|
|
testScript = ''
|
|
|
|
start_all();
|
|
|
|
|
|
|
|
${config.setupScript}
|
|
|
|
|
|
|
|
### SETUP COMPLETE ###
|
|
|
|
|
|
|
|
${lib.concatStringsSep "\n" (map execTestCase config.testCases)}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
}
|