From 79ad1e50411bb06c965ce549e4852b30d75a9c92 Mon Sep 17 00:00:00 2001 From: phvalguima Date: Fri, 8 Mar 2024 19:01:48 +0100 Subject: [PATCH] [DPE-3647] Pin snap to rev37 (2.10.0-ubuntu1) (#197) Add snap pinning and make sure we are installing the correct version in integration tests. Closes #123 --- src/opensearch.py | 11 ++++++++--- tests/integration/helpers.py | 1 + tests/integration/test_charm.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/opensearch.py b/src/opensearch.py index 0db342b0d..421e75616 100644 --- a/src/opensearch.py +++ b/src/opensearch.py @@ -34,6 +34,9 @@ logger = logging.getLogger(__name__) +OPENSEARCH_SNAP_REVISION = 37 + + class OpenSearchSnap(OpenSearchDistribution): """Snap distribution of opensearch, only overrides properties and logic proper to the snap.""" @@ -58,11 +61,13 @@ def __init__(self, charm, peer_relation: str): @override def install(self): """Install opensearch from the snapcraft store.""" - if self._opensearch.present: - return try: - self._opensearch.ensure(snap.SnapState.Latest, channel="edge") + self._opensearch.ensure(snap.SnapState.Latest, revision=OPENSEARCH_SNAP_REVISION) self._opensearch.connect("process-control") + if not self._opensearch.held: + # hold the snap in charm determined revision + self._opensearch.hold() + except SnapError as e: logger.error(f"Failed to install opensearch. \n{e}") raise OpenSearchInstallError() diff --git a/tests/integration/helpers.py b/tests/integration/helpers.py index aa6aeee72..55c19eb00 100644 --- a/tests/integration/helpers.py +++ b/tests/integration/helpers.py @@ -29,6 +29,7 @@ METADATA = yaml.safe_load(Path("./metadata.yaml").read_text()) APP_NAME = METADATA["name"] +EXPECTED_SNAP_REVISION = 37 SERIES = "jammy" UNIT_IDS = [0, 1, 2] IDLE_PERIOD = 75 diff --git a/tests/integration/test_charm.py b/tests/integration/test_charm.py index ee28eacc7..8a4b550f2 100644 --- a/tests/integration/test_charm.py +++ b/tests/integration/test_charm.py @@ -3,12 +3,15 @@ # See LICENSE file for licensing details. import logging +import subprocess import pytest +import yaml from pytest_operator.plugin import OpsTest from .helpers import ( APP_NAME, + EXPECTED_SNAP_REVISION, MODEL_CONFIG, SERIES, get_admin_secrets, @@ -141,3 +144,31 @@ async def test_actions_rotate_admin_password(ops_test: OpsTest) -> None: ops_test, "GET", test_url, resp_status_code=True, user_password=password1 ) assert http_resp_code == 401 + + +@pytest.mark.group(1) +@pytest.mark.abort_on_fail +async def test_check_pinned_revision(ops_test: OpsTest) -> None: + """Test check the pinned revision.""" + leader_id = await get_leader_unit_id(ops_test) + + installed_info = yaml.safe_load( + subprocess.check_output( + [ + "juju", + "ssh", + f"opensearch/{leader_id}", + "--", + "sudo", + "snap", + "info", + "opensearch", + "--color=never", + "--unicode=always", + ], + text=True, + ).replace("\r\n", "\n") + )["installed"].split() + logger.info(f"Installed snap: {installed_info}") + assert installed_info[1] == f"({EXPECTED_SNAP_REVISION})" + assert installed_info[3] == "held"