Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaAltieri committed Nov 21, 2023
1 parent eb35ecc commit b979e3b
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ website:
summary: A mongos operator charm
series:
- jammy
# subordinate: true
subordinate: true

peers:
router-peers:
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.
31 changes: 31 additions & 0 deletions tests/unit/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python3
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.

"""Helper functions for writing tests."""

from typing import Callable
from unittest.mock import patch


def patch_network_get(private_address="10.1.157.116") -> Callable:
def network_get(*args, **kwargs) -> dict:
"""Patch for the not-yet-implemented testing backend needed for `bind_address`.
This patch decorator can be used for cases such as:
self.model.get_binding(event.relation).network.bind_address
"""
return {
"bind-addresses": [
{
"mac-address": "",
"interface-name": "",
"addresses": [{"hostname": "", "value": private_address, "cidr": ""}],
}
],
"bind-address": private_address,
"egress-subnets": ["10.152.183.65/32"],
"ingress-addresses": ["10.152.183.65"],
}

return patch("ops.testing._TestingModelBackend.network_get", network_get)
30 changes: 30 additions & 0 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.

import unittest
from unittest.mock import patch

from charms.operator_libs_linux.v1 import snap
from ops.model import BlockedStatus
from ops.testing import Harness

from charm import MongosOperatorCharm

from .helpers import patch_network_get


class TestCharm(unittest.TestCase):
def setUp(self, *unused):
self.harness = Harness(MongosOperatorCharm)
self.addCleanup(self.harness.cleanup)
self.harness.begin()
self.peer_rel_id = self.harness.add_relation("router-peers", "router-peers")

@patch_network_get(private_address="1.1.1.1")
@patch("charm.snap.SnapCache")
@patch("subprocess.check_call")
def test_install_snap_packages_failure(self, _call, snap_cache):
"""Test verifies that install hook fails when a snap error occurs."""
snap_cache.side_effect = snap.SnapError
self.harness.charm.on.install.emit()
self.assertTrue(isinstance(self.harness.charm.unit.status, BlockedStatus))
15 changes: 15 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ commands =
ruff {[vars]all_path}
black --check --diff {[vars]all_path}

[testenv:unit]
description = Run unit tests
deps =
pytest
requests
pytest-mock
juju==3.2.0.1
coverage[toml]
parameterized
-r {tox_root}/requirements.txt
commands =
coverage run --source={[vars]src_path} \
-m pytest -v --tb native -s {posargs} {[vars]tests_path}/unit
coverage report


[testenv:static]
description = Run static type checks
Expand Down

0 comments on commit b979e3b

Please sign in to comment.