|
| 1 | +import pytest |
| 2 | +import os |
| 3 | +from importlib import import_module |
| 4 | +from plenum.common.config_util import extend_with_external_config, extend_with_default_external_config |
| 5 | + |
| 6 | + |
| 7 | +TEST_NETWORK_NAME = 'test_network' |
| 8 | +GENERAL_CONFIG_FILE_NAME = 'test_config.py' |
| 9 | +USER_CONFIG_FILE_NAME = 'user_config.py' |
| 10 | +NETWORK_CONFIG_FILE_NAME = 'net_config.py' |
| 11 | +CFG_REPL_VALS = [1, 2, 3] |
| 12 | + |
| 13 | +@pytest.fixture(scope="function") |
| 14 | +def default_config(tdir): |
| 15 | + plenum_cfg = import_module("plenum.config") |
| 16 | + cfg = {'NETWORK_NAME': TEST_NETWORK_NAME, |
| 17 | + 'GENERAL_CONFIG_DIR': tdir, |
| 18 | + 'GENERAL_CONFIG_FILE': GENERAL_CONFIG_FILE_NAME, |
| 19 | + 'NETWORK_CONFIG_FILE': NETWORK_CONFIG_FILE_NAME, |
| 20 | + 'baseDir': tdir, |
| 21 | + 'USER_CONFIG_FILE': USER_CONFIG_FILE_NAME, |
| 22 | + 'int_val': -1, |
| 23 | + 'str_val': 'value-1'} |
| 24 | + plenum_cfg.__dict__.update(cfg) |
| 25 | + return plenum_cfg |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture(scope="function") |
| 29 | +def default_config_file_system(default_config): |
| 30 | + assert os.path.isdir(default_config.baseDir) |
| 31 | + gen_net_dir = os.path.join(default_config.GENERAL_CONFIG_DIR, default_config.NETWORK_NAME) |
| 32 | + gen_base_dir = os.path.join(default_config.baseDir, default_config.NETWORK_NAME) |
| 33 | + os.makedirs(gen_net_dir, exist_ok=True) |
| 34 | + os.makedirs(gen_base_dir, exist_ok=True) |
| 35 | + assert os.path.isdir(gen_net_dir) |
| 36 | + assert os.path.isdir(gen_base_dir) |
| 37 | + gen_cfg_file = os.path.join(default_config.GENERAL_CONFIG_DIR, default_config.GENERAL_CONFIG_FILE) |
| 38 | + net_cfg_file = os.path.join(gen_net_dir, default_config.NETWORK_CONFIG_FILE) |
| 39 | + usr_cfg_file = os.path.join(gen_base_dir, default_config.USER_CONFIG_FILE) |
| 40 | + for i, f in zip(CFG_REPL_VALS, [gen_cfg_file, net_cfg_file, usr_cfg_file]): |
| 41 | + with open(f, "w") as file_cfg: |
| 42 | + file_cfg.write("int_val = {}\n".format(i)) |
| 43 | + file_cfg.write("str_val = 'value{}'\n".format(i)) |
| 44 | + assert os.path.isfile(gen_cfg_file) |
| 45 | + assert os.path.isfile(net_cfg_file) |
| 46 | + assert os.path.isfile(usr_cfg_file) |
| 47 | + |
| 48 | + |
| 49 | +def test_extend_with_external_config_req_exist(default_config, default_config_file_system): |
| 50 | + assert default_config.int_val == -1 |
| 51 | + assert default_config.str_val == 'value-1' |
| 52 | + extend_with_external_config(default_config, (default_config.GENERAL_CONFIG_DIR, default_config.GENERAL_CONFIG_FILE), |
| 53 | + required=True) |
| 54 | + assert default_config.int_val == 1 |
| 55 | + assert default_config.str_val == 'value1' |
| 56 | + |
| 57 | + |
| 58 | +def test_extend_with_external_config_req_not_exist(default_config, default_config_file_system): |
| 59 | + assert default_config.int_val == -1 |
| 60 | + assert default_config.str_val == 'value-1' |
| 61 | + with pytest.raises(FileNotFoundError): |
| 62 | + extend_with_external_config(default_config, ("/some/bad/path", "some_bad.py"), required=True) |
| 63 | + assert default_config.int_val == -1 |
| 64 | + assert default_config.str_val == 'value-1' |
| 65 | + |
| 66 | + |
| 67 | +def test_extend_with_external_config_not_req_exist(default_config, default_config_file_system): |
| 68 | + assert default_config.int_val == -1 |
| 69 | + assert default_config.str_val == 'value-1' |
| 70 | + extend_with_external_config(default_config, (default_config.GENERAL_CONFIG_DIR, default_config.GENERAL_CONFIG_FILE), |
| 71 | + required=False) |
| 72 | + assert default_config.int_val == 1 |
| 73 | + assert default_config.str_val == 'value1' |
| 74 | + |
| 75 | + |
| 76 | +def test_extend_with_external_config_not_req_not_exist(default_config, default_config_file_system): |
| 77 | + assert default_config.int_val == -1 |
| 78 | + assert default_config.str_val == 'value-1' |
| 79 | + extend_with_external_config(default_config, ("/some/bad/path", "some_bad.py"), required=False) |
| 80 | + assert default_config.int_val == -1 |
| 81 | + assert default_config.str_val == 'value-1' |
| 82 | + |
| 83 | + |
| 84 | +def test_extend_with_default_external_config_no_any(default_config, default_config_file_system): |
| 85 | + gen_net_dir = os.path.join(default_config.GENERAL_CONFIG_DIR, default_config.NETWORK_NAME) |
| 86 | + gen_base_dir = os.path.join(default_config.baseDir, default_config.NETWORK_NAME) |
| 87 | + gen_cfg_file = os.path.join(default_config.GENERAL_CONFIG_DIR, default_config.GENERAL_CONFIG_FILE) |
| 88 | + net_cfg_file = os.path.join(gen_net_dir, default_config.NETWORK_CONFIG_FILE) |
| 89 | + usr_cfg_file = os.path.join(gen_base_dir, default_config.USER_CONFIG_FILE) |
| 90 | + os.remove(gen_cfg_file) |
| 91 | + os.remove(net_cfg_file) |
| 92 | + os.remove(usr_cfg_file) |
| 93 | + assert default_config.int_val == -1 |
| 94 | + assert default_config.str_val == 'value-1' |
| 95 | + extend_with_default_external_config(default_config) |
| 96 | + assert default_config.int_val == -1 |
| 97 | + assert default_config.str_val == 'value-1' |
| 98 | + |
| 99 | + |
| 100 | +def test_extend_with_default_external_config_only_gen(default_config, default_config_file_system): |
| 101 | + gen_net_dir = os.path.join(default_config.GENERAL_CONFIG_DIR, default_config.NETWORK_NAME) |
| 102 | + gen_base_dir = os.path.join(default_config.baseDir, default_config.NETWORK_NAME) |
| 103 | + net_cfg_file = os.path.join(gen_net_dir, default_config.NETWORK_CONFIG_FILE) |
| 104 | + usr_cfg_file = os.path.join(gen_base_dir, default_config.USER_CONFIG_FILE) |
| 105 | + os.remove(net_cfg_file) |
| 106 | + os.remove(usr_cfg_file) |
| 107 | + assert default_config.int_val == -1 |
| 108 | + assert default_config.str_val == 'value-1' |
| 109 | + extend_with_default_external_config(default_config) |
| 110 | + assert default_config.int_val == 1 |
| 111 | + assert default_config.str_val == 'value1' |
| 112 | + |
| 113 | + |
| 114 | +def test_extend_with_default_external_config_gen_net(default_config, default_config_file_system): |
| 115 | + gen_base_dir = os.path.join(default_config.baseDir, default_config.NETWORK_NAME) |
| 116 | + usr_cfg_file = os.path.join(gen_base_dir, default_config.USER_CONFIG_FILE) |
| 117 | + os.remove(usr_cfg_file) |
| 118 | + assert default_config.int_val == -1 |
| 119 | + assert default_config.str_val == 'value-1' |
| 120 | + extend_with_default_external_config(default_config) |
| 121 | + assert default_config.int_val == 2 |
| 122 | + assert default_config.str_val == 'value2' |
| 123 | + |
| 124 | + |
| 125 | +def test_extend_with_default_external_config_all(default_config, default_config_file_system): |
| 126 | + assert default_config.int_val == -1 |
| 127 | + assert default_config.str_val == 'value-1' |
| 128 | + extend_with_default_external_config(default_config) |
| 129 | + assert default_config.int_val == 3 |
| 130 | + assert default_config.str_val == 'value3' |
0 commit comments