Moving exporter charms to use opslib
[osm/devops.git] / installers / charm / kafka-exporter / tests / test_charm.py
1 #!/usr/bin/env python3
2 # Copyright 2021 Canonical Ltd.
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
23 import sys
24 from typing import NoReturn
25 import unittest
26
27
28 from charm import KafkaExporterCharm
29 from ops.model import ActiveStatus, BlockedStatus
30 from ops.testing import Harness
31
32
33 class TestCharm(unittest.TestCase):
34 """Kafka Exporter Charm unit tests."""
35
36 def setUp(self) -> NoReturn:
37 """Test setup"""
38 self.image_info = sys.modules["oci_image"].OCIImageResource().fetch()
39 self.harness = Harness(KafkaExporterCharm)
40 self.harness.set_leader(is_leader=True)
41 self.harness.begin()
42 self.config = {
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",
47 }
48 self.harness.update_config(self.config)
49
50 def test_config_changed_no_relations(
51 self,
52 ) -> NoReturn:
53 """Test ingress resources without HTTP."""
54
55 self.harness.charm.on.config_changed.emit()
56
57 # Assertions
58 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
59 print(self.harness.charm.unit.status.message)
60 self.assertTrue(
61 all(
62 relation in self.harness.charm.unit.status.message
63 for relation in ["kafka"]
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"
81 self.initialize_kafka_relation()
82
83 # Verifying status
84 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
85
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}
91 )
92
93
94 if __name__ == "__main__":
95 unittest.main()
96
97
98 # class TestCharm(unittest.TestCase):
99 # """Kafka Exporter Charm unit tests."""
100 #
101 # def setUp(self) -> NoReturn:
102 # """Test setup"""
103 # self.harness = Harness(KafkaExporterCharm)
104 # self.harness.set_leader(is_leader=True)
105 # self.harness.begin()
106 #
107 # def test_on_start_without_relations(self) -> NoReturn:
108 # """Test installation without any relation."""
109 # self.harness.charm.on.start.emit()
110 #
111 # # Verifying status
112 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
113 #
114 # # Verifying status message
115 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
116 # self.assertTrue(
117 # self.harness.charm.unit.status.message.startswith("Waiting for ")
118 # )
119 # self.assertIn("kafka", self.harness.charm.unit.status.message)
120 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
121 #
122 # def test_on_start_with_relations_without_http(self) -> NoReturn:
123 # """Test deployment."""
124 # expected_result = {
125 # "version": 3,
126 # "containers": [
127 # {
128 # "name": "kafka-exporter",
129 # "imageDetails": self.harness.charm.image.fetch(),
130 # "imagePullPolicy": "Always",
131 # "ports": [
132 # {
133 # "name": "kafka-exporter",
134 # "containerPort": 9308,
135 # "protocol": "TCP",
136 # }
137 # ],
138 # "envConfig": {},
139 # "command": ["kafka_exporter", "--kafka.server=kafka:9090"],
140 # "kubernetes": {
141 # "readinessProbe": {
142 # "httpGet": {
143 # "path": "/api/health",
144 # "port": 9308,
145 # },
146 # "initialDelaySeconds": 10,
147 # "periodSeconds": 10,
148 # "timeoutSeconds": 5,
149 # "successThreshold": 1,
150 # "failureThreshold": 3,
151 # },
152 # "livenessProbe": {
153 # "httpGet": {
154 # "path": "/api/health",
155 # "port": 9308,
156 # },
157 # "initialDelaySeconds": 60,
158 # "timeoutSeconds": 30,
159 # "failureThreshold": 10,
160 # },
161 # },
162 # },
163 # ],
164 # "kubernetesResources": {"ingressResources": []},
165 # }
166 #
167 # self.harness.charm.on.start.emit()
168 #
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(
173 # relation_id,
174 # "kafka/0",
175 # {
176 # "host": "kafka",
177 # "port": "9090",
178 # },
179 # )
180 #
181 # # Verifying status
182 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
183 #
184 # pod_spec, _ = self.harness.get_pod_spec()
185 #
186 # self.assertDictEqual(expected_result, pod_spec)
187 #
188 # def test_ingress_resources_with_http(self) -> NoReturn:
189 # """Test ingress resources with HTTP."""
190 # expected_result = {
191 # "version": 3,
192 # "containers": [
193 # {
194 # "name": "kafka-exporter",
195 # "imageDetails": self.harness.charm.image.fetch(),
196 # "imagePullPolicy": "Always",
197 # "ports": [
198 # {
199 # "name": "kafka-exporter",
200 # "containerPort": 9308,
201 # "protocol": "TCP",
202 # }
203 # ],
204 # "envConfig": {},
205 # "command": ["kafka_exporter", "--kafka.server=kafka:9090"],
206 # "kubernetes": {
207 # "readinessProbe": {
208 # "httpGet": {
209 # "path": "/api/health",
210 # "port": 9308,
211 # },
212 # "initialDelaySeconds": 10,
213 # "periodSeconds": 10,
214 # "timeoutSeconds": 5,
215 # "successThreshold": 1,
216 # "failureThreshold": 3,
217 # },
218 # "livenessProbe": {
219 # "httpGet": {
220 # "path": "/api/health",
221 # "port": 9308,
222 # },
223 # "initialDelaySeconds": 60,
224 # "timeoutSeconds": 30,
225 # "failureThreshold": 10,
226 # },
227 # },
228 # },
229 # ],
230 # "kubernetesResources": {
231 # "ingressResources": [
232 # {
233 # "name": "kafka-exporter-ingress",
234 # "annotations": {
235 # "nginx.ingress.kubernetes.io/ssl-redirect": "false",
236 # },
237 # "spec": {
238 # "rules": [
239 # {
240 # "host": "kafka-exporter",
241 # "http": {
242 # "paths": [
243 # {
244 # "path": "/",
245 # "backend": {
246 # "serviceName": "kafka-exporter",
247 # "servicePort": 9308,
248 # },
249 # }
250 # ]
251 # },
252 # }
253 # ]
254 # },
255 # }
256 # ],
257 # },
258 # }
259 #
260 # self.harness.charm.on.start.emit()
261 #
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(
266 # relation_id,
267 # "kafka/0",
268 # {
269 # "host": "kafka",
270 # "port": "9090",
271 # },
272 # )
273 #
274 # self.harness.update_config({"site_url": "http://kafka-exporter"})
275 #
276 # pod_spec, _ = self.harness.get_pod_spec()
277 #
278 # self.assertDictEqual(expected_result, pod_spec)
279 #
280 # def test_ingress_resources_with_https(self) -> NoReturn:
281 # """Test ingress resources with HTTPS."""
282 # expected_result = {
283 # "version": 3,
284 # "containers": [
285 # {
286 # "name": "kafka-exporter",
287 # "imageDetails": self.harness.charm.image.fetch(),
288 # "imagePullPolicy": "Always",
289 # "ports": [
290 # {
291 # "name": "kafka-exporter",
292 # "containerPort": 9308,
293 # "protocol": "TCP",
294 # }
295 # ],
296 # "envConfig": {},
297 # "command": ["kafka_exporter", "--kafka.server=kafka:9090"],
298 # "kubernetes": {
299 # "readinessProbe": {
300 # "httpGet": {
301 # "path": "/api/health",
302 # "port": 9308,
303 # },
304 # "initialDelaySeconds": 10,
305 # "periodSeconds": 10,
306 # "timeoutSeconds": 5,
307 # "successThreshold": 1,
308 # "failureThreshold": 3,
309 # },
310 # "livenessProbe": {
311 # "httpGet": {
312 # "path": "/api/health",
313 # "port": 9308,
314 # },
315 # "initialDelaySeconds": 60,
316 # "timeoutSeconds": 30,
317 # "failureThreshold": 10,
318 # },
319 # },
320 # },
321 # ],
322 # "kubernetesResources": {
323 # "ingressResources": [
324 # {
325 # "name": "kafka-exporter-ingress",
326 # "annotations": {},
327 # "spec": {
328 # "rules": [
329 # {
330 # "host": "kafka-exporter",
331 # "http": {
332 # "paths": [
333 # {
334 # "path": "/",
335 # "backend": {
336 # "serviceName": "kafka-exporter",
337 # "servicePort": 9308,
338 # },
339 # }
340 # ]
341 # },
342 # }
343 # ],
344 # "tls": [
345 # {
346 # "hosts": ["kafka-exporter"],
347 # "secretName": "kafka-exporter",
348 # }
349 # ],
350 # },
351 # }
352 # ],
353 # },
354 # }
355 #
356 # self.harness.charm.on.start.emit()
357 #
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(
362 # relation_id,
363 # "kafka/0",
364 # {
365 # "host": "kafka",
366 # "port": "9090",
367 # },
368 # )
369 #
370 # self.harness.update_config(
371 # {
372 # "site_url": "https://kafka-exporter",
373 # "tls_secret_name": "kafka-exporter",
374 # }
375 # )
376 #
377 # pod_spec, _ = self.harness.get_pod_spec()
378 #
379 # self.assertDictEqual(expected_result, pod_spec)
380 #
381 # def test_ingress_resources_with_https_and_ingress_whitelist(self) -> NoReturn:
382 # """Test ingress resources with HTTPS and ingress whitelist."""
383 # expected_result = {
384 # "version": 3,
385 # "containers": [
386 # {
387 # "name": "kafka-exporter",
388 # "imageDetails": self.harness.charm.image.fetch(),
389 # "imagePullPolicy": "Always",
390 # "ports": [
391 # {
392 # "name": "kafka-exporter",
393 # "containerPort": 9308,
394 # "protocol": "TCP",
395 # }
396 # ],
397 # "envConfig": {},
398 # "command": ["kafka_exporter", "--kafka.server=kafka:9090"],
399 # "kubernetes": {
400 # "readinessProbe": {
401 # "httpGet": {
402 # "path": "/api/health",
403 # "port": 9308,
404 # },
405 # "initialDelaySeconds": 10,
406 # "periodSeconds": 10,
407 # "timeoutSeconds": 5,
408 # "successThreshold": 1,
409 # "failureThreshold": 3,
410 # },
411 # "livenessProbe": {
412 # "httpGet": {
413 # "path": "/api/health",
414 # "port": 9308,
415 # },
416 # "initialDelaySeconds": 60,
417 # "timeoutSeconds": 30,
418 # "failureThreshold": 10,
419 # },
420 # },
421 # },
422 # ],
423 # "kubernetesResources": {
424 # "ingressResources": [
425 # {
426 # "name": "kafka-exporter-ingress",
427 # "annotations": {
428 # "nginx.ingress.kubernetes.io/whitelist-source-range": "0.0.0.0/0",
429 # },
430 # "spec": {
431 # "rules": [
432 # {
433 # "host": "kafka-exporter",
434 # "http": {
435 # "paths": [
436 # {
437 # "path": "/",
438 # "backend": {
439 # "serviceName": "kafka-exporter",
440 # "servicePort": 9308,
441 # },
442 # }
443 # ]
444 # },
445 # }
446 # ],
447 # "tls": [
448 # {
449 # "hosts": ["kafka-exporter"],
450 # "secretName": "kafka-exporter",
451 # }
452 # ],
453 # },
454 # }
455 # ],
456 # },
457 # }
458 #
459 # self.harness.charm.on.start.emit()
460 #
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(
465 # relation_id,
466 # "kafka/0",
467 # {
468 # "host": "kafka",
469 # "port": "9090",
470 # },
471 # )
472 #
473 # self.harness.update_config(
474 # {
475 # "site_url": "https://kafka-exporter",
476 # "tls_secret_name": "kafka-exporter",
477 # "ingress_whitelist_source_range": "0.0.0.0/0",
478 # }
479 # )
480 #
481 # pod_spec, _ = self.harness.get_pod_spec()
482 #
483 # self.assertDictEqual(expected_result, pod_spec)
484 #
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()
488 #
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(
492 # relation_id,
493 # "kafka/0",
494 # {
495 # "host": "kafka",
496 # "port": "9090",
497 # },
498 # )
499 #
500 # # Verifying status
501 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
502 #
503 # def test_publish_target_info(self) -> NoReturn:
504 # """Test to see if target relation is updated."""
505 # expected_result = {
506 # "hostname": "kafka-exporter",
507 # "port": "9308",
508 # "metrics_path": "/metrics",
509 # "scrape_interval": "30s",
510 # "scrape_timeout": "15s",
511 # }
512 #
513 # self.harness.charm.on.start.emit()
514 #
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")
518 #
519 # self.assertDictEqual(expected_result, relation_data)
520 #
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",
525 # "port": "80",
526 # "metrics_path": "/metrics",
527 # "scrape_interval": "30s",
528 # "scrape_timeout": "15s",
529 # }
530 #
531 # self.harness.charm.on.start.emit()
532 #
533 # self.harness.update_config({"site_url": "http://kafka-exporter-osm"})
534 #
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")
538 #
539 # self.assertDictEqual(expected_result, relation_data)
540 #
541 # def test_publish_dashboard_info(self) -> NoReturn:
542 # """Test to see if dashboard relation is updated."""
543 # self.harness.charm.on.start.emit()
544 #
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")
548 #
549 # self.assertTrue("dashboard" in relation_data)
550 # self.assertTrue(len(relation_data["dashboard"]) > 0)
551 #
552 #
553 # if __name__ == "__main__":
554 # unittest.main()