-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb35ecc
commit b979e3b
Showing
5 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Copyright 2023 Canonical Ltd. | ||
# See LICENSE file for licensing details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters