mirror of
https://github.com/NixOS/nix
synced 2025-07-07 01:51:47 +02:00
initialize test suite for git fetchers
solves #9388 This utilizes nixos vm tests to allow: - writing tests for fetchTree and fetchGit involving actual networking. - writing small independent test cases by automating local and remote repository setup per test case. This adds: - a gitea module setting up a gitea server - a setup module that simplifies writing test cases by automating the repo setup. - a simple git http test case Other improvements: For all nixos tests, add capability of overriding the nix version to test against. This should make it easier to prevent regressions. If a new test is added it can simply be ran against any older nix version without having to backport the test. For example, for running the container tests against nix 2.12.0: `nix build "$(nix eval --raw .#hydraJobs.tests.containers --impure --apply 't: (t.forNix "2.12.0").drvPath')^*" -L`
This commit is contained in:
parent
75d509eb08
commit
813c113b9e
4 changed files with 252 additions and 7 deletions
106
tests/nixos/fetch-git/testsupport/setup.nix
Normal file
106
tests/nixos/fetch-git/testsupport/setup.nix
Normal file
|
@ -0,0 +1,106 @@
|
|||
{ 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
|
||||
self.git = f"git -C {self.path}"
|
||||
self.create()
|
||||
|
||||
def create(self):
|
||||
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"}}' )}
|
||||
""")
|
||||
client.succeed(f"""
|
||||
mkdir -p {self.path} \
|
||||
&& git init -b main {self.path} \
|
||||
&& {self.git} remote add origin {self.remote}
|
||||
""")
|
||||
'';
|
||||
testScript = ''
|
||||
start_all();
|
||||
|
||||
${config.setupScript}
|
||||
|
||||
### SETUP COMPLETE ###
|
||||
|
||||
${lib.concatStringsSep "\n" (map execTestCase config.testCases)}
|
||||
'';
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue