| David Garcia | 95ba7e1 | 2021-02-03 11:10:28 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 2 | # Copyright 2021 Canonical Ltd. |
| David Garcia | 95ba7e1 | 2021-02-03 11:10:28 +0100 | [diff] [blame] | 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 Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 23 | import sys |
| David Garcia | 95ba7e1 | 2021-02-03 11:10:28 +0100 | [diff] [blame] | 24 | from typing import NoReturn |
| 25 | import unittest |
| David Garcia | 95ba7e1 | 2021-02-03 11:10:28 +0100 | [diff] [blame] | 26 | |
| 27 | from charm import NgUiCharm |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 28 | from ops.model import ActiveStatus, BlockedStatus |
| 29 | from ops.testing import Harness |
| David Garcia | 95ba7e1 | 2021-02-03 11:10:28 +0100 | [diff] [blame] | 30 | |
| 31 | |
| 32 | class TestCharm(unittest.TestCase): |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 33 | """Prometheus Charm unit tests.""" |
| David Garcia | 95ba7e1 | 2021-02-03 11:10:28 +0100 | [diff] [blame] | 34 | |
| 35 | def setUp(self) -> NoReturn: |
| 36 | """Test setup""" |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 37 | self.image_info = sys.modules["oci_image"].OCIImageResource().fetch() |
| David Garcia | 95ba7e1 | 2021-02-03 11:10:28 +0100 | [diff] [blame] | 38 | self.harness = Harness(NgUiCharm) |
| 39 | self.harness.set_leader(is_leader=True) |
| 40 | self.harness.begin() |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 41 | self.config = { |
| 42 | "server_name": "localhost", |
| 43 | "port": 80, |
| 44 | "max_file_size": 0, |
| 45 | "ingress_whitelist_source_range": "", |
| 46 | "tls_secret_name": "", |
| 47 | "site_url": "https://ui.192.168.100.100.xip.io", |
| 48 | } |
| 49 | self.harness.update_config(self.config) |
| David Garcia | 95ba7e1 | 2021-02-03 11:10:28 +0100 | [diff] [blame] | 50 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 51 | def test_config_changed_no_relations( |
| 52 | self, |
| 53 | ) -> NoReturn: |
| 54 | """Test ingress resources without HTTP.""" |
| 55 | |
| David Garcia | 95ba7e1 | 2021-02-03 11:10:28 +0100 | [diff] [blame] | 56 | self.harness.charm.on.config_changed.emit() |
| 57 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 58 | # Assertions |
| 59 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 60 | self.assertTrue( |
| 61 | all( |
| 62 | relation in self.harness.charm.unit.status.message |
| 63 | for relation in ["nbi"] |
| 64 | ) |
| 65 | ) |
| 66 | |
| 67 | def test_config_changed_non_leader( |
| 68 | self, |
| 69 | ) -> NoReturn: |
| 70 | """Test ingress resources without HTTP.""" |
| 71 | self.harness.set_leader(is_leader=False) |
| 72 | self.harness.charm.on.config_changed.emit() |
| 73 | |
| 74 | # Assertions |
| 75 | self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus) |
| 76 | |
| 77 | def test_with_relations( |
| 78 | self, |
| 79 | ) -> NoReturn: |
| 80 | "Test with relations (internal)" |
| 81 | self.initialize_nbi_relation() |
| 82 | # Verifying status |
| 83 | self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 84 | |
| 85 | def initialize_nbi_relation(self): |
| 86 | http_relation_id = self.harness.add_relation("nbi", "nbi") |
| 87 | self.harness.add_relation_unit(http_relation_id, "nbi") |
| 88 | self.harness.update_relation_data( |
| 89 | http_relation_id, |
| 90 | "nbi", |
| 91 | {"host": "nbi", "port": 9999}, |
| 92 | ) |
| 93 | |
| David Garcia | 95ba7e1 | 2021-02-03 11:10:28 +0100 | [diff] [blame] | 94 | |
| 95 | if __name__ == "__main__": |
| 96 | unittest.main() |