Move lcm certificate to lcm folder in OSM helm chart
[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 test_missing_relations(harness: Harness):
46 harness.charm.on.config_changed.emit()
47 assert type(harness.charm.unit.status) == BlockedStatus
48 assert all(
49 relation in harness.charm.unit.status.message
50 for relation in ["mongodb", "kafka", "prometheus", "keystone"]
51 )
52
53
54 def test_ready(harness: Harness):
55 _add_relations(harness)
56 assert harness.charm.unit.status == ActiveStatus()
57
58
59 def test_container_stops_after_relation_broken(harness: Harness):
60 harness.charm.on[container_name].pebble_ready.emit(container_name)
61 container = harness.charm.unit.get_container(container_name)
62 relation_ids = _add_relations(harness)
63 check_service_active(container, service_name)
64 harness.remove_relation(relation_ids[0])
65 with pytest.raises(CharmError):
66 check_service_active(container, service_name)
67
68
69 def test_nbi_relation_joined(harness: Harness):
70 harness.set_leader(True)
71 _add_relations(harness)
72 relation_id = harness.add_relation("nbi", "ng-ui")
73 harness.add_relation_unit(relation_id, "ng-ui/0")
74 relation_data = harness.get_relation_data(relation_id, harness.charm.app.name)
75 assert harness.charm.unit.status == ActiveStatus()
76 assert relation_data == {"host": harness.charm.app.name, "port": "9999"}
77
78
79 def _add_relations(harness: Harness):
80 relation_ids = []
81 # Add mongo relation
82 relation_id = harness.add_relation("mongodb", "mongodb")
83 harness.add_relation_unit(relation_id, "mongodb/0")
84 harness.update_relation_data(
85 relation_id,
86 "mongodb",
87 {"uris": "mongodb://:1234", "username": "user", "password": "password"},
88 )
89 relation_ids.append(relation_id)
90 # Add kafka relation
91 relation_id = harness.add_relation("kafka", "kafka")
92 harness.add_relation_unit(relation_id, "kafka/0")
93 harness.update_relation_data(relation_id, "kafka", {"host": "kafka", "port": "9092"})
94 relation_ids.append(relation_id)
95 # Add prometheus relation
96 relation_id = harness.add_relation("prometheus", "prometheus")
97 harness.add_relation_unit(relation_id, "prometheus/0")
98 harness.update_relation_data(
99 relation_id, "prometheus", {"hostname": "prometheus", "port": "9090"}
100 )
101 relation_ids.append(relation_id)
102 # Add keystone relation
103 relation_id = harness.add_relation("keystone", "keystone")
104 harness.add_relation_unit(relation_id, "keystone/0")
105 harness.update_relation_data(
106 relation_id,
107 "keystone",
108 {
109 "host": "host",
110 "port": "port",
111 "user_domain_name": "user_domain_name",
112 "project_domain_name": "project_domain_name",
113 "username": "username",
114 "password": "password",
115 "service": "service",
116 "keystone_db_password": "keystone_db_password",
117 "region_id": "region_id",
118 "admin_username": "admin_username",
119 "admin_password": "admin_password",
120 "admin_project_name": "admin_project_name",
121 },
122 )
123 relation_ids.append(relation_id)
124 return relation_ids