From 3de8d5688e6ffb45dd6bfe275939e737bdb28081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Nowosielski?= Date: Tue, 16 Mar 2021 08:17:19 +0100 Subject: [PATCH 1/4] expose 'payment drivers' CLI command to requestor probe --- goth/runner/cli/yagna_payment_cmd.py | 36 +++++++++++++++++++ .../goth/runner/cli/test_yagna_payment_cmd.py | 9 +++++ 2 files changed, 45 insertions(+) diff --git a/goth/runner/cli/yagna_payment_cmd.py b/goth/runner/cli/yagna_payment_cmd.py index 6d8f96038..422bd6811 100644 --- a/goth/runner/cli/yagna_payment_cmd.py +++ b/goth/runner/cli/yagna_payment_cmd.py @@ -45,6 +45,30 @@ def from_dict(source: dict) -> "PaymentStatus": ) +@dataclass(frozen=True) +class Network: + """Contains information about `Network`.""" + + default_token: str + tokens: Dict[str, str] + + +@dataclass(frozen=True) +class Driver: + """Contains driver details fields.""" + + default_network: str + networks: Dict[str, Network] + + @staticmethod + def from_dict(source: dict): + """Parse a dict into an instance of `Driver` class.""" + return Driver( + default_network=source["default_network"], + networks={key: Network(**val) for key, val in source["networks"].items()}, + ) + + class YagnaPaymentMixin: """A mixin class that adds support for ` payment` commands.""" @@ -97,3 +121,15 @@ def payment_status( args = make_args("payment", "status", driver.name, data_dir=data_dir) output = self.run_json_command(Dict, *args) return PaymentStatus.from_dict(output) + + def payment_drivers( + self: CommandRunner, + ) -> Dict[str, Driver]: + """Run ` payment drivers` without any extra args. + + Parse the command's output as a `Dict[str, Driver]` and return it. + """ + + args = make_args("payment", "drivers") + output = self.run_json_command(Dict, *args) + return {key: Driver.from_dict(val) for key, val in output.items()} diff --git a/test/goth/runner/cli/test_yagna_payment_cmd.py b/test/goth/runner/cli/test_yagna_payment_cmd.py index 7abd14ace..03407864d 100644 --- a/test/goth/runner/cli/test_yagna_payment_cmd.py +++ b/test/goth/runner/cli/test_yagna_payment_cmd.py @@ -43,3 +43,12 @@ def test_payment_status_with_address(yagna_container): status = yagna.payment_status() assert status + + +def test_payment_drivers(yagna_container): + """Test `payment drivers` subcommand.""" + + yagna = Cli(yagna_container).yagna + + drivers = yagna.payment_drivers() + assert drivers From 94ca3bf919996bf2fab9b0290aed9cea97498b7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Nowosielski?= Date: Tue, 23 Mar 2021 12:54:09 +0100 Subject: [PATCH 2/4] e2e test instead of internal unit test --- .../goth/runner/cli/test_yagna_payment_cmd.py | 9 --- .../payments/test_payment_driver_list_cmd.py | 65 +++++++++++++++++++ 2 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 test/yagna/module/payments/test_payment_driver_list_cmd.py diff --git a/test/goth/runner/cli/test_yagna_payment_cmd.py b/test/goth/runner/cli/test_yagna_payment_cmd.py index 03407864d..7abd14ace 100644 --- a/test/goth/runner/cli/test_yagna_payment_cmd.py +++ b/test/goth/runner/cli/test_yagna_payment_cmd.py @@ -43,12 +43,3 @@ def test_payment_status_with_address(yagna_container): status = yagna.payment_status() assert status - - -def test_payment_drivers(yagna_container): - """Test `payment drivers` subcommand.""" - - yagna = Cli(yagna_container).yagna - - drivers = yagna.payment_drivers() - assert drivers diff --git a/test/yagna/module/payments/test_payment_driver_list_cmd.py b/test/yagna/module/payments/test_payment_driver_list_cmd.py new file mode 100644 index 000000000..37db1cfb7 --- /dev/null +++ b/test/yagna/module/payments/test_payment_driver_list_cmd.py @@ -0,0 +1,65 @@ +"""Tests payment driver list CLI command.""" + +import logging +from pathlib import Path +from typing import List + +import pytest + +from goth.address import ( + PROXY_HOST, + YAGNA_REST_URL, +) +from goth.node import node_environment +from goth.runner import Runner +from goth.runner.container.payment import PaymentIdPool +from goth.runner.container.yagna import YagnaContainerConfig +from goth.runner.requestor import RequestorProbeWithApiSteps + +logger = logging.getLogger(__name__) + + +def _topology(payment_id_pool: PaymentIdPool) -> List[YagnaContainerConfig]: + # Nodes are configured to communicate via proxy + + requestor_env = node_environment( + rest_api_url_base=YAGNA_REST_URL.substitute(host=PROXY_HOST), + ) + + return [ + YagnaContainerConfig( + name="requestor", + probe_type=RequestorProbeWithApiSteps, + environment=requestor_env, + payment_id=payment_id_pool.get_id(), + ), + ] + + +@pytest.mark.asyncio +async def test_payment_driver_list( + assets_path: Path, + demand_constraints: str, + payment_id_pool: PaymentIdPool, + runner: Runner, + task_package_template: str, +): + """Test just the requestor's CLI command, no need to setup provider.""" + + topology = _topology(payment_id_pool) + + async with runner(topology): + requestor = runner.get_probes(probe_type=RequestorProbeWithApiSteps)[0] + + res = requestor.cli.payment_drivers() + assert res and res.items() + driver = next(iter(res.values()), None) + + assert driver.default_network, "Default network should be set" + + network = driver.networks.get(driver.default_network, None) + assert network, "Network should belong to the Driver" + assert network.default_token, "Default taken should be set" + + token = network.tokens.get(network.default_token, None) + assert token, "Token should belong to the Network" From 414b64dc331a4158b47aafcd7ac52feaee976a65 Mon Sep 17 00:00:00 2001 From: azawlocki Date: Fri, 9 Apr 2021 15:52:21 +0200 Subject: [PATCH 3/4] Add a fixture that specifies yagna commit hash to use in a single test --- .../payments/test_payment_driver_list_cmd.py | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/test/yagna/module/payments/test_payment_driver_list_cmd.py b/test/yagna/module/payments/test_payment_driver_list_cmd.py index 37db1cfb7..7306980a8 100644 --- a/test/yagna/module/payments/test_payment_driver_list_cmd.py +++ b/test/yagna/module/payments/test_payment_driver_list_cmd.py @@ -2,7 +2,7 @@ import logging from pathlib import Path -from typing import List +from typing import List, Optional import pytest @@ -15,10 +15,34 @@ from goth.runner.container.payment import PaymentIdPool from goth.runner.container.yagna import YagnaContainerConfig from goth.runner.requestor import RequestorProbeWithApiSteps +from goth.runner.container.build import YagnaBuildEnvironment + logger = logging.getLogger(__name__) +@pytest.fixture(scope="module") +def yagna_build_env( + assets_path: Path, + yagna_binary_path: Optional[Path], + yagna_branch: Optional[str], + # yagna_commit_hash: Optional[str], + deb_package: Optional[Path], + yagna_release: Optional[str], +) -> YagnaBuildEnvironment: + """Override the default fixture to force using specific yagna commit.""" + + """Fixture which provides the build environment for yagna Docker images.""" + return YagnaBuildEnvironment( + docker_dir=assets_path / "docker", + binary_path=yagna_binary_path, + branch=yagna_branch, + commit_hash="c4d1dd7", + deb_path=deb_package, + release_tag=yagna_release, + ) + + def _topology(payment_id_pool: PaymentIdPool) -> List[YagnaContainerConfig]: # Nodes are configured to communicate via proxy From 18d3c18bd093e96cdb4500ca3b3ce7b9d37bb089 Mon Sep 17 00:00:00 2001 From: azawlocki Date: Fri, 9 Apr 2021 16:01:02 +0200 Subject: [PATCH 4/4] Adjust class/module names to changes in goth's master --- .../module/payments/test_payment_driver_list_cmd.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/yagna/module/payments/test_payment_driver_list_cmd.py b/test/yagna/module/payments/test_payment_driver_list_cmd.py index 7306980a8..d1657fd10 100644 --- a/test/yagna/module/payments/test_payment_driver_list_cmd.py +++ b/test/yagna/module/payments/test_payment_driver_list_cmd.py @@ -12,10 +12,10 @@ ) from goth.node import node_environment from goth.runner import Runner +from goth.runner.container.build import YagnaBuildEnvironment from goth.runner.container.payment import PaymentIdPool from goth.runner.container.yagna import YagnaContainerConfig -from goth.runner.requestor import RequestorProbeWithApiSteps -from goth.runner.container.build import YagnaBuildEnvironment +from goth.runner.probe import RequestorProbe logger = logging.getLogger(__name__) @@ -32,7 +32,6 @@ def yagna_build_env( ) -> YagnaBuildEnvironment: """Override the default fixture to force using specific yagna commit.""" - """Fixture which provides the build environment for yagna Docker images.""" return YagnaBuildEnvironment( docker_dir=assets_path / "docker", binary_path=yagna_binary_path, @@ -53,7 +52,7 @@ def _topology(payment_id_pool: PaymentIdPool) -> List[YagnaContainerConfig]: return [ YagnaContainerConfig( name="requestor", - probe_type=RequestorProbeWithApiSteps, + probe_type=RequestorProbe, environment=requestor_env, payment_id=payment_id_pool.get_id(), ), @@ -73,7 +72,7 @@ async def test_payment_driver_list( topology = _topology(payment_id_pool) async with runner(topology): - requestor = runner.get_probes(probe_type=RequestorProbeWithApiSteps)[0] + requestor = runner.get_probes(probe_type=RequestorProbe)[0] res = requestor.cli.payment_drivers() assert res and res.items()