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
27 from charm
import MysqlExporterCharm
28 from ops
.model
import ActiveStatus
, BlockedStatus
29 from ops
.testing
import Harness
32 class TestCharm(unittest
.TestCase
):
33 """Mysql Exporter Charm unit tests."""
35 def setUp(self
) -> NoReturn
:
37 self
.image_info
= sys
.modules
["oci_image"].OCIImageResource().fetch()
38 self
.harness
= Harness(MysqlExporterCharm
)
39 self
.harness
.set_leader(is_leader
=True)
42 "ingress_whitelist_source_range": "",
43 "tls_secret_name": "",
44 "site_url": "https://mysql-exporter.192.168.100.100.nip.io",
45 "cluster_issuer": "vault-issuer",
47 self
.harness
.update_config(self
.config
)
49 def test_config_changed_no_relations(
52 """Test ingress resources without HTTP."""
54 self
.harness
.charm
.on
.config_changed
.emit()
57 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
58 print(self
.harness
.charm
.unit
.status
.message
)
61 relation
in self
.harness
.charm
.unit
.status
.message
62 for relation
in ["mysql"]
66 def test_config_changed_non_leader(
69 """Test ingress resources without HTTP."""
70 self
.harness
.set_leader(is_leader
=False)
71 self
.harness
.charm
.on
.config_changed
.emit()
74 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, ActiveStatus
)
76 def test_with_relations(
80 self
.initialize_mysql_relation()
83 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
89 self
.initialize_mysql_relation()
92 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
94 def test_mysql_exception_relation_and_config(
97 self
.initialize_mysql_config()
98 self
.initialize_mysql_relation()
101 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
103 def initialize_mysql_relation(self
):
104 mongodb_relation_id
= self
.harness
.add_relation("mysql", "mysql")
105 self
.harness
.add_relation_unit(mongodb_relation_id
, "mysql/0")
106 self
.harness
.update_relation_data(
115 "root_password": "root_password",
119 def initialize_mysql_config(self
):
120 self
.harness
.update_config({"mysql_uri": "mysql://user:pass@mysql-host:3306"})
123 if __name__
== "__main__":
127 # class TestCharm(unittest.TestCase):
128 # """Mysql Exporter Charm unit tests."""
130 # def setUp(self) -> NoReturn:
132 # self.harness = Harness(MysqldExporterCharm)
133 # self.harness.set_leader(is_leader=True)
134 # self.harness.begin()
136 # def test_on_start_without_relations(self) -> NoReturn:
137 # """Test installation without any relation."""
138 # self.harness.charm.on.start.emit()
141 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
143 # # Verifying status message
144 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
146 # self.harness.charm.unit.status.message.startswith("Waiting for ")
148 # self.assertIn("mysql", self.harness.charm.unit.status.message)
149 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
151 # def test_on_start_with_relations_without_http(self) -> NoReturn:
152 # """Test deployment."""
153 # expected_result = {
157 # "name": "mysqld-exporter",
158 # "imageDetails": self.harness.charm.image.fetch(),
159 # "imagePullPolicy": "Always",
162 # "name": "mysqld-exporter",
163 # "containerPort": 9104,
167 # "envConfig": {"DATA_SOURCE_NAME": "root:rootpw@(mysql:3306)/"},
169 # "readinessProbe": {
171 # "path": "/api/health",
174 # "initialDelaySeconds": 10,
175 # "periodSeconds": 10,
176 # "timeoutSeconds": 5,
177 # "successThreshold": 1,
178 # "failureThreshold": 3,
182 # "path": "/api/health",
185 # "initialDelaySeconds": 60,
186 # "timeoutSeconds": 30,
187 # "failureThreshold": 10,
192 # "kubernetesResources": {"ingressResources": []},
195 # self.harness.charm.on.start.emit()
197 # # Initializing the mysql relation
198 # relation_id = self.harness.add_relation("mysql", "mysql")
199 # self.harness.add_relation_unit(relation_id, "mysql/0")
200 # self.harness.update_relation_data(
207 # "password": "manopw",
208 # "root_password": "rootpw",
213 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
215 # pod_spec, _ = self.harness.get_pod_spec()
217 # self.assertDictEqual(expected_result, pod_spec)
219 # def test_ingress_resources_with_http(self) -> NoReturn:
220 # """Test ingress resources with HTTP."""
221 # expected_result = {
225 # "name": "mysqld-exporter",
226 # "imageDetails": self.harness.charm.image.fetch(),
227 # "imagePullPolicy": "Always",
230 # "name": "mysqld-exporter",
231 # "containerPort": 9104,
235 # "envConfig": {"DATA_SOURCE_NAME": "root:rootpw@(mysql:3306)/"},
237 # "readinessProbe": {
239 # "path": "/api/health",
242 # "initialDelaySeconds": 10,
243 # "periodSeconds": 10,
244 # "timeoutSeconds": 5,
245 # "successThreshold": 1,
246 # "failureThreshold": 3,
250 # "path": "/api/health",
253 # "initialDelaySeconds": 60,
254 # "timeoutSeconds": 30,
255 # "failureThreshold": 10,
260 # "kubernetesResources": {
261 # "ingressResources": [
263 # "name": "mysqld-exporter-ingress",
265 # "nginx.ingress.kubernetes.io/ssl-redirect": "false",
270 # "host": "mysqld-exporter",
276 # "serviceName": "mysqld-exporter",
277 # "servicePort": 9104,
290 # self.harness.charm.on.start.emit()
292 # # Initializing the mysql relation
293 # relation_id = self.harness.add_relation("mysql", "mysql")
294 # self.harness.add_relation_unit(relation_id, "mysql/0")
295 # self.harness.update_relation_data(
302 # "password": "manopw",
303 # "root_password": "rootpw",
307 # self.harness.update_config({"site_url": "http://mysqld-exporter"})
309 # pod_spec, _ = self.harness.get_pod_spec()
311 # self.assertDictEqual(expected_result, pod_spec)
313 # def test_ingress_resources_with_https(self) -> NoReturn:
314 # """Test ingress resources with HTTPS."""
315 # expected_result = {
319 # "name": "mysqld-exporter",
320 # "imageDetails": self.harness.charm.image.fetch(),
321 # "imagePullPolicy": "Always",
324 # "name": "mysqld-exporter",
325 # "containerPort": 9104,
329 # "envConfig": {"DATA_SOURCE_NAME": "root:rootpw@(mysql:3306)/"},
331 # "readinessProbe": {
333 # "path": "/api/health",
336 # "initialDelaySeconds": 10,
337 # "periodSeconds": 10,
338 # "timeoutSeconds": 5,
339 # "successThreshold": 1,
340 # "failureThreshold": 3,
344 # "path": "/api/health",
347 # "initialDelaySeconds": 60,
348 # "timeoutSeconds": 30,
349 # "failureThreshold": 10,
354 # "kubernetesResources": {
355 # "ingressResources": [
357 # "name": "mysqld-exporter-ingress",
362 # "host": "mysqld-exporter",
368 # "serviceName": "mysqld-exporter",
369 # "servicePort": 9104,
378 # "hosts": ["mysqld-exporter"],
379 # "secretName": "mysqld-exporter",
388 # self.harness.charm.on.start.emit()
390 # # Initializing the mysql relation
391 # relation_id = self.harness.add_relation("mysql", "mysql")
392 # self.harness.add_relation_unit(relation_id, "mysql/0")
393 # self.harness.update_relation_data(
400 # "password": "manopw",
401 # "root_password": "rootpw",
405 # self.harness.update_config(
407 # "site_url": "https://mysqld-exporter",
408 # "tls_secret_name": "mysqld-exporter",
412 # pod_spec, _ = self.harness.get_pod_spec()
414 # self.assertDictEqual(expected_result, pod_spec)
416 # def test_ingress_resources_with_https_and_ingress_whitelist(self) -> NoReturn:
417 # """Test ingress resources with HTTPS and ingress whitelist."""
418 # expected_result = {
422 # "name": "mysqld-exporter",
423 # "imageDetails": self.harness.charm.image.fetch(),
424 # "imagePullPolicy": "Always",
427 # "name": "mysqld-exporter",
428 # "containerPort": 9104,
432 # "envConfig": {"DATA_SOURCE_NAME": "root:rootpw@(mysql:3306)/"},
434 # "readinessProbe": {
436 # "path": "/api/health",
439 # "initialDelaySeconds": 10,
440 # "periodSeconds": 10,
441 # "timeoutSeconds": 5,
442 # "successThreshold": 1,
443 # "failureThreshold": 3,
447 # "path": "/api/health",
450 # "initialDelaySeconds": 60,
451 # "timeoutSeconds": 30,
452 # "failureThreshold": 10,
457 # "kubernetesResources": {
458 # "ingressResources": [
460 # "name": "mysqld-exporter-ingress",
462 # "nginx.ingress.kubernetes.io/whitelist-source-range": "0.0.0.0/0",
467 # "host": "mysqld-exporter",
473 # "serviceName": "mysqld-exporter",
474 # "servicePort": 9104,
483 # "hosts": ["mysqld-exporter"],
484 # "secretName": "mysqld-exporter",
493 # self.harness.charm.on.start.emit()
495 # # Initializing the mysql relation
496 # relation_id = self.harness.add_relation("mysql", "mysql")
497 # self.harness.add_relation_unit(relation_id, "mysql/0")
498 # self.harness.update_relation_data(
505 # "password": "manopw",
506 # "root_password": "rootpw",
510 # self.harness.update_config(
512 # "site_url": "https://mysqld-exporter",
513 # "tls_secret_name": "mysqld-exporter",
514 # "ingress_whitelist_source_range": "0.0.0.0/0",
518 # pod_spec, _ = self.harness.get_pod_spec()
520 # self.assertDictEqual(expected_result, pod_spec)
522 # def test_on_mysql_unit_relation_changed(self) -> NoReturn:
523 # """Test to see if mysql relation is updated."""
524 # self.harness.charm.on.start.emit()
526 # relation_id = self.harness.add_relation("mysql", "mysql")
527 # self.harness.add_relation_unit(relation_id, "mysql/0")
528 # self.harness.update_relation_data(
535 # "password": "manopw",
536 # "root_password": "rootpw",
541 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
543 # def test_publish_target_info(self) -> NoReturn:
544 # """Test to see if target relation is updated."""
545 # expected_result = {
546 # "hostname": "mysqld-exporter",
548 # "metrics_path": "/metrics",
549 # "scrape_interval": "30s",
550 # "scrape_timeout": "15s",
553 # self.harness.charm.on.start.emit()
555 # relation_id = self.harness.add_relation("prometheus-scrape", "prometheus")
556 # self.harness.add_relation_unit(relation_id, "prometheus/0")
557 # relation_data = self.harness.get_relation_data(relation_id, "mysqld-exporter/0")
559 # self.assertDictEqual(expected_result, relation_data)
561 # def test_publish_scrape_info_with_site_url(self) -> NoReturn:
562 # """Test to see if target relation is updated."""
563 # expected_result = {
564 # "hostname": "mysqld-exporter-osm",
566 # "metrics_path": "/metrics",
567 # "scrape_interval": "30s",
568 # "scrape_timeout": "15s",
571 # self.harness.charm.on.start.emit()
573 # self.harness.update_config({"site_url": "http://mysqld-exporter-osm"})
575 # relation_id = self.harness.add_relation("prometheus-scrape", "prometheus")
576 # self.harness.add_relation_unit(relation_id, "prometheus/0")
577 # relation_data = self.harness.get_relation_data(relation_id, "mysqld-exporter/0")
579 # self.assertDictEqual(expected_result, relation_data)
581 # def test_publish_dashboard_info(self) -> NoReturn:
582 # """Test to see if dashboard relation is updated."""
583 # self.harness.charm.on.start.emit()
585 # relation_id = self.harness.add_relation("grafana-dashboard", "grafana")
586 # self.harness.add_relation_unit(relation_id, "grafana/0")
587 # relation_data = self.harness.get_relation_data(relation_id, "mysqld-exporter/0")
589 # self.assertTrue("dashboard" in relation_data)
590 # self.assertTrue(len(relation_data["dashboard"]) > 0)
591 # self.assertEqual(relation_data["name"], "osm-mysql")
594 # if __name__ == "__main__":