From 855eb6f264ca1de1f2dbdc85df0382d1cd0170bf Mon Sep 17 00:00:00 2001 From: Jacek Galowicz Date: Wed, 20 Nov 2019 18:33:36 +0100 Subject: [PATCH 1/3] nixos/mosquitto: Port integration test to python --- nixos/tests/mosquitto.nix | 49 ++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/nixos/tests/mosquitto.nix b/nixos/tests/mosquitto.nix index b4c897c3ab5d..21df2a2ee1a4 100644 --- a/nixos/tests/mosquitto.nix +++ b/nixos/tests/mosquitto.nix @@ -1,4 +1,4 @@ -import ./make-test.nix ({ pkgs, ... }: +import ./make-test-python.nix ({ pkgs, ... }: let port = 1888; @@ -52,37 +52,44 @@ in { sub = args: "(${cmd "sub"} -C 1 ${args} | tee ${file} &)"; in '' - startAll; - $server->waitForUnit("mosquitto.service"); - - $server->fail("test -f ${file}"); - $client1->fail("test -f ${file}"); - $client2->fail("test -f ${file}"); + start_all() + server.wait_for_unit("mosquitto.service") + for machine in server, client1, client2: + machine.fail("test -f ${file}") # QoS = 0, so only one subscribers should get it - $server->execute("${sub "-q 0"}"); + server.execute( + "${sub "-q 0"}" + ) # we need to give the subscribers some time to connect - $client2->execute("sleep 5"); - $client2->succeed("${cmd "pub"} -m FOO -q 0"); - - $server->waitUntilSucceeds("grep -q FOO ${file}"); - $server->execute("rm ${file}"); + client2.execute("sleep 5") + client2.succeed( + "${cmd "pub"} -m FOO -q 0" + ) + server.wait_until_succeeds("grep -q FOO ${file}") + server.execute("rm ${file}") # QoS = 1, so both subscribers should get it - $server->execute("${sub "-q 1"}"); - $client1->execute("${sub "-q 1"}"); + server.execute( + "${sub "-q 1"}" + ) + client1.execute( + "${sub "-q 1"}" + ) # we need to give the subscribers some time to connect - $client2->execute("sleep 5"); - $client2->succeed("${cmd "pub"} -m BAR -q 1"); + client2.execute("sleep 5") + client2.succeed( + "${cmd "pub"} -m BAR -q 1" + ) - $server->waitUntilSucceeds("grep -q BAR ${file}"); - $server->execute("rm ${file}"); + server.wait_until_succeeds("grep -q BAR ${file}") + server.execute("rm ${file}") - $client1->waitUntilSucceeds("grep -q BAR ${file}"); - $client1->execute("rm ${file}"); + client1.wait_until_succeeds("grep -q BAR ${file}") + client1.execute("rm ${file}") ''; }) From 4dba4db1db3ac4aaffc6fb5faaa4703ae59c0e6c Mon Sep 17 00:00:00 2001 From: Jacek Galowicz Date: Wed, 20 Nov 2019 18:57:52 +0100 Subject: [PATCH 2/3] nixos/test: let python driver evaluate test code in global scope so the user can define and use functions --- nixos/lib/test-driver/test-driver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nixos/lib/test-driver/test-driver.py b/nixos/lib/test-driver/test-driver.py index 9d89960876eb..dff31a8f8e1e 100644 --- a/nixos/lib/test-driver/test-driver.py +++ b/nixos/lib/test-driver/test-driver.py @@ -749,7 +749,7 @@ def run_tests() -> None: if tests is not None: with log.nested("running the VM test script"): try: - exec(tests) + exec(tests, globals()) except Exception as e: eprint("error: {}".format(str(e))) sys.exit(1) From 432f8a424b654955d01496db207cbcd6f10d587f Mon Sep 17 00:00:00 2001 From: Jacek Galowicz Date: Wed, 20 Nov 2019 18:58:24 +0100 Subject: [PATCH 3/3] nixos/mosquitto: Refactor integration test code --- nixos/tests/mosquitto.nix | 61 ++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/nixos/tests/mosquitto.nix b/nixos/tests/mosquitto.nix index 21df2a2ee1a4..1f2fdf4237fa 100644 --- a/nixos/tests/mosquitto.nix +++ b/nixos/tests/mosquitto.nix @@ -5,17 +5,6 @@ let username = "mqtt"; password = "VERY_secret"; topic = "test/foo"; - - cmd = bin: pkgs.lib.concatStringsSep " " [ - "${pkgs.mosquitto}/bin/mosquitto_${bin}" - "-V mqttv311" - "-h server" - "-p ${toString port}" - "-u ${username}" - "-P '${password}'" - "-t ${topic}" - ]; - in { name = "mosquitto"; meta = with pkgs.stdenv.lib; { @@ -49,9 +38,27 @@ in { testScript = let file = "/tmp/msg"; - sub = args: - "(${cmd "sub"} -C 1 ${args} | tee ${file} &)"; in '' + def mosquitto_cmd(binary): + return ( + "${pkgs.mosquitto}/bin/mosquitto_{} " + "-V mqttv311 " + "-h server " + "-p ${toString port} " + "-u ${username} " + "-P '${password}' " + "-t ${topic}" + ).format(binary) + + + def publish(args): + return "{} {}".format(mosquitto_cmd("pub"), args) + + + def subscribe(args): + return "({} -C 1 {} | tee ${file} &)".format(mosquitto_cmd("sub"), args) + + start_all() server.wait_for_unit("mosquitto.service") @@ -59,37 +66,25 @@ in { machine.fail("test -f ${file}") # QoS = 0, so only one subscribers should get it - server.execute( - "${sub "-q 0"}" - ) + server.execute(subscribe("-q 0")) # we need to give the subscribers some time to connect client2.execute("sleep 5") - client2.succeed( - "${cmd "pub"} -m FOO -q 0" - ) + client2.succeed(publish("-m FOO -q 0")) server.wait_until_succeeds("grep -q FOO ${file}") server.execute("rm ${file}") # QoS = 1, so both subscribers should get it - server.execute( - "${sub "-q 1"}" - ) - client1.execute( - "${sub "-q 1"}" - ) + server.execute(subscribe("-q 1")) + client1.execute(subscribe("-q 1")) # we need to give the subscribers some time to connect client2.execute("sleep 5") - client2.succeed( - "${cmd "pub"} -m BAR -q 1" - ) + client2.succeed(publish("-m BAR -q 1")) - server.wait_until_succeeds("grep -q BAR ${file}") - server.execute("rm ${file}") - - client1.wait_until_succeeds("grep -q BAR ${file}") - client1.execute("rm ${file}") + for machine in server, client1: + machine.wait_until_succeeds("grep -q BAR ${file}") + machine.execute("rm ${file}") ''; })