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
, OsmNgUiCharm
, check_service_active
31 container_name
= "ng-ui"
32 service_name
= "ng-ui"
37 server_name localhost;
38 root /usr/share/nginx/html;
39 index index.html index.htm;
40 client_max_body_size 50M;
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 "";
49 try_files $uri $uri/ /index.html;
56 def harness(mocker
: MockerFixture
):
57 mocker
.patch("charm.KubernetesServicePatch", lambda x
, y
: None)
58 harness
= Harness(OsmNgUiCharm
)
60 container
= harness
.charm
.unit
.get_container("ng-ui")
61 harness
.set_can_connect(container
, True)
62 container
.push("/etc/nginx/sites-available/default", sites_default
, make_dirs
=True)
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"
73 def test_ready(harness
: Harness
):
74 _add_nbi_relation(harness
)
75 assert harness
.charm
.unit
.status
== ActiveStatus()
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_nbi_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 assert type(harness
.charm
.unit
.status
) == BlockedStatus
87 assert harness
.charm
.unit
.status
.message
== "need nbi relation"
90 def _add_nbi_relation(harness
: Harness
):
91 relation_id
= harness
.add_relation("nbi", "nbi")
92 harness
.add_relation_unit(relation_id
, "nbi/0")
93 harness
.update_relation_data(relation_id
, "nbi", {"host": "nbi", "port": "9999"})