|
17 | 17 | import shutil |
18 | 18 | import tempfile |
19 | 19 |
|
| 20 | +from collections import OrderedDict |
| 21 | + |
20 | 22 | from sawtooth_rest_api.config import load_default_rest_api_config |
21 | 23 | from sawtooth_rest_api.config import load_toml_rest_api_config |
| 24 | +from sawtooth_rest_api.config import merge_rest_api_config |
| 25 | +from sawtooth_rest_api.config import RestApiConfig |
22 | 26 | from sawtooth_rest_api.exceptions import RestApiConfigurationError |
23 | 27 |
|
24 | 28 |
|
| 29 | + |
| 30 | + |
25 | 31 | class TestRestApiConfig(unittest.TestCase): |
26 | 32 | def test_rest_api_defaults_sawtooth_home(self): |
27 | 33 | """Tests the default REST API configuration. |
@@ -114,3 +120,66 @@ def test_path_config_invalid_setting_in_file(self): |
114 | 120 | os.environ.clear() |
115 | 121 | os.environ.update(orig_environ) |
116 | 122 | shutil.rmtree(directory) |
| 123 | + def test_load_toml_rest_api_config(self): |
| 124 | + '''Tests with the file name which does not exists ''' |
| 125 | + filename = os.path.join('res_api.toml') |
| 126 | + load_toml_rest_api_config(filename) |
| 127 | + def test_rest_api_config_load_merge(self): |
| 128 | + '''Tests the "merge_rest_api_config(configs)" function. |
| 129 | + Given a list of PathConfig objects, merges them into |
| 130 | + a single PathConfig,giving priority in the order of |
| 131 | + the configs (first has highest priority).''' |
| 132 | + os.environ.clear() |
| 133 | + directory = tempfile.mkdtemp(prefix="test-path-config-") |
| 134 | + os.environ['SAWTOOTH_HOME'] = directory |
| 135 | + config_dir = os.path.join(directory, 'etc') |
| 136 | + os.mkdir(config_dir) |
| 137 | + conf_file = os.path.join(config_dir, 'rest_api.toml') |
| 138 | + toml_str = ['bind = "test:1234"', 'connect = "tcp://test:4004"', |
| 139 | + 'timeout = 10', 'opentsdb_url = "data_base"', |
| 140 | + 'opentsdb_db = "http://data_base:0000"', |
| 141 | + 'opentsdb_username = "name"', 'opentsdb_password = "secret"'] |
| 142 | + default_dict = OrderedDict([ |
| 143 | + ('bind', "test:1234"), |
| 144 | + ('connect', "tcp://test:4004"), |
| 145 | + ('timeout', 10), |
| 146 | + ('opentsdb_url', "data_base"), |
| 147 | + ('opentsdb_db', "http://data_base:0000"), |
| 148 | + ('opentsdb_username', "name"), |
| 149 | + ('opentsdb_password', "secret")] |
| 150 | + ) |
| 151 | + first_config = RestApiConfig( |
| 152 | + bind="test:1234", |
| 153 | + connect="tcp://test:4004", |
| 154 | + timeout=10, |
| 155 | + opentsdb_url="data_base", |
| 156 | + opentsdb_db="http://data_base:0000") |
| 157 | + try: |
| 158 | + filename = os.path.join(config_dir, 'rest_api.toml') |
| 159 | + with open(filename, 'w') as fd: |
| 160 | + fd.write('bind = ["test:1234"]') |
| 161 | + fd.write(os.linesep) |
| 162 | + fd.write('connect = "tcp://test:4004"') |
| 163 | + fd.write(os.linesep) |
| 164 | + fd.write('timeout = 10') |
| 165 | + fd.write(os.linesep) |
| 166 | + fd.write('opentsdb_db = "data_base"') |
| 167 | + fd.write(os.linesep) |
| 168 | + fd.write('opentsdb_url = "http://data_base:0000"') |
| 169 | + fd.write(os.linesep) |
| 170 | + fd.write('opentsdb_username = "name"') |
| 171 | + fd.write(os.linesep) |
| 172 | + fd.write('opentsdb_password = "secret"') |
| 173 | + finally: |
| 174 | + pass |
| 175 | + toml_config = load_toml_rest_api_config(conf_file) |
| 176 | + default_config = load_default_rest_api_config() |
| 177 | + merge_rest_api_config( |
| 178 | + configs=[first_config, toml_config, default_config]).__repr__() |
| 179 | + result_dict = merge_rest_api_config( |
| 180 | + configs=[first_config, toml_config, default_config]).to_dict() |
| 181 | + toml_string = merge_rest_api_config( |
| 182 | + configs=[first_config, toml_config, default_config]).to_toml_string() |
| 183 | + self.assertDictEqual(default_dict, result_dict) |
| 184 | + self.assertEqual(toml_str, toml_string) |
| 185 | + |
0 commit comments