github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/validator/tests/test_config/tests.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  
    16  import os
    17  import unittest
    18  import shutil
    19  import tempfile
    20  
    21  from sawtooth_validator.config.path import load_path_config
    22  from sawtooth_validator.exceptions import LocalConfigurationError
    23  from sawtooth_validator.config.validator import load_default_validator_config
    24  from sawtooth_validator.config.validator import load_toml_validator_config
    25  
    26  
    27  class TestPathConfig(unittest.TestCase):
    28      def test_path_config_defaults_sawtooth_home(self):
    29          """Tests the default path configuration settings when SAWTOOTH_HOME
    30          is set.
    31  
    32          Sets the SAWTOOTH_HOME environment variable to
    33          /tmp/no-such-sawtooth-home, then compares all path settings for their
    34          expected values.
    35  
    36              - config_dir = /tmp/no-such-sawtooth-home/etc
    37              - key_dir    = /tmp/no-such-sawtooth-home/keys
    38              - data_dir   = /tmp/no-such-sawtooth-home/data
    39              - log_dir    = /tmp/no-such-sawtooth-home/logs
    40  
    41          Also specifies a configuration directory which does not exist (as we
    42          want to avoid loading any files for this test).  We use a different
    43          directory than SAWTOOTH_HOME as the config directory specified for
    44          config files should not impact the settings.
    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          try:
    52              os.environ['SAWTOOTH_HOME'] = '/tmp/no-such-sawtooth-home'
    53  
    54              config = load_path_config()
    55              self.assertEqual(config.config_dir,
    56                               "/tmp/no-such-sawtooth-home/etc")
    57              self.assertEqual(config.key_dir,
    58                               "/tmp/no-such-sawtooth-home/keys")
    59              self.assertEqual(config.data_dir,
    60                               "/tmp/no-such-sawtooth-home/data")
    61              self.assertEqual(config.log_dir,
    62                               "/tmp/no-such-sawtooth-home/logs")
    63              self.assertEqual(config.policy_dir,
    64                               "/tmp/no-such-sawtooth-home/policy")
    65          finally:
    66              os.environ.clear()
    67              os.environ.update(orig_environ)
    68  
    69      def test_path_config_load_from_file(self):
    70          """Tests loading config settings from a TOML configuration file.
    71  
    72          Sets the SAWTOOTH_HOME environment variable to a temporary directory,
    73          writes a path.toml config file, then loads that config and verifies
    74          all the path settings are their expected values.
    75  
    76          The test also attempts to avoid environment variables from interfering
    77          with the test by clearing os.environ and restoring it after the test.
    78          """
    79          orig_environ = dict(os.environ)
    80          os.environ.clear()
    81          directory = tempfile.mkdtemp(prefix="test-path-config-")
    82          try:
    83              os.environ['SAWTOOTH_HOME'] = directory
    84  
    85              config_dir = os.path.join(directory, 'etc')
    86              os.mkdir(config_dir)
    87  
    88              with open(os.path.join(config_dir, 'path.toml'), 'w') as fd:
    89                  fd.write('key_dir = "/tmp/no-such-dir-from-config/keys"')
    90                  fd.write(os.linesep)
    91                  fd.write('data_dir = "/tmp/no-such-dir-from-config/data"')
    92                  fd.write(os.linesep)
    93                  fd.write('log_dir = "/tmp/no-such-dir-from-config/logs"')
    94                  fd.write(os.linesep)
    95                  fd.write('policy_dir = "/tmp/no-such-dir-from-config/policy"')
    96                  fd.write(os.linesep)
    97  
    98              config = load_path_config()
    99              self.assertEqual(config.config_dir, config_dir)
   100              self.assertEqual(config.key_dir,
   101                               "/tmp/no-such-dir-from-config/keys")
   102              self.assertEqual(config.data_dir,
   103                               "/tmp/no-such-dir-from-config/data")
   104              self.assertEqual(config.log_dir,
   105                               "/tmp/no-such-dir-from-config/logs")
   106              self.assertEqual(config.policy_dir,
   107                               "/tmp/no-such-dir-from-config/policy")
   108          finally:
   109              os.environ.clear()
   110              os.environ.update(orig_environ)
   111              shutil.rmtree(directory)
   112  
   113      def test_path_config_invalid_setting_in_file(self):
   114          """Tests detecting invalid settings defined in a TOML configuration
   115          file.
   116  
   117          Sets the SAWTOOTH_HOME environment variable to a temporary directory,
   118          writes a path.toml config file with an invalid setting inside, then
   119          loads that config and verifies an exception is thrown.
   120  
   121          The test also attempts to avoid environment variables from interfering
   122          with the test by clearing os.environ and restoring it after the test.
   123          """
   124          orig_environ = dict(os.environ)
   125          os.environ.clear()
   126          directory = tempfile.mkdtemp(prefix="test-path-config-")
   127          try:
   128              os.environ['SAWTOOTH_HOME'] = directory
   129  
   130              config_dir = os.path.join(directory, 'etc')
   131              os.mkdir(config_dir)
   132  
   133              with open(os.path.join(config_dir, 'path.toml'), 'w') as fd:
   134                  fd.write('invalid = "a value"')
   135                  fd.write(os.linesep)
   136  
   137              self.assertRaises(LocalConfigurationError, load_path_config)
   138          finally:
   139              os.environ.clear()
   140              os.environ.update(orig_environ)
   141              shutil.rmtree(directory)
   142  
   143  
   144  class TestValidatorConfig(unittest.TestCase):
   145      def test_validator_config_defaults(self):
   146          """Tests the default validator configuration when no other configs.
   147          The defaults should be as follows:
   148              - bind_network = "tcp://127.0.0.1:8800"
   149              - bind_component = "tcp://127.0.0.1:4004"
   150              - peering = "static"
   151              - endpoint = None
   152          """
   153          config = load_default_validator_config()
   154          self.assertEqual(config.bind_network, "tcp://127.0.0.1:8800")
   155          self.assertEqual(config.bind_component, "tcp://127.0.0.1:4004")
   156          self.assertEqual(config.endpoint, None)
   157          self.assertEqual(config.peering, "static")
   158          self.assertEqual(config.scheduler, "serial")
   159          self.assertEqual(config.minimum_peer_connectivity, 3)
   160          self.assertEqual(config.maximum_peer_connectivity, 10)
   161  
   162      def test_validator_config_load_from_file(self):
   163          """Tests loading config settings from a TOML configuration file.
   164  
   165          Creates a temporary directory and writes a validator.toml config file,
   166          then loads that config and verifies all the validator settings are
   167          their expected values.
   168  
   169          The test also attempts to avoid environment variables from interfering
   170          with the test by clearing os.environ and restoring it after the test.
   171          """
   172          orig_environ = dict(os.environ)
   173          os.environ.clear()
   174          directory = tempfile.mkdtemp(prefix="test-path-config-")
   175          try:
   176              os.environ['SAWTOOTH_HOME'] = directory
   177  
   178              config_dir = os.path.join(directory, 'etc')
   179              os.mkdir(config_dir)
   180              filename = os.path.join(config_dir, 'validator.toml')
   181              with open(filename, 'w') as fd:
   182                  fd.write('bind = ["network:tcp://test:8800",'
   183                           '"component:tcp://test:4004"]')
   184                  fd.write(os.linesep)
   185                  fd.write('peering = "dynamic"')
   186                  fd.write(os.linesep)
   187                  fd.write('endpoint = "tcp://test:8800"')
   188                  fd.write(os.linesep)
   189                  fd.write('peers = ["tcp://peer:8801"]')
   190                  fd.write(os.linesep)
   191                  fd.write('seeds = ["tcp://peer:8802"]')
   192                  fd.write(os.linesep)
   193                  fd.write('scheduler = "serial"')
   194                  fd.write(os.linesep)
   195                  fd.write('opentsdb_db = "data_base"')
   196                  fd.write(os.linesep)
   197                  fd.write('opentsdb_url = "http://data_base:0000"')
   198                  fd.write(os.linesep)
   199                  fd.write('opentsdb_username = "name"')
   200                  fd.write(os.linesep)
   201                  fd.write('opentsdb_password = "secret"')
   202                  fd.write(os.linesep)
   203                  fd.write('minimum_peer_connectivity = 1')
   204                  fd.write(os.linesep)
   205                  fd.write('maximum_peer_connectivity = 100')
   206                  fd.write(os.linesep)
   207                  fd.write('[roles]')
   208                  fd.write(os.linesep)
   209                  fd.write('network = "trust"')
   210                  fd.write(os.linesep)
   211  
   212              config = load_toml_validator_config(filename)
   213              self.assertEqual(config.bind_network, "tcp://test:8800")
   214              self.assertEqual(config.bind_component, "tcp://test:4004")
   215              self.assertEqual(config.peering, "dynamic")
   216              self.assertEqual(config.endpoint, "tcp://test:8800")
   217              self.assertEqual(config.peers, ["tcp://peer:8801"])
   218              self.assertEqual(config.seeds, ["tcp://peer:8802"])
   219              self.assertEqual(config.scheduler, "serial")
   220              self.assertEqual(config.roles, {"network": "trust"})
   221              self.assertEqual(config.opentsdb_db, "data_base")
   222              self.assertEqual(config.opentsdb_url, "http://data_base:0000")
   223              self.assertEqual(config.opentsdb_username, "name")
   224              self.assertEqual(config.opentsdb_password, "secret")
   225              self.assertEqual(config.minimum_peer_connectivity, 1)
   226              self.assertEqual(config.maximum_peer_connectivity, 100)
   227  
   228          finally:
   229              os.environ.clear()
   230              os.environ.update(orig_environ)
   231              shutil.rmtree(directory)
   232  
   233      def test_path_config_invalid_setting_in_file(self):
   234          """Tests detecting invalid settings defined in a TOML configuration
   235          file.
   236  
   237          Creates a temporary directory and writes a validator.toml
   238          config file with an invalid setting inside, then loads that
   239          config and verifies an exception is thrown.
   240  
   241  
   242          The test also attempts to avoid environment variables from
   243          interfering with the test by clearing os.environ and restoring
   244          it after the test.
   245          """
   246          orig_environ = dict(os.environ)
   247          os.environ.clear()
   248          directory = tempfile.mkdtemp(prefix="test-path-config-")
   249          try:
   250              os.environ['SAWTOOTH_HOME'] = directory
   251  
   252              config_dir = os.path.join(directory, 'etc')
   253              os.mkdir(config_dir)
   254              filename = os.path.join(config_dir, 'validator.toml')
   255              with open(filename, 'w') as fd:
   256                  fd.write('invalid = "a value"')
   257                  fd.write(os.linesep)
   258              with self.assertRaises(LocalConfigurationError):
   259                  load_toml_validator_config(filename)
   260          finally:
   261              os.environ.clear()
   262              os.environ.update(orig_environ)
   263              shutil.rmtree(directory)