Add juju simplestreams charm
[osm/devops.git] / installers / charm / juju-simplestreams-operator / tests / unit / test_charm.py
1 #!/usr/bin/env python3
2 # Copyright 2022 Canonical Ltd.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15 #
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: legal@canonical.com
18 #
19 # To get in touch with the maintainers, please contact:
20 # osm-charmers@lists.launchpad.net
21 #
22 # Learn more about testing at: https://juju.is/docs/sdk/testing
23
24 import pytest
25 from ops.model import ActiveStatus
26 from ops.testing import Harness
27 from pytest_mock import MockerFixture
28
29 from charm import JujuSimplestreamsCharm
30
31 container_name = "server"
32 service_name = "server"
33
34
35 @pytest.fixture
36 def harness(mocker: MockerFixture):
37 mocker.patch("charm.KubernetesServicePatch", lambda x, y: None)
38 harness = Harness(JujuSimplestreamsCharm)
39 harness.begin()
40 harness.charm.container.make_dir("/etc/nginx", make_parents=True)
41 yield harness
42 harness.cleanup()
43
44
45 def test_ready(harness: Harness):
46 harness.charm.on.server_pebble_ready.emit(container_name)
47 assert harness.charm.unit.status == ActiveStatus()
48
49
50 def test_add_metadata_action(harness: Harness, mocker: MockerFixture):
51 harness.set_leader(True)
52 remote_unit = f"{harness.charm.app.name}/1"
53 relation_id = harness.add_relation("peer", harness.charm.app.name)
54 harness.add_relation_unit(relation_id, remote_unit)
55 event = mocker.Mock()
56 event.params = {
57 "region": "microstack",
58 "auth-url": "localhost",
59 "image-id": "id",
60 "series": "focal",
61 }
62 harness.charm._on_add_image_metadata_action(event)
63 # Harness not emitting relation changed event when in the action
64 # I update application data in the peer relation.
65 # Manually emitting it here:
66 relation = harness.charm.model.get_relation("peer")
67 harness.charm.on["peer"].relation_changed.emit(relation)
68 assert harness.charm.container.exists("/app/static/simplestreams/images/streams/v1/index.json")