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
, OsmMonCharm
, check_service_active
31 container_name
= "mon"
33 url_configs
= ["grafana-url", "prometheus-url"]
37 def harness(mocker
: MockerFixture
):
38 mocker
.patch("charm.KubernetesServicePatch", lambda x
, y
: None)
39 harness
= Harness(OsmMonCharm
)
41 harness
.container_pebble_ready(container_name
)
46 def _set_mandatory_config(harness
: Harness
):
47 harness
.update_config(
49 "grafana-url": "http://grafana:1234",
50 "grafana-user": "user",
51 "grafana-password": "password",
52 "prometheus-url": "http://someurl",
57 def test_default_config_is_invalid_charm_is_blocked(harness
: Harness
):
58 harness
.charm
.on
.config_changed
.emit()
59 assert isinstance(harness
.charm
.unit
.status
, BlockedStatus
)
61 "need grafana-url, grafana-user, grafana-password, prometheus-url config"
62 == harness
.charm
.unit
.status
.message
66 def test_missing_relations(harness
: Harness
):
67 _set_mandatory_config(harness
)
68 harness
.charm
.on
.config_changed
.emit()
69 assert isinstance(harness
.charm
.unit
.status
, BlockedStatus
)
71 relation
in harness
.charm
.unit
.status
.message
72 for relation
in ["mongodb", "kafka", "prometheus", "keystone"]
76 @pytest.mark
.parametrize("config_param", url_configs
)
77 def test_url_config_without_schema_block_status(harness
: Harness
, config_param
):
78 _set_mandatory_config(harness
)
79 _add_relations(harness
)
80 harness
.update_config({config_param
: "foo.com"})
81 assert isinstance(harness
.charm
.unit
.status
, BlockedStatus
)
83 f
"Invalid value for {config_param} config: 'foo.com'" in harness
.charm
.unit
.status
.message
87 @pytest.mark
.parametrize("config_param", url_configs
)
88 def test_url_config_with_port_without_schema_block_status(harness
: Harness
, config_param
):
89 _set_mandatory_config(harness
)
90 _add_relations(harness
)
91 harness
.update_config({config_param
: "foo.com:9090"})
92 assert isinstance(harness
.charm
.unit
.status
, BlockedStatus
)
94 f
"Invalid value for {config_param} config: 'foo.com:9090'"
95 in harness
.charm
.unit
.status
.message
99 @pytest.mark
.parametrize("config_param", url_configs
)
100 def test_url_config_without_port_is_valid(harness
: Harness
, config_param
):
101 _set_mandatory_config(harness
)
102 _add_relations(harness
)
103 harness
.update_config({config_param
: "http://foo"})
104 assert harness
.charm
.unit
.status
== ActiveStatus()
107 @pytest.mark
.parametrize("config_param", url_configs
)
108 def test_url_config_with_port_is_valid(harness
: Harness
, config_param
):
109 _set_mandatory_config(harness
)
110 _add_relations(harness
)
111 harness
.update_config({config_param
: "http://foo:90"})
112 assert harness
.charm
.unit
.status
== ActiveStatus()
115 def test_ready(harness
: Harness
):
116 _set_mandatory_config(harness
)
117 _add_relations(harness
)
118 assert harness
.charm
.unit
.status
== ActiveStatus()
121 def test_container_stops_after_relation_broken(harness
: Harness
):
122 _set_mandatory_config(harness
)
123 harness
.charm
.on
[container_name
].pebble_ready
.emit(container_name
)
124 container
= harness
.charm
.unit
.get_container(container_name
)
125 relation_ids
= _add_relations(harness
)
126 check_service_active(container
, service_name
)
127 harness
.remove_relation(relation_ids
[0])
128 with pytest
.raises(CharmError
):
129 check_service_active(container
, service_name
)
132 def _add_relations(harness
: Harness
):
135 relation_id
= harness
.add_relation("mongodb", "mongodb")
136 harness
.add_relation_unit(relation_id
, "mongodb/0")
137 harness
.update_relation_data(
140 {"uris": "mongodb://:1234", "username": "user", "password": "password"},
142 relation_ids
.append(relation_id
)
144 relation_id
= harness
.add_relation("kafka", "kafka")
145 harness
.add_relation_unit(relation_id
, "kafka/0")
146 harness
.update_relation_data(relation_id
, "kafka", {"host": "kafka", "port": "9092"})
147 relation_ids
.append(relation_id
)
148 # Add prometheus relation
149 relation_id
= harness
.add_relation("metrics-endpoint", "prometheus")
150 relation_ids
.append(relation_id
)
151 # Add keystone relation
152 relation_id
= harness
.add_relation("keystone", "keystone")
153 harness
.add_relation_unit(relation_id
, "keystone/0")
154 harness
.update_relation_data(
160 "user_domain_name": "user_domain_name",
161 "project_domain_name": "project_domain_name",
162 "username": "username",
163 "password": "password",
164 "service": "service",
165 "keystone_db_password": "keystone_db_password",
166 "region_id": "region_id",
167 "admin_username": "admin_username",
168 "admin_password": "admin_password",
169 "admin_project_name": "admin_project_name",
172 relation_ids
.append(relation_id
)