blob: b1a782032d57d2d6c95faf8e46c9580cdea92061 [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 Garcia49379ce2021-02-24 13:48:22 +010026from ops.model import ActiveStatus, BlockedStatus
David Garcia95ba7e12021-02-03 11:10:28 +010027from ops.testing import Harness
28
David Garcia49379ce2021-02-24 13:48:22 +010029from charm import PlaCharm
David Garcia95ba7e12021-02-03 11:10:28 +010030
31
32class TestCharm(unittest.TestCase):
David Garcia49379ce2021-02-24 13:48:22 +010033 """Pla 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()
38 self.harness = Harness(PlaCharm)
David Garcia95ba7e12021-02-03 11:10:28 +010039 self.harness.set_leader(is_leader=True)
40 self.harness.begin()
David Garcia49379ce2021-02-24 13:48:22 +010041 self.config = {
42 "log_level": "INFO",
43 }
44 self.harness.update_config(self.config)
David Garcia95ba7e12021-02-03 11:10:28 +010045
David Garcia49379ce2021-02-24 13:48:22 +010046 def test_config_changed_no_relations(
47 self,
48 ) -> NoReturn:
49 """Test ingress resources without HTTP."""
50
David Garcia95ba7e12021-02-03 11:10:28 +010051 self.harness.charm.on.config_changed.emit()
52
David Garcia49379ce2021-02-24 13:48:22 +010053 # Assertions
54 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
55 self.assertTrue(
56 all(
57 relation in self.harness.charm.unit.status.message
58 for relation in ["mongodb", "kafka"]
59 )
60 )
61
62 def test_config_changed_non_leader(
63 self,
64 ) -> NoReturn:
65 """Test ingress resources without HTTP."""
66 self.harness.set_leader(is_leader=False)
67 self.harness.charm.on.config_changed.emit()
68
69 # Assertions
70 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
71
72 def test_with_relations(
73 self,
74 ) -> NoReturn:
75 "Test with relations (internal)"
76 self.initialize_kafka_relation()
77 self.initialize_mongo_relation()
78 # Verifying status
79 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
80
81 def initialize_kafka_relation(self):
82 kafka_relation_id = self.harness.add_relation("kafka", "kafka")
83 self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
84 self.harness.update_relation_data(
85 kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
86 )
87
88 def initialize_mongo_relation(self):
89 mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
90 self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
91 self.harness.update_relation_data(
92 mongodb_relation_id,
93 "mongodb/0",
94 {"connection_string": "mongodb://mongo:27017"},
95 )
96
David Garcia95ba7e12021-02-03 11:10:28 +010097
98if __name__ == "__main__":
99 unittest.main()