Skip to content

Commit 84ee41b

Browse files
committed
rest-api:test cases added for test_config and test_rest_api
1 parent 9fa5f09 commit 84ee41b

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

rest_api/tests/unit/test_config.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@
1717
import shutil
1818
import tempfile
1919

20+
from collections import OrderedDict
21+
2022
from sawtooth_rest_api.config import load_default_rest_api_config
2123
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
2226
from sawtooth_rest_api.exceptions import RestApiConfigurationError
2327

2428

29+
30+
2531
class TestRestApiConfig(unittest.TestCase):
2632
def test_rest_api_defaults_sawtooth_home(self):
2733
"""Tests the default REST API configuration.
@@ -114,3 +120,66 @@ def test_path_config_invalid_setting_in_file(self):
114120
os.environ.clear()
115121
os.environ.update(orig_environ)
116122
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+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
import unittest
3+
import tempfile
4+
import logging
5+
6+
from sawtooth_rest_api.rest_api import load_rest_api_config
7+
from sawtooth_rest_api.config import RestApiConfig
8+
9+
LOGGER = logging.getLogger(__name__)
10+
class Testparseargs(unittest.TestCase):
11+
def test_load_rest_api_config(self):
12+
"""Tests function test_load_rest_api_config()
13+
to check if it returns a RestApiConfig created
14+
by loading a TOML file from the filesystem."""
15+
first_config = RestApiConfig(
16+
bind="test:1234",
17+
connect="tcp://test:4004",
18+
timeout=10,
19+
opentsdb_url="data_base",
20+
opentsdb_db="http://data_base:0000")
21+
os.environ.clear()
22+
directory = tempfile.mkdtemp(prefix="test-path-config-")
23+
os.environ['SAWTOOTH_HOME'] = directory
24+
config_dir = os.path.join(directory, 'etc')
25+
os.mkdir(config_dir)
26+
load_rest_api_config(first_config)
27+

0 commit comments

Comments
 (0)