Skip to content

Commit

Permalink
let's try from scratch
Browse files Browse the repository at this point in the history
  • Loading branch information
Paweł Nowosielski committed Apr 7, 2021
1 parent 4e79f92 commit 5c56e2e
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
36 changes: 36 additions & 0 deletions goth/runner/cli/yagna_payment_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<yagna-cmd> payment` commands."""

Expand Down Expand Up @@ -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 `<cmd> 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()}
65 changes: 65 additions & 0 deletions test/yagna/module/payments/test_payment_driver_list_cmd.py
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 5c56e2e

Please sign in to comment.