Skip to content

Commit

Permalink
fix: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lengau committed Dec 12, 2024
1 parent 6289e38 commit 4d64ae4
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
14 changes: 12 additions & 2 deletions charmcraft/services/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,22 @@ def setup(self) -> None:
def create_tracks(
self, name: str, *tracks: publisher.CreateTrackRequest
) -> Sequence[publisher.TrackMetadata]:
"""Create tracks in the store."""
"""Create tracks in the store.
:param name: The package name to which the tracks should be attached.
:param tracks: Each item is a dictionary of the track request.
:returns: A sequence of the created tracks as dictionaries.
"""
track_names = {track.get("name") for track in tracks}
self._publisher.create_tracks(name, *tracks)

metadata = self._publisher.get_package_metadata(name)
# "or []" here because tracks could be None.
return metadata["tracks"] or []
return [
track
for track in metadata["tracks"] or []
if track.get("name") in track_names
]

def set_resource_revisions_architectures(
self, name: str, resource_name: str, updates: dict[int, list[str]]
Expand Down
24 changes: 24 additions & 0 deletions tests/integration/commands/test_store_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from charmcraft import env
from charmcraft.application.commands import FetchLibCommand
from charmcraft.application.commands.store import CreateTrack
from charmcraft.store.models import Library
from tests import factory

Expand Down Expand Up @@ -510,3 +511,26 @@ def test_fetchlib_store_same_versions_different_hash(


# endregion


def test_create_track(emitter, service_factory, config):
cmd = CreateTrack(config)
args = argparse.Namespace(
name="my-charm",
track=["my-track"],
automatic_phasing_percentage=None,
format="json",
)
mock_create_tracks = mock.Mock()
mock_get_package_metadata = mock.Mock(
return_value={"tracks": [{"name": "latest"}, {"name": "my-track"}]}
)

service_factory.store._publisher.create_tracks = mock_create_tracks
service_factory.store._publisher.get_package_metadata = mock_get_package_metadata

cmd.run(args)

emitter.assert_json_output(
[{"name": "my-track", "automatic-phasing-percentage": None}]
)
6 changes: 6 additions & 0 deletions tests/spread/store/charm-upload-and-release/task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ execute: |
# release that last revision to edge
charmcraft release $CHARM_DEFAULT_NAME -r $uploaded_revno -c edge
# Create a track and release it to that, too.
track_name=$(date +%s)
charmcraft create-track $CHARM_DEFAULT_NAME $track_name
charmcraft release $CHARM_DEFAULT_NAME -r $uploaded_revno -c ${track_name}/edge
# Releases may have some delay in the craft store, so sleep for a bit before trying and try up to 10 times.
sleep 10
for i in {1..10}
Expand All @@ -57,6 +62,7 @@ execute: |
break
fi
done
track_release=$(charmcraft status $CHARM_DEFAULT_NAME --format=json | jq -r ".[] | select(.track==\"${track_name}\") | .mappings[0].releases | .[] | select(.channel==\"${track_name}/edge\")")
if [ $edge_revision -lt $uploaded_revno ]; then
ERROR "Revision wasn't released. Uploaded revision: $uploaded_revno; Currently on edge: $edge_revision"
fi
23 changes: 23 additions & 0 deletions tests/unit/services/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import charmcraft
from charmcraft import application, errors, services
from charmcraft.models.project import CharmLib
from charmcraft.services.store import StoreService
from charmcraft.store import client
from tests import get_fake_revision

Expand All @@ -47,6 +48,7 @@ def store(service_factory) -> services.StoreService:
def reusable_store():
store = services.StoreService(app=application.APP_METADATA, services=None)
store.client = mock.Mock(spec_set=craft_store.StoreClient)
store._publisher = mock.Mock(spec_set=craft_store.PublisherGateway)
return store


Expand Down Expand Up @@ -168,6 +170,27 @@ def test_logout(store):
client.logout.assert_called_once_with()


def test_create_tracks(reusable_store: StoreService):
mock_create = cast(mock.Mock, reusable_store._publisher.create_tracks)
mock_md = cast(mock.Mock, reusable_store._publisher.get_package_metadata)
user_track = {
"name": "my-track",
"automatic-phasing-percentage": None,
}
mock_md.return_value = {
"tracks": [
user_track,
{
"name": "latest",
"automatic-phasing-percentage": None,
},
]
}

assert reusable_store.create_tracks("my-name", user_track) == [user_track]
mock_create.assert_called_once_with("my-name", user_track)


@pytest.mark.parametrize(
("updates", "expected_request"),
[
Expand Down
4 changes: 2 additions & 2 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4d64ae4

Please sign in to comment.