diff --git a/goth/runner/cli/yagna_payment_cmd.py b/goth/runner/cli/yagna_payment_cmd.py index 6d8f96038..a172c8fb8 100644 --- a/goth/runner/cli/yagna_payment_cmd.py +++ b/goth/runner/cli/yagna_payment_cmd.py @@ -44,6 +44,29 @@ def from_dict(source: dict) -> "PaymentStatus": reserved=float(source["reserved"]), ) +@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 +120,14 @@ 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/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..519248b21 --- /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.probe import RequestorProbe + +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=RequestorProbe, + 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=RequestorProbe)[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"