github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/rest_api/tests/unit/test_config.py (about)

     1  # Copyright 2017 Intel Corporation
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #     http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  # ------------------------------------------------------------------------------
    15  import os
    16  import unittest
    17  import shutil
    18  import tempfile
    19  
    20  from sawtooth_rest_api.config import load_default_rest_api_config
    21  from sawtooth_rest_api.config import load_toml_rest_api_config
    22  from sawtooth_rest_api.exceptions import RestApiConfigurationError
    23  
    24  
    25  class TestRestApiConfig(unittest.TestCase):
    26      def test_rest_api_defaults_sawtooth_home(self):
    27          """Tests the default REST API configuration.
    28  
    29              - bind = ["127.0.0.1:8008"]
    30              - connect = "tcp://localhost:4004"
    31              - timeout = 300
    32  
    33          """
    34          config = load_default_rest_api_config()
    35          self.assertEqual(config.bind, ["127.0.0.1:8008"])
    36          self.assertEqual(config.connect, "tcp://localhost:4004")
    37          self.assertEqual(config.timeout, 300)
    38  
    39      def test_rest_api_config_load_from_file(self):
    40          """Tests loading config settings from a TOML configuration file.
    41  
    42          Sets the SAWTOOTH_HOME environment variable to a temporary directory,
    43          writes a rest_api.toml config file, then loads that config and verifies
    44          all the rest_api settings are their expected values.
    45  
    46          The test also attempts to avoid environment variables from interfering
    47          with the test by clearing os.environ and restoring it after the test.
    48          """
    49          orig_environ = dict(os.environ)
    50          os.environ.clear()
    51          directory = tempfile.mkdtemp(prefix="test-path-config-")
    52          try:
    53              os.environ['SAWTOOTH_HOME'] = directory
    54  
    55              config_dir = os.path.join(directory, 'etc')
    56              os.mkdir(config_dir)
    57              filename = os.path.join(config_dir, 'rest_api.toml')
    58              with open(filename, 'w') as fd:
    59                  fd.write('bind = ["test:1234"]')
    60                  fd.write(os.linesep)
    61                  fd.write('connect = "tcp://test:4004"')
    62                  fd.write(os.linesep)
    63                  fd.write('timeout = 10')
    64                  fd.write(os.linesep)
    65                  fd.write('opentsdb_db = "data_base"')
    66                  fd.write(os.linesep)
    67                  fd.write('opentsdb_url = "http://data_base:0000"')
    68                  fd.write(os.linesep)
    69                  fd.write('opentsdb_username = "name"')
    70                  fd.write(os.linesep)
    71                  fd.write('opentsdb_password = "secret"')
    72  
    73              config = load_toml_rest_api_config(filename)
    74              self.assertEqual(config.bind, ["test:1234"])
    75              self.assertEqual(config.connect, "tcp://test:4004")
    76              self.assertEqual(config.timeout, 10)
    77              self.assertEqual(config.opentsdb_db, "data_base")
    78              self.assertEqual(config.opentsdb_url, "http://data_base:0000")
    79              self.assertEqual(config.opentsdb_username, "name")
    80              self.assertEqual(config.opentsdb_password, "secret")
    81  
    82          finally:
    83              os.environ.clear()
    84              os.environ.update(orig_environ)
    85              shutil.rmtree(directory)
    86  
    87      def test_path_config_invalid_setting_in_file(self):
    88          """Tests detecting invalid settings defined in a TOML configuration
    89          file.
    90  
    91          Sets the SAWTOOTH_HOME environment variable to a temporary directory,
    92          writes a rest_api.toml config file with an invalid setting inside, then
    93          loads that config and verifies an exception is thrown.
    94  
    95          The test also attempts to avoid environment variables from interfering
    96          with the test by clearing os.environ and restoring it after the test.
    97          """
    98          orig_environ = dict(os.environ)
    99          os.environ.clear()
   100          directory = tempfile.mkdtemp(prefix="test-path-config-")
   101          try:
   102              os.environ['SAWTOOTH_HOME'] = directory
   103  
   104              config_dir = os.path.join(directory, 'etc')
   105              os.mkdir(config_dir)
   106              filename = os.path.join(config_dir, 'rest_api.toml')
   107              with open(filename, 'w') as fd:
   108                  fd.write('invalid = "a value"')
   109                  fd.write(os.linesep)
   110  
   111              with self.assertRaises(RestApiConfigurationError):
   112                  load_toml_rest_api_config(filename)
   113          finally:
   114              os.environ.clear()
   115              os.environ.update(orig_environ)
   116              shutil.rmtree(directory)