af248bfcfd1c18cd137cb32d95e0533cb2cbb237
[osm/devops.git] / installers / charm / osm-nbi / 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, BlockedStatus
26 from ops.testing import Harness
27 from pytest_mock import MockerFixture
28
29 from charm import CharmError, OsmNbiCharm, check_service_active
30
31 container_name = "nbi"
32 service_name = "nbi"
33
34
35 @pytest.fixture
36 def harness(mocker: MockerFixture):
37 mocker.patch("charm.KubernetesServicePatch", lambda x, y: None)
38 harness = Harness(OsmNbiCharm)
39 harness.begin()
40 harness.container_pebble_ready(container_name)
41 yield harness
42 harness.cleanup()
43
44
45 def _set_prometheus_config(harness: Harness):
46 harness.update_config({"prometheus-url": "http://prometheus:1234"})
47
48
49 def test_default_config_prometheus_url_is_invalid_charm_is_blocked(harness: Harness):
50 harness.charm.on.config_changed.emit()
51 assert type(harness.charm.unit.status) == BlockedStatus
52 assert "need prometheus-url config" == harness.charm.unit.status.message
53
54
55 def test_missing_relations(harness: Harness):
56 _set_prometheus_config(harness)
57 harness.charm.on.config_changed.emit()
58 assert type(harness.charm.unit.status) == BlockedStatus
59 assert all(
60 relation in harness.charm.unit.status.message
61 for relation in ["mongodb", "kafka", "keystone"]
62 )
63
64
65 def test_prometheus_url_without_schema_blocked_status(harness: Harness):
66 _add_relations(harness)
67 harness.update_config({"prometheus-url": "foo.com"})
68 assert type(harness.charm.unit.status) == BlockedStatus
69 assert (
70 "Invalid value for prometheus-url config: 'foo.com'" in harness.charm.unit.status.message
71 )
72
73
74 def test_prometheus_url_with_port_without_schema_blocked_status(harness: Harness):
75 _add_relations(harness)
76 harness.update_config({"prometheus-url": "foo.com:9090"})
77 assert type(harness.charm.unit.status) == BlockedStatus
78 assert (
79 "Invalid value for prometheus-url config: 'foo.com:9090'"
80 in harness.charm.unit.status.message
81 )
82
83
84 def test_prometheus_url_without_port_is_valid(harness: Harness):
85 _add_relations(harness)
86 harness.update_config({"prometheus-url": "http://foo"})
87 assert harness.charm.unit.status == ActiveStatus()
88
89
90 def test_prometheus_url_with_port_is_valid(harness: Harness):
91 _add_relations(harness)
92 harness.update_config({"prometheus-url": "http://foo:90"})
93 assert harness.charm.unit.status == ActiveStatus()
94
95
96 def test_ready(harness: Harness):
97 _set_prometheus_config(harness)
98 _add_relations(harness)
99 assert harness.charm.unit.status == ActiveStatus()
100
101
102 def test_container_stops_after_relation_broken(harness: Harness):
103 _set_prometheus_config(harness)
104 harness.charm.on[container_name].pebble_ready.emit(container_name)
105 container = harness.charm.unit.get_container(container_name)
106 relation_ids = _add_relations(harness)
107 check_service_active(container, service_name)
108 harness.remove_relation(relation_ids[0])
109 with pytest.raises(CharmError):
110 check_service_active(container, service_name)
111
112
113 def test_nbi_relation_joined(harness: Harness):
114 _set_prometheus_config(harness)
115 harness.set_leader(True)
116 _add_relations(harness)
117 relation_id = harness.add_relation("nbi", "ng-ui")
118 harness.add_relation_unit(relation_id, "ng-ui/0")
119 relation_data = harness.get_relation_data(relation_id, harness.charm.app.name)
120 assert harness.charm.unit.status == ActiveStatus()
121 assert relation_data == {"host": harness.charm.app.name, "port": "9999"}
122
123
124 def _add_relations(harness: Harness):
125 relation_ids = []
126 # Add mongo relation
127 relation_id = harness.add_relation("mongodb", "mongodb")
128 harness.add_relation_unit(relation_id, "mongodb/0")
129 harness.update_relation_data(
130 relation_id,
131 "mongodb",
132 {"uris": "mongodb://:1234", "username": "user", "password": "password"},
133 )
134 relation_ids.append(relation_id)
135 # Add kafka relation
136 relation_id = harness.add_relation("kafka", "kafka")
137 harness.add_relation_unit(relation_id, "kafka/0")
138 harness.update_relation_data(relation_id, "kafka", {"host": "kafka", "port": "9092"})
139 relation_ids.append(relation_id)
140 # Add keystone relation
141 relation_id = harness.add_relation("keystone", "keystone")
142 harness.add_relation_unit(relation_id, "keystone/0")
143 harness.update_relation_data(
144 relation_id,
145 "keystone",
146 {
147 "host": "host",
148 "port": "port",
149 "user_domain_name": "user_domain_name",
150 "project_domain_name": "project_domain_name",
151 "username": "username",
152 "password": "password",
153 "service": "service",
154 "keystone_db_password": "keystone_db_password",
155 "region_id": "region_id",
156 "admin_username": "admin_username",
157 "admin_password": "admin_password",
158 "admin_project_name": "admin_project_name",
159 },
160 )
161 relation_ids.append(relation_id)
162 return relation_ids