Example #1
0
    def _cli_launch(self,
                    agents: List[str],
                    options: Optional[List[str]] = None) -> Generator:
        """
        Run aea.cli wrapped with Pexpect.

        :param agents: list of agent names to run
        :param options: list of string options to pass to aea launch.

        :return: PexpectWrapper
        """
        proc = PexpectWrapper(  # nosec
            [
                sys.executable,
                "-m",
                "aea.cli",
                "-v",
                "DEBUG",
                "launch",
                *(options or []),
                *(agents or []),
            ],
            env=os.environ,
            maxread=10000,
            encoding="utf-8",
            logfile=sys.stdout,
        )
        try:
            yield proc
        finally:
            proc.wait_to_complete(10)
Example #2
0
    def test_multiplexer_disconnected_on_termination_after_connected_one_connection(
        self, ):
        """Test multiplexer disconnected properly on termination after connected."""

        result = self.runner.invoke(
            cli,
            [
                *CLI_LOG_OPTION, "add", "--local", "connection",
                str(STUB_CONNECTION_ID)
            ],
        )
        assert result.exit_code == 0, result.stdout_bytes

        self.proc = PexpectWrapper(  # nosec
            [sys.executable, "-m", "aea.cli", "-v", "DEBUG", "run"],
            env=os.environ,
            maxread=10000,
            encoding="utf-8",
            logfile=sys.stdout,
        )

        self.proc.expect_all(
            ["Start processing messages..."],
            timeout=20,
        )
        self.proc.control_c()
        self.proc.expect_all(
            ["Multiplexer disconnecting...", "Multiplexer disconnected.", EOF],
            timeout=20,
        )
Example #3
0
    def test_gui(self):
        """Test that the gui process has been spawned correctly."""
        self.proc = PexpectWrapper(  # nosec
            [sys.executable, "-m", "aea.cli", "-v", "DEBUG", "gui"],
            encoding="utf-8",
            logfile=sys.stdout,
        )
        self.proc.expect_exact(["Running on http://"], timeout=20)

        assert tcpping("127.0.0.1", 8080)
Example #4
0
    def test_multiplexer_disconnected_on_termination_after_connected_no_connection(
        self, ):
        """Test multiplexer disconnected properly on termination after connected."""
        self.proc = PexpectWrapper(  # nosec
            [sys.executable, "-m", "aea.cli", "-v", "DEBUG", "run"],
            env=os.environ,
            maxread=10000,
            encoding="utf-8",
            logfile=sys.stdout,
        )

        self.proc.expect_all(
            ["Start processing messages..."],
            timeout=20,
        )
        self.proc.control_c()
        self.proc.expect_all(
            ["Multiplexer disconnecting...", "Multiplexer disconnected.", EOF],
            timeout=20,
        )
Example #5
0
def test_gym_ex():
    """Run the gym ex sequence."""
    try:
        process = PexpectWrapper(  # nosec
            [
                sys.executable,
                str(Path("examples/gym_ex/train.py").resolve()),
                "--nb-steps",
                "50",
            ],
            env=os.environ.copy(),
            maxread=1,
            encoding="utf-8",
            logfile=sys.stdout,
        )

        process.expect(["Step 50/50"], timeout=10)
        process.wait_to_complete(5)
        assert process.returncode == 0, "Test failed"
    finally:
        process.terminate()
        process.wait()
Example #6
0
    def test_gym_ex(self):
        """Run the gym ex sequence."""
        try:
            env = os.environ.copy()
            env["PYTHONPATH"] = f"{self.t}{env_path_separator()}{env.get('PYTHONPATH', '')}"
            process = PexpectWrapper(  # nosec
                [
                    sys.executable,
                    str(Path("examples/gym_ex/train.py").resolve()),
                    "--nb-steps",
                    "50",
                ],
                env=env,
                maxread=1,
                encoding="utf-8",
                logfile=sys.stdout,
            )

            process.expect(["Step 50/50"], timeout=10)
            process.wait_to_complete(5)
            assert process.returncode == 0, "Test failed"
        finally:
            process.terminate()
            process.wait()
Example #7
0
class TestGui:
    """Test that the command 'aea gui' works as expected."""
    def setup(self):
        """Set the test up."""
        self.runner = CliRunner()
        self.schema = json.load(open(AGENT_CONFIGURATION_SCHEMA))
        self.resolver = jsonschema.RefResolver(
            make_jsonschema_base_uri(
                Path(CONFIGURATION_SCHEMA_DIR).absolute()),
            self.schema,
        )
        self.validator = Draft4Validator(self.schema, resolver=self.resolver)

        self.agent_name = "myagent"
        self.cwd = os.getcwd()
        self.t = tempfile.mkdtemp()
        os.chdir(self.t)
        result = self.runner.invoke(
            cli,
            [*CLI_LOG_OPTION, "init", "--local", "--author", "test_author"],
        )

        assert result.exit_code == 0

    @pytest.mark.flaky(reruns=MAX_FLAKY_RERUNS)
    def test_gui(self):
        """Test that the gui process has been spawned correctly."""
        self.proc = PexpectWrapper(  # nosec
            [sys.executable, "-m", "aea.cli", "-v", "DEBUG", "gui"],
            encoding="utf-8",
            logfile=sys.stdout,
        )
        self.proc.expect_exact(["Running on http://"], timeout=20)

        assert tcpping("127.0.0.1", 8080)

    def teardown(self):
        """Tear the test down."""
        self.proc.terminate()
        self.proc.wait_to_complete(10)
        os.chdir(self.cwd)
        try:
            shutil.rmtree(self.t)
        except OSError:
            pass
Example #8
0
def test_run_with_install_deps_and_requirement_file():
    """Test that the command 'aea run --install-deps' with requirement file does not crash."""
    runner = CliRunner()
    agent_name = "myagent"
    cwd = os.getcwd()
    t = tempfile.mkdtemp()
    # copy the 'packages' directory in the parent of the agent folder.
    shutil.copytree(Path(ROOT_DIR, "packages"), Path(t, "packages"))

    os.chdir(t)
    result = runner.invoke(
        cli, [*CLI_LOG_OPTION, "init", "--local", "--author", AUTHOR])
    assert result.exit_code == 0

    result = runner.invoke(cli,
                           [*CLI_LOG_OPTION, "create", "--local", agent_name])
    assert result.exit_code == 0

    os.chdir(Path(t, agent_name))

    result = runner.invoke(
        cli,
        [
            *CLI_LOG_OPTION, "add", "--local", "connection",
            "fetchai/http_client:0.7.0"
        ],
    )
    assert result.exit_code == 0
    result = runner.invoke(
        cli,
        [
            *CLI_LOG_OPTION,
            "config",
            "set",
            "agent.default_connection",
            "fetchai/http_client:0.7.0",
        ],
    )
    assert result.exit_code == 0

    result = runner.invoke(cli, [*CLI_LOG_OPTION, "freeze"])
    assert result.exit_code == 0
    Path(t, agent_name, "requirements.txt").write_text(result.output)

    try:
        process = PexpectWrapper(
            [
                sys.executable,
                "-m",
                "aea.cli",
                "-v",
                "DEBUG",
                "run",
                "--install-deps",
                "--connections",
                "fetchai/http_client:0.7.0",
            ],
            env=os.environ,
            maxread=10000,
            encoding="utf-8",
            logfile=sys.stdout,
        )
        process.expect_all(["Start processing messages..."], timeout=30)
        time.sleep(1.0)
        process.control_c()
        process.wait_to_complete(10)
        assert process.returncode == 0

    finally:
        process.terminate()
        process.wait_to_complete(10)
        os.chdir(cwd)
        try:
            shutil.rmtree(t)
        except (OSError, IOError):
            pass
Example #9
0
def test_run():
    """Test that the command 'aea run' works as expected."""
    runner = CliRunner()
    agent_name = "myagent"
    cwd = os.getcwd()
    t = tempfile.mkdtemp()
    # copy the 'packages' directory in the parent of the agent folder.
    shutil.copytree(Path(ROOT_DIR, "packages"), Path(t, "packages"))

    os.chdir(t)
    result = runner.invoke(
        cli, [*CLI_LOG_OPTION, "init", "--local", "--author", AUTHOR])
    assert result.exit_code == 0

    result = runner.invoke(cli,
                           [*CLI_LOG_OPTION, "create", "--local", agent_name])
    assert result.exit_code == 0

    os.chdir(Path(t, agent_name))

    result = runner.invoke(
        cli,
        [
            *CLI_LOG_OPTION, "add", "--local", "connection",
            "fetchai/http_client:0.7.0"
        ],
    )
    assert result.exit_code == 0

    result = runner.invoke(
        cli,
        [
            *CLI_LOG_OPTION,
            "config",
            "set",
            "agent.default_connection",
            "fetchai/http_client:0.7.0",
        ],
    )
    assert result.exit_code == 0

    try:
        process = PexpectWrapper(  # nosec
            [sys.executable, "-m", "aea.cli", "run"],
            env=os.environ.copy(),
            maxread=10000,
            encoding="utf-8",
            logfile=sys.stdout,
        )

        process.expect("Start processing messages", timeout=10)
        process.control_c()
        process.wait_to_complete(10)

        assert process.returncode == 0

    finally:
        process.terminate()
        process.wait_to_complete(10)

        os.chdir(cwd)
        try:
            shutil.rmtree(t)
        except (OSError, IOError):
            pass
Example #10
0
def test_run_multiple_connections(connection_ids):
    """Test that the command 'aea run' works as expected when specifying multiple connections."""
    runner = CliRunner()
    agent_name = "myagent"
    cwd = os.getcwd()
    t = tempfile.mkdtemp()
    # copy the 'packages' directory in the parent of the agent folder.
    shutil.copytree(Path(ROOT_DIR, "packages"), Path(t, "packages"))

    os.chdir(t)
    result = runner.invoke(
        cli, [*CLI_LOG_OPTION, "init", "--local", "--author", AUTHOR])
    assert result.exit_code == 0

    result = runner.invoke(cli,
                           [*CLI_LOG_OPTION, "create", "--local", agent_name])
    assert result.exit_code == 0

    os.chdir(Path(t, agent_name))

    result = runner.invoke(
        cli,
        [
            *CLI_LOG_OPTION, "add", "--local", "connection",
            "fetchai/http_client:0.7.0"
        ],
    )
    assert result.exit_code == 0

    # stub is the default connection, so it should fail
    result = runner.invoke(cli, [
        *CLI_LOG_OPTION, "add", "--local", "connection",
        str(DEFAULT_CONNECTION)
    ])
    assert result.exit_code == 1
    process = PexpectWrapper(  # nosec
        [
            sys.executable, "-m", "aea.cli", "run", "--connections",
            connection_ids
        ],
        env=os.environ,
        maxread=10000,
        encoding="utf-8",
        logfile=sys.stdout,
    )

    try:
        process.expect_all(["Start processing messages"], timeout=20)
        process.control_c()
        process.expect(
            EOF,
            timeout=20,
        )
        process.wait_to_complete(10)
        assert process.returncode == 0
    finally:
        process.wait_to_complete(10)
        os.chdir(cwd)
        try:
            shutil.rmtree(t)
        except (OSError, IOError):
            pass
Example #11
0
def test_run_with_install_deps():
    """Test that the command 'aea run --install-deps' does not crash."""
    runner = CliRunner()
    agent_name = "myagent"
    cwd = os.getcwd()
    t = tempfile.mkdtemp()
    # copy the 'packages' directory in the parent of the agent folder.
    packages_src = os.path.join(ROOT_DIR, "packages")
    packages_dst = os.path.join(t, "packages")
    shutil.copytree(packages_src, packages_dst)

    os.chdir(t)
    result = runner.invoke(
        cli, [*CLI_LOG_OPTION, "init", "--local", "--author", AUTHOR])
    assert result.exit_code == 0

    result = runner.invoke(cli,
                           [*CLI_LOG_OPTION, "create", "--local", agent_name])
    assert result.exit_code == 0

    os.chdir(Path(t, agent_name))

    result = runner.invoke(
        cli,
        [
            *CLI_LOG_OPTION, "add", "--local", "connection",
            str(HTTP_ClIENT_PUBLIC_ID)
        ],
    )
    assert result.exit_code == 0
    result = runner.invoke(
        cli,
        [
            *CLI_LOG_OPTION,
            "config",
            "set",
            "agent.default_connection",
            str(HTTP_ClIENT_PUBLIC_ID),
        ],
    )
    assert result.exit_code == 0

    try:
        process = PexpectWrapper(
            [
                sys.executable,
                "-m",
                "aea.cli",
                "-v",
                "DEBUG",
                "run",
                "--install-deps",
                "--connections",
                str(HTTP_ClIENT_PUBLIC_ID),
            ],
            env=os.environ,
            maxread=10000,
            encoding="utf-8",
            logfile=sys.stdout,
        )
        process.expect_all(["Start processing messages..."], timeout=30)
        time.sleep(1.0)
        process.control_c()
        process.wait_to_complete(10)
        assert process.returncode == 0

    finally:
        process.terminate()
        process.wait_to_complete(10)
        os.chdir(cwd)
        try:
            shutil.rmtree(t)
        except (OSError, IOError):
            pass
Example #12
0
    def test_end_to_end(self):
        """Perform end to end test with simple register/search agents."""
        registration_agent_name = "registration_agent"
        self.value = uuid.uuid4().hex
        self.fetch_agent(
            "fetchai/simple_service_registration",
            agent_name=registration_agent_name,
            is_local=True,
        )

        self.run_cli_command(
            "config",
            "set",
            "vendor.fetchai.connections.p2p_libp2p.config",
            "--type",
            "dict",
            json.dumps(self.registration_agent_connection),
            cwd=registration_agent_name,
        )
        self.run_cli_command(
            "config",
            "set",
            "vendor.fetchai.skills.simple_service_registration.models.strategy.args.service_data",
            "--type",
            "dict",
            json.dumps({
                "key": self.key,
                "value": self.value
            }),
            cwd=registration_agent_name,
        )
        self.run_cli_command(
            "config",
            "set",
            "vendor.fetchai.connections.soef.config.token_storage_path",
            os.path.join(self.t, registration_agent_name, "soef_key.txt"),
            cwd=registration_agent_name,
        )

        search_agent_name = "search_agent"
        self.fetch_agent(
            "fetchai/simple_service_search",
            agent_name=search_agent_name,
            is_local=True,
        )
        self.run_cli_command(
            "config",
            "set",
            "vendor.fetchai.connections.p2p_libp2p.config",
            "--type",
            "dict",
            json.dumps(self.search_agent_connection),
            cwd=search_agent_name,
        )
        self.run_cli_command(
            "config",
            "set",
            "vendor.fetchai.skills.simple_service_search.models.strategy.args.search_query",
            "--type",
            "dict",
            json.dumps({
                "constraint_type": "==",
                "search_key": self.key,
                "search_value": self.value,
            }),
            cwd=search_agent_name,
        )
        self.run_cli_command(
            "config",
            "set",
            "vendor.fetchai.skills.simple_service_search.behaviours.service_search.args.tick_interval",
            "--type",
            "int",
            "2",
            cwd=search_agent_name,
        )
        self.run_cli_command(
            "config",
            "set",
            "vendor.fetchai.connections.soef.config.token_storage_path",
            os.path.join(self.t, search_agent_name, "soef_key.txt"),
            cwd=search_agent_name,
        )
        self.run_cli_command(
            "build",
            cwd=registration_agent_name,
        )
        self.run_cli_command(
            "build",
            cwd=search_agent_name,
        )
        self.set_agent_context(registration_agent_name)
        self.generate_private_key(FETCHAI, FETCHAI_PRIVATE_KEY_FILE_CONNECTION)
        self.add_private_key(FETCHAI,
                             FETCHAI_PRIVATE_KEY_FILE_CONNECTION,
                             connection=True)
        self.generate_private_key()
        self.add_private_key()
        self.unset_agent_context()
        self.run_cli_command(
            "issue-certificates",
            cwd=registration_agent_name,
        )
        self.set_agent_context(search_agent_name)
        self.generate_private_key(FETCHAI, FETCHAI_PRIVATE_KEY_FILE_CONNECTION)
        self.add_private_key(FETCHAI,
                             FETCHAI_PRIVATE_KEY_FILE_CONNECTION,
                             connection=True)
        self.generate_private_key()
        self.add_private_key()
        self.unset_agent_context()
        self.run_cli_command(
            "issue-certificates",
            cwd=search_agent_name,
        )

        proc = PexpectWrapper(  # nosec
            [
                sys.executable,
                "-m",
                "aea.cli",
                "-v",
                "DEBUG",
                "launch",
                registration_agent_name,
                search_agent_name,
            ],
            env=os.environ,
            maxread=10000,
            encoding="utf-8",
            logfile=sys.stdout,
        )
        try:
            proc.expect_all(
                [
                    f"[{search_agent_name}] found number of agents=1, search_response"
                ],
                timeout=30,
            )
        finally:
            proc.control_c()
            proc.expect("Exit cli. code: 0", timeout=30)
Example #13
0
    def test_multiplexer_disconnected_on_early_interruption(self):
        """Test multiplexer disconnected properly on termination before connected."""
        result = self.runner.invoke(cli, [
            *CLI_LOG_OPTION, "add", "--local", "connection",
            str(P2P_PUBLIC_ID)
        ])
        assert result.exit_code == 0, result.stdout_bytes

        result = self.runner.invoke(cli, [*CLI_LOG_OPTION, "build"])
        assert result.exit_code == 0, result.stdout_bytes

        result = self.runner.invoke(
            cli,
            [*CLI_LOG_OPTION, "generate-key", DEFAULT_LEDGER, self.key_path])
        assert result.exit_code == 0, result.stdout_bytes

        result = self.runner.invoke(
            cli, [*CLI_LOG_OPTION, "add-key", DEFAULT_LEDGER, self.key_path])
        assert result.exit_code == 0, result.stdout_bytes

        result = self.runner.invoke(cli, [
            *CLI_LOG_OPTION, "generate-key", DEFAULT_LEDGER, self.conn_key_path
        ])
        assert result.exit_code == 0, result.stdout_bytes

        result = self.runner.invoke(
            cli,
            [
                *CLI_LOG_OPTION,
                "add-key",
                DEFAULT_LEDGER,
                self.conn_key_path,
                "--connection",
            ],
        )
        assert result.exit_code == 0, result.stdout_bytes

        result = self.runner.invoke(cli,
                                    [*CLI_LOG_OPTION, "issue-certificates"])
        assert result.exit_code == 0, result.stdout_bytes

        self.proc = PexpectWrapper(  # nosec
            [sys.executable, "-m", "aea.cli", "-v", "DEBUG", "run"],
            env=os.environ,
            maxread=10000,
            encoding="utf-8",
            logfile=sys.stdout,
        )

        self.proc.expect_all(
            ["Starting libp2p node..."],
            timeout=50,
        )
        self.proc.control_c()
        self.proc.expect_all(
            ["Multiplexer .*disconnected."],
            timeout=20,
            strict=False,
        )

        self.proc.expect_all(
            [EOF],
            timeout=20,
        )
Example #14
0
class TestMultiplexerDisconnectsOnTermination:  # pylint: disable=attribute-defined-outside-init
    """Test multiplexer disconnects on  agent process keyboard interrupted."""
    def setup(self):
        """Set the test up."""
        self.proc = None
        self.runner = CliRunner()
        self.agent_name = "myagent"
        self.cwd = os.getcwd()
        self.t = tempfile.mkdtemp()
        shutil.copytree(Path(ROOT_DIR, "packages"), Path(self.t, "packages"))
        os.chdir(self.t)
        self.key_path = os.path.join(self.t, "fetchai_private_key.txt")
        self.conn_key_path = os.path.join(self.t, "conn_private_key.txt")

        result = self.runner.invoke(
            cli, [*CLI_LOG_OPTION, "init", "--local", "--author", AUTHOR])
        assert result.exit_code == 0

        result = self.runner.invoke(
            cli, [*CLI_LOG_OPTION, "create", "--local", self.agent_name])
        assert result.exit_code == 0

        os.chdir(Path(self.t, self.agent_name))

    def test_multiplexer_disconnected_on_early_interruption(self):
        """Test multiplexer disconnected properly on termination before connected."""
        result = self.runner.invoke(cli, [
            *CLI_LOG_OPTION, "add", "--local", "connection",
            str(P2P_PUBLIC_ID)
        ])
        assert result.exit_code == 0, result.stdout_bytes

        result = self.runner.invoke(cli, [*CLI_LOG_OPTION, "build"])
        assert result.exit_code == 0, result.stdout_bytes

        result = self.runner.invoke(
            cli,
            [*CLI_LOG_OPTION, "generate-key", DEFAULT_LEDGER, self.key_path])
        assert result.exit_code == 0, result.stdout_bytes

        result = self.runner.invoke(
            cli, [*CLI_LOG_OPTION, "add-key", DEFAULT_LEDGER, self.key_path])
        assert result.exit_code == 0, result.stdout_bytes

        result = self.runner.invoke(cli, [
            *CLI_LOG_OPTION, "generate-key", DEFAULT_LEDGER, self.conn_key_path
        ])
        assert result.exit_code == 0, result.stdout_bytes

        result = self.runner.invoke(
            cli,
            [
                *CLI_LOG_OPTION,
                "add-key",
                DEFAULT_LEDGER,
                self.conn_key_path,
                "--connection",
            ],
        )
        assert result.exit_code == 0, result.stdout_bytes

        result = self.runner.invoke(cli,
                                    [*CLI_LOG_OPTION, "issue-certificates"])
        assert result.exit_code == 0, result.stdout_bytes

        self.proc = PexpectWrapper(  # nosec
            [sys.executable, "-m", "aea.cli", "-v", "DEBUG", "run"],
            env=os.environ,
            maxread=10000,
            encoding="utf-8",
            logfile=sys.stdout,
        )

        self.proc.expect_all(
            ["Starting libp2p node..."],
            timeout=50,
        )
        self.proc.control_c()
        self.proc.expect_all(
            ["Multiplexer .*disconnected."],
            timeout=20,
            strict=False,
        )

        self.proc.expect_all(
            [EOF],
            timeout=20,
        )

    def test_multiplexer_disconnected_on_termination_after_connected_no_connection(
        self, ):
        """Test multiplexer disconnected properly on termination after connected."""
        self.proc = PexpectWrapper(  # nosec
            [sys.executable, "-m", "aea.cli", "-v", "DEBUG", "run"],
            env=os.environ,
            maxread=10000,
            encoding="utf-8",
            logfile=sys.stdout,
        )

        self.proc.expect_all(
            ["Start processing messages..."],
            timeout=20,
        )
        self.proc.control_c()
        self.proc.expect_all(
            ["Multiplexer disconnecting...", "Multiplexer disconnected.", EOF],
            timeout=20,
        )

    def test_multiplexer_disconnected_on_termination_after_connected_one_connection(
        self, ):
        """Test multiplexer disconnected properly on termination after connected."""

        result = self.runner.invoke(
            cli,
            [
                *CLI_LOG_OPTION, "add", "--local", "connection",
                str(STUB_CONNECTION_ID)
            ],
        )
        assert result.exit_code == 0, result.stdout_bytes

        self.proc = PexpectWrapper(  # nosec
            [sys.executable, "-m", "aea.cli", "-v", "DEBUG", "run"],
            env=os.environ,
            maxread=10000,
            encoding="utf-8",
            logfile=sys.stdout,
        )

        self.proc.expect_all(
            ["Start processing messages..."],
            timeout=20,
        )
        self.proc.control_c()
        self.proc.expect_all(
            ["Multiplexer disconnecting...", "Multiplexer disconnected.", EOF],
            timeout=20,
        )

    def teardown(self):
        """Tear the test down."""
        if self.proc:
            self.proc.wait_to_complete(10)
        os.chdir(self.cwd)
        try:
            shutil.rmtree(self.t)
        except (OSError, IOError):
            pass