Add the sp_rand_init and sp_rand_cleanup tools.

These will be used on the worker node to set up a random volume name
prefix for the StorPool volumes and snapshots created during
the OpenStack CI run, and detach and remove them all later.

Change-Id: Ic558e2183db8068545f7454f956dc8bc740959c6
diff --git a/tools/sp-rand/unit_tests/test_init.py b/tools/sp-rand/unit_tests/test_init.py
new file mode 100644
index 0000000..121bfa9
--- /dev/null
+++ b/tools/sp-rand/unit_tests/test_init.py
@@ -0,0 +1,53 @@
+"""Test the creation of the StorPool volume name random seed."""
+
+import pathlib
+import re
+import tempfile
+
+from sp_rand import defs as r_defs
+from sp_rand import init as r_init
+
+TAG = "testvol"
+RE_PREFIX = re.compile(r"^ testvol-\d{8}-\d{4}-\d{4}- $", re.X)
+
+
+def test_generate() -> None:
+    """Make sure the prefix is generated correctly."""
+    cfg = r_init.Config.default()._replace(tag=TAG)
+    prefix = r_init.generate_prefix(cfg)
+    assert RE_PREFIX.match(prefix)
+
+
+def do_test_store(noop: bool) -> None:
+    """Make sure the prefix is stored into the config file... or not."""
+    with tempfile.TemporaryDirectory() as tempd_obj:
+        tempd = pathlib.Path(tempd_obj)
+        assert sorted(tempd.iterdir()) == []
+
+        cfg = r_init.Config.default()._replace(confdir=tempd, noop=noop, tag=TAG)
+        conffile, prefix = r_init.store_prefix(cfg)
+
+        if noop:
+            assert sorted(tempd.iterdir()) == []
+        else:
+            files = sorted(tempd.iterdir())
+            assert files == [conffile]
+            assert conffile.is_file()
+            assert conffile.name.endswith(".conf") and not conffile.name.startswith(".")
+
+            lines = conffile.read_text(encoding="UTF-8").splitlines()
+            assert len(lines) == 1
+            fields = lines[0].split("=", maxsplit=1)
+            assert fields == [r_defs.PREFIX_VAR, prefix]
+
+        assert RE_PREFIX.match(prefix)
+
+
+def test_store() -> None:
+    """Make sure the prefix is stored into the config file."""
+    do_test_store(False)
+
+
+def test_store_noop() -> None:
+    """Make sure the prefix is not stored into the config file."""
+    do_test_store(True)