Skip to content

Commit

Permalink
Add another test
Browse files Browse the repository at this point in the history
  • Loading branch information
tfc committed Feb 29, 2024
1 parent 888e58a commit 32b35de
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 8 deletions.
13 changes: 13 additions & 0 deletions debug-host-module.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
services.openssh = {
enable = true;
settings = {
PermitRootLogin = "yes";
PermitEmptyPasswords = "yes";
UsePAM = "no";
};
};
virtualisation.forwardPorts = [
{ from = "host"; host.port = 2222; guest.port = 22; }
];
}
24 changes: 24 additions & 0 deletions echo/echo-nixos-module.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{ config, pkgs, lib, ... }:
let
cfg = config.services.echo;
in
{
options.services.echo = {
enable = lib.mkEnableOption "TCP echo as a Service";
port = lib.mkOption {
type = lib.types.port;
default = 8081;
description = "Port to listen on";
};
};

config = lib.mkIf cfg.enable {
systemd.services.echo = {
description = "Friendly TCP Echo as a Service Daemon";
wantedBy = [ "multi-user.target" ];
serviceConfig.ExecStart = ''
${pkgs.python3}/bin/python3 ${./echo-server.py} ${builtins.toString cfg.port}
'';
};
};
}
23 changes: 23 additions & 0 deletions echo/echo-server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import socket
import sys

HOST = ""

try:
PORT = int(sys.argv[1])
except:
print("Please provide a port number on the command line")
sys.exit(1)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()

while True:
conn, addr = s.accept()
with conn:
while True:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
36 changes: 36 additions & 0 deletions echo/test.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
name = "Echo Service Test";

nodes = {
server = { config, pkgs, ... }: {
imports = [
./echo-nixos-module.nix
];

services.echo.enable = true;

networking.firewall.allowedTCPPorts = [
config.services.echo.port
];
};
client = { ... }: { };
};

globalTimeout = 30;

interactive.nodes.server = import ../debug-host-module.nix;

testScript = { nodes, ... }: ''
ECHO_PORT = ${builtins.toString nodes.server.services.echo.port}
ECHO_TEXT = "Hello, world!"
start_all()
server.wait_for_unit("echo.service")
server.wait_for_open_port(ECHO_PORT)
client.wait_for_unit("network-online.target")
output = client.succeed(f"echo '{ECHO_TEXT}' | nc -N server {ECHO_PORT}")
assert ECHO_TEXT in output
'';
}
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
systems = [ "x86_64-linux" "aarch64-linux" ];
perSystem = { config, pkgs, ... }: {
packages.default = pkgs.testers.runNixOSTest ./test.nix;
packages.echo = pkgs.testers.runNixOSTest ./echo/test.nix;

checks = config.packages;
};
Expand Down
9 changes: 1 addition & 8 deletions test.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@
machine2 = { pkgs, ... }: { };
};

interactive.nodes.machine1 = { ... }: {
services.openssh.enable = true;
services.openssh.settings.PermitRootLogin = "yes";
users.extraUsers.root.initialPassword = "";
virtualisation.forwardPorts = [
{ from = "host"; host.port = 2222; guest.port = 22; }
];
};
interactive.nodes.machine1 = import ./debug-host-module.nix;

testScript = ''
machine1.wait_for_unit("network-online.target")
Expand Down

0 comments on commit 32b35de

Please sign in to comment.