| """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) |