blob: 80cb3158bf84dcd4b8b0c6e97b086b733a202fa1 [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 Garciac753dc52021-03-17 15:28:47 +010026
David Garcia95ba7e12021-02-03 11:10:28 +010027
David Garcia49379ce2021-02-24 13:48:22 +010028from charm import PlaCharm
David Garciac753dc52021-03-17 15:28:47 +010029from ops.model import ActiveStatus, BlockedStatus
30from ops.testing import Harness
David Garcia95ba7e12021-02-03 11:10:28 +010031
32
33class TestCharm(unittest.TestCase):
David Garcia49379ce2021-02-24 13:48:22 +010034 """Pla Charm unit tests."""
David Garcia95ba7e12021-02-03 11:10:28 +010035
36 def setUp(self) -> NoReturn:
37 """Test setup"""
David Garcia49379ce2021-02-24 13:48:22 +010038 self.image_info = sys.modules["oci_image"].OCIImageResource().fetch()
39 self.harness = Harness(PlaCharm)
David Garcia95ba7e12021-02-03 11:10:28 +010040 self.harness.set_leader(is_leader=True)
41 self.harness.begin()
David Garcia49379ce2021-02-24 13:48:22 +010042 self.config = {
43 "log_level": "INFO",
44 }
45 self.harness.update_config(self.config)
David Garcia95ba7e12021-02-03 11:10:28 +010046
David Garcia49379ce2021-02-24 13:48:22 +010047 def test_config_changed_no_relations(
48 self,
49 ) -> NoReturn:
50 """Test ingress resources without HTTP."""
51
David Garcia95ba7e12021-02-03 11:10:28 +010052 self.harness.charm.on.config_changed.emit()
53
David Garcia49379ce2021-02-24 13:48:22 +010054 # Assertions
55 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
56 self.assertTrue(
57 all(
58 relation in self.harness.charm.unit.status.message
59 for relation in ["mongodb", "kafka"]
60 )
61 )
62
63 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()
69
70 # Assertions
71 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
72
73 def test_with_relations(
74 self,
75 ) -> NoReturn:
76 "Test with relations (internal)"
77 self.initialize_kafka_relation()
78 self.initialize_mongo_relation()
79 # Verifying status
80 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
81
82 def initialize_kafka_relation(self):
83 kafka_relation_id = self.harness.add_relation("kafka", "kafka")
84 self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
85 self.harness.update_relation_data(
86 kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
87 )
88
89 def initialize_mongo_relation(self):
90 mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
91 self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
92 self.harness.update_relation_data(
93 mongodb_relation_id,
94 "mongodb/0",
95 {"connection_string": "mongodb://mongo:27017"},
96 )
97
David Garcia95ba7e12021-02-03 11:10:28 +010098
99if __name__ == "__main__":
100 unittest.main()