Skip to content

Commit

Permalink
Merge pull request #479 from golemfactory/pay-102-payment-driver-list…
Browse files Browse the repository at this point in the history
…-with-custom-yagna

Variant of `payment driver` PR that uses custom yagna for one test
  • Loading branch information
azawlocki authored Apr 12, 2021
2 parents 56cdba9 + 18d3c18 commit 4c638e5
Show file tree
Hide file tree
Showing 2 changed files with 124 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()}
88 changes: 88 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,88 @@
"""Tests payment driver list CLI command."""

import logging
from pathlib import Path
from typing import List, Optional

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.build import YagnaBuildEnvironment
from goth.runner.container.payment import PaymentIdPool
from goth.runner.container.yagna import YagnaContainerConfig
from goth.runner.probe import RequestorProbe


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."""

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

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 4c638e5

Please sign in to comment.