Example #1
0
    def create_channel(self):

        client = Client()
        client.state_store = FileKeyValueStore(self.kv_store_path +
                                               'build-channel')

        logger.info("start to create channel")
        request = build_channel_request(client, self.channel_tx,
                                        self.channel_name)

        q = Queue(1)
        response = client.create_channel(request)
        response.subscribe(on_next=lambda x: q.put(x),
                           on_error=lambda x: q.put(x))

        status, _ = q.get(timeout=5)

        self.assertEqual(status.status, 200)

        logger.info("successfully create the channel: %s", self.channel_name)
        client.state_store = None
Example #2
0
class E2eTest(unittest.TestCase):
    def setUp(self):

        self.base_path = "/tmp/fabric-sdk-py"
        self.kv_store_path = os.path.join(self.base_path, "key-value-store")
        self.client = Client()
        self.client.state_store = FileKeyValueStore(self.kv_store_path)
        self.channel_tx = \
            E2E_CONFIG['test-network']['channel-artifacts']['channel.tx']
        self.channel_name = \
            E2E_CONFIG['test-network']['channel-artifacts']['channel_id']
        self.compose_file_path = \
            E2E_CONFIG['test-network']['docker']['compose_file_tls']

        self.start_test_env()

    def tearDown(self):

        self.kv_store_path = None
        self.shutdown_test_env()

    def start_test_env(self):

        cli_call(["docker-compose", "-f", self.compose_file_path, "up", "-d"])

    def shutdown_test_env(self):

        cli_call(["docker-compose", "-f", self.compose_file_path, "down"])

    def create_channel(self):

        logger.info("start to create channel")
        request = build_channel_request(self.client, self.channel_tx,
                                        self.channel_name)

        q = Queue(1)
        response = self.client.create_channel(request)
        response.subscribe(on_next=lambda x: q.put(x),
                           on_error=lambda x: q.put(x))

        status, _ = q.get(timeout=5)

        try:
            self.assertEqual(status.status, 200)
        except AssertionError:
            logger.error("fail to create the channel with status code",
                         status.status)
            raise Exception("fail to create channel.")

        logger.info("successfully create the channel")

    def join_channel(self):

        pass

    def install_chaincode(self):

        pass

    def install_chaincode_fail(self):

        pass

    def instantiate_chaincode(self):

        pass

    def invoke_transaction(self):

        pass

    def query(self):

        pass

    def test_in_sequence(self):

        self.create_channel()
        self.join_channel()
        self.install_chaincode()
        self.install_chaincode_fail()
        self.instantiate_chaincode()
        self.invoke_transaction()
        self.query()
Example #3
0
class ClientTest(unittest.TestCase):
    def setUp(self):
        self.client = Client()

    def test_create_client(self):

        self.client.crypto_suite = 'my_crypto_suite'
        self.assertEqual(self.client.crypto_suite, 'my_crypto_suite')

        self.client.tx_context = 'my_tx_context'
        self.assertEqual(self.client.tx_context, 'my_tx_context')

        self.client.user_context = 'my_user_context'
        self.assertEqual(self.client.user_context, 'my_user_context')

        self.client.state_store = 'my_state_store'
        self.assertEqual(self.client.state_store, 'my_state_store')

    def test_new_channel(self):
        test_channel = self.client.new_channel('test')
        self.assertEqual(test_channel, self.client.get_channel('test'))

    def test_get_channel(self):
        test_channel = self.client.new_channel('test')
        self.assertEqual(test_channel, self.client.get_channel('test'))

        no_chain = self.client.get_channel('test1')
        self.assertIsNone(no_chain)

    def test_create_channel_missing_signatures(self):
        request = {}
        request['config'] = 'config'
        request['channel_name'] = 'channel_name'
        request['orderer'] = 'orderer'
        request['tx_id'] = 'tx_id'
        with self.assertRaises(ValueError):
            self.client.create_channel(request)

    def test_create_channel_not_list_of_signatures(self):
        request = {}
        request['config'] = 'config'
        request['signatures'] = 'signatures'
        request['channel_name'] = 'channel_name'
        request['orderer'] = 'orderer'
        request['tx_id'] = 'tx_id'
        with self.assertRaises(ValueError):
            self.client.create_channel(request)

    def test_create_channel_missing_missing_tx_id(self):
        request = {}
        request['config'] = 'config'
        request['channel_name'] = 'channel_name'
        request['orderer'] = 'orderer'

        with self.assertRaises(ValueError):
            self.client.create_channel(request)

    def test_create_channel_missing_orderer(self):
        request = {}
        request['config'] = 'config'
        request['channel_name'] = 'channel_name'
        request['tx_id'] = 'tx_id'

        with self.assertRaises(ValueError):
            self.client.create_channel(request)

    def test_create_channel_missing_channel_name(self):
        request = {}
        request['config'] = 'config'
        request['orderer'] = 'orderer'
        request['tx_id'] = 'tx_id'

        with self.assertRaises(ValueError):
            self.client.create_channel(request)
class ClientTest(unittest.TestCase):
    """ Integration tests for the client module. """
    def setUp(self):
        self.configtx_path = \
            E2E_CONFIG['test-network']['channel-artifacts']['channel.tx']
        self.orderer_tls_certs = \
            E2E_CONFIG['test-network']['orderer']['tls_cacerts']
        self.orderer_tls_hostname = \
            E2E_CONFIG['test-network']['orderer']['server_hostname']
        self.compose_file_path = \
            E2E_CONFIG['test-network']['docker']['compose_file_tls']
        self.base_path = "/tmp/fabric-sdk-py"
        self.kv_store_path = os.path.join(self.base_path, "key-value-store")
        self.client = Client()
        self.start_test_env()

    def tearDown(self):
        self.shutdown_test_env()

    def start_test_env(self):
        cli_call(["docker-compose", "-f", self.compose_file_path, "up", "-d"])

    def shutdown_test_env(self):
        cli_call(["docker-compose", "-f", self.compose_file_path, "down"])

    def test_create_channel_missing_signatures(self):
        signatures = []

        self.client.state_store = FileKeyValueStore(self.kv_store_path)

        with open(self.orderer_tls_certs) as f:
            pem = f.read()

        opts = (('grpc.ssl_target_name_override', 'orderer.example.com'), )
        orderer = Orderer(pem=pem, opts=opts)

        with open(self.configtx_path, 'rb') as f:
            envelope = f.read()

        # convert envelope to config
        config = extract_channel_config(envelope)

        channel_name = 'businesschannel'

        # signatures orderer admin
        orderer_admin = get_orderer_org_admin(self.client)
        orderer_admin_tx_context = TXContext(orderer_admin, Ecies(), {})
        self.client.tx_context = orderer_admin_tx_context

        orderer_admin_signature = self.client.sign_channel_config(config)
        orderer_admin_signature.SerializeToString()

        # take the tx_id and nonce from the oderer user context
        tx_id = orderer_admin_tx_context.tx_id
        nonce = orderer_admin_tx_context.nonce

        # reset the state store to handle different
        # users with one client object
        self.client.state_store = FileKeyValueStore(self.kv_store_path)

        # signatures org1 admin
        org1_admin = get_peer_org_admin(self.client, 'org1.example.com')
        org1_admin_tx_context = TXContext(org1_admin, Ecies(), {})
        self.client.tx_context = org1_admin_tx_context

        org1_admin_signature = self.client.sign_channel_config(config)
        org1_admin_signature.SerializeToString()

        # reset the state store to handle different
        # users with one client object
        self.client.state_store = FileKeyValueStore(self.kv_store_path)

        # signatures org2 admin
        org2_admin = get_peer_org_admin(self.client, 'org2.example.com')
        org2_admin_tx_context = TXContext(org2_admin, Ecies(), {})
        self.client.tx_context = org2_admin_tx_context

        org2_admin_signature = self.client.sign_channel_config(config)
        org2_admin_signature.SerializeToString()

        request = {
            'tx_id': tx_id,
            'nonce': nonce,
            'signatures': signatures,
            'config': config,
            'orderer': orderer,
            'channel_name': channel_name
        }

        queue = Queue(1)

        response = self.client.create_channel(request)

        response.subscribe(on_next=lambda x: queue.put(x),
                           on_error=lambda x: queue.put(x))

        status, _ = queue.get(timeout=5)
        self.assertEqual(status.status, 400)

    def test_create_channel(self):
        signatures = []

        self.client.state_store = FileKeyValueStore(self.kv_store_path)

        with open(self.orderer_tls_certs) as f:
            pem = f.read()

        opts = (('grpc.ssl_target_name_override', 'orderer.example.com'), )
        orderer = Orderer(pem=pem, opts=opts)

        with open(self.configtx_path, 'rb') as f:
            envelope = f.read()

        # convert envelope to config
        config = extract_channel_config(envelope)

        channel_name = 'businesschannel'

        # signatures orderer admin
        orderer_admin = get_orderer_org_admin(self.client)
        orderer_admin_tx_context = TXContext(orderer_admin, Ecies(), {})
        self.client.tx_context = orderer_admin_tx_context

        orderer_admin_signature = self.client.sign_channel_config(config)
        orderer_admin_signature_bytes = \
            orderer_admin_signature.SerializeToString()

        # take the tx_id and nonce from the oderer user context
        tx_id = orderer_admin_tx_context.tx_id
        nonce = orderer_admin_tx_context.nonce

        # append orderer_org_admin signatures
        signatures.append(orderer_admin_signature_bytes)

        # reset the state store to handle different
        # users with one client object
        self.client.state_store = FileKeyValueStore(self.kv_store_path)

        # signatures org1 admin
        org1_admin = get_peer_org_admin(self.client, 'org1.example.com')
        org1_admin_tx_context = TXContext(org1_admin, Ecies(), {})
        self.client.tx_context = org1_admin_tx_context

        org1_admin_signature = self.client.sign_channel_config(config)
        org1_admin_signature_bytes = org1_admin_signature.SerializeToString()

        # append org1_admin_signature to signatures
        signatures.append(org1_admin_signature_bytes)

        # reset the state store to handle different
        # users with one client object
        self.client.state_store = FileKeyValueStore(self.kv_store_path)

        # signatures org2 admin
        org2_admin = get_peer_org_admin(self.client, 'org2.example.com')
        org2_admin_tx_context = TXContext(org2_admin, Ecies(), {})
        self.client.tx_context = org2_admin_tx_context

        org2_admin_signature = self.client.sign_channel_config(config)
        org2_admin_signature_bytes = org2_admin_signature.SerializeToString()

        # append org1_admin_signature to signatures
        signatures.append(org2_admin_signature_bytes)

        request = {
            'tx_id': tx_id,
            'nonce': nonce,
            'signatures': signatures,
            'config': config,
            'orderer': orderer,
            'channel_name': channel_name
        }
        response = self.client.create_channel(request)

        q = Queue(1)
        response.subscribe(on_next=lambda x: q.put(x),
                           on_error=lambda x: q.put(x))

        status, _ = q.get(timeout=5)
        self.assertEqual(status.status, 200)

    @unittest.skip
    def test_create_channel_with_envelope(self):
        # TODO missing impl
        # signed envelope necessary
        pass