summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers+gitlab@digital-scurf.org>2021-07-28 21:35:03 +0000
committerDaniel Silverstone <dsilvers+gitlab@digital-scurf.org>2021-07-28 21:35:03 +0000
commit5a6e40e10eeb8ee106d0fae4b9e25b779bebfe57 (patch)
treefaa7cf5af03a0924319a20d949b15923b98f89fd
parent08063f6d7241817cf1a04367be25b0c43202c15b (diff)
parent28ddadc41d41fbcd41948a412377471b374b677b (diff)
downloadsubplot-5a6e40e10eeb8ee106d0fae4b9e25b779bebfe57.tar.gz
Merge branch 'merged' into 'main'
Fix daemon.py tests' dependence on netcat See merge request subplot/subplot!192
-rw-r--r--share/python/lib/daemon.md44
-rw-r--r--share/python/lib/daemon.py11
-rw-r--r--share/python/lib/daemon.yaml3
3 files changed, 40 insertions, 18 deletions
diff --git a/share/python/lib/daemon.md b/share/python/lib/daemon.md
index 9484926..c4f5e27 100644
--- a/share/python/lib/daemon.md
+++ b/share/python/lib/daemon.md
@@ -27,31 +27,39 @@ then there is no "/bin/sleep 12765" process
# Daemon takes a while to open its port
-[netcat]: https://en.wikipedia.org/wiki/Netcat
-
-This scenario verifies that if the background process never starts
-listening on its port, the daemon library handles that correctly. We
-do this by using [netcat][] to start a dummy daemon, after a short
-delay. The lib/daemon code will wait for netcat to open its port, by
-connecting to the port. It then closes the port, which causes netcat
-to terminate.
+This scenario verifies that if the background process doesn't immediately start
+listening on its port, the daemon library handles that correctly. We do this
+with a helper script that waits 2 seconds before opening the port. The
+lib/daemon code will wait for the script by repeatedly trying to connect. Once
+successful, it immediately closes the port, which causes the script to
+terminate.
~~~scenario
-given a daemon helper shell script slow-start-daemon.sh
-given there is no "slow-start-daemon.sh" process
-when I try to start "./slow-start-daemon.sh" as slow-daemon, on port 8888
+given a daemon helper shell script slow-start-daemon.py
+given there is no "slow-start-daemon.py" process
+when I try to start "./slow-start-daemon.py" as slow-daemon, on port 8888
+then starting the daemon succeeds
when I stop background process slow-daemon
-then there is no "slow-start-daemon.sh" process
+then there is no "slow-start-daemon.py" process
~~~
-~~~{#slow-start-daemon.sh .file .sh .numberLines}
-#!/bin/bash
+~~~{#slow-start-daemon.py .file .python .numberLines}
+#!/usr/bin/env python3
-set -euo pipefail
+import socket
+import time
+
+time.sleep(2)
+
+s = socket.socket()
+s.bind(("127.0.0.1", 8888))
+s.listen()
+
+(conn, _) = s.accept()
+conn.recv(1)
+s.close()
-sleep 2
-netcat -l 8888 > /dev/null
-echo OK
+print("OK")
~~~
# Daemon never opens the intended port
diff --git a/share/python/lib/daemon.py b/share/python/lib/daemon.py
index 0b47fe9..0573a64 100644
--- a/share/python/lib/daemon.py
+++ b/share/python/lib/daemon.py
@@ -220,6 +220,17 @@ def daemon_start_fails_with(ctx, message=None):
assert message.lower() in error.lower()
+def daemon_start_succeeds(ctx):
+ daemon = ctx.declare("_daemon")
+ logging.debug(f"daemon_start_succeeds: {daemon}")
+ for name in daemon.keys():
+ if isinstance(daemon[name], dict):
+ logging.debug(f"name={name}")
+ stderr = daemon_get_stderr(ctx, name)
+ logging.debug(f"daemon_start_succeeds: {name}:\n{stderr}")
+ assert "_start_error" not in daemon
+
+
def daemon_get_stdout(ctx, name):
return _daemon_get_output(ctx, name, "stdout")
diff --git a/share/python/lib/daemon.yaml b/share/python/lib/daemon.yaml
index 4fab1f6..424a98f 100644
--- a/share/python/lib/daemon.yaml
+++ b/share/python/lib/daemon.yaml
@@ -29,6 +29,9 @@
- then: starting daemon fails with "{message:text}"
function: daemon_start_fails_with
+- then: starting the daemon succeeds
+ function: daemon_start_succeeds
+
- then: daemon {name} stdout is "{text:text}"
function: daemon_stdout_is