blob: 2765e81cb3c4d41877493245d5a57f60b4b37aad [file] [log] [blame]
David Garcia95ba7e12021-02-03 11:10:28 +01001#!/usr/bin/env python3
David Garcia49379ce2021-02-24 13:48:22 +01002# Copyright 2021 Canonical Ltd.
David Garcia95ba7e12021-02-03 11:10:28 +01003#
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
David Garcia49379ce2021-02-24 13:48:22 +010023import sys
David Garcia95ba7e12021-02-03 11:10:28 +010024from typing import NoReturn
25import unittest
David Garcia95ba7e12021-02-03 11:10:28 +010026
27from charm import NgUiCharm
David Garciac753dc52021-03-17 15:28:47 +010028from ops.model import ActiveStatus, BlockedStatus
29from ops.testing import Harness
David Garcia95ba7e12021-02-03 11:10:28 +010030
31
32class TestCharm(unittest.TestCase):
David Garcia49379ce2021-02-24 13:48:22 +010033 """Prometheus Charm unit tests."""
David Garcia95ba7e12021-02-03 11:10:28 +010034
35 def setUp(self) -> NoReturn:
36 """Test setup"""
David Garcia49379ce2021-02-24 13:48:22 +010037 self.image_info = sys.modules["oci_image"].OCIImageResource().fetch()
David Garcia95ba7e12021-02-03 11:10:28 +010038 self.harness = Harness(NgUiCharm)
39 self.harness.set_leader(is_leader=True)
40 self.harness.begin()
David Garcia49379ce2021-02-24 13:48:22 +010041 self.config = {
42 "server_name": "localhost",
43 "port": 80,
44 "max_file_size": 0,
45 "ingress_whitelist_source_range": "",
46 "tls_secret_name": "",
David Garciac395a452021-05-05 14:22:26 +020047 "site_url": "https://ui.192.168.100.100.nip.io",
sousaedu3cc03162021-04-29 16:53:12 +020048 "cluster_issuer": "vault-issuer",
David Garcia49379ce2021-02-24 13:48:22 +010049 }
50 self.harness.update_config(self.config)
David Garcia95ba7e12021-02-03 11:10:28 +010051
David Garcia49379ce2021-02-24 13:48:22 +010052 def test_config_changed_no_relations(
53 self,
54 ) -> NoReturn:
55 """Test ingress resources without HTTP."""
56
David Garcia95ba7e12021-02-03 11:10:28 +010057 self.harness.charm.on.config_changed.emit()
58
David Garcia49379ce2021-02-24 13:48:22 +010059 # Assertions
60 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
61 self.assertTrue(
62 all(
63 relation in self.harness.charm.unit.status.message
64 for relation in ["nbi"]
65 )
66 )
67
68 def test_config_changed_non_leader(
69 self,
70 ) -> NoReturn:
71 """Test ingress resources without HTTP."""
72 self.harness.set_leader(is_leader=False)
73 self.harness.charm.on.config_changed.emit()
74
75 # Assertions
76 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
77
78 def test_with_relations(
79 self,
80 ) -> NoReturn:
81 "Test with relations (internal)"
82 self.initialize_nbi_relation()
83 # Verifying status
84 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
85
86 def initialize_nbi_relation(self):
87 http_relation_id = self.harness.add_relation("nbi", "nbi")
88 self.harness.add_relation_unit(http_relation_id, "nbi")
89 self.harness.update_relation_data(
90 http_relation_id,
91 "nbi",
92 {"host": "nbi", "port": 9999},
93 )
94
David Garcia95ba7e12021-02-03 11:10:28 +010095
96if __name__ == "__main__":
97 unittest.main()