blob: 121bfa959223e32bac1e29d479951da2cf790603 [file] [log] [blame]
Peter Pentchevd00519f2021-11-08 01:17:13 +02001"""Test the creation of the StorPool volume name random seed."""
2
3import pathlib
4import re
5import tempfile
6
7from sp_rand import defs as r_defs
8from sp_rand import init as r_init
9
10TAG = "testvol"
11RE_PREFIX = re.compile(r"^ testvol-\d{8}-\d{4}-\d{4}- $", re.X)
12
13
14def test_generate() -> None:
15 """Make sure the prefix is generated correctly."""
16 cfg = r_init.Config.default()._replace(tag=TAG)
17 prefix = r_init.generate_prefix(cfg)
18 assert RE_PREFIX.match(prefix)
19
20
21def do_test_store(noop: bool) -> None:
22 """Make sure the prefix is stored into the config file... or not."""
23 with tempfile.TemporaryDirectory() as tempd_obj:
24 tempd = pathlib.Path(tempd_obj)
25 assert sorted(tempd.iterdir()) == []
26
27 cfg = r_init.Config.default()._replace(confdir=tempd, noop=noop, tag=TAG)
28 conffile, prefix = r_init.store_prefix(cfg)
29
30 if noop:
31 assert sorted(tempd.iterdir()) == []
32 else:
33 files = sorted(tempd.iterdir())
34 assert files == [conffile]
35 assert conffile.is_file()
36 assert conffile.name.endswith(".conf") and not conffile.name.startswith(".")
37
38 lines = conffile.read_text(encoding="UTF-8").splitlines()
39 assert len(lines) == 1
40 fields = lines[0].split("=", maxsplit=1)
41 assert fields == [r_defs.PREFIX_VAR, prefix]
42
43 assert RE_PREFIX.match(prefix)
44
45
46def test_store() -> None:
47 """Make sure the prefix is stored into the config file."""
48 do_test_store(False)
49
50
51def test_store_noop() -> None:
52 """Make sure the prefix is not stored into the config file."""
53 do_test_store(True)