Skip to content

Commit aa5bcf6

Browse files
committed
nixos/tests/forgejo: test nixos/forgejo-runner
1 parent 8f77e07 commit aa5bcf6

1 file changed

Lines changed: 86 additions & 27 deletions

File tree

nixos/tests/forgejo.nix

Lines changed: 86 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ let
6060
];
6161
services.openssh.enable = true;
6262

63-
specialisation.runner = {
63+
specialisation.gitea-actions-runner = {
6464
inheritParentConfig = true;
6565
configuration.services.gitea-actions-runner = {
6666
package = pkgs.forgejo-runner;
@@ -76,6 +76,45 @@ let
7676
};
7777
};
7878
};
79+
specialisation.forgejo-runner = {
80+
inheritParentConfig = true;
81+
configuration = (
82+
{ config, ... }:
83+
84+
{
85+
services.forgejo-runner = {
86+
instances."test" = {
87+
enable = true;
88+
settings = {
89+
runner.labels = [
90+
# type ":host" does not depend on docker/podman/lxc
91+
"native:host"
92+
];
93+
server.connections.default = {
94+
url = "http://localhost:3000";
95+
uuid = "@UUID@";
96+
};
97+
};
98+
secrets.server.connections.default.token_url = "/forgejo-runner_token";
99+
};
100+
};
101+
102+
# FIXME: Remove once upstream supports uuid_url just like token_url
103+
systemd.services.forgejo-runner-test = {
104+
preStart = ''
105+
cp -v ${config.services.forgejo-runner.instances."test".configFile} ./config.yaml
106+
chmod u+w ./config.yaml
107+
${lib.getExe pkgs.replace-secret} "@UUID@" "$CREDENTIALS_DIRECTORY/UUID" ./config.yaml
108+
chmod u-w ./config.yaml
109+
'';
110+
serviceConfig = {
111+
ExecStart = lib.mkForce "${lib.getExe config.services.forgejo-runner.package} daemon --config ./config.yaml";
112+
LoadCredential = [ "UUID:/forgejo-runner_uuid" ];
113+
};
114+
};
115+
}
116+
);
117+
};
79118
specialisation.dump = {
80119
inheritParentConfig = true;
81120
configuration.services.forgejo.dump = {
@@ -172,7 +211,7 @@ let
172211
+ "Please contact your site administrator.'"
173212
)
174213
server.succeed(
175-
"su -l forgejo -c 'GITEA_WORK_DIR=/var/lib/forgejo forgejo admin user create "
214+
"su -l forgejo -c 'GITEA_WORK_DIR=/var/lib/forgejo forgejo admin user create --admin "
176215
+ "--username test --password totallysafe --email test@localhost --must-change-password=false'"
177216
)
178217
@@ -217,22 +256,22 @@ let
217256
server.fail("curl --fail http://localhost:3000/metrics")
218257
server.succeed('curl --fail http://localhost:3000/metrics -H "Authorization: Bearer ${metricSecret}"')
219258
220-
with subtest("Testing runner registration and action workflow"):
221-
server.succeed(
222-
"su -l forgejo -c 'GITEA_WORK_DIR=/var/lib/forgejo forgejo actions generate-runner-token' | sed 's/^/TOKEN=/' | tee /var/lib/forgejo/runner_token"
223-
)
224-
server.succeed("${serverSystem}/specialisation/runner/bin/switch-to-configuration test")
225-
server.wait_for_unit("gitea-runner-test.service")
226-
server.succeed("journalctl -o cat -u gitea-runner-test.service | grep -q 'Runner registered successfully'")
259+
def poll_workflow_action_status(id: int) -> bool:
260+
try:
261+
response = server.succeed("curl --fail http://localhost:3000/api/v1/repos/test/repo/actions/tasks")
262+
status = json.loads(response).get("workflow_runs")[id].get("status")
227263
228-
# enable actions feature for this repository, defaults to disabled
229-
server.succeed(
230-
"curl --fail -X PATCH http://localhost:3000/api/v1/repos/test/repo "
231-
+ "-H 'Accept: application/json' -H 'Content-Type: application/json' "
232-
+ f"-H 'Authorization: token {api_token}'"
233-
+ ' -d \'{"has_actions":true}\'''
234-
)
264+
except IndexError:
265+
status = "???"
266+
267+
server.log(f"Workflow status: {status}")
268+
269+
if status == "failure":
270+
raise Exception("Workflow failed")
271+
272+
return status == "success"
235273
274+
with subtest("Testing deprecated gitea-actions-runner registration and action workflow"):
236275
# mirror "actions/checkout" action
237276
client.succeed("cp -R ${checkoutActionSource}/ /tmp/checkout")
238277
client.succeed("git -C /tmp/checkout init")
@@ -248,23 +287,43 @@ let
248287
client.succeed("git -C /tmp/repo commit -m 'Add dummy workflow'")
249288
client.succeed("git -C /tmp/repo push origin main")
250289
251-
def poll_workflow_action_status(_) -> bool:
252-
try:
253-
response = server.succeed("curl --fail http://localhost:3000/api/v1/repos/test/repo/actions/tasks")
254-
status = json.loads(response).get("workflow_runs")[0].get("status")
290+
# enable actions feature for this repository, defaults to disabled
291+
server.succeed(
292+
"curl --fail -X PATCH http://localhost:3000/api/v1/repos/test/repo "
293+
+ "-H 'Accept: application/json' -H 'Content-Type: application/json' "
294+
+ f"-H 'Authorization: token {api_token}'"
295+
+ ' -d \'{"has_actions":true}\'''
296+
)
297+
server.succeed(
298+
"su -l forgejo -c 'GITEA_WORK_DIR=/var/lib/forgejo forgejo actions generate-runner-token' | sed 's/^/TOKEN=/' | tee /var/lib/forgejo/runner_token"
299+
)
300+
server.succeed("${serverSystem}/specialisation/gitea-actions-runner/bin/switch-to-configuration test")
301+
server.wait_for_unit("gitea-runner-test.service")
302+
server.succeed("journalctl -o cat -u gitea-runner-test.service | grep -q 'Runner registered successfully'")
255303
256-
except IndexError:
257-
status = "???"
304+
with server.nested("Waiting for the workflow run to be successful"):
305+
retry(lambda _: poll_workflow_action_status(0), 180)
306+
307+
with subtest("Testing forgejo-runner registration and action workflow"):
308+
runner_registration_response = server.succeed(
309+
"curl --fail http://localhost:3000/api/v1/admin/actions/runners "
310+
+ f"-H 'Authorization: token {api_token}' "
311+
+ '--json \'{"name":"NixOS", "ephemeral":false}\'''
312+
)
258313
259-
server.log(f"Workflow status: {status}")
314+
runner_registration = json.loads(runner_registration_response)
315+
server.succeed(f"echo {runner_registration.get("token")} > /forgejo-runner_token")
316+
server.succeed(f"echo {runner_registration.get("uuid")} > /forgejo-runner_uuid")
260317
261-
if status == "failure":
262-
raise Exception("Workflow failed")
318+
server.succeed("${serverSystem}/specialisation/forgejo-runner/bin/switch-to-configuration test")
319+
server.wait_for_unit("forgejo-runner-test.service")
320+
server.succeed("journalctl -o cat -u forgejo-runner-test.service | grep -q 'declared successfully'")
263321
264-
return status == "success"
322+
client.succeed("git -C /tmp/repo commit --allow-empty -m 'Retrigger dummy workflow'")
323+
client.succeed("git -C /tmp/repo push origin main")
265324
266325
with server.nested("Waiting for the workflow run to be successful"):
267-
retry(poll_workflow_action_status, 60)
326+
retry(lambda _: poll_workflow_action_status(1), 180)
268327
269328
with subtest("Testing backup service"):
270329
server.succeed("${serverSystem}/specialisation/dump/bin/switch-to-configuration test")

0 commit comments

Comments
 (0)