def __init__(self, config_file):
     super(TestEthereumWorkerRegistryListImpl, self).__init__()
     if not path.isfile(config_file):
         raise FileNotFoundError("File not found at path: {0}".format(
             path.realpath(config_file)))
     try:
         with open(config_file) as fd:
             self.__config = toml.load(fd)
     except IOError as e:
         if e.errno != errno.ENOENT:
             raise Exception('Could not open config file: %s', e)
     self.__eth_conn = EthereumWorkerRegistryListImpl(self.__config)
Example #2
0
def main():
    logger.info("Testing Direct registry bridge functionality.")

    eth_direct_registry = dir_registry.EthereumDirectRegistry("0x8c99670a15047248403a3E5A38eb8FBE7a12533e",
                                                 '../contracts/WorkerRegistryList.sol')
    kv_storage = KvStorage()
    kv_storage.open("kv_storage")

    logger.info("------------------- test_sync_to_lmdb  ---------------------- \n")
    test_sync_to_lmdb(eth_direct_registry, kv_storage)
    logger.info("\n------------------- test_sync_to_smart_contract  ---------------------- \n")
    test_sync_to_smart_contract(eth_direct_registry, kv_storage)
 def create_worker_registry_list_adaptor(self, config):
     if self.__blockchain_type == "Ethereum":
         if self.__worker_registry_list is None:
             self.__worker_registry_list = EthereumWorkerRegistryListImpl(config)
         return self.__worker_registry_list
class TestEthereumWorkerRegistryListImpl(unittest.TestCase):
    def __init__(self, config_file):
        super(TestEthereumWorkerRegistryListImpl, self).__init__()
        if not path.isfile(config_file):
            raise FileNotFoundError("File not found at path: {0}".format(
                path.realpath(config_file)))
        try:
            with open(config_file) as fd:
                self.__config = toml.load(fd)
        except IOError as e:
            if e.errno != errno.ENOENT:
                raise Exception('Could not open config file: %s', e)
        self.__eth_conn = EthereumWorkerRegistryListImpl(self.__config)

    def test_registry_add(self):
        self.__org_id = urandom(32)
        self.__uri = "http://worker1:8008"
        self.__sc_addr = urandom(32)
        self.__app_type_ids = [urandom(32), urandom(32)]
        logging.info(
            'Calling registry_add contract..\n org_id: %s\n ' +
            'uri: %s\n ' + 'sc_addr: %s\n application_ids: %s',
            hex_to_utf8(self.__org_id), self.__uri,
            hex_to_utf8(self.__sc_addr), pretty_ids(self.__app_type_ids))
        result = self.__eth_conn.registry_add(self.__org_id, self.__uri,
                                              self.__sc_addr,
                                              self.__app_type_ids)
        logging.info(
            "registry_add contract status \n{'status': %s', \n" +
            "'txn_receipt': %s}", result["status"],
            json.dumps(json.loads(Web3.toJSON(result["txn_receipt"])),
                       indent=4))
        self.assertEqual(result['status'], 'added',
                         "Registry add response not matched")

    def test_registry_update(self):
        self.__new_app_id = [urandom(32)]
        self.__new_uri = 'http://worker2:8008'
        logging.info(
            'Calling registry_update contract..\n org_id: %s\n uri: %s\n ' +
            'sc_addr: %s\n application_ids: %s', hex_to_utf8(self.__org_id),
            self.__new_uri, hex_to_utf8(self.__sc_addr),
            pretty_ids(self.__new_app_id))
        result = self.__eth_conn.registry_update(self.__org_id, self.__new_uri,
                                                 self.__sc_addr,
                                                 self.__new_app_id)
        logging.info(
            "registry_update contract status \n{'status': %s', \n" +
            "'txn_receipt': %s}", result["status"],
            json.dumps(json.loads(Web3.toJSON(result["txn_receipt"])),
                       indent=4))
        self.assertEqual(result['status'], 'added',
                         "Registry update response not matched")

    def test_registry_set_status(self):
        self.__new_status = RegistryStatus.OFF_LINE
        logging.info(
            'Calling registry_set_status contract..\n org_id: %s\n status: %d',
            hex_to_utf8(self.__org_id), self.__new_status.value)
        result = self.__eth_conn.registry_set_status(self.__org_id,
                                                     self.__new_status)
        logging.info(
            "registry_set_status contract status \n{'status': %s', \n" +
            "'txn_receipt': %s}", result["status"],
            json.dumps(json.loads(Web3.toJSON(result["txn_receipt"])),
                       indent=4))
        self.assertEqual(result['status'], 'added',
                         "Registry set status response not matched")

    def test_registry_lookup(self):
        logging.info('Calling registry_lookup..\n application_id: %s',
                     hex_to_utf8(self.__app_type_ids[0]))
        result = self.__eth_conn.registry_lookup(self.__app_type_ids[0])
        logging.info('registry_lookup contract status [%d, %s, %s]', result[0],
                     result[1], pretty_ids(result[2]))
        self.assertEqual(result[0], 1,
                         "Registry lookup response total count not matched")
        self.assertEqual(result[2][0], self.__org_id,
                         "Registry lookup response not matched for org id")

    def test_registry_retrieve(self):
        logging.info('Calling registry_retrieve..\n org_id: %s',
                     hex_to_utf8(self.__org_id))
        result = self.__eth_conn.registry_retrieve(self.__org_id)
        logging.info('registry_retrieve contract status [%s, %s, %s, %d]',
                     result[0], hex_to_utf8(result[1]), pretty_ids(result[2]),
                     result[3])
        self.assertEqual(result[0], self.__new_uri,
                         "Registry retrieve response not matched for uri")
        self.assertEqual(
            hex_to_utf8(result[1]), hex_to_utf8(self.__sc_addr),
            "Registry retrieve response not matched for " +
            "smart contract address")
        self.assertEqual(
            result[2][0], self.__app_type_ids[0],
            "Registry retrieve response not matched for app id type list " +
            "index 0")
        self.assertEqual(
            result[2][1], self.__app_type_ids[1],
            "Registry retrieve response not matched for app id type list " +
            "index 1")
        self.assertEqual(
            result[2][2], self.__new_app_id[0],
            "Registry retrieve response not matched for app id type list " +
            "index 2")
        self.assertEqual(result[3], self.__new_status.value,
                         "Registry retrieve response not matched for status")

    def test_registry_lookup_next(self):
        lookup_tag = "test"
        logging.info(
            'Calling registry_lookup_next..\n application_id: %s\n ' +
            'lookup_tag: %s', hex_to_utf8(self.__app_type_ids[0]), lookup_tag)
        result = self.__eth_conn.registry_lookup_next(self.__app_type_ids[0],
                                                      lookup_tag)
        logging.info('registry_lookup_next contract status [%d, %s, %s]',
                     result[0], result[1], pretty_ids(result[2]))