github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/integration/sawtooth_integration/tests/test_config_smoke.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 logging
    17  import unittest
    18  import traceback
    19  import tempfile
    20  import os
    21  import subprocess
    22  import sys
    23  from io import StringIO
    24  
    25  from sawtooth_cli.main import main
    26  from sawtooth_integration.tests.integration_tools import wait_for_rest_apis
    27  
    28  LOGGER = logging.getLogger(__name__)
    29  LOGGER.addHandler(logging.StreamHandler())
    30  LOGGER.setLevel(logging.DEBUG)
    31  
    32  TEST_PRIVKEY = \
    33      '2f1e7b7a130d7ba9da0068b3bb0ba1d79e7e77110302c9f746c3c2a63fe40088'
    34  TEST_PUBKEY = \
    35      '026a2c795a9776f75464aa3bda3534c3154a6e91b357b1181d3f515110f84b67c5'
    36  
    37  
    38  class TestConfigSmoke(unittest.TestCase):
    39      @classmethod
    40      def setUpClass(cls):
    41          wait_for_rest_apis(['rest-api:8008'])
    42  
    43      def setUp(self):
    44          self._temp_dir = tempfile.mkdtemp()
    45          self._data_dir = os.path.join(self._temp_dir, 'data')
    46          os.makedirs(self._data_dir)
    47  
    48          self._batch_file = os.path.join(self._temp_dir, 'batch')
    49  
    50          # create a private key for signing
    51          self._priv_file = os.path.join(self._temp_dir, 'test.priv')
    52          with open(self._priv_file, 'wb') as priv:
    53              priv.write(TEST_PRIVKEY.encode())
    54  
    55      def _run(self, args):
    56          try:
    57              LOGGER.debug("Running %s", " ".join(args))
    58              proc = subprocess.run(
    59                  args,
    60                  stdout=subprocess.PIPE,
    61                  stderr=subprocess.PIPE,
    62                  check=True)
    63              LOGGER.debug(proc.stdout.decode())
    64          except subprocess.CalledProcessError as err:
    65              LOGGER.debug(err)
    66              LOGGER.debug(err.stderr.decode())
    67              traceback.print_exc()
    68              self.fail(self.__class__.__name__)
    69  
    70      def _read_from_stdout(self, cmd, args):
    71          # Retrieve string of setting contents for comparison to input settings
    72          backup = sys.stdout  # backup the environment
    73          sys.stdout = StringIO()  # Capture the output of next statement
    74  
    75          main(cmd, args)
    76          settings = sys.stdout.getvalue()  # release the output and store
    77          sys.stdout.close()
    78          # Restore the environment
    79          sys.stdout = backup
    80          return settings
    81  
    82      def test_submit_then_list_settings(self):
    83          ''' Test ability to list settings after submission of a setting.
    84              Test submits a simple config transaction to the validator,
    85              then confirms that the settings can be retrieved by the
    86              command 'sawset settings list', and that the retrieved
    87              setting equals the input setting.
    88              '''
    89          # Submit transaction, then list it using subprocess
    90          cmds = [
    91              ['sawset', 'proposal', 'create', '-k', self._priv_file,
    92               '--output', self._batch_file, 'x=1', 'y=1'],
    93              ['sawtooth', 'batch', 'submit', '--url', 'http://rest-api:8008',
    94               '--wait', '--filename', self._batch_file],
    95              ['sawtooth', 'settings', 'list', '--url',
    96               'http://rest-api:8008']
    97          ]
    98  
    99          for cmd in cmds:
   100              self._run(cmd)
   101  
   102          command = 'sawtooth'
   103          args = ['settings', 'list', '--url', 'http://rest-api:8008']
   104          settings = self._read_from_stdout(command, args).split('\n')
   105  
   106          _expected_output = [
   107              'sawtooth.settings.vote.authorized_keys: {:15}'.format(
   108                  TEST_PUBKEY),
   109              'x: 1',
   110              'y: 1']
   111          _fail_msg = 'Setting results did not match.'
   112  
   113          self.assertTrue(settings[0].startswith(_expected_output[0]), _fail_msg)
   114          self.assertEqual(settings[1], _expected_output[1], _fail_msg)
   115          self.assertEqual(settings[2], _expected_output[2], _fail_msg)