blob: 965400a4a7184a00424d59fab704a672b72df383 [file] [log] [blame]
beierlm794fa722023-08-25 23:01:16 +02001#!/usr/bin/env python3
2# Copyright 2020 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
23import sys
24from typing import NoReturn
25import unittest
26
27from charm import PrometheusCharm
28from ops.model import ActiveStatus
29from ops.testing import Harness
30
31
32class TestCharm(unittest.TestCase):
33 """Prometheus Charm unit tests."""
34
35 def setUp(self) -> NoReturn:
36 """Test setup"""
37 self.image_info = sys.modules["oci_image"].OCIImageResource().fetch()
38 self.harness = Harness(PrometheusCharm)
39 self.harness.set_leader(is_leader=True)
40 self.harness.begin()
41 self.config = {
42 "web-subpath": "/",
43 "default-target": "",
44 "max_file_size": 0,
45 "ingress_whitelist_source_range": "",
46 "tls_secret_name": "",
47 "site_url": "https://prometheus.192.168.100.100.nip.io",
48 "cluster_issuer": "vault-issuer",
49 "enable_web_admin_api": False,
50 "web_config_username": "admin",
51 "web_config_password": "1234",
52 }
53 self.harness.update_config(self.config)
54
55 def test_config_changed(
56 self,
57 ) -> NoReturn:
58 """Test ingress resources without HTTP."""
59
60 self.harness.charm.on.config_changed.emit()
61
62 # Assertions
63 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
64
65 def test_config_changed_non_leader(
66 self,
67 ) -> NoReturn:
68 """Test ingress resources without HTTP."""
69 self.harness.set_leader(is_leader=False)
70 self.harness.charm.on.config_changed.emit()
71
72 # Assertions
73 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
74
75 def test_publish_prometheus_info(
76 self,
77 ) -> NoReturn:
78 """Test to see if prometheus relation is updated."""
79 expected_result = {
80 "hostname": self.harness.charm.app.name,
81 "port": "9090",
82 "user": "admin",
83 "password": "1234",
84 }
85
86 relation_id = self.harness.add_relation("prometheus", "mon")
87 self.harness.add_relation_unit(relation_id, "mon/0")
88 relation_data = self.harness.get_relation_data(
89 relation_id, self.harness.charm.app.name
90 )
91
92 self.assertDictEqual(expected_result, relation_data)
93
94 def test_publish_prometheus_info_non_leader(
95 self,
96 ) -> NoReturn:
97 """Test to see if prometheus relation is updated."""
98 expected_result = {}
99
100 self.harness.set_leader(is_leader=False)
101 relation_id = self.harness.add_relation("prometheus", "mon")
102 self.harness.add_relation_unit(relation_id, "mon/0")
103 relation_data = self.harness.get_relation_data(
104 relation_id, self.harness.charm.app.name
105 )
106
107 self.assertDictEqual(expected_result, relation_data)
108
109
110if __name__ == "__main__":
111 unittest.main()