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
23 from typing
import NoReturn
26 from ops
.model
import BlockedStatus
27 from ops
.testing
import Harness
29 from charm
import KafkaExporterCharm
32 class TestCharm(unittest
.TestCase
):
33 """Kafka Exporter Charm unit tests."""
35 def setUp(self
) -> NoReturn
:
37 self
.harness
= Harness(KafkaExporterCharm
)
38 self
.harness
.set_leader(is_leader
=True)
41 def test_on_start_without_relations(self
) -> NoReturn
:
42 """Test installation without any relation."""
43 self
.harness
.charm
.on
.start
.emit()
46 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
48 # Verifying status message
49 self
.assertGreater(len(self
.harness
.charm
.unit
.status
.message
), 0)
51 self
.harness
.charm
.unit
.status
.message
.startswith("Waiting for ")
53 self
.assertIn("kafka", self
.harness
.charm
.unit
.status
.message
)
54 self
.assertTrue(self
.harness
.charm
.unit
.status
.message
.endswith(" relation"))
56 def test_on_start_with_relations_without_http(self
) -> NoReturn
:
57 """Test deployment."""
62 "name": "kafka-exporter",
63 "imageDetails": self
.harness
.charm
.image
.fetch(),
64 "imagePullPolicy": "Always",
67 "name": "kafka-exporter",
68 "containerPort": 9308,
73 "command": ["kafka-exporter", "--kafka.server=kafka:9090"],
77 "path": "/api/health",
80 "initialDelaySeconds": 10,
83 "successThreshold": 1,
84 "failureThreshold": 3,
88 "path": "/api/health",
91 "initialDelaySeconds": 60,
93 "failureThreshold": 10,
98 "kubernetesResources": {"ingressResources": []},
101 self
.harness
.charm
.on
.start
.emit()
103 # Initializing the kafka relation
104 relation_id
= self
.harness
.add_relation("kafka", "kafka")
105 self
.harness
.add_relation_unit(relation_id
, "kafka/0")
106 self
.harness
.update_relation_data(
116 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
118 pod_spec
, _
= self
.harness
.get_pod_spec()
120 self
.assertDictEqual(expected_result
, pod_spec
)
122 def test_ingress_resources_with_http(self
) -> NoReturn
:
123 """Test ingress resources with HTTP."""
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"],
143 "path": "/api/health",
146 "initialDelaySeconds": 10,
149 "successThreshold": 1,
150 "failureThreshold": 3,
154 "path": "/api/health",
157 "initialDelaySeconds": 60,
158 "timeoutSeconds": 30,
159 "failureThreshold": 10,
164 "kubernetesResources": {
165 "ingressResources": [
167 "name": "kafka-exporter-ingress",
169 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
174 "host": "kafka-exporter",
180 "serviceName": "kafka-exporter",
194 self
.harness
.charm
.on
.start
.emit()
196 # Initializing the kafka relation
197 relation_id
= self
.harness
.add_relation("kafka", "kafka")
198 self
.harness
.add_relation_unit(relation_id
, "kafka/0")
199 self
.harness
.update_relation_data(
208 self
.harness
.update_config({"site_url": "http://kafka-exporter"})
210 pod_spec
, _
= self
.harness
.get_pod_spec()
212 self
.assertDictEqual(expected_result
, pod_spec
)
214 def test_ingress_resources_with_https(self
) -> NoReturn
:
215 """Test ingress resources with HTTPS."""
220 "name": "kafka-exporter",
221 "imageDetails": self
.harness
.charm
.image
.fetch(),
222 "imagePullPolicy": "Always",
225 "name": "kafka-exporter",
226 "containerPort": 9308,
231 "command": ["kafka-exporter", "--kafka.server=kafka:9090"],
235 "path": "/api/health",
238 "initialDelaySeconds": 10,
241 "successThreshold": 1,
242 "failureThreshold": 3,
246 "path": "/api/health",
249 "initialDelaySeconds": 60,
250 "timeoutSeconds": 30,
251 "failureThreshold": 10,
256 "kubernetesResources": {
257 "ingressResources": [
259 "name": "kafka-exporter-ingress",
264 "host": "kafka-exporter",
270 "serviceName": "kafka-exporter",
280 "hosts": ["kafka-exporter"],
281 "secretName": "kafka-exporter",
290 self
.harness
.charm
.on
.start
.emit()
292 # Initializing the kafka relation
293 relation_id
= self
.harness
.add_relation("kafka", "kafka")
294 self
.harness
.add_relation_unit(relation_id
, "kafka/0")
295 self
.harness
.update_relation_data(
304 self
.harness
.update_config(
306 "site_url": "https://kafka-exporter",
307 "tls_secret_name": "kafka-exporter",
311 pod_spec
, _
= self
.harness
.get_pod_spec()
313 self
.assertDictEqual(expected_result
, pod_spec
)
315 def test_ingress_resources_with_https_and_ingress_whitelist(self
) -> NoReturn
:
316 """Test ingress resources with HTTPS and ingress whitelist."""
321 "name": "kafka-exporter",
322 "imageDetails": self
.harness
.charm
.image
.fetch(),
323 "imagePullPolicy": "Always",
326 "name": "kafka-exporter",
327 "containerPort": 9308,
332 "command": ["kafka-exporter", "--kafka.server=kafka:9090"],
336 "path": "/api/health",
339 "initialDelaySeconds": 10,
342 "successThreshold": 1,
343 "failureThreshold": 3,
347 "path": "/api/health",
350 "initialDelaySeconds": 60,
351 "timeoutSeconds": 30,
352 "failureThreshold": 10,
357 "kubernetesResources": {
358 "ingressResources": [
360 "name": "kafka-exporter-ingress",
362 "nginx.ingress.kubernetes.io/whitelist-source-range": "0.0.0.0/0",
367 "host": "kafka-exporter",
373 "serviceName": "kafka-exporter",
383 "hosts": ["kafka-exporter"],
384 "secretName": "kafka-exporter",
393 self
.harness
.charm
.on
.start
.emit()
395 # Initializing the kafka relation
396 relation_id
= self
.harness
.add_relation("kafka", "kafka")
397 self
.harness
.add_relation_unit(relation_id
, "kafka/0")
398 self
.harness
.update_relation_data(
407 self
.harness
.update_config(
409 "site_url": "https://kafka-exporter",
410 "tls_secret_name": "kafka-exporter",
411 "ingress_whitelist_source_range": "0.0.0.0/0",
415 pod_spec
, _
= self
.harness
.get_pod_spec()
417 self
.assertDictEqual(expected_result
, pod_spec
)
419 def test_on_kafka_unit_relation_changed(self
) -> NoReturn
:
420 """Test to see if kafka relation is updated."""
421 self
.harness
.charm
.on
.start
.emit()
423 relation_id
= self
.harness
.add_relation("kafka", "kafka")
424 self
.harness
.add_relation_unit(relation_id
, "kafka/0")
425 self
.harness
.update_relation_data(
435 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
437 def test_publish_target_info(self
) -> NoReturn
:
438 """Test to see if target relation is updated."""
440 "hostname": "kafka-exporter",
442 "metrics_path": "/metrics",
443 "scrape_interval": "30s",
444 "scrape_timeout": "15s",
447 self
.harness
.charm
.on
.start
.emit()
449 relation_id
= self
.harness
.add_relation("prometheus-scrape", "prometheus")
450 self
.harness
.add_relation_unit(relation_id
, "prometheus/0")
451 relation_data
= self
.harness
.get_relation_data(relation_id
, "kafka-exporter/0")
453 self
.assertDictEqual(expected_result
, relation_data
)
455 def test_publish_target_info_with_site_url(self
) -> NoReturn
:
456 """Test to see if target relation is updated."""
458 "hostname": "kafka-exporter-osm",
460 "metrics_path": "/metrics",
461 "scrape_interval": "30s",
462 "scrape_timeout": "15s",
465 self
.harness
.charm
.on
.start
.emit()
467 self
.harness
.update_config({"site_url": "http://kafka-exporter-osm"})
469 relation_id
= self
.harness
.add_relation("prometheus-scrape", "prometheus")
470 self
.harness
.add_relation_unit(relation_id
, "prometheus/0")
471 relation_data
= self
.harness
.get_relation_data(relation_id
, "kafka-exporter/0")
473 self
.assertDictEqual(expected_result
, relation_data
)
475 def test_publish_dashboard_info(self
) -> NoReturn
:
476 """Test to see if dashboard relation is updated."""
477 self
.harness
.charm
.on
.start
.emit()
479 relation_id
= self
.harness
.add_relation("grafana-dashboard", "grafana")
480 self
.harness
.add_relation_unit(relation_id
, "grafana/0")
481 relation_data
= self
.harness
.get_relation_data(relation_id
, "kafka-exporter/0")
483 self
.assertTrue("dashboard" in relation_data
)
484 self
.assertTrue(len(relation_data
["dashboard"]) > 0)
487 if __name__
== "__main__":