blob: 66e199d2e9af179c90b3a8289b4102421bea5b04 [file] [log] [blame]
sousaedu2459af62021-01-15 16:50:26 +00001#!/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
David Garcia49379ce2021-02-24 13:48:22 +010023import sys
sousaedu2459af62021-01-15 16:50:26 +000024from typing import NoReturn
25import unittest
sousaedu2459af62021-01-15 16:50:26 +000026
27from charm import PrometheusCharm
David Garciac753dc52021-03-17 15:28:47 +010028from ops.model import ActiveStatus
29from ops.testing import Harness
sousaedu2459af62021-01-15 16:50:26 +000030
31
32class TestCharm(unittest.TestCase):
33 """Prometheus Charm unit tests."""
34
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()
sousaedu2459af62021-01-15 16:50:26 +000038 self.harness = Harness(PrometheusCharm)
39 self.harness.set_leader(is_leader=True)
40 self.harness.begin()
David Garcia49379ce2021-02-24 13:48:22 +010041 self.config = {
42 "web-subpath": "/",
43 "default-target": "",
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://prometheus.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 "enable_web_admin_api": False,
50 }
51 self.harness.update_config(self.config)
sousaedu2459af62021-01-15 16:50:26 +000052
David Garcia49379ce2021-02-24 13:48:22 +010053 def test_config_changed(
54 self,
55 ) -> NoReturn:
sousaedu2459af62021-01-15 16:50:26 +000056 """Test ingress resources without HTTP."""
sousaedu2459af62021-01-15 16:50:26 +000057
David Garcia49379ce2021-02-24 13:48:22 +010058 self.harness.charm.on.config_changed.emit()
sousaedu2459af62021-01-15 16:50:26 +000059
David Garcia49379ce2021-02-24 13:48:22 +010060 # Assertions
61 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
sousaedu2459af62021-01-15 16:50:26 +000062
David Garcia49379ce2021-02-24 13:48:22 +010063 def test_config_changed_non_leader(
64 self,
65 ) -> NoReturn:
66 """Test ingress resources without HTTP."""
67 self.harness.set_leader(is_leader=False)
68 self.harness.charm.on.config_changed.emit()
sousaedu2459af62021-01-15 16:50:26 +000069
David Garcia49379ce2021-02-24 13:48:22 +010070 # Assertions
71 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
sousaedu2459af62021-01-15 16:50:26 +000072
David Garcia49379ce2021-02-24 13:48:22 +010073 def test_publish_prometheus_info(
74 self,
75 ) -> NoReturn:
sousaedu2459af62021-01-15 16:50:26 +000076 """Test to see if prometheus relation is updated."""
77 expected_result = {
David Garcia49379ce2021-02-24 13:48:22 +010078 "hostname": "prometheus",
sousaedu2459af62021-01-15 16:50:26 +000079 "port": "9090",
80 }
81
David Garcia49379ce2021-02-24 13:48:22 +010082 relation_id = self.harness.add_relation("prometheus", "mon")
83 self.harness.add_relation_unit(relation_id, "mon/0")
84 relation_data = self.harness.get_relation_data(relation_id, "prometheus")
sousaedu2459af62021-01-15 16:50:26 +000085
David Garcia49379ce2021-02-24 13:48:22 +010086 self.assertDictEqual(expected_result, relation_data)
87
88 def test_publish_prometheus_info_non_leader(
89 self,
90 ) -> NoReturn:
91 """Test to see if prometheus relation is updated."""
92 expected_result = {}
93
94 self.harness.set_leader(is_leader=False)
sousaedu2459af62021-01-15 16:50:26 +000095 relation_id = self.harness.add_relation("prometheus", "mon")
96 self.harness.add_relation_unit(relation_id, "mon/0")
97 relation_data = self.harness.get_relation_data(relation_id, "prometheus")
98
99 self.assertDictEqual(expected_result, relation_data)
100
101
102if __name__ == "__main__":
103 unittest.main()