github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/validator/tests/test_config_view/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 shutil
    18  import tempfile
    19  import hashlib
    20  import unittest
    21  
    22  from sawtooth_validator.database.native_lmdb import NativeLmdbDatabase
    23  from sawtooth_validator.protobuf.setting_pb2 import Setting
    24  
    25  from sawtooth_validator.state.settings_view import SettingsViewFactory
    26  from sawtooth_validator.state.state_view import StateViewFactory
    27  
    28  from sawtooth_validator.state.merkle import MerkleDatabase
    29  
    30  
    31  class TestSettingsView(unittest.TestCase):
    32      def __init__(self, test_name):
    33          super().__init__(test_name)
    34          self._temp_dir = None
    35          self._settings_view_factory = None
    36          self._current_root_hash = None
    37  
    38      def setUp(self):
    39          self._temp_dir = tempfile.mkdtemp()
    40          database = NativeLmdbDatabase(
    41              os.path.join(self._temp_dir, 'test_config_view.lmdb'),
    42              indexes=MerkleDatabase.create_index_configuration(),
    43              _size=10 * 1024 * 1024)
    44          state_view_factory = StateViewFactory(database)
    45          self._settings_view_factory = SettingsViewFactory(state_view_factory)
    46  
    47          merkle_db = MerkleDatabase(database)
    48          self._current_root_hash = merkle_db.update({
    49              TestSettingsView._address('my.setting'):
    50                  TestSettingsView._setting_entry('my.setting', '10'),
    51              TestSettingsView._address('my.setting.list'):
    52                  TestSettingsView._setting_entry('my.setting.list', '10,11,12'),
    53              TestSettingsView._address('my.other.list'):
    54                  TestSettingsView._setting_entry('my.other.list', '13;14;15')
    55          }, virtual=False)
    56  
    57      def tearDown(self):
    58          shutil.rmtree(self._temp_dir)
    59  
    60      def test_get_setting(self):
    61          """Verifies the correct operation of get_setting() by using it to get
    62          the config setting stored as "my.setting" and compare it to '10' (the
    63          value set during setUp()).
    64          """
    65          settings_view = self._settings_view_factory.create_settings_view(
    66              self._current_root_hash)
    67  
    68          self.assertEqual('10', settings_view.get_setting('my.setting'))
    69  
    70      def test_get_setting_with_type_coercion(self):
    71          """Verifies the correct operation of get_setting() by using it to get
    72          the config setting stored as "my.setting" with a int type coercion
    73          function and compare it to the int 10 (the value set during setUp()).
    74          """
    75          settings_view = self._settings_view_factory.create_settings_view(
    76              self._current_root_hash)
    77          self.assertEqual(10, settings_view.get_setting('my.setting',
    78                                                         value_type=int))
    79  
    80      def test_get_setting_not_found(self):
    81          """Verifies the correct operation of get_setting() by using it to
    82          return None when an unknown setting is requested.
    83          """
    84          settings_view = self._settings_view_factory.create_settings_view(
    85              self._current_root_hash)
    86  
    87          self.assertIsNone(settings_view.get_setting('non-existant.setting'))
    88  
    89      def test_get_setting_not_found_with_default(self):
    90          """Verifies the correct operation of get_setting() by using it to
    91          return a default value when an unknown setting is requested.
    92          """
    93          settings_view = self._settings_view_factory.create_settings_view(
    94              self._current_root_hash)
    95  
    96          self.assertEqual('default',
    97                           settings_view.get_setting('non-existant.setting',
    98                                                     default_value='default'))
    99  
   100      def test_get_setting_list(self):
   101          """Verifies the correct operation of get_setting_list() by using it to
   102          get the config setting stored as "my.setting.list" and compare it to
   103          ['10', '11', '12'] (the split value set during setUp()).
   104          """
   105          settings_view = self._settings_view_factory.create_settings_view(
   106              self._current_root_hash)
   107  
   108          # Verify we can still get the "raw" setting
   109          self.assertEqual('10,11,12',
   110                           settings_view.get_setting('my.setting.list'))
   111          # And now the split setting
   112          self.assertEqual(
   113              ['10', '11', '12'],
   114              settings_view.get_setting_list('my.setting.list'))
   115  
   116      def test_get_setting_list_not_found(self):
   117          """Verifies the correct operation of get_setting_list() by using it to
   118          return None when an unknown setting is requested.
   119          """
   120          settings_view = self._settings_view_factory.create_settings_view(
   121              self._current_root_hash)
   122          self.assertIsNone(
   123              settings_view.get_setting_list('non-existant.setting.list'))
   124  
   125      def test_get_setting_list_not_found_with_default(self):
   126          """Verifies the correct operation of get_setting_list() by using it to
   127          return a default value when an unknown setting is requested.
   128          """
   129          settings_view = self._settings_view_factory.create_settings_view(
   130              self._current_root_hash)
   131          self.assertEqual(
   132              [],
   133              settings_view.get_setting_list('non-existant.list',
   134                                             default_value=[]))
   135  
   136      def test_get_setting_list_alternate_delimiter(self):
   137          """Verifies the correct operation of get_setting_list() by using it to
   138          get the config setting stored as "my.other.list" and compare it to
   139          ['13', '14', '15'] (the value, split along an alternate delimiter, set
   140          during setUp()).
   141          """
   142          settings_view = self._settings_view_factory.create_settings_view(
   143              self._current_root_hash)
   144          self.assertEqual(
   145              ['13', '14', '15'],
   146              settings_view.get_setting_list('my.other.list', delimiter=';'))
   147  
   148      def test_get_setting_list_with_type_coercion(self):
   149          """Verifies the correct operation of get_setting_list() by using it to
   150          get the integer type-coerced config setting stored as "my.setting.list"
   151          and compare it to [10, 11, 12] (the split, type-coerced, value set
   152          during setUp()).
   153          """
   154          settings_view = self._settings_view_factory.create_settings_view(
   155              self._current_root_hash)
   156          self.assertEqual(
   157              [10, 11, 12],
   158              settings_view.get_setting_list('my.setting.list', value_type=int))
   159  
   160      @staticmethod
   161      def _address(key):
   162          return '000000' + _key_to_address(key)
   163  
   164      @staticmethod
   165      def _setting_entry(key, value):
   166          return Setting(
   167              entries=[Setting.Entry(key=key, value=value)]
   168          ).SerializeToString()
   169  
   170  
   171  _MAX_KEY_PARTS = 4
   172  _ADDRESS_PART_SIZE = 16
   173  
   174  
   175  def _short_hash(name):
   176      return hashlib.sha256(name.encode()).hexdigest()[:_ADDRESS_PART_SIZE]
   177  
   178  
   179  def _key_to_address(key):
   180      key_parts = key.split('.', maxsplit=_MAX_KEY_PARTS - 1)
   181      key_parts.extend([''] * (_MAX_KEY_PARTS - len(key_parts)))
   182      return ''.join(_short_hash(x) for x in key_parts)