Skip to content

Commit

Permalink
Changes out repo-file from accepting json to accepting yaml files.
Browse files Browse the repository at this point in the history
  • Loading branch information
jajreidy committed Nov 19, 2024
1 parent 62734fa commit 87ae76c
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 504 deletions.
2 changes: 1 addition & 1 deletion docs/common/mappings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ The destination mapping is given by an internal service named ``StArMap`` throug

The tool expects a valid endpoint to the StArMap service and/or a JSON string containing the list of mappings
using the parameter ``--repo``. Instead of using ``--repo`` you can instead use ``--repo-file`` and provide
a json file to be parsed.
a yaml file to be parsed.

The following sections will cover all the details about the destination mappings.

Expand Down
1 change: 1 addition & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ sphinx
sphinx-argparse
requests_mock
types-mock
types-PyYAML
types-requests
6 changes: 4 additions & 2 deletions src/pubtools/_marketplacesvm/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from argparse import Action, ArgumentError
from typing import Callable, Optional

import yaml


def from_environ(key, delegate_converter=lambda x: x):
"""
Expand Down Expand Up @@ -187,7 +189,7 @@ class RepoFileQueryLoad(Action):
"""
Argparse Action subclass for loading StArMap mappings from the ``repo-file`` argument.
This action is intended to allow the optional load of mappings from a json file
This action is intended to allow the optional load of mappings from a yaml file
instead of having to request data from server.
It will evaluate the input file and set it as a StArMap's QueryResponseContainer.
Expand All @@ -202,7 +204,7 @@ def __call__(self, parser, namespace, values, options=None):
items = getattr(namespace, self.dest, None) or []
if values and isinstance(values, str):
with open(values, "r") as v:
items = json.load(v)
items = yaml.safe_load(v)
if not isinstance(items, list):
raise ArgumentError(self, f"Expected value to be a list, got: {type(items)}")
setattr(namespace, self.dest, items)
2 changes: 1 addition & 1 deletion src/pubtools/_marketplacesvm/services/starmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def add_service_args(self, parser: ArgumentParser) -> None:

group.add_argument(
"--repo-file",
help="Override the StArMap mappings for push items with json file.",
help="Override the StArMap mappings for push items with yaml file.",
type=str,
action=RepoFileQueryLoad,
)
Expand Down
62 changes: 0 additions & 62 deletions tests/combined_push/test_combined_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,65 +445,3 @@ def test_do_combined_push_overriden_destination(
# Ensure the "server" was not called
mock_starmap_mkt.query_image_by_name.assert_not_called()
mock_starmap_cmt.query_image_by_name.assert_not_called()


@mock.patch("pubtools._marketplacesvm.tasks.community_push.command.Source")
@mock.patch("pubtools._marketplacesvm.tasks.push.command.Source")
def test_do_combined_push_overriden_destination_file(
marketplace_source: mock.MagicMock,
community_source: mock.MagicMock,
ami_push_item: AmiPushItem,
starmap_query_aws_marketplace: QueryResponseContainer,
starmap_query_aws_community: QueryResponseContainer,
command_tester: CommandTester,
monkeypatch: pytest.MonkeyPatch,
tmpdir: pytest.Testdir,
) -> None:
"""Test a successfull combined push for marketplaces and community workflows."""
# Store the auto-assigned mocks for StArMap on both workflows
mock_starmap_mkt = MarketplacesVMPush.starmap
mock_starmap_cmt = CommunityVMPush.starmap

# The policy name must be the same for community and marketplace workflows
qre = starmap_query_aws_marketplace.responses.pop()
qre = evolve(qre, name="sample_product")
starmap_query_aws_marketplace.responses.append(qre)
binfo = KojiBuildInfo(name="sample_product", version="7.0", release="20230101")
ami_push_item = evolve(ami_push_item, build_info=binfo)

# Create a testing StArMap instance which will fail to resolve the server
# it is, it can only be used offline
responses = starmap_query_aws_community.responses + starmap_query_aws_marketplace.responses
provider = InMemoryMapProviderV2(QueryResponseContainer(responses))
starmap = StarmapClient("https://foo.com/bar", provider=provider)
monkeypatch.setattr(MarketplacesVMPush, 'starmap', starmap)
monkeypatch.setattr(CommunityVMPush, 'starmap', starmap)

# Add the push items in the queue
marketplace_source.get.return_value.__enter__.return_value = [ami_push_item]
community_source.get.return_value.__enter__.return_value = [ami_push_item]

p = tmpdir.mkdir('data').join('test.json')
p.write(json.dumps([asdict(x) for x in responses], default=str))

# Test file
command_tester.test(
lambda: entry_point(CombinedVMPush),
[
"test-push",
"--starmap-url",
"https://starmap-example.com",
"--credentials",
"eyJtYXJrZXRwbGFjZV9hY2NvdW50IjogInRlc3QtbmEiLCAiYXV0aCI6eyJmb28iOiJiYXIifQo=",
"--rhsm-url",
"https://rhsm.com/test/api/",
"--repo-file",
f"{p}",
"--debug",
"koji:https://fakekoji.com?vmi_build=ami_build",
],
)

# Ensure the "server" was not called
mock_starmap_mkt.query_image_by_name.assert_not_called()
mock_starmap_cmt.query_image_by_name.assert_not_called()
5 changes: 3 additions & 2 deletions tests/community_push/test_community_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from unittest import mock

import pytest
import yaml
from _pytest.capture import CaptureFixture
from attrs import evolve
from pushsource import AmiPushItem, KojiBuildInfo, VHDPushItem
Expand Down Expand Up @@ -325,8 +326,8 @@ def test_do_community_push_overridden_destination_file(
}
]

p = tmpdir.mkdir('data').join('test.json')
p.write(json.dumps(policy))
p = tmpdir.mkdir('data').join('test.yaml')
p.write(yaml.dump(policy))

command_tester.test(
lambda: entry_point(CommunityVMPush),
Expand Down

Large diffs are not rendered by default.

Loading

0 comments on commit 87ae76c

Please sign in to comment.