Add Ng-UI sidecar charm
[osm/devops.git] / installers / charm / osm-ng-ui / 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, OsmNgUiCharm, check_service_active
30
31 container_name = "ng-ui"
32 service_name = "ng-ui"
33
34 sites_default = """
35 server {
36 listen 80;
37 server_name localhost;
38 root /usr/share/nginx/html;
39 index index.html index.htm;
40 client_max_body_size 50M;
41
42 location /osm {
43 proxy_pass https://nbi:9999;
44 proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
45 proxy_set_header Accept-Encoding "";
46 }
47
48 location / {
49 try_files $uri $uri/ /index.html;
50 }
51 }
52 """
53
54
55 @pytest.fixture
56 def harness(mocker: MockerFixture):
57 mocker.patch("charm.KubernetesServicePatch", lambda x, y: None)
58 harness = Harness(OsmNgUiCharm)
59 harness.begin()
60 harness.charm.unit.get_container("ng-ui").push(
61 "/etc/nginx/sites-available/default", sites_default, make_dirs=True
62 )
63 yield harness
64 harness.cleanup()
65
66
67 def test_missing_relations(harness: Harness):
68 harness.charm.on.config_changed.emit()
69 assert type(harness.charm.unit.status) == BlockedStatus
70 assert harness.charm.unit.status.message == "need nbi relation"
71
72
73 def test_ready(harness: Harness):
74 _add_relation(harness)
75 assert harness.charm.unit.status == ActiveStatus()
76
77
78 def test_container_stops_after_relation_broken(harness: Harness):
79 harness.charm.on[container_name].pebble_ready.emit(container_name)
80 container = harness.charm.unit.get_container(container_name)
81 relation_id = _add_relation(harness)
82 check_service_active(container, service_name)
83 harness.remove_relation(relation_id)
84 with pytest.raises(CharmError):
85 check_service_active(container, service_name)
86
87
88 def _add_relation(harness: Harness):
89 # Add nbi relation
90 relation_id = harness.add_relation("nbi", "nbi")
91 harness.add_relation_unit(relation_id, "nbi/0")
92 harness.update_relation_data(relation_id, "nbi", {"host": "nbi", "port": 9999})
93 return relation_id