2 # Copyright 2022 Canonical Ltd.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: legal@canonical.com
19 # To get in touch with the maintainers, please contact:
20 # osm-charmers@lists.launchpad.net
22 # Learn more about testing at: https://juju.is/docs/sdk/testing
25 from ops
.model
import ActiveStatus
, BlockedStatus
26 from ops
.testing
import Harness
27 from pytest_mock
import MockerFixture
29 from charm
import CharmError
, OsmPolCharm
, check_service_active
31 container_name
= "pol"
36 def harness(mocker
: MockerFixture
):
37 harness
= Harness(OsmPolCharm
)
39 harness
.container_pebble_ready(container_name
)
44 def test_missing_relations(harness
: Harness
):
45 harness
.charm
.on
.config_changed
.emit()
46 assert type(harness
.charm
.unit
.status
) == BlockedStatus
48 relation
in harness
.charm
.unit
.status
.message
for relation
in ["mongodb", "kafka", "mysql"]
52 def test_ready(harness
: Harness
):
53 _add_relations(harness
)
54 assert harness
.charm
.unit
.status
== ActiveStatus()
57 def test_container_stops_after_relation_broken(harness
: Harness
):
58 harness
.charm
.on
[container_name
].pebble_ready
.emit(container_name
)
59 container
= harness
.charm
.unit
.get_container(container_name
)
60 relation_ids
= _add_relations(harness
)
61 check_service_active(container
, service_name
)
62 harness
.remove_relation(relation_ids
[0])
63 with pytest
.raises(CharmError
):
64 check_service_active(container
, service_name
)
67 def _add_relations(harness
: Harness
):
70 relation_id
= harness
.add_relation("mongodb", "mongodb")
71 harness
.add_relation_unit(relation_id
, "mongodb/0")
72 harness
.update_relation_data(
75 {"uris": "mongodb://:1234", "username": "user", "password": "password"},
77 relation_ids
.append(relation_id
)
79 relation_id
= harness
.add_relation("kafka", "kafka")
80 harness
.add_relation_unit(relation_id
, "kafka/0")
81 harness
.update_relation_data(relation_id
, "kafka", {"host": "kafka", "port": "9092"})
82 relation_ids
.append(relation_id
)
84 relation_id
= harness
.add_relation("mysql", "mysql")
85 harness
.add_relation_unit(relation_id
, "mysql/0")
86 harness
.update_relation_data(
94 "root_password": "rootmanopw",
97 relation_ids
.append(relation_id
)