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
, OsmNbiCharm
, check_service_active
31 container_name
= "nbi"
36 def harness(mocker
: MockerFixture
):
37 mocker
.patch("charm.KubernetesServicePatch", lambda x
, y
: None)
38 harness
= Harness(OsmNbiCharm
)
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
49 for relation
in ["mongodb", "kafka", "prometheus", "keystone"]
53 def test_ready(harness
: Harness
):
54 _add_relations(harness
)
55 assert harness
.charm
.unit
.status
== ActiveStatus()
58 def test_container_stops_after_relation_broken(harness
: Harness
):
59 harness
.charm
.on
[container_name
].pebble_ready
.emit(container_name
)
60 container
= harness
.charm
.unit
.get_container(container_name
)
61 relation_ids
= _add_relations(harness
)
62 check_service_active(container
, service_name
)
63 harness
.remove_relation(relation_ids
[0])
64 with pytest
.raises(CharmError
):
65 check_service_active(container
, service_name
)
68 def test_nbi_relation_joined(harness
: Harness
):
69 harness
.set_leader(True)
70 _add_relations(harness
)
71 relation_id
= harness
.add_relation("nbi", "ng-ui")
72 harness
.add_relation_unit(relation_id
, "ng-ui/0")
73 relation_data
= harness
.get_relation_data(relation_id
, harness
.charm
.app
.name
)
74 assert harness
.charm
.unit
.status
== ActiveStatus()
75 assert relation_data
== {"host": harness
.charm
.app
.name
, "port": "9999"}
78 def _add_relations(harness
: Harness
):
81 relation_id
= harness
.add_relation("mongodb", "mongodb")
82 harness
.add_relation_unit(relation_id
, "mongodb/0")
83 harness
.update_relation_data(
84 relation_id
, "mongodb/0", {"connection_string": "mongodb://:1234"}
86 relation_ids
.append(relation_id
)
88 relation_id
= harness
.add_relation("kafka", "kafka")
89 harness
.add_relation_unit(relation_id
, "kafka/0")
90 harness
.update_relation_data(relation_id
, "kafka", {"host": "kafka", "port": 9092})
91 relation_ids
.append(relation_id
)
92 # Add prometheus relation
93 relation_id
= harness
.add_relation("prometheus", "prometheus")
94 harness
.add_relation_unit(relation_id
, "prometheus/0")
95 harness
.update_relation_data(
96 relation_id
, "prometheus", {"hostname": "prometheus", "port": 9090}
98 relation_ids
.append(relation_id
)
99 # Add keystone relation
100 relation_id
= harness
.add_relation("keystone", "keystone")
101 harness
.add_relation_unit(relation_id
, "keystone/0")
102 harness
.update_relation_data(
108 "user_domain_name": "user_domain_name",
109 "project_domain_name": "project_domain_name",
110 "username": "username",
111 "password": "password",
112 "service": "service",
113 "keystone_db_password": "keystone_db_password",
114 "region_id": "region_id",
115 "admin_username": "admin_username",
116 "admin_password": "admin_password",
117 "admin_project_name": "admin_project_name",
120 relation_ids
.append(relation_id
)