Tomáš Pecka | 04eb737 | 2022-06-07 08:03:04 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
Jan Kundrát | 86cc646 | 2024-05-07 19:28:39 +0200 | [diff] [blame] | 3 | import glob |
Tomáš Pecka | 04eb737 | 2022-06-07 08:03:04 +0200 | [diff] [blame] | 4 | import json |
| 5 | import re |
| 6 | import os |
| 7 | import pathlib |
Jan Kundrát | e950cf7 | 2024-09-06 17:46:11 +0200 | [diff] [blame] | 8 | import pytest |
Tomáš Pecka | 04eb737 | 2022-06-07 08:03:04 +0200 | [diff] [blame] | 9 | import shutil |
| 10 | import subprocess |
| 11 | import sys |
| 12 | |
Tomáš Pecka | 04eb737 | 2022-06-07 08:03:04 +0200 | [diff] [blame] | 13 | |
| 14 | SCRIPT_ROOT = pathlib.Path(__file__).resolve().parent |
Jan Kundrát | e950cf7 | 2024-09-06 17:46:11 +0200 | [diff] [blame] | 15 | BR2_EXT_ROOT = (SCRIPT_ROOT / '../../').resolve() |
| 16 | CFG_FS_SCRIPTS_PATH = BR2_EXT_ROOT / 'package' / 'czechlight-cfg-fs' |
Tomáš Pecka | 04eb737 | 2022-06-07 08:03:04 +0200 | [diff] [blame] | 17 | |
Jan Kundrát | e950cf7 | 2024-09-06 17:46:11 +0200 | [diff] [blame] | 18 | def run_and_wait(ctx, command_args): |
| 19 | print(f'RUN {command_args}') |
Tomáš Pecka | 04eb737 | 2022-06-07 08:03:04 +0200 | [diff] [blame] | 20 | with subprocess.Popen(command_args, stdout=sys.stdout, stderr=sys.stderr, env=ctx.get_env()) as proc: |
| 21 | proc.wait() |
| 22 | assert proc.returncode == 0 |
| 23 | |
| 24 | |
| 25 | class SysrepoFixture: |
| 26 | def __init__(self, test_directory, tmp_path): |
Jan Kundrát | e950cf7 | 2024-09-06 17:46:11 +0200 | [diff] [blame] | 27 | self.test_directory = SCRIPT_ROOT / test_directory |
| 28 | test_name = self.test_directory.name |
| 29 | self.running_directory = tmp_path / test_name |
| 30 | self.running_directory.mkdir() |
| 31 | self.tmp_path = tmp_path |
Tomáš Pecka | 04eb737 | 2022-06-07 08:03:04 +0200 | [diff] [blame] | 32 | |
Jan Kundrát | e950cf7 | 2024-09-06 17:46:11 +0200 | [diff] [blame] | 33 | for fn in ('startup.json', 'version'): |
| 34 | try: |
| 35 | shutil.copy(self.test_directory / fn, self.running_directory) |
| 36 | except FileNotFoundError: |
| 37 | pass |
Tomáš Pecka | 04eb737 | 2022-06-07 08:03:04 +0200 | [diff] [blame] | 38 | |
Jan Kundrát | e950cf7 | 2024-09-06 17:46:11 +0200 | [diff] [blame] | 39 | query_file = self.test_directory / 'query' |
| 40 | self.query = query_file.read_text() if query_file.is_file() \ |
| 41 | else 'del(."ietf-keystore:keystore"."asymmetric-keys"."asymmetric-key"[0]."cleartext-private-key")' |
Tomáš Pecka | 04eb737 | 2022-06-07 08:03:04 +0200 | [diff] [blame] | 42 | |
Jan Kundrát | 86cc646 | 2024-05-07 19:28:39 +0200 | [diff] [blame] | 43 | # all tests must run with clean sysrepo state |
Jan Kundrát | e950cf7 | 2024-09-06 17:46:11 +0200 | [diff] [blame] | 44 | self._shm_prefix = 'br2-migr-' + test_name |
| 45 | self._repo_path = self.running_directory / 'sysrepo_repository' |
Jan Kundrát | 86cc646 | 2024-05-07 19:28:39 +0200 | [diff] [blame] | 46 | |
Tomáš Pecka | 04eb737 | 2022-06-07 08:03:04 +0200 | [diff] [blame] | 47 | def get_env(self): |
| 48 | res = os.environ.copy() |
Jan Kundrát | 86cc646 | 2024-05-07 19:28:39 +0200 | [diff] [blame] | 49 | res['SYSREPO_SHM_PREFIX'] = self._shm_prefix |
Jan Kundrát | e950cf7 | 2024-09-06 17:46:11 +0200 | [diff] [blame] | 50 | res['SYSREPO_REPOSITORY_PATH'] = self._repo_path |
| 51 | res['LN2_MODULE_DIR'] = pathlib.Path(os.environ['LIBNETCONF2_SRCDIR']) / 'modules' |
| 52 | res['NP2_MODULE_DIR'] = pathlib.Path(os.environ['NETOPEER2_SRCDIR']) / 'modules' |
| 53 | res['NETOPEER2_SETUP_DIR'] = BR2_EXT_ROOT / 'submodules' / 'buildroot' / 'package' / 'netopeer2' |
Jan Kundrát | 07a8727 | 2024-04-22 23:40:17 +0200 | [diff] [blame] | 54 | res['CLA_YANG'] = pathlib.Path(os.environ['CLA_SYSREPO_SRCDIR']) / 'yang' |
| 55 | res['VELIA_YANG'] = pathlib.Path(os.environ['VELIA_SRCDIR']) / 'yang' |
Jan Kundrát | 07a8727 | 2024-04-22 23:40:17 +0200 | [diff] [blame] | 56 | res['ALARMS_YANG'] = pathlib.Path(os.environ['SYSREPO_IETF_ALARMS_SRCDIR']) / 'yang' |
Jan Kundrát | e950cf7 | 2024-09-06 17:46:11 +0200 | [diff] [blame] | 57 | res['ROUSETTE_YANG'] = pathlib.Path(os.environ['ROUSETTE_SRCDIR']) / 'yang' |
| 58 | res['CFG_FS_YANG'] = CFG_FS_SCRIPTS_PATH / 'yang' |
| 59 | res['CFG_STATIC_DATA'] = CFG_FS_SCRIPTS_PATH / 'static-data' |
| 60 | res['VELIA_STATIC_DATA'] = pathlib.Path(os.environ['VELIA_SRCDIR']) / 'yang' |
| 61 | res['CLA_STATIC_DATA'] = pathlib.Path(os.environ['CLA_SYSREPO_SRCDIR']) / 'yang' |
| 62 | res['PROC_CMDLINE'] = self.test_directory / 'cmdline' |
| 63 | res['CFG_SYSREPO_DIR'] = self.running_directory |
| 64 | res['CURRENT_VERSION_FILE'] = CFG_FS_SCRIPTS_PATH / 'CURRENT_CONFIG_VERSION' |
| 65 | res['TMPDIR'] = self.tmp_path |
Tomáš Pecka | 04eb737 | 2022-06-07 08:03:04 +0200 | [diff] [blame] | 66 | return res |
| 67 | |
Jan Kundrát | e950cf7 | 2024-09-06 17:46:11 +0200 | [diff] [blame] | 68 | def nuke_shm(self): |
| 69 | for f in glob.glob(f'/dev/shm/{self._shm_prefix}*'): |
| 70 | os.remove(f) |
| 71 | |
Tomáš Pecka | 04eb737 | 2022-06-07 08:03:04 +0200 | [diff] [blame] | 72 | |
| 73 | @pytest.fixture(scope='session') |
| 74 | def max_version(): |
Jan Kundrát | e950cf7 | 2024-09-06 17:46:11 +0200 | [diff] [blame] | 75 | return (CFG_FS_SCRIPTS_PATH / 'CURRENT_CONFIG_VERSION').read_text() |
Tomáš Pecka | 04eb737 | 2022-06-07 08:03:04 +0200 | [diff] [blame] | 76 | |
| 77 | |
| 78 | @pytest.fixture |
| 79 | def sysrepo_fixture(request, tmp_path): |
| 80 | return SysrepoFixture(pathlib.Path(SCRIPT_ROOT / 'data' / request.param), tmp_path) |
| 81 | |
| 82 | |
| 83 | def find_test_directories(): |
| 84 | return [pytest.param(dirname) for dirname in os.listdir(path=SCRIPT_ROOT / 'data')] |
| 85 | |
| 86 | |
| 87 | @pytest.mark.parametrize("sysrepo_fixture", find_test_directories(), indirect=True) |
| 88 | def test(sysrepo_fixture, max_version): |
Jan Kundrát | e950cf7 | 2024-09-06 17:46:11 +0200 | [diff] [blame] | 89 | # prevent running with a stale SHM state |
| 90 | sysrepo_fixture.nuke_shm() |
Tomáš Pecka | 04eb737 | 2022-06-07 08:03:04 +0200 | [diff] [blame] | 91 | |
Jan Kundrát | e950cf7 | 2024-09-06 17:46:11 +0200 | [diff] [blame] | 92 | run_and_wait(sysrepo_fixture, [CFG_FS_SCRIPTS_PATH / "cfg-migrate.sh"]) |
| 93 | run_and_wait(sysrepo_fixture, [CFG_FS_SCRIPTS_PATH / "cfg-yang.sh"]) |
Tomáš Pecka | 04eb737 | 2022-06-07 08:03:04 +0200 | [diff] [blame] | 94 | |
Jan Kundrát | e950cf7 | 2024-09-06 17:46:11 +0200 | [diff] [blame] | 95 | assert (sysrepo_fixture.running_directory / 'version').read_text() == max_version |
Tomáš Pecka | 04eb737 | 2022-06-07 08:03:04 +0200 | [diff] [blame] | 96 | |
Jan Kundrát | e950cf7 | 2024-09-06 17:46:11 +0200 | [diff] [blame] | 97 | dump = subprocess.run(['sysrepocfg', '-f', 'json', '-d', 'startup', '-X'], check=True, capture_output=True, env=sysrepo_fixture.get_env()) |
| 98 | jq = subprocess.run(['jq', sysrepo_fixture.query], input=dump.stdout, check=True, capture_output=True) |
| 99 | actual = json.loads(jq.stdout.decode('utf-8')) |
| 100 | with open(sysrepo_fixture.test_directory / 'expected.json', 'r') as fp_expected: |
| 101 | expected = json.load(fp_expected) |
| 102 | assert actual == expected |
Tomáš Pecka | 04eb737 | 2022-06-07 08:03:04 +0200 | [diff] [blame] | 103 | |
Jan Kundrát | e950cf7 | 2024-09-06 17:46:11 +0200 | [diff] [blame] | 104 | sysrepo_fixture.nuke_shm() |