Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
300 changes: 184 additions & 116 deletions rest_api/tests/unit/test_config.py
Original file line number Diff line number Diff line change
@@ -1,116 +1,184 @@
# Copyright 2017 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------------------------
import os
import unittest
import shutil
import tempfile

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.exceptions import RestApiConfigurationError


class TestRestApiConfig(unittest.TestCase):
def test_rest_api_defaults_sawtooth_home(self):
"""Tests the default REST API configuration.

- bind = ["127.0.0.1:8008"]
- connect = "tcp://localhost:4004"
- timeout = 300

"""
config = load_default_rest_api_config()
self.assertEqual(config.bind, ["127.0.0.1:8008"])
self.assertEqual(config.connect, "tcp://localhost:4004")
self.assertEqual(config.timeout, 300)

def test_rest_api_config_load_from_file(self):
"""Tests loading config settings from a TOML configuration file.

Sets the SAWTOOTH_HOME environment variable to a temporary directory,
writes a rest_api.toml config file, then loads that config and verifies
all the rest_api settings are their expected values.

The test also attempts to avoid environment variables from interfering
with the test by clearing os.environ and restoring it after the test.
"""
orig_environ = dict(os.environ)
os.environ.clear()
directory = tempfile.mkdtemp(prefix="test-path-config-")
try:
os.environ['SAWTOOTH_HOME'] = directory

config_dir = os.path.join(directory, 'etc')
os.mkdir(config_dir)
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"')

config = load_toml_rest_api_config(filename)
self.assertEqual(config.bind, ["test:1234"])
self.assertEqual(config.connect, "tcp://test:4004")
self.assertEqual(config.timeout, 10)
self.assertEqual(config.opentsdb_db, "data_base")
self.assertEqual(config.opentsdb_url, "http://data_base:0000")
self.assertEqual(config.opentsdb_username, "name")
self.assertEqual(config.opentsdb_password, "secret")

finally:
os.environ.clear()
os.environ.update(orig_environ)
shutil.rmtree(directory)

def test_path_config_invalid_setting_in_file(self):
"""Tests detecting invalid settings defined in a TOML configuration
file.

Sets the SAWTOOTH_HOME environment variable to a temporary directory,
writes a rest_api.toml config file with an invalid setting inside, then
loads that config and verifies an exception is thrown.

The test also attempts to avoid environment variables from interfering
with the test by clearing os.environ and restoring it after the test.
"""
orig_environ = dict(os.environ)
os.environ.clear()
directory = tempfile.mkdtemp(prefix="test-path-config-")
try:
os.environ['SAWTOOTH_HOME'] = directory

config_dir = os.path.join(directory, 'etc')
os.mkdir(config_dir)
filename = os.path.join(config_dir, 'rest_api.toml')
with open(filename, 'w') as fd:
fd.write('invalid = "a value"')
fd.write(os.linesep)

with self.assertRaises(RestApiConfigurationError):
load_toml_rest_api_config(filename)
finally:
os.environ.clear()
os.environ.update(orig_environ)
shutil.rmtree(directory)
# Copyright 2017 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------------------------
import os
import unittest
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.

- bind = ["127.0.0.1:8008"]
- connect = "tcp://localhost:4004"
- timeout = 300

"""
config = load_default_rest_api_config()
self.assertEqual(config.bind, ["127.0.0.1:8008"])
self.assertEqual(config.connect, "tcp://localhost:4004")
self.assertEqual(config.timeout, 300)

def test_rest_api_config_load_from_file(self):
"""Tests loading config settings from a TOML configuration file.

Sets the SAWTOOTH_HOME environment variable to a temporary directory,
writes a rest_api.toml config file, then loads that config and verifies
all the rest_api settings are their expected values.

The test also attempts to avoid environment variables from interfering
with the test by clearing os.environ and restoring it after the test.
"""
orig_environ = dict(os.environ)
os.environ.clear()
directory = tempfile.mkdtemp(prefix="test-path-config-")
try:
os.environ['SAWTOOTH_HOME'] = directory

config_dir = os.path.join(directory, 'etc')
os.mkdir(config_dir)
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"')

config = load_toml_rest_api_config(filename)
self.assertEqual(config.bind, ["test:1234"])
self.assertEqual(config.connect, "tcp://test:4004")
self.assertEqual(config.timeout, 10)
self.assertEqual(config.opentsdb_db, "data_base")
self.assertEqual(config.opentsdb_url, "http://data_base:0000")
self.assertEqual(config.opentsdb_username, "name")
self.assertEqual(config.opentsdb_password, "secret")

finally:
os.environ.clear()
os.environ.update(orig_environ)
shutil.rmtree(directory)

def test_path_config_invalid_setting_in_file(self):
"""Tests detecting invalid settings defined in a TOML configuration
file.

Sets the SAWTOOTH_HOME environment variable to a temporary directory,
writes a rest_api.toml config file with an invalid setting inside, then
loads that config and verifies an exception is thrown.

The test also attempts to avoid environment variables from interfering
with the test by clearing os.environ and restoring it after the test.
"""
orig_environ = dict(os.environ)
os.environ.clear()
directory = tempfile.mkdtemp(prefix="test-path-config-")
try:
os.environ['SAWTOOTH_HOME'] = directory

config_dir = os.path.join(directory, 'etc')
os.mkdir(config_dir)
filename = os.path.join(config_dir, 'rest_api.toml')
with open(filename, 'w') as fd:
fd.write('invalid = "a value"')
fd.write(os.linesep)

with self.assertRaises(RestApiConfigurationError):
load_toml_rest_api_config(filename)
finally:
os.environ.clear()
os.environ.update(orig_environ)
shutil.rmtree(directory)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be one blank line before every new method definition.

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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a space after the comma

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"),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@askmish is this the correct depth of indentation?

('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(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make sure to add blank lines to the code where it makes sense, so that the code becomes more readable

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)
40 changes: 40 additions & 0 deletions rest_api/tests/unit/test_rest_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2018 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------------------------
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)