blob: 687c38a6127f0302db033aa6489522e472a45dde [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,
David Garciade440ed2021-10-11 19:56:53 +020050 "web_config_username": "admin",
51 "web_config_password": "1234",
David Garcia49379ce2021-02-24 13:48:22 +010052 }
53 self.harness.update_config(self.config)
sousaedu2459af62021-01-15 16:50:26 +000054
David Garcia49379ce2021-02-24 13:48:22 +010055 def test_config_changed(
56 self,
57 ) -> NoReturn:
sousaedu2459af62021-01-15 16:50:26 +000058 """Test ingress resources without HTTP."""
sousaedu2459af62021-01-15 16:50:26 +000059
David Garcia49379ce2021-02-24 13:48:22 +010060 self.harness.charm.on.config_changed.emit()
sousaedu2459af62021-01-15 16:50:26 +000061
David Garcia49379ce2021-02-24 13:48:22 +010062 # Assertions
63 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
sousaedu2459af62021-01-15 16:50:26 +000064
David Garcia49379ce2021-02-24 13:48:22 +010065 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()
sousaedu2459af62021-01-15 16:50:26 +000071
David Garcia49379ce2021-02-24 13:48:22 +010072 # Assertions
73 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
sousaedu2459af62021-01-15 16:50:26 +000074
David Garcia49379ce2021-02-24 13:48:22 +010075 def test_publish_prometheus_info(
76 self,
77 ) -> NoReturn:
sousaedu2459af62021-01-15 16:50:26 +000078 """Test to see if prometheus relation is updated."""
79 expected_result = {
David Garcia49379ce2021-02-24 13:48:22 +010080 "hostname": "prometheus",
sousaedu2459af62021-01-15 16:50:26 +000081 "port": "9090",
David Garciade440ed2021-10-11 19:56:53 +020082 "user": "admin",
83 "password": "1234",
sousaedu2459af62021-01-15 16:50:26 +000084 }
85
David Garcia49379ce2021-02-24 13:48:22 +010086 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(relation_id, "prometheus")
sousaedu2459af62021-01-15 16:50:26 +000089
David Garcia49379ce2021-02-24 13:48:22 +010090 self.assertDictEqual(expected_result, relation_data)
91
92 def test_publish_prometheus_info_non_leader(
93 self,
94 ) -> NoReturn:
95 """Test to see if prometheus relation is updated."""
96 expected_result = {}
97
98 self.harness.set_leader(is_leader=False)
sousaedu2459af62021-01-15 16:50:26 +000099 relation_id = self.harness.add_relation("prometheus", "mon")
100 self.harness.add_relation_unit(relation_id, "mon/0")
101 relation_data = self.harness.get_relation_data(relation_id, "prometheus")
102
103 self.assertDictEqual(expected_result, relation_data)
104
105
106if __name__ == "__main__":
107 unittest.main()