Skip to content

Commit

Permalink
Unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
juditnovak committed Jan 11, 2024
1 parent 839e7e3 commit 7139e68
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
15 changes: 15 additions & 0 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.

from unittest.mock import PropertyMock

import pytest


@pytest.fixture(autouse=True)
def ensure_secrets(mocker):
mocker.patch(
"charms.data_platform_libs.v0.data_interfaces.JujuVersion.has_secrets",
new_callable=PropertyMock,
return_value=True,
)
55 changes: 39 additions & 16 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@

import pytest
from charms.operator_libs_linux.v1 import snap
from ops.model import ActiveStatus, BlockedStatus, MaintenanceStatus, WaitingStatus
from ops.model import (
ActiveStatus,
BlockedStatus,
MaintenanceStatus,
RelationDataTypeError,
WaitingStatus,
)
from ops.testing import Harness
from parameterized import parameterized
from pymongo.errors import ConfigurationError, ConnectionFailure, OperationFailure
Expand Down Expand Up @@ -724,6 +730,7 @@ def test_set_reset_existing_password_app(self):
self._setup_secrets()

# Getting current password
self.harness.set_leader(True)
self.harness.charm.set_secret("app", "monitor-password", "bla")
assert self.harness.charm.get_secret("app", "monitor-password") == "bla"

Expand All @@ -739,6 +746,7 @@ def test_set_secret_returning_secret_id(self, scope):
def test_set_reset_new_secret(self, scope):
"""NOTE: currently ops.testing seems to allow for non-leader to set secrets too!"""
# Getting current password
self.harness.set_leader(True)
self.harness.charm.set_secret(scope, "new-secret", "bla")
assert self.harness.charm.get_secret(scope, "new-secret") == "bla"

Expand All @@ -752,7 +760,7 @@ def test_set_reset_new_secret(self, scope):

@parameterized.expand([("app"), ("unit")])
def test_invalid_secret(self, scope):
with self.assertRaises(TypeError):
with self.assertRaises(RelationDataTypeError):
self.harness.charm.set_secret("unit", "somekey", 1)

self.harness.charm.set_secret("unit", "somekey", "")
Expand All @@ -762,45 +770,57 @@ def test_invalid_secret(self, scope):
def test_delete_password(self):
"""NOTE: currently ops.testing seems to allow for non-leader to remove secrets too!"""
self._setup_secrets()
self.harness.set_leader(True)

assert self.harness.charm.get_secret("app", "monitor-password")
self.harness.charm.remove_secret("app", "monitor-password")
assert self.harness.charm.get_secret("app", "monitor-password") is None

assert self.harness.charm.set_secret("unit", "somekey", "somesecret")
self.harness.charm.remove_secret("unit", "somekey")
assert self.harness.charm.get_secret("unit", "somekey") is None
assert self.harness.charm.set_secret("unit", "ca-secret", "somesecret")
self.harness.charm.remove_secret("unit", "ca-secret")
assert self.harness.charm.get_secret("unit", "ca-secret") is None

with self._caplog.at_level(logging.ERROR):
self.harness.charm.remove_secret("app", "monitor-password")
assert (
"Non-existing secret app:monitor-password was attempted to be removed."
"Non-existing secret {'monitor-password'} was attempted to be removed."
in self._caplog.text
)

self.harness.charm.remove_secret("unit", "somekey")
self.harness.charm.remove_secret("unit", "ca-secret")
assert (
"Non-existing secret unit:somekey was attempted to be removed."
"Non-existing secret {'ca-secret'} was attempted to be removed."
in self._caplog.text
)

self.harness.charm.remove_secret("app", "non-existing-secret")
assert (
"Non-existing secret app:non-existing-secret was attempted to be removed."
"Non-existing field 'non-existing-secret' was attempted to be removed"
in self._caplog.text
)

self.harness.charm.remove_secret("unit", "non-existing-secret")
self.harness.charm.remove_secret("unit", "non-existing-secret-unit")
assert (
"Non-existing secret unit:non-existing-secret was attempted to be removed."
"Non-existing field 'non-existing-secret-unit' was attempted to be removed"
in self._caplog.text
)

@parameterized.expand([("app"), ("unit")])
@patch("charm.MongodbOperatorCharm._connect_mongodb_exporter")
def test_on_secret_changed(self, scope, connect_exporter):
def test_on_secret_changed(self, connect_exporter):
"""NOTE: currently ops.testing seems to allow for non-leader to set secrets too!"""
self.harness.set_leader(True)
secret_label = self.harness.charm.set_secret("app", "operator-password", "bla")
secret = self.harness.charm.model.get_secret(label=secret_label)

event = mock.Mock()
event.secret = secret
secret_label = self.harness.charm._on_secret_changed(event)
connect_exporter.assert_called()

@patch("charm.MongodbOperatorCharm._connect_mongodb_exporter")
def test_on_secret_changed_unit(self, connect_exporter):
"""NOTE: currently ops.testing seems to allow for non-leader to set secrets too!"""
secret_label = self.harness.charm.set_secret(scope, "new-secret", "bla")
secret_label = self.harness.charm.set_secret("unit", "ca-secret", "bla")
secret = self.harness.charm.model.get_secret(label=secret_label)

event = mock.Mock()
Expand All @@ -815,14 +835,17 @@ def test_on_other_secret_changed(self, scope, connect_exporter):
"""NOTE: currently ops.testing seems to allow for non-leader to set secrets too!"""
# "Hack": creating a secret outside of the normal MongodbOperatorCharm.set_secret workflow
scope_obj = self.harness.charm._scope_obj(scope)
secret = scope_obj.add_secret({"key": "value"})
secret = scope_obj.add_secret({"key": "value"}, label="some-label")

event = mock.Mock()
event.secret = secret

with self._caplog.at_level(logging.DEBUG):
self.harness.charm._on_secret_changed(event)
assert f"Secret {secret.id} changed, but it's unknown" in self._caplog.text
assert (
f"Secret ID: {secret.id} label: some-label changed, but it's unknown"
in self._caplog.text
)

connect_exporter.assert_not_called()

Expand Down

0 comments on commit 7139e68

Please sign in to comment.