Skip to content

Commit

Permalink
nixos/tests/lemmy: init
Browse files Browse the repository at this point in the history
Co-authored-by: Ctem <c@ctem.me>
Co-authored-by: cab <cab404@mailbox.org>
Co-authored-by: a-kenji <aks.kenji@protonmail.com>
Co-authored-by: Shahar Dawn Or <mightyiampresence@gmail.com>
Co-authored-by: Matthias Meschede <MMesch@users.noreply.github.com>
Co-authored-by: Ilan Joselevich <personal@ilanjoselevich.com>
  • Loading branch information
7 people authored and Yt committed Sep 2, 2022
1 parent 50361fb commit cbf8e91
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions nixos/tests/all-tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ in {
kubernetes = handleTestOn ["x86_64-linux"] ./kubernetes {};
latestKernel.login = handleTest ./login.nix { latestKernel = true; };
leaps = handleTest ./leaps.nix {};
lemmy = handleTest ./lemmy.nix {};
libinput = handleTest ./libinput.nix {};
libreddit = handleTest ./libreddit.nix {};
libresprite = handleTest ./libresprite.nix {};
Expand Down
85 changes: 85 additions & 0 deletions nixos/tests/lemmy.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import ./make-test-python.nix ({ pkgs, lib, ... }:
let
uiPort = 1234;
backendPort = 5678;
lemmyNodeName = "server";
in
{
name = "lemmy";
meta = with lib.maintainers; { maintainers = [ mightyiam ]; };

nodes = {
client = { };

"${lemmyNodeName}" = {
services.lemmy = {
enable = true;
jwtSecretPath = pkgs.writeTextFile {
name = "lemmy-secret";
text = "very-secret-password123";
};
ui.port = uiPort;
settings = {
hostname = "http://${lemmyNodeName}";
port = backendPort;
database.createLocally = true;
};
caddy.enable = true;
};

networking.firewall.allowedTCPPorts = [ 80 ];

# pict-rs seems to need more than 1025114112 bytes
virtualisation.memorySize = 2000;
};
};

testScript = ''
server = ${lemmyNodeName}
with subtest("the backend starts and responds"):
server.wait_for_unit("lemmy.service")
server.wait_for_open_port(${toString backendPort})
server.succeed("curl --fail localhost:${toString backendPort}/api/v3/site")
with subtest("the UI starts and responds"):
server.wait_for_unit("lemmy-ui.service")
server.wait_for_open_port(${toString uiPort})
server.succeed("curl --fail localhost:${toString uiPort}")
with subtest("Lemmy-UI responds through the caddy reverse proxy"):
server.wait_for_unit("network-online.target")
server.wait_for_unit("caddy.service")
server.wait_for_open_port(80)
body = server.execute("curl --fail --location ${lemmyNodeName}")[1]
assert "Lemmy" in body, f"String Lemmy not found in response for ${lemmyNodeName}: \n{body}"
with subtest("the server is exposed externally"):
client.wait_for_unit("network-online.target")
client.succeed("curl -v --fail ${lemmyNodeName}")
with subtest("caddy correctly routes backend requests"):
# Make sure we are not hitting frontend
server.execute("systemctl stop lemmy-ui.service")
def assert_http_code(url, expected_http_code, extra_curl_args=""):
_, http_code = server.execute(f'curl --silent -o /dev/null {extra_curl_args} --fail --write-out "%{{http_code}}" {url}')
assert http_code == str(expected_http_code), f"expected http code {expected_http_code}, got {http_code}"
# Caddy responds with HTTP code 502 if it cannot handle the requested path
assert_http_code("${lemmyNodeName}/obviously-wrong-path/", 502)
assert_http_code("${lemmyNodeName}/static/js/client.js", 200)
assert_http_code("${lemmyNodeName}/api/v3/site", 200)
# A 404 confirms that the request goes to the backend
# No path can return 200 until after we upload an image to pict-rs
assert_http_code("${lemmyNodeName}/pictrs/", 404)
# The paths `/feeds/*` and `/nodeinfo/*` are not tested because they seem to be misconfigured
assert_http_code("${lemmyNodeName}/some-other-made-up-path/", 404, "-X POST")
assert_http_code("${lemmyNodeName}/some-other-path", 404, "-H 'Accept: application/activity+json'")
assert_http_code("${lemmyNodeName}/some-other-path", 404, "-H 'Accept: application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"'")
'';
})
2 changes: 2 additions & 0 deletions pkgs/servers/web-apps/lemmy/server.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
, Security
, protobuf
, rustfmt
, nixosTests
}:
let
pinData = lib.importJSON ./pin.json;
Expand Down Expand Up @@ -42,6 +43,7 @@ rustPlatform.buildRustPackage rec {
nativeBuildInputs = [ protobuf rustfmt ];

passthru.updateScript = ./update.sh;
passthru.tests.lemmy-server = nixosTests.lemmy;

meta = with lib; {
description = "🐀 Building a federated alternative to reddit in rust";
Expand Down
2 changes: 2 additions & 0 deletions pkgs/servers/web-apps/lemmy/ui.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
, pkg-config
, fetchFromGitHub
, fetchYarnDeps
, nixosTests
}:

let
Expand Down Expand Up @@ -67,6 +68,7 @@ mkYarnPackage {
distPhase = "true";

passthru.updateScript = ./update.sh;
passthru.tests.lemmy-ui = nixosTests.lemmy;

meta = with lib; {
description = "Building a federated alternative to reddit in rust";
Expand Down

0 comments on commit cbf8e91

Please sign in to comment.