|
| 1 | +from behave import * |
| 2 | +import os |
| 3 | +import random |
| 4 | +import subprocess |
| 5 | +import tempfile |
| 6 | +import time |
| 7 | +import socket |
| 8 | + |
| 9 | +@given(u'a fabric peer and orderer') |
| 10 | +def step_impl(context): |
| 11 | + |
| 12 | + # create network |
| 13 | + context.network_name = 'behave_' + ''.join(random.choice('0123456789') for i in xrange(7)) |
| 14 | + context.network_id = subprocess.check_output([ |
| 15 | + 'docker', 'network', 'create', context.network_name |
| 16 | + ]).strip() |
| 17 | + |
| 18 | + # start orderer |
| 19 | + context.orderer_container_id = subprocess.check_output([ |
| 20 | + 'docker', 'run', '-d', '-p', '7050', |
| 21 | + '--expose', '7050', |
| 22 | + '--network', context.network_name, |
| 23 | + '--network-alias', 'orderer', |
| 24 | + '--env', 'ORDERER_GENERAL_LISTENADDRESS=0.0.0.0', |
| 25 | + 'hyperledger/fabric-orderer' |
| 26 | + ]).strip() |
| 27 | + context.orderer_address = subprocess.check_output(['docker', 'port', context.orderer_container_id, '7050']).strip() |
| 28 | + |
| 29 | + # start peer |
| 30 | + context.peer_container_id = subprocess.check_output([ |
| 31 | + 'docker', 'run', '-d', '-p', '7051', |
| 32 | + '--network', context.network_name, |
| 33 | + '--network-alias', 'vp0', |
| 34 | + '--env', 'CORE_PEER_ADDRESSAUTODETECT=true', |
| 35 | + '--env', 'CORE_PEER_ID=vp0', |
| 36 | + '--env', 'CORE_PEER_COMMITTER_LEDGER_ORDERER=orderer:7050', |
| 37 | + '--volume', '/var/run/docker.sock:/var/run/docker.sock', |
| 38 | + 'hyperledger/fabric-peer' |
| 39 | + ]).strip() |
| 40 | + context.peer_address = subprocess.check_output(['docker', 'port', context.peer_container_id, '7051']).strip() |
| 41 | + time.sleep(1) |
| 42 | + |
| 43 | + # setup env for peer cli commands |
| 44 | + context.peer_env = os.environ.copy() |
| 45 | + context.peer_env['CORE_PEER_ADDRESS'] = context.peer_address |
| 46 | + context.peer_env['CORE_PEER_COMMITTER_LEDGER_ORDERER'] = context.orderer_address |
| 47 | + |
| 48 | +@when(r'a (?P<lang>java|go|golang|car) chaincode is installed via the CLI') |
| 49 | +def step_impl(context, lang): |
| 50 | + context.chaincode_lang = 'golang' if lang == 'go' else lang |
| 51 | + context.chaincode_id_name = lang + '_cc_' + ''.join(random.choice('0123456789') for i in xrange(7)) |
| 52 | + context.chaincode_id_version = '1000' |
| 53 | + try: |
| 54 | + print(subprocess.check_output([ |
| 55 | + context.peer_exe, 'chaincode', 'install', |
| 56 | + '--logging-level', 'debug', |
| 57 | + '--name', context.chaincode_id_name, |
| 58 | + '--path', context.sample_chaincode[context.chaincode_lang], |
| 59 | + '--version', context.chaincode_id_version, |
| 60 | + '--lang', context.chaincode_lang |
| 61 | + ], cwd=context.fabric_dir, stderr=subprocess.STDOUT, env=context.peer_env)) |
| 62 | + except subprocess.CalledProcessError as e: |
| 63 | + print(e.output) |
| 64 | + print('CORE_PEER_ADDRESS = ' + context.peer_env['CORE_PEER_ADDRESS']) |
| 65 | + print('CORE_PEER_COMMITTER_LEDGER_ORDERER = ' + context.peer_env['CORE_PEER_COMMITTER_LEDGER_ORDERER']) |
| 66 | + raise |
| 67 | + |
| 68 | +@step(u'the chaincode is installed on the peer') |
| 69 | +def step_impl(context): |
| 70 | + print(subprocess.check_output([ |
| 71 | + 'docker', 'exec', context.peer_container_id, 'ls', '-l', '/var/hyperledger/production/chaincodes/' + context.chaincode_id_name + '.' + context.chaincode_id_version |
| 72 | + ])) |
| 73 | + |
| 74 | +@step(r'version (?P<version>\S+) of a (?P<lang>java|go|golang|car) chaincode is installed via the CLI') |
| 75 | +def step_impl(context, version, lang): |
| 76 | + context.chaincode_lang = 'golang' if lang == 'go' else lang |
| 77 | + context.chaincode_id_name = lang + '_cc_' + ''.join(random.choice('0123456789') for i in xrange(7)) |
| 78 | + context.chaincode_id_version = version |
| 79 | + try: |
| 80 | + print(subprocess.check_output([ |
| 81 | + context.peer_exe, 'chaincode', 'install', |
| 82 | + '--logging-level', 'debug', |
| 83 | + '--name', context.chaincode_id_name, |
| 84 | + '--path', context.sample_chaincode[context.chaincode_lang], |
| 85 | + '--version', context.chaincode_id_version, |
| 86 | + '--lang', context.chaincode_lang |
| 87 | + ], cwd=context.fabric_dir, stderr=subprocess.STDOUT, env=context.peer_env)) |
| 88 | + except subprocess.CalledProcessError as e: |
| 89 | + print(e.output) |
| 90 | + raise |
| 91 | + |
| 92 | +@step(r'installing version (?P<version>\S+) of the same chaincode via the CLI will fail') |
| 93 | +def step_impl(context, version): |
| 94 | + assert getattr(context, 'chaincode_id_name', None), 'No chaincode previously installed.' |
| 95 | + context.chaincode_id_version = version |
| 96 | + try: |
| 97 | + print(subprocess.check_output([ |
| 98 | + context.peer_exe, 'chaincode', 'install', |
| 99 | + '--logging-level', 'debug', |
| 100 | + '--name', context.chaincode_id_name, |
| 101 | + '--path', context.sample_chaincode[context.chaincode_lang], |
| 102 | + '--version', context.chaincode_id_version, |
| 103 | + '--lang', context.chaincode_lang |
| 104 | + ], cwd=context.fabric_dir, stderr=subprocess.STDOUT, env=context.peer_env)) |
| 105 | + except subprocess.CalledProcessError as e: |
| 106 | + print(e.output) |
| 107 | + raise |
0 commit comments