def test_from_json_and_to_json(self, agent_path): """Test the 'from_json' method and 'to_json' work correctly.""" f = open(agent_path) original_json = yaml.safe_load(f) expected_config = AgentConfig.from_json(original_json) assert isinstance(expected_config, AgentConfig) expected_json = expected_config.json actual_config = AgentConfig.from_json(expected_json) actual_json = actual_config.json assert expected_json == actual_json
def test_from_json_and_to_json(self, agent_path): """Test the 'from_json' method and 'to_json' work correctly.""" f = open(agent_path) original_jsons = list(yaml.safe_load_all(f)) components = original_jsons[1:] original_json = original_jsons[0] original_json["component_configurations"] = components expected_config = AgentConfig.from_json(original_json) assert isinstance(expected_config, AgentConfig) expected_json = expected_config.json actual_config = AgentConfig.from_json(expected_json) actual_json = actual_config.json assert expected_json == actual_json
def setup_class(cls): """Set the test up.""" cls.runner = CliRunner() cls.agent_name = "myagent" cls.cwd = os.getcwd() cls.t = tempfile.mkdtemp() cls.skill_name = "gym" cls.patch = unittest.mock.patch.object(aea.cli.common.logger, 'error') cls.mocked_logger_error = cls.patch.__enter__() os.chdir(cls.t) result = cls.runner.invoke(cli, ["create", cls.agent_name]) assert result.exit_code == 0 os.chdir(cls.agent_name) # change default registry path config = AgentConfig.from_json( yaml.safe_load(open(DEFAULT_AEA_CONFIG_FILE))) config.registry_path = os.path.join(ROOT_DIR, "packages") yaml.safe_dump(config.json, open(DEFAULT_AEA_CONFIG_FILE, "w")) result = cls.runner.invoke(cli, ["add", "skill", cls.skill_name]) assert result.exit_code == 0 cls.result = cls.runner.invoke(cli, ["remove", "skill", cls.skill_name])
def test_add_many_keys(self, pytestconfig): """Test that the keys are added correctly.""" result = self.runner.invoke( cli, [ *CLI_LOG_OPTION, "add-key", FetchAICrypto.identifier, FETCHAI_PRIVATE_KEY_FILE, ], ) assert result.exit_code == 0 result = self.runner.invoke( cli, [ *CLI_LOG_OPTION, "add-key", EthereumCrypto.identifier, ETHEREUM_PRIVATE_KEY_FILE, ], ) assert result.exit_code == 0 f = open(Path(self.agent_folder, DEFAULT_AEA_CONFIG_FILE)) expected_json = yaml.safe_load(f) config = AgentConfig.from_json(expected_json) private_key_path_ethereum = config.private_key_paths.read( FetchAICrypto.identifier) assert private_key_path_ethereum == FETCHAI_PRIVATE_KEY_FILE private_key_path_ethereum = config.private_key_paths.read( EthereumCrypto.identifier) assert private_key_path_ethereum == ETHEREUM_PRIVATE_KEY_FILE assert len(config.private_key_paths.read_all()) == 2
def setup_class(cls): """Set the test up.""" cls.runner = CliRunner() cls.agent_name = "myagent" cls.cwd = os.getcwd() cls.t = tempfile.mkdtemp() cls.skill_name = "echo" cls.patch = unittest.mock.patch.object(aea.cli.common.logger, 'error') cls.mocked_logger_error = cls.patch.__enter__() os.chdir(cls.t) result = cls.runner.invoke(cli, ["create", cls.agent_name]) assert result.exit_code == 0 os.chdir(cls.agent_name) # change default registry path config = AgentConfig.from_json( yaml.safe_load(open(DEFAULT_AEA_CONFIG_FILE))) config.registry_path = os.path.join(ROOT_DIR, "packages") yaml.safe_dump(config.json, open(DEFAULT_AEA_CONFIG_FILE, "w")) # change the serialization of the AgentConfig class so to make the parsing to fail. cls.patch = unittest.mock.patch.object( aea.configurations.base.SkillConfig, "from_json", side_effect=ValidationError("test error message")) cls.patch.__enter__() cls.result = cls.runner.invoke(cli, ["add", "skill", cls.skill_name])
def setup_class(cls): """Set the test up.""" cls.runner = CliRunner() cls.agent_name = "myagent" cls.cwd = os.getcwd() cls.t = tempfile.mkdtemp() cls.skill_name = "error" cls.patch = unittest.mock.patch.object(aea.cli.common.logger, 'error') cls.mocked_logger_error = cls.patch.__enter__() os.chdir(cls.t) result = cls.runner.invoke(cli, [*CLI_LOG_OPTION, "create", cls.agent_name], standalone_mode=False) # this also by default adds the oef connection and error skill assert result.exit_code == 0 os.chdir(cls.agent_name) # change default registry path config = AgentConfig.from_json( yaml.safe_load(open(DEFAULT_AEA_CONFIG_FILE))) config.registry_path = os.path.join(ROOT_DIR, "packages") yaml.safe_dump(config.json, open(DEFAULT_AEA_CONFIG_FILE, "w")) # add the error skill again cls.result = cls.runner.invoke( cli, [*CLI_LOG_OPTION, "add", "skill", cls.skill_name], standalone_mode=False)
def test_add_key_fails_bad_ledger_id(): """Test that 'aea add-key' fails because the ledger id is not valid.""" oldcwd = os.getcwd() runner = CliRunner() agent_name = "myagent" tmpdir = tempfile.mkdtemp() os.chdir(tmpdir) try: result = runner.invoke( cli, [*CLI_LOG_OPTION, "init", "--local", "--author", AUTHOR] ) result = runner.invoke(cli, [*CLI_LOG_OPTION, "create", "--local", agent_name]) assert result.exit_code == 0 os.chdir(Path(tmpdir, agent_name)) # generate a private key file result = runner.invoke(cli, [*CLI_LOG_OPTION, "generate-key", FETCHAI]) assert result.exit_code == 0 assert Path(FETCHAI_PRIVATE_KEY_FILE).exists() bad_ledger_id = "this_is_a_bad_ledger_id" result = runner.invoke( cli, [*CLI_LOG_OPTION, "add-key", bad_ledger_id, FETCHAI_PRIVATE_KEY_FILE] ) assert result.exit_code == 2 # check that no key has been added. f = open(Path(DEFAULT_AEA_CONFIG_FILE)) expected_json = yaml.safe_load(f) config = AgentConfig.from_json(expected_json) assert len(config.private_key_paths.read_all()) == 0 finally: os.chdir(oldcwd)
def test_add_key_fails_bad_key(): """Test that 'aea add-key' fails because the key is not valid.""" oldcwd = os.getcwd() runner = CliRunner() agent_name = "myagent" with tempfile.TemporaryDirectory() as tmpdir: with mock.patch.object(aea.crypto.helpers.logger, "error") as mock_logger_error: os.chdir(tmpdir) result = runner.invoke(cli, [*CLI_LOG_OPTION, "create", agent_name]) assert result.exit_code == 0 os.chdir(Path(tmpdir, agent_name)) # create an empty file - surely not a private key pvk_file = "this_is_not_a_key.pem" Path(pvk_file).touch() result = runner.invoke(cli, [*CLI_LOG_OPTION, "add-key", DEFAULT, pvk_file]) assert result.exit_code == 1 mock_logger_error.assert_called_with("This is not a valid private key file: '{}'".format(pvk_file)) # check that no key has been added. f = open(Path(DEFAULT_AEA_CONFIG_FILE)) expected_json = yaml.safe_load(f) config = AgentConfig.from_json(expected_json) assert len(config.private_key_paths.read_all()) == 0 os.chdir(oldcwd)
def test_key_added(self): """Test that the fetch private key has been added correctly.""" f = open(Path(self.agent_folder, DEFAULT_AEA_CONFIG_FILE)) expected_json = yaml.safe_load(f) config = AgentConfig.from_json(expected_json) private_key_path = config.private_key_paths.read(FetchAICrypto.identifier) assert private_key_path == FETCHAI_PRIVATE_KEY_FILE assert len(config.private_key_paths.read_all()) == 1
def test_from_json_and_to_json(self): """Test the 'from_json' method and 'to_json' work correctly.""" f = open(os.path.join(CUR_PATH, "data", "aea-config.example.yaml")) expected_json = yaml.safe_load(f) config = AgentConfig.from_json(expected_json) assert isinstance(config, AgentConfig) actual_json = config.json expected_protocols = expected_json.pop("protocols") actual_protocols = actual_json.pop("protocols") assert set(expected_protocols) == set(actual_protocols) assert actual_json == expected_json
def with_config_update(self): """Context manager to update item version to 0.0.1.""" original_config = self.load_config() config_data = original_config.json if str(self.ITEM_PUBLIC_ID) in config_data[f"{self.ITEM_TYPE}s"]: config_data[f"{self.ITEM_TYPE}s"].remove(str(self.ITEM_PUBLIC_ID)) config_data[f"{self.ITEM_TYPE}s"].append( f"{self.ITEM_PUBLIC_ID.author}/{self.ITEM_PUBLIC_ID.name}:0.0.1") self.dump_config(AgentConfig.from_json(config_data)) try: yield finally: self.dump_config(original_config)
def setup_class(cls): """Set the test up.""" cls.runner = CliRunner() cls.agent_name = "myagent" cls.cwd = os.getcwd() cls.t = tempfile.mkdtemp() dir_path = Path("packages") tmp_dir = cls.t / dir_path src_dir = cls.cwd / Path(ROOT_DIR, dir_path) shutil.copytree(str(src_dir), str(tmp_dir)) cls.skill_id = str(GYM_SKILL_PUBLIC_ID) cls.skill_name = "gym" os.chdir(cls.t) result = cls.runner.invoke( cli, [*CLI_LOG_OPTION, "init", "--local", "--author", AUTHOR] ) assert result.exit_code == 0 result = cls.runner.invoke( cli, [*CLI_LOG_OPTION, "create", "--local", cls.agent_name], standalone_mode=False, ) assert result.exit_code == 0 os.chdir(cls.agent_name) # change default registry path config = AgentConfig.from_json(yaml.safe_load(open(DEFAULT_AEA_CONFIG_FILE))) config.registry_path = os.path.join(ROOT_DIR, "packages") yaml.safe_dump(dict(config.json), open(DEFAULT_AEA_CONFIG_FILE, "w")) result = cls.runner.invoke( cli, [*CLI_LOG_OPTION, "add", "--local", "skill", cls.skill_id], standalone_mode=False, ) assert result.exit_code == 0 cls.patch = unittest.mock.patch( "shutil.rmtree", side_effect=BaseException("an exception") ) cls.patch.start() cls.result = cls.runner.invoke( cli, [*CLI_LOG_OPTION, "remove", "skill", cls.skill_name], standalone_mode=False, )
def test_add_key_fails_bad_key(): """Test that 'aea add-key' fails because the key is not valid.""" oldcwd = os.getcwd() runner = CliRunner() agent_name = "myagent" tmpdir = tempfile.mkdtemp() dir_path = Path("packages") tmp_dir = tmpdir / dir_path src_dir = oldcwd / Path(ROOT_DIR, dir_path) shutil.copytree(str(src_dir), str(tmp_dir)) os.chdir(tmpdir) try: with mock.patch.object( aea.crypto.helpers._default_logger, "error" ) as mock_logger_error: result = runner.invoke( cli, [*CLI_LOG_OPTION, "init", "--local", "--author", AUTHOR] ) result = runner.invoke( cli, [*CLI_LOG_OPTION, "create", "--local", agent_name] ) assert result.exit_code == 0 os.chdir(Path(tmpdir, agent_name)) # create an empty file - surely not a private key pvk_file = "this_is_not_a_key.txt" Path(pvk_file).touch() result = runner.invoke(cli, [*CLI_LOG_OPTION, "add-key", FETCHAI, pvk_file]) assert result.exit_code == 1 error_message = "Invalid length of private key, received 0, expected 32" mock_logger_error.assert_called_with( "This is not a valid private key file: '{}'\n Exception: '{}'".format( pvk_file, error_message ), exc_info=True, ) # check that no key has been added. f = open(Path(DEFAULT_AEA_CONFIG_FILE)) expected_json = yaml.safe_load(f) config = AgentConfig.from_json(expected_json) assert len(config.private_key_paths.read_all()) == 0 finally: os.chdir(oldcwd)
def setup_class(cls): """Set the test up.""" cls.runner = CliRunner() cls.agent_name = "myagent" cls.cwd = os.getcwd() cls.t = tempfile.mkdtemp() cls.skill_id = "fetchai/echo:0.1.0" cls.skill_name = "echo" cls.patch = unittest.mock.patch.object(aea.cli.common.logger, "error") cls.mocked_logger_error = cls.patch.__enter__() # copy the 'packages' directory in the parent of the agent folder. shutil.copytree(Path(CUR_PATH, "..", "packages"), Path(cls.t, "packages")) os.chdir(cls.t) result = cls.runner.invoke( cli, [*CLI_LOG_OPTION, "init", "--local", "--author", AUTHOR]) assert result.exit_code == 0 result = cls.runner.invoke( cli, [*CLI_LOG_OPTION, "create", "--local", cls.agent_name], standalone_mode=False, ) assert result.exit_code == 0 os.chdir(cls.agent_name) # change default registry path config = AgentConfig.from_json( yaml.safe_load(open(DEFAULT_AEA_CONFIG_FILE))) config.registry_path = os.path.join(ROOT_DIR, "packages") yaml.safe_dump(dict(config.json), open(DEFAULT_AEA_CONFIG_FILE, "w")) # change the serialization of the AgentConfig class so to make the parsing to fail. cls.patch = unittest.mock.patch.object( aea.configurations.base.SkillConfig, "from_json", side_effect=ValidationError("test error message"), ) cls.patch.__enter__() cls.result = cls.runner.invoke( cli, [*CLI_LOG_OPTION, "add", "--local", "skill", cls.skill_id], standalone_mode=False, )
def setup_class(cls): """Set the test up.""" cls.runner = CliRunner() cls.agent_name = "myagent" cls.cwd = os.getcwd() cls.t = tempfile.mkdtemp() cls.skill_id = "fetchai/gym:0.1.0" cls.skill_name = "gym" cls.patch = unittest.mock.patch.object(aea.cli.common.logger, "error") cls.mocked_logger_error = cls.patch.__enter__() os.chdir(cls.t) result = cls.runner.invoke( cli, [*CLI_LOG_OPTION, "init", "--local", "--author", AUTHOR]) assert result.exit_code == 0 result = cls.runner.invoke( cli, [*CLI_LOG_OPTION, "create", "--local", cls.agent_name], standalone_mode=False, ) assert result.exit_code == 0 os.chdir(cls.agent_name) # change default registry path config = AgentConfig.from_json( yaml.safe_load(open(DEFAULT_AEA_CONFIG_FILE))) config.registry_path = os.path.join(ROOT_DIR, "packages") yaml.safe_dump(dict(config.json), open(DEFAULT_AEA_CONFIG_FILE, "w")) result = cls.runner.invoke( cli, [*CLI_LOG_OPTION, "add", "--local", "skill", cls.skill_id], standalone_mode=False, ) assert result.exit_code == 0 cls.patch = unittest.mock.patch( "shutil.rmtree", side_effect=BaseException("an exception")) cls.patch.__enter__() cls.result = cls.runner.invoke( cli, [*CLI_LOG_OPTION, "remove", "skill", cls.skill_name], standalone_mode=False, )
def test_fetch(self): """Test that the fetch private key is created correctly.""" result = self.runner.invoke(cli, [*CLI_LOG_OPTION, "generate-key", FETCHAI]) assert result.exit_code == 0 assert Path(FETCHAI_PRIVATE_KEY_FILE).exists() # this line tests that the content of the file is correct. FetchAICrypto(FETCHAI_PRIVATE_KEY_FILE) result = self.runner.invoke(cli, [*CLI_LOG_OPTION, "add-key", FETCHAI, FETCHAI_PRIVATE_KEY_FILE]) assert result.exit_code == 0 f = open(Path(self.agent_folder, DEFAULT_AEA_CONFIG_FILE)) expected_json = yaml.safe_load(f) config = AgentConfig.from_json(expected_json) private_key_configuration = config.private_key_paths.read(FETCHAI) assert private_key_configuration is not None assert private_key_configuration.ledger == FETCHAI assert private_key_configuration.path == FETCHAI_PRIVATE_KEY_FILE assert len(config.private_key_paths.read_all()) == 2
def setup_class(cls): """Set the test up.""" cls.runner = CliRunner() cls.agent_name = "myagent" cls.cwd = os.getcwd() cls.t = tempfile.mkdtemp() cls.skill_id = "fetchai/gym:0.4.0" cls.skill_name = "gym" os.chdir(cls.t) result = cls.runner.invoke( cli, [*CLI_LOG_OPTION, "init", "--local", "--author", AUTHOR] ) assert result.exit_code == 0 result = cls.runner.invoke( cli, [*CLI_LOG_OPTION, "create", "--local", cls.agent_name], standalone_mode=False, ) assert result.exit_code == 0 os.chdir(cls.agent_name) # change default registry path config = AgentConfig.from_json(yaml.safe_load(open(DEFAULT_AEA_CONFIG_FILE))) config.registry_path = os.path.join(ROOT_DIR, "packages") yaml.safe_dump(dict(config.json), open(DEFAULT_AEA_CONFIG_FILE, "w")) result = cls.runner.invoke( cli, [*CLI_LOG_OPTION, "add", "--local", "skill", cls.skill_id], standalone_mode=False, ) assert result.exit_code == 0 cls.result = cls.runner.invoke( cli, [*CLI_LOG_OPTION, "remove", "skill", cls.skill_id], standalone_mode=False, )
def setup_class(cls): """Set the test up.""" cls.runner = CliRunner() cls.agent_name = "myagent" cls.cwd = os.getcwd() cls.t = tempfile.mkdtemp() cls.skill_id = str(ECHO_PUBLIC_ID) cls.skill_name = "echo" # copy the 'packages' directory in the parent of the agent folder. shutil.copytree(Path(CUR_PATH, "..", "packages"), Path(cls.t, "packages")) os.chdir(cls.t) result = cls.runner.invoke( cli, [*CLI_LOG_OPTION, "init", "--local", "--author", AUTHOR] ) assert result.exit_code == 0 result = cls.runner.invoke( cli, [*CLI_LOG_OPTION, "create", "--local", cls.agent_name], standalone_mode=False, ) assert result.exit_code == 0 os.chdir(cls.agent_name) # change default registry path config = AgentConfig.from_json(yaml.safe_load(open(DEFAULT_AEA_CONFIG_FILE))) config.registry_path = os.path.join(ROOT_DIR, "packages") yaml.safe_dump(dict(config.json), open(DEFAULT_AEA_CONFIG_FILE, "w")) Path( cls.t, cls.agent_name, "vendor", "fetchai", "skills", cls.skill_name ).mkdir(parents=True, exist_ok=True) cls.result = cls.runner.invoke( cli, [*CLI_LOG_OPTION, "add", "--local", "skill", cls.skill_id], standalone_mode=False, )
def test_echo(self, pytestconfig): """Run the echo skill sequence.""" if pytestconfig.getoption("ci"): pytest.skip("Skipping the test since it doesn't work in CI.") # add packages folder packages_src = os.path.join(self.cwd, 'packages') packages_dst = os.path.join(os.getcwd(), 'packages') shutil.copytree(packages_src, packages_dst) # create agent result = self.runner.invoke( cli, [*CLI_LOG_OPTION, "create", self.agent_name], standalone_mode=False) assert result.exit_code == 0 agent_dir_path = os.path.join(self.t, self.agent_name) os.chdir(agent_dir_path) # disable logging aea_config_path = Path(self.t, self.agent_name, DEFAULT_AEA_CONFIG_FILE) aea_config = AgentConfig.from_json( yaml.safe_load(open(aea_config_path))) aea_config.logging_config = { "disable_existing_loggers": False, "version": 1, "loggers": { "aea.echo_skill": { "level": "CRITICAL" } } } yaml.safe_dump(aea_config.json, open(aea_config_path, "w")) # add skills result = self.runner.invoke(cli, [*CLI_LOG_OPTION, "add", "skill", "echo"], standalone_mode=False) assert result.exit_code == 0 result = self.runner.invoke( cli, [*CLI_LOG_OPTION, "add", "connection", "stub"], standalone_mode=False) assert result.exit_code == 0 # run the agent process = subprocess.Popen( [sys.executable, '-m', 'aea.cli', "run", "--connections", "stub"], stdout=subprocess.PIPE, env=os.environ.copy()) # add sending and receiving envelope from input/output files time.sleep(2.0) message = DefaultMessage(type=DefaultMessage.Type.BYTES, content=b"hello") expected_envelope = Envelope( to=self.agent_name, sender="sender", protocol_id="default", message=DefaultSerializer().encode(message)) encoded_envelope = "{},{},{},{}".format( expected_envelope.to, expected_envelope.sender, expected_envelope.protocol_id, expected_envelope.message.decode("utf-8")) encoded_envelope = encoded_envelope.encode("utf-8") with open(Path(self.t, self.agent_name, "input_file"), "ab+") as f: f.write(encoded_envelope + b"\n") f.flush() time.sleep(2.0) with open(Path(self.t, self.agent_name, "output_file"), "rb+") as f: lines = f.readlines() assert len(lines) == 1 line = lines[0] to, sender, protocol_id, message = line.strip().split(b",", maxsplit=3) to = to.decode("utf-8") sender = sender.decode("utf-8") protocol_id = protocol_id.decode("utf-8") actual_envelope = Envelope(to=to, sender=sender, protocol_id=protocol_id, message=message) assert expected_envelope.to == actual_envelope.sender assert expected_envelope.sender == actual_envelope.to assert expected_envelope.protocol_id == actual_envelope.protocol_id assert expected_envelope.message == actual_envelope.message time.sleep(2.0) process.send_signal(signal.SIGINT) process.wait(timeout=20) assert process.returncode == 0 poll = process.poll() if poll is None: process.terminate() process.wait(2) os.chdir(self.t) self.result = self.runner.invoke( cli, [*CLI_LOG_OPTION, "delete", self.agent_name], standalone_mode=False)
def ipfs_hashing( package_hashes: Dict[str, str], target_dir: str, package_type: str, package_name: str, ipfs_hash_only: IPFSHashOnly, ): """Hashes a package and its components.""" print("Processing package {} of type {}".format(package_name, package_type)) # load config file to get ignore patterns, dump again immediately to impose ordering if package_type == "agents": config = AgentConfig.from_json( yaml.safe_load(open(Path(target_dir, DEFAULT_AEA_CONFIG_FILE)))) yaml_dump(config.json, open(Path(target_dir, DEFAULT_AEA_CONFIG_FILE), "w")) elif package_type == "connections": config = ConnectionConfig.from_json( yaml.safe_load( open(Path(target_dir, DEFAULT_CONNECTION_CONFIG_FILE)))) yaml_dump(config.json, open(Path(target_dir, DEFAULT_CONNECTION_CONFIG_FILE), "w")) elif package_type == "contracts": config = ContractConfig.from_json( yaml.safe_load(open(Path(target_dir, DEFAULT_CONTRACT_CONFIG_FILE)))) yaml_dump(config.json, open(Path(target_dir, DEFAULT_CONTRACT_CONFIG_FILE), "w")) elif package_type == "protocols": config = ProtocolConfig.from_json( yaml.safe_load(open(Path(target_dir, DEFAULT_PROTOCOL_CONFIG_FILE)))) yaml_dump(config.json, open(Path(target_dir, DEFAULT_PROTOCOL_CONFIG_FILE), "w")) elif package_type == "skills": config = SkillConfig.from_json( yaml.safe_load(open(Path(target_dir, DEFAULT_SKILL_CONFIG_FILE)))) yaml_dump(config.json, open(Path(target_dir, DEFAULT_SKILL_CONFIG_FILE), "w")) config = yaml.safe_load(next(Path(target_dir).glob("*.yaml")).open()) ignore_patterns = config.get("fingerprint_ignore_patterns", []) if package_type != "agents": # hash inner components fingerprints_dict = _compute_fingerprint(Path(target_dir), ignore_patterns) # confirm ipfs only generates same hash: for file_name, ipfs_hash in fingerprints_dict.items(): path = os.path.join(target_dir, file_name) ipfsho_hash = ipfs_hash_only.get(path) if ipfsho_hash != ipfs_hash: print("WARNING, hashes don't match for: {}".format(path)) # update fingerprints file_name = package_type[:-1] + ".yaml" yaml_path = os.path.join(target_dir, file_name) file = open(yaml_path, mode="r") # read all lines at once whole_file = file.read() # close the file file.close() file = open(yaml_path, mode="r") # find and replace # TODO this can be simplified after https://github.com/fetchai/agents-aea/issues/932 existing = "" fingerprint_block = False for line in file: if line.find("fingerprint:") == 0: existing += line fingerprint_block = True elif fingerprint_block: if line.find(" ") == 0: # still inside fingerprint block existing += line else: # fingerprint block has ended break if len(fingerprints_dict) > 0: replacement = "fingerprint:\n" ordered_fingerprints_dict = collections.OrderedDict( sorted(fingerprints_dict.items())) for file_name, ipfs_hash in ordered_fingerprints_dict.items(): replacement += " " + file_name + ": " + ipfs_hash + "\n" else: replacement = "fingerprint: {}\n" whole_file = whole_file.replace(existing, replacement) # close the file file.close() # update fingerprints with open(yaml_path, "w") as f: f.write(whole_file) # hash again to get outer hash (this time all files): # TODO we still need to ignore some files result_list = client.add(target_dir) for result_dict in result_list: if package_name == result_dict["Name"]: key = os.path.join(AUTHOR, package_type, package_name) package_hashes[key] = result_dict["Hash"]
def test_echo(self, pytestconfig): """Run the echo skill sequence.""" if pytestconfig.getoption("ci"): pytest.skip("Skipping the test since it doesn't work in CI.") # add packages folder packages_src = os.path.join(self.cwd, "packages") packages_dst = os.path.join(self.t, "packages") shutil.copytree(packages_src, packages_dst) result = self.runner.invoke( cli, [*CLI_LOG_OPTION, "init", "--local", "--author", AUTHOR], standalone_mode=False, ) assert result.exit_code == 0 # create agent result = self.runner.invoke( cli, [*CLI_LOG_OPTION, "create", "--local", self.agent_name], standalone_mode=False, ) assert result.exit_code == 0 agent_dir_path = os.path.join(self.t, self.agent_name) os.chdir(agent_dir_path) # disable logging aea_config_path = Path(self.t, self.agent_name, DEFAULT_AEA_CONFIG_FILE) aea_config = AgentConfig.from_json( yaml.safe_load(open(aea_config_path))) aea_config.logging_config = { "disable_existing_loggers": False, "version": 1, "loggers": { "aea.echo_skill": { "level": "CRITICAL" } }, } yaml.safe_dump(dict(aea_config.json), open(aea_config_path, "w")) # add skills result = self.runner.invoke( cli, [*CLI_LOG_OPTION, "add", "--local", "skill", "fetchai/echo:0.1.0"], standalone_mode=False, ) assert result.exit_code == 0 try: # run the agent process = subprocess.Popen( # nosec [sys.executable, "-m", "aea.cli", "run"], stdout=subprocess.PIPE, env=os.environ.copy(), ) time.sleep(2.0) # add sending and receiving envelope from input/output files message = DefaultMessage( dialogue_reference=("", ""), message_id=1, target=0, performative=DefaultMessage.Performative.BYTES, content=b"hello", ) expected_envelope = Envelope( to=self.agent_name, sender="sender", protocol_id=DefaultMessage.protocol_id, message=DefaultSerializer().encode(message), ) encoded_envelope = "{},{},{},{},".format( expected_envelope.to, expected_envelope.sender, expected_envelope.protocol_id, expected_envelope.message.decode("utf-8"), ) encoded_envelope = encoded_envelope.encode("utf-8") with open(Path(self.t, self.agent_name, "input_file"), "ab+") as f: f.write(encoded_envelope) f.flush() time.sleep(2.0) with open(Path(self.t, self.agent_name, "output_file"), "rb+") as f: lines = f.readlines() assert len(lines) == 2 line = lines[0] + lines[1] to, sender, protocol_id, message, end = line.strip().split( b",", maxsplit=4) to = to.decode("utf-8") sender = sender.decode("utf-8") protocol_id = PublicId.from_str(protocol_id.decode("utf-8")) assert end in [b"", b"\n"] actual_envelope = Envelope(to=to, sender=sender, protocol_id=protocol_id, message=message) assert expected_envelope.to == actual_envelope.sender assert expected_envelope.sender == actual_envelope.to assert expected_envelope.protocol_id == actual_envelope.protocol_id assert expected_envelope.message == actual_envelope.message time.sleep(2.0) finally: process.send_signal(signal.SIGINT) process.wait(timeout=20) if not process.returncode == 0: poll = process.poll() if poll is None: process.terminate() process.wait(2) os.chdir(self.t) result = self.runner.invoke( cli, [*CLI_LOG_OPTION, "delete", self.agent_name], standalone_mode=False) assert result.exit_code == 0