github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/cli/tests/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 16 import os 17 import shlex 18 import shutil 19 import subprocess 20 import tempfile 21 import unittest 22 23 from sawtooth_cli.protobuf.batch_pb2 import BatchHeader 24 from sawtooth_cli.protobuf.batch_pb2 import BatchList 25 26 27 PRIV_HEX = \ 28 '2f1e7b7a130d7ba9da0068b3bb0ba1d79e7e77110302c9f746c3c2a63fe40088' 29 30 31 class TestConfigBatchlist(unittest.TestCase): 32 def __init__(self, test_name): 33 super().__init__(test_name) 34 self._temp_dir = None 35 36 def setUp(self): 37 self._temp_dir = tempfile.mkdtemp() 38 39 # create a hex key for signing 40 self._priv_file = os.path.join(self._temp_dir, 'test.priv') 41 with open(self._priv_file, 'wb') as priv: 42 priv.write(PRIV_HEX.encode()) 43 44 def tearDown(self): 45 shutil.rmtree(self._temp_dir) 46 47 def _read_target_file_as(self, target_type): 48 target_path = os.path.join(self._temp_dir, 'myconfig.batch') 49 with open(target_path, 'r+b') as result: 50 output = target_type() 51 output.ParseFromString(result.read()) 52 53 return output 54 55 def test_set_value_creates_batch_list(self): 56 subprocess.run(shlex.split( 57 'sawset proposal create -k {} -o {} x=1 y=1'.format( 58 self._priv_file, 59 os.path.join(self._temp_dir, 'myconfig.batch')))) 60 61 batch_list = self._read_target_file_as(BatchList) 62 63 self.assertEqual(1, len(batch_list.batches)) 64 65 batch_header = BatchHeader() 66 batch_header.ParseFromString(batch_list.batches[0].header) 67 self.assertEqual(2, len(batch_header.transaction_ids))