diff --git a/rest_api/tests/unit/test_config.py b/rest_api/tests/unit/test_config.py index 08c3814a7f..86b0445da6 100644 --- a/rest_api/tests/unit/test_config.py +++ b/rest_api/tests/unit/test_config.py @@ -17,11 +17,17 @@ import shutil import tempfile +from collections import OrderedDict + from sawtooth_rest_api.config import load_default_rest_api_config from sawtooth_rest_api.config import load_toml_rest_api_config +from sawtooth_rest_api.config import merge_rest_api_config +from sawtooth_rest_api.config import RestApiConfig from sawtooth_rest_api.exceptions import RestApiConfigurationError + + class TestRestApiConfig(unittest.TestCase): def test_rest_api_defaults_sawtooth_home(self): """Tests the default REST API configuration. @@ -114,3 +120,66 @@ def test_path_config_invalid_setting_in_file(self): os.environ.clear() os.environ.update(orig_environ) shutil.rmtree(directory) + def test_load_toml_rest_api_config(self): + '''Tests with the file name which does not exists ''' + filename = os.path.join('res_api.toml') + load_toml_rest_api_config(filename) + def test_rest_api_config_load_merge(self): + '''Tests the "merge_rest_api_config(configs)" function. + Given a list of PathConfig objects, merges them into + a single PathConfig,giving priority in the order of + the configs (first has highest priority).''' + os.environ.clear() + directory = tempfile.mkdtemp(prefix="test-path-config-") + os.environ['SAWTOOTH_HOME'] = directory + config_dir = os.path.join(directory, 'etc') + os.mkdir(config_dir) + conf_file = os.path.join(config_dir, 'rest_api.toml') + toml_str = ['bind = "test:1234"', 'connect = "tcp://test:4004"', + 'timeout = 10', 'opentsdb_url = "data_base"', + 'opentsdb_db = "http://data_base:0000"', + 'opentsdb_username = "name"', 'opentsdb_password = "secret"'] + default_dict = OrderedDict([ + ('bind', "test:1234"), + ('connect', "tcp://test:4004"), + ('timeout', 10), + ('opentsdb_url', "data_base"), + ('opentsdb_db', "http://data_base:0000"), + ('opentsdb_username', "name"), + ('opentsdb_password', "secret")] + ) + first_config = RestApiConfig( + bind="test:1234", + connect="tcp://test:4004", + timeout=10, + opentsdb_url="data_base", + opentsdb_db="http://data_base:0000") + try: + filename = os.path.join(config_dir, 'rest_api.toml') + with open(filename, 'w') as fd: + fd.write('bind = ["test:1234"]') + fd.write(os.linesep) + fd.write('connect = "tcp://test:4004"') + fd.write(os.linesep) + fd.write('timeout = 10') + fd.write(os.linesep) + fd.write('opentsdb_db = "data_base"') + fd.write(os.linesep) + fd.write('opentsdb_url = "http://data_base:0000"') + fd.write(os.linesep) + fd.write('opentsdb_username = "name"') + fd.write(os.linesep) + fd.write('opentsdb_password = "secret"') + finally: + pass + toml_config = load_toml_rest_api_config(conf_file) + default_config = load_default_rest_api_config() + merge_rest_api_config( + configs=[first_config, toml_config, default_config]).__repr__() + result_dict = merge_rest_api_config( + configs=[first_config, toml_config, default_config]).to_dict() + toml_string = merge_rest_api_config( + configs=[first_config, toml_config, default_config]).to_toml_string() + self.assertDictEqual(default_dict, result_dict) + self.assertEqual(toml_str, toml_string) + diff --git a/rest_api/tests/unit/test_rest_api.py b/rest_api/tests/unit/test_rest_api.py new file mode 100644 index 0000000000..4097b284ac --- /dev/null +++ b/rest_api/tests/unit/test_rest_api.py @@ -0,0 +1,27 @@ +import os +import unittest +import tempfile +import logging + +from sawtooth_rest_api.rest_api import load_rest_api_config +from sawtooth_rest_api.config import RestApiConfig + +LOGGER = logging.getLogger(__name__) +class Testparseargs(unittest.TestCase): + def test_load_rest_api_config(self): + """Tests function test_load_rest_api_config() + to check if it returns a RestApiConfig created + by loading a TOML file from the filesystem.""" + first_config = RestApiConfig( + bind="test:1234", + connect="tcp://test:4004", + timeout=10, + opentsdb_url="data_base", + opentsdb_db="http://data_base:0000") + os.environ.clear() + directory = tempfile.mkdtemp(prefix="test-path-config-") + os.environ['SAWTOOTH_HOME'] = directory + config_dir = os.path.join(directory, 'etc') + os.mkdir(config_dir) + load_rest_api_config(first_config) + \ No newline at end of file