2 # Copyright 2021 Canonical Ltd.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: legal@canonical.com
19 # To get in touch with the maintainers, please contact:
20 # osm-charmers@lists.launchpad.net
24 from typing
import NoReturn
28 from charm
import KafkaExporterCharm
29 from ops
.model
import ActiveStatus
, BlockedStatus
30 from ops
.testing
import Harness
33 class TestCharm(unittest
.TestCase
):
34 """Kafka Exporter Charm unit tests."""
36 def setUp(self
) -> NoReturn
:
38 self
.image_info
= sys
.modules
["oci_image"].OCIImageResource().fetch()
39 self
.harness
= Harness(KafkaExporterCharm
)
40 self
.harness
.set_leader(is_leader
=True)
43 "ingress_whitelist_source_range": "",
44 "tls_secret_name": "",
45 "site_url": "https://kafka-exporter.192.168.100.100.nip.io",
46 "cluster_issuer": "vault-issuer",
48 self
.harness
.update_config(self
.config
)
50 def test_config_changed_no_relations(
53 """Test ingress resources without HTTP."""
55 self
.harness
.charm
.on
.config_changed
.emit()
58 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
59 print(self
.harness
.charm
.unit
.status
.message
)
62 relation
in self
.harness
.charm
.unit
.status
.message
63 for relation
in ["kafka"]
67 def test_config_changed_non_leader(
70 """Test ingress resources without HTTP."""
71 self
.harness
.set_leader(is_leader
=False)
72 self
.harness
.charm
.on
.config_changed
.emit()
75 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, ActiveStatus
)
77 def test_with_relations(
81 self
.initialize_kafka_relation()
84 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
86 def initialize_kafka_relation(self
):
87 kafka_relation_id
= self
.harness
.add_relation("kafka", "kafka")
88 self
.harness
.add_relation_unit(kafka_relation_id
, "kafka/0")
89 self
.harness
.update_relation_data(
90 kafka_relation_id
, "kafka/0", {"host": "kafka", "port": 9092}
94 if __name__
== "__main__":
98 # class TestCharm(unittest.TestCase):
99 # """Kafka Exporter Charm unit tests."""
101 # def setUp(self) -> NoReturn:
103 # self.harness = Harness(KafkaExporterCharm)
104 # self.harness.set_leader(is_leader=True)
105 # self.harness.begin()
107 # def test_on_start_without_relations(self) -> NoReturn:
108 # """Test installation without any relation."""
109 # self.harness.charm.on.start.emit()
112 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
114 # # Verifying status message
115 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
117 # self.harness.charm.unit.status.message.startswith("Waiting for ")
119 # self.assertIn("kafka", self.harness.charm.unit.status.message)
120 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
122 # def test_on_start_with_relations_without_http(self) -> NoReturn:
123 # """Test deployment."""
124 # expected_result = {
128 # "name": "kafka-exporter",
129 # "imageDetails": self.harness.charm.image.fetch(),
130 # "imagePullPolicy": "Always",
133 # "name": "kafka-exporter",
134 # "containerPort": 9308,
139 # "command": ["kafka_exporter", "--kafka.server=kafka:9090"],
141 # "readinessProbe": {
143 # "path": "/api/health",
146 # "initialDelaySeconds": 10,
147 # "periodSeconds": 10,
148 # "timeoutSeconds": 5,
149 # "successThreshold": 1,
150 # "failureThreshold": 3,
154 # "path": "/api/health",
157 # "initialDelaySeconds": 60,
158 # "timeoutSeconds": 30,
159 # "failureThreshold": 10,
164 # "kubernetesResources": {"ingressResources": []},
167 # self.harness.charm.on.start.emit()
169 # # Initializing the kafka relation
170 # relation_id = self.harness.add_relation("kafka", "kafka")
171 # self.harness.add_relation_unit(relation_id, "kafka/0")
172 # self.harness.update_relation_data(
182 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
184 # pod_spec, _ = self.harness.get_pod_spec()
186 # self.assertDictEqual(expected_result, pod_spec)
188 # def test_ingress_resources_with_http(self) -> NoReturn:
189 # """Test ingress resources with HTTP."""
190 # expected_result = {
194 # "name": "kafka-exporter",
195 # "imageDetails": self.harness.charm.image.fetch(),
196 # "imagePullPolicy": "Always",
199 # "name": "kafka-exporter",
200 # "containerPort": 9308,
205 # "command": ["kafka_exporter", "--kafka.server=kafka:9090"],
207 # "readinessProbe": {
209 # "path": "/api/health",
212 # "initialDelaySeconds": 10,
213 # "periodSeconds": 10,
214 # "timeoutSeconds": 5,
215 # "successThreshold": 1,
216 # "failureThreshold": 3,
220 # "path": "/api/health",
223 # "initialDelaySeconds": 60,
224 # "timeoutSeconds": 30,
225 # "failureThreshold": 10,
230 # "kubernetesResources": {
231 # "ingressResources": [
233 # "name": "kafka-exporter-ingress",
235 # "nginx.ingress.kubernetes.io/ssl-redirect": "false",
240 # "host": "kafka-exporter",
246 # "serviceName": "kafka-exporter",
247 # "servicePort": 9308,
260 # self.harness.charm.on.start.emit()
262 # # Initializing the kafka relation
263 # relation_id = self.harness.add_relation("kafka", "kafka")
264 # self.harness.add_relation_unit(relation_id, "kafka/0")
265 # self.harness.update_relation_data(
274 # self.harness.update_config({"site_url": "http://kafka-exporter"})
276 # pod_spec, _ = self.harness.get_pod_spec()
278 # self.assertDictEqual(expected_result, pod_spec)
280 # def test_ingress_resources_with_https(self) -> NoReturn:
281 # """Test ingress resources with HTTPS."""
282 # expected_result = {
286 # "name": "kafka-exporter",
287 # "imageDetails": self.harness.charm.image.fetch(),
288 # "imagePullPolicy": "Always",
291 # "name": "kafka-exporter",
292 # "containerPort": 9308,
297 # "command": ["kafka_exporter", "--kafka.server=kafka:9090"],
299 # "readinessProbe": {
301 # "path": "/api/health",
304 # "initialDelaySeconds": 10,
305 # "periodSeconds": 10,
306 # "timeoutSeconds": 5,
307 # "successThreshold": 1,
308 # "failureThreshold": 3,
312 # "path": "/api/health",
315 # "initialDelaySeconds": 60,
316 # "timeoutSeconds": 30,
317 # "failureThreshold": 10,
322 # "kubernetesResources": {
323 # "ingressResources": [
325 # "name": "kafka-exporter-ingress",
330 # "host": "kafka-exporter",
336 # "serviceName": "kafka-exporter",
337 # "servicePort": 9308,
346 # "hosts": ["kafka-exporter"],
347 # "secretName": "kafka-exporter",
356 # self.harness.charm.on.start.emit()
358 # # Initializing the kafka relation
359 # relation_id = self.harness.add_relation("kafka", "kafka")
360 # self.harness.add_relation_unit(relation_id, "kafka/0")
361 # self.harness.update_relation_data(
370 # self.harness.update_config(
372 # "site_url": "https://kafka-exporter",
373 # "tls_secret_name": "kafka-exporter",
377 # pod_spec, _ = self.harness.get_pod_spec()
379 # self.assertDictEqual(expected_result, pod_spec)
381 # def test_ingress_resources_with_https_and_ingress_whitelist(self) -> NoReturn:
382 # """Test ingress resources with HTTPS and ingress whitelist."""
383 # expected_result = {
387 # "name": "kafka-exporter",
388 # "imageDetails": self.harness.charm.image.fetch(),
389 # "imagePullPolicy": "Always",
392 # "name": "kafka-exporter",
393 # "containerPort": 9308,
398 # "command": ["kafka_exporter", "--kafka.server=kafka:9090"],
400 # "readinessProbe": {
402 # "path": "/api/health",
405 # "initialDelaySeconds": 10,
406 # "periodSeconds": 10,
407 # "timeoutSeconds": 5,
408 # "successThreshold": 1,
409 # "failureThreshold": 3,
413 # "path": "/api/health",
416 # "initialDelaySeconds": 60,
417 # "timeoutSeconds": 30,
418 # "failureThreshold": 10,
423 # "kubernetesResources": {
424 # "ingressResources": [
426 # "name": "kafka-exporter-ingress",
428 # "nginx.ingress.kubernetes.io/whitelist-source-range": "0.0.0.0/0",
433 # "host": "kafka-exporter",
439 # "serviceName": "kafka-exporter",
440 # "servicePort": 9308,
449 # "hosts": ["kafka-exporter"],
450 # "secretName": "kafka-exporter",
459 # self.harness.charm.on.start.emit()
461 # # Initializing the kafka relation
462 # relation_id = self.harness.add_relation("kafka", "kafka")
463 # self.harness.add_relation_unit(relation_id, "kafka/0")
464 # self.harness.update_relation_data(
473 # self.harness.update_config(
475 # "site_url": "https://kafka-exporter",
476 # "tls_secret_name": "kafka-exporter",
477 # "ingress_whitelist_source_range": "0.0.0.0/0",
481 # pod_spec, _ = self.harness.get_pod_spec()
483 # self.assertDictEqual(expected_result, pod_spec)
485 # def test_on_kafka_unit_relation_changed(self) -> NoReturn:
486 # """Test to see if kafka relation is updated."""
487 # self.harness.charm.on.start.emit()
489 # relation_id = self.harness.add_relation("kafka", "kafka")
490 # self.harness.add_relation_unit(relation_id, "kafka/0")
491 # self.harness.update_relation_data(
501 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
503 # def test_publish_target_info(self) -> NoReturn:
504 # """Test to see if target relation is updated."""
505 # expected_result = {
506 # "hostname": "kafka-exporter",
508 # "metrics_path": "/metrics",
509 # "scrape_interval": "30s",
510 # "scrape_timeout": "15s",
513 # self.harness.charm.on.start.emit()
515 # relation_id = self.harness.add_relation("prometheus-scrape", "prometheus")
516 # self.harness.add_relation_unit(relation_id, "prometheus/0")
517 # relation_data = self.harness.get_relation_data(relation_id, "kafka-exporter/0")
519 # self.assertDictEqual(expected_result, relation_data)
521 # def test_publish_target_info_with_site_url(self) -> NoReturn:
522 # """Test to see if target relation is updated."""
523 # expected_result = {
524 # "hostname": "kafka-exporter-osm",
526 # "metrics_path": "/metrics",
527 # "scrape_interval": "30s",
528 # "scrape_timeout": "15s",
531 # self.harness.charm.on.start.emit()
533 # self.harness.update_config({"site_url": "http://kafka-exporter-osm"})
535 # relation_id = self.harness.add_relation("prometheus-scrape", "prometheus")
536 # self.harness.add_relation_unit(relation_id, "prometheus/0")
537 # relation_data = self.harness.get_relation_data(relation_id, "kafka-exporter/0")
539 # self.assertDictEqual(expected_result, relation_data)
541 # def test_publish_dashboard_info(self) -> NoReturn:
542 # """Test to see if dashboard relation is updated."""
543 # self.harness.charm.on.start.emit()
545 # relation_id = self.harness.add_relation("grafana-dashboard", "grafana")
546 # self.harness.add_relation_unit(relation_id, "grafana/0")
547 # relation_data = self.harness.get_relation_data(relation_id, "kafka-exporter/0")
549 # self.assertTrue("dashboard" in relation_data)
550 # self.assertTrue(len(relation_data["dashboard"]) > 0)
553 # if __name__ == "__main__":