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 MysqldExporterCharm
32 class TestCharm(unittest
.TestCase
):
33 """Mysql Exporter Charm unit tests."""
35 def setUp(self
) -> NoReturn
:
37 self
.harness
= Harness(MysqldExporterCharm
)
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("mysql", 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": "mysqld-exporter",
63 "imageDetails": self
.harness
.charm
.image
.fetch(),
64 "imagePullPolicy": "Always",
67 "name": "mysqld-exporter",
68 "containerPort": 9104,
72 "envConfig": {"DATA_SOURCE_NAME": "root:rootpw@(mysql:3306)/"},
76 "path": "/api/health",
79 "initialDelaySeconds": 10,
82 "successThreshold": 1,
83 "failureThreshold": 3,
87 "path": "/api/health",
90 "initialDelaySeconds": 60,
92 "failureThreshold": 10,
97 "kubernetesResources": {"ingressResources": []},
100 self
.harness
.charm
.on
.start
.emit()
102 # Initializing the mysql relation
103 relation_id
= self
.harness
.add_relation("mysql", "mysql")
104 self
.harness
.add_relation_unit(relation_id
, "mysql/0")
105 self
.harness
.update_relation_data(
112 "password": "manopw",
113 "root_password": "rootpw",
118 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
120 pod_spec
, _
= self
.harness
.get_pod_spec()
122 self
.assertDictEqual(expected_result
, pod_spec
)
124 def test_ingress_resources_with_http(self
) -> NoReturn
:
125 """Test ingress resources with HTTP."""
130 "name": "mysqld-exporter",
131 "imageDetails": self
.harness
.charm
.image
.fetch(),
132 "imagePullPolicy": "Always",
135 "name": "mysqld-exporter",
136 "containerPort": 9104,
140 "envConfig": {"DATA_SOURCE_NAME": "root:rootpw@(mysql:3306)/"},
144 "path": "/api/health",
147 "initialDelaySeconds": 10,
150 "successThreshold": 1,
151 "failureThreshold": 3,
155 "path": "/api/health",
158 "initialDelaySeconds": 60,
159 "timeoutSeconds": 30,
160 "failureThreshold": 10,
165 "kubernetesResources": {
166 "ingressResources": [
168 "name": "mysqld-exporter-ingress",
170 "nginx.ingress.kubernetes.io/ssl-redirect": "false",
175 "host": "mysqld-exporter",
181 "serviceName": "mysqld-exporter",
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",
212 self
.harness
.update_config({"site_url": "http://mysqld-exporter"})
214 pod_spec
, _
= self
.harness
.get_pod_spec()
216 self
.assertDictEqual(expected_result
, pod_spec
)
218 def test_ingress_resources_with_https(self
) -> NoReturn
:
219 """Test ingress resources with HTTPS."""
224 "name": "mysqld-exporter",
225 "imageDetails": self
.harness
.charm
.image
.fetch(),
226 "imagePullPolicy": "Always",
229 "name": "mysqld-exporter",
230 "containerPort": 9104,
234 "envConfig": {"DATA_SOURCE_NAME": "root:rootpw@(mysql:3306)/"},
238 "path": "/api/health",
241 "initialDelaySeconds": 10,
244 "successThreshold": 1,
245 "failureThreshold": 3,
249 "path": "/api/health",
252 "initialDelaySeconds": 60,
253 "timeoutSeconds": 30,
254 "failureThreshold": 10,
259 "kubernetesResources": {
260 "ingressResources": [
262 "name": "mysqld-exporter-ingress",
267 "host": "mysqld-exporter",
273 "serviceName": "mysqld-exporter",
283 "hosts": ["mysqld-exporter"],
284 "secretName": "mysqld-exporter",
293 self
.harness
.charm
.on
.start
.emit()
295 # Initializing the mysql relation
296 relation_id
= self
.harness
.add_relation("mysql", "mysql")
297 self
.harness
.add_relation_unit(relation_id
, "mysql/0")
298 self
.harness
.update_relation_data(
305 "password": "manopw",
306 "root_password": "rootpw",
310 self
.harness
.update_config(
312 "site_url": "https://mysqld-exporter",
313 "tls_secret_name": "mysqld-exporter",
317 pod_spec
, _
= self
.harness
.get_pod_spec()
319 self
.assertDictEqual(expected_result
, pod_spec
)
321 def test_ingress_resources_with_https_and_ingress_whitelist(self
) -> NoReturn
:
322 """Test ingress resources with HTTPS and ingress whitelist."""
327 "name": "mysqld-exporter",
328 "imageDetails": self
.harness
.charm
.image
.fetch(),
329 "imagePullPolicy": "Always",
332 "name": "mysqld-exporter",
333 "containerPort": 9104,
337 "envConfig": {"DATA_SOURCE_NAME": "root:rootpw@(mysql:3306)/"},
341 "path": "/api/health",
344 "initialDelaySeconds": 10,
347 "successThreshold": 1,
348 "failureThreshold": 3,
352 "path": "/api/health",
355 "initialDelaySeconds": 60,
356 "timeoutSeconds": 30,
357 "failureThreshold": 10,
362 "kubernetesResources": {
363 "ingressResources": [
365 "name": "mysqld-exporter-ingress",
367 "nginx.ingress.kubernetes.io/whitelist-source-range": "0.0.0.0/0",
372 "host": "mysqld-exporter",
378 "serviceName": "mysqld-exporter",
388 "hosts": ["mysqld-exporter"],
389 "secretName": "mysqld-exporter",
398 self
.harness
.charm
.on
.start
.emit()
400 # Initializing the mysql relation
401 relation_id
= self
.harness
.add_relation("mysql", "mysql")
402 self
.harness
.add_relation_unit(relation_id
, "mysql/0")
403 self
.harness
.update_relation_data(
410 "password": "manopw",
411 "root_password": "rootpw",
415 self
.harness
.update_config(
417 "site_url": "https://mysqld-exporter",
418 "tls_secret_name": "mysqld-exporter",
419 "ingress_whitelist_source_range": "0.0.0.0/0",
423 pod_spec
, _
= self
.harness
.get_pod_spec()
425 self
.assertDictEqual(expected_result
, pod_spec
)
427 def test_on_mysql_unit_relation_changed(self
) -> NoReturn
:
428 """Test to see if mysql relation is updated."""
429 self
.harness
.charm
.on
.start
.emit()
431 relation_id
= self
.harness
.add_relation("mysql", "mysql")
432 self
.harness
.add_relation_unit(relation_id
, "mysql/0")
433 self
.harness
.update_relation_data(
440 "password": "manopw",
441 "root_password": "rootpw",
446 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
448 def test_publish_target_info(self
) -> NoReturn
:
449 """Test to see if target relation is updated."""
451 "hostname": "mysqld-exporter",
453 "metrics_path": "/metrics",
454 "scrape_interval": "30s",
455 "scrape_timeout": "15s",
458 self
.harness
.charm
.on
.start
.emit()
460 relation_id
= self
.harness
.add_relation("prometheus-scrape", "prometheus")
461 self
.harness
.add_relation_unit(relation_id
, "prometheus/0")
462 relation_data
= self
.harness
.get_relation_data(relation_id
, "mysqld-exporter/0")
464 self
.assertDictEqual(expected_result
, relation_data
)
466 def test_publish_scrape_info_with_site_url(self
) -> NoReturn
:
467 """Test to see if target relation is updated."""
469 "hostname": "mysqld-exporter-osm",
471 "metrics_path": "/metrics",
472 "scrape_interval": "30s",
473 "scrape_timeout": "15s",
476 self
.harness
.charm
.on
.start
.emit()
478 self
.harness
.update_config({"site_url": "http://mysqld-exporter-osm"})
480 relation_id
= self
.harness
.add_relation("prometheus-scrape", "prometheus")
481 self
.harness
.add_relation_unit(relation_id
, "prometheus/0")
482 relation_data
= self
.harness
.get_relation_data(relation_id
, "mysqld-exporter/0")
484 self
.assertDictEqual(expected_result
, relation_data
)
486 def test_publish_dashboard_info(self
) -> NoReturn
:
487 """Test to see if dashboard relation is updated."""
488 self
.harness
.charm
.on
.start
.emit()
490 relation_id
= self
.harness
.add_relation("grafana-dashboard", "grafana")
491 self
.harness
.add_relation_unit(relation_id
, "grafana/0")
492 relation_data
= self
.harness
.get_relation_data(relation_id
, "mysqld-exporter/0")
494 self
.assertTrue("dashboard" in relation_data
)
495 self
.assertTrue(len(relation_data
["dashboard"]) > 0)
496 self
.assertEqual(relation_data
["name"], "osm-mysql")
499 if __name__
== "__main__":