From 5b277c393e68b884568e1a2055eea2c93a29d120 Mon Sep 17 00:00:00 2001 From: Mia Altieri <32723809+MiaAltieri@users.noreply.github.com> Date: Tue, 18 Jun 2024 09:09:39 +0200 Subject: [PATCH] [DPE-4494] add unit tests for revision checker (#421) # Issue Code for version check has no unit tests. This is a requirement before publishing as a package # Solution Add unit tests for version check --- src/version_check.py | 2 +- tests/unit/test_version_check.py | 93 ++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 tests/unit/test_version_check.py diff --git a/src/version_check.py b/src/version_check.py index c5ed3c870..4099cdb40 100644 --- a/src/version_check.py +++ b/src/version_check.py @@ -128,7 +128,7 @@ def get_invalid_versions(self) -> List[Tuple[str, int]]: for relation in self.charm.model.relations[relation_name]: related_version = relation.data[relation.app][VERSION_CONST] if int(related_version) != self.version: - invalid_relations.append((relation.app.name, related_version)) + invalid_relations.append((relation.app.name, int(related_version))) except KeyError: raise NoVersionError(f"Expected {relation.app.name} to have version info.") diff --git a/tests/unit/test_version_check.py b/tests/unit/test_version_check.py new file mode 100644 index 000000000..63daf6152 --- /dev/null +++ b/tests/unit/test_version_check.py @@ -0,0 +1,93 @@ +# Copyright 2024 Canonical Ltd. +# See LICENSE file for licensing details. +import unittest +from unittest.mock import patch + +from ops.testing import Harness + +from charm import MongodbOperatorCharm +from version_check import DEPLOYMENT_TYPE, VERSION_CONST, NoVersionError + +from .helpers import patch_network_get + +CHARMHUB_DEPLOYMENT = "ch" +LOCAL_DEPLOYMENT = "local" +RELATION_TO_CHECK_VERSION = "sharding" +CHARM_VERSION = 123 +VALID_VERSION = CHARM_VERSION +INVALID_VERSION = 456 +APP_0 = "APP_0" +APP_1 = "APP_1" +APP_2 = "APP_2" + + +class TestCharm(unittest.TestCase): + @patch("charm.get_charm_revision") + @patch_network_get(private_address="1.1.1.1") + def setUp(self, get_charm_revision): + get_charm_revision.return_value = CHARM_VERSION + self.harness = Harness(MongodbOperatorCharm) + self.addCleanup(self.harness.cleanup) + self.harness.begin() + + def add_invalid_relation(self, deployment=LOCAL_DEPLOYMENT): + rel_id = self.harness.add_relation(RELATION_TO_CHECK_VERSION, APP_0) + self.harness.add_relation_unit(rel_id, f"{APP_0}/0") + self.harness.update_relation_data( + rel_id, + f"{APP_0}", + {VERSION_CONST: str(INVALID_VERSION), DEPLOYMENT_TYPE: deployment}, + ) + + def add_valid_relation(self, deployment=LOCAL_DEPLOYMENT): + rel_id = self.harness.add_relation(RELATION_TO_CHECK_VERSION, APP_1) + self.harness.add_relation_unit(rel_id, f"{APP_1}/0") + self.harness.update_relation_data( + rel_id, + f"{APP_1}", + {VERSION_CONST: str(VALID_VERSION), DEPLOYMENT_TYPE: deployment}, + ) + + def add_relation_with_no_version(self): + rel_id = self.harness.add_relation(RELATION_TO_CHECK_VERSION, RELATION_TO_CHECK_VERSION) + self.harness.add_relation_unit(rel_id, f"{APP_2}/0") + + def test_get_invalid_versions(self): + """Verifies that get invalid versions returns the expected content.""" + # case one: retrieves invalid versions + valid + self.add_invalid_relation() + self.add_valid_relation() + invalid_version = self.harness.charm.version_checker.get_invalid_versions() + assert invalid_version == [(APP_0, INVALID_VERSION)] + + # case two: missing version info + self.add_relation_with_no_version() + with self.assertRaises(NoVersionError): + self.harness.charm.version_checker.get_invalid_versions() + + def test_get_version_of_related_app(self): + """Verifies that version checker can retrieve integrated application versions.""" + # case one: get version + self.add_invalid_relation() + version = self.harness.charm.version_checker.get_version_of_related_app(APP_0) + assert version == INVALID_VERSION + + # case two: missing version info + self.add_relation_with_no_version() + with self.assertRaises(NoVersionError): + self.harness.charm.version_checker.get_version_of_related_app(APP_2) + + def test_is_related_app_locally_built_charm(self): + """Verifies that version checker can retrieve integrated application deployment types.""" + # case one: local deployment + self.add_valid_relation(deployment=LOCAL_DEPLOYMENT) + assert self.harness.charm.version_checker.is_local_charm(APP_1) + + # case two: charmhub deployment + self.add_invalid_relation(deployment=CHARMHUB_DEPLOYMENT) + assert not self.harness.charm.version_checker.is_local_charm(APP_0) + + # case three: missing version info + self.add_relation_with_no_version() + with self.assertRaises(NoVersionError): + self.harness.charm.version_checker.is_local_charm(APP_2)