Peter Pentchev | d00519f | 2021-11-08 01:17:13 +0200 | [diff] [blame] | 1 | """Test the creation of the StorPool volume name random seed.""" |
| 2 | |
| 3 | import pathlib |
| 4 | import re |
| 5 | import tempfile |
| 6 | |
| 7 | from sp_rand import defs as r_defs |
| 8 | from sp_rand import init as r_init |
| 9 | |
| 10 | TAG = "testvol" |
| 11 | RE_PREFIX = re.compile(r"^ testvol-\d{8}-\d{4}-\d{4}- $", re.X) |
| 12 | |
| 13 | |
| 14 | def 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 | |
| 21 | def 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 | |
| 46 | def test_store() -> None: |
| 47 | """Make sure the prefix is stored into the config file.""" |
| 48 | do_test_store(False) |
| 49 | |
| 50 | |
| 51 | def test_store_noop() -> None: |
| 52 | """Make sure the prefix is not stored into the config file.""" |
| 53 | do_test_store(True) |