Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add workload version to juju status #18

Merged
merged 19 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ description: |
A charm for the matrix synapse chat server.
Synapse is a drop in replacement for other chat servers like Mattermost and Slack.
This charm is useful if you want to spin up your own chat instance.
docs: ""
amandahla marked this conversation as resolved.
Show resolved Hide resolved
issues: https://github.com/canonical/synapse-operator/issues
maintainers:
- launchpad.net/~canonical-is-devops
source: https://github.com/canonical/synapse-operator
docs: "https://discourse.charmhub.io/t/synapse-documentation-overview/11358"
assumes:
- k8s-api

Expand Down
15 changes: 15 additions & 0 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from ops.main import main

import actions
import synapse
from charm_state import CharmConfigInvalidError, CharmState
from constants import SYNAPSE_CONTAINER_NAME, SYNAPSE_PORT
from database_observer import DatabaseObserver
Expand Down Expand Up @@ -79,13 +80,27 @@ def change_config(self, _: ops.HookEvent) -> None:
return
self.model.unit.status = ops.ActiveStatus()

def _set_workload_version(self) -> None:
"""Set workload version with Synapse version."""
container = self.unit.get_container(SYNAPSE_CONTAINER_NAME)
if not container.can_connect():
self.unit.status = ops.MaintenanceStatus("Waiting for pebble")
return
try:
synapse_version = synapse.get_version()
self.unit.set_workload_version(synapse_version)
except synapse.APIError as exc:
logger.debug("Cannot set workload version at this time: %s", exc)

def _on_config_changed(self, event: ops.HookEvent) -> None:
"""Handle changed configuration.

Args:
event: Event triggering after config is changed.
"""
self.change_config(event)
logger.debug("Setting workload in config-changed event")
self._set_workload_version()

def _on_pebble_ready(self, event: ops.HookEvent) -> None:
"""Handle pebble ready event.
Expand Down
1 change: 1 addition & 0 deletions src/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""This module defines constants used throughout the Synapse application."""

CHECK_READY_NAME = "synapse-ready"
CHECK_ALIVE_NAME = "synapse-alive"
COMMAND_MIGRATE_CONFIG = "migrate_config"
PROMETHEUS_TARGET_PORT = "9000"
SYNAPSE_CONFIG_DIR = "/data"
Expand Down
4 changes: 2 additions & 2 deletions src/database_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def prepare(self) -> None:
).format(sql.Literal(self._database_name))
)
except psycopg2.Error as exc:
logger.error("Failed to prepare database: %s", str(exc))
logger.exception("Failed to prepare database: %r", exc)
raise
finally:
self._close()
Expand All @@ -111,7 +111,7 @@ def erase(self) -> None:
).format(sql.Identifier(self._database_name))
)
except psycopg2.Error as exc:
logger.error("Failed to erase database: %s", str(exc))
logger.exception("Failed to erase database: %r", exc)
raise
finally:
self._close()
2 changes: 2 additions & 0 deletions src/pebble.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import synapse
from charm_state import CharmState
from constants import (
CHECK_ALIVE_NAME,
CHECK_READY_NAME,
SYNAPSE_COMMAND_PATH,
SYNAPSE_CONTAINER_NAME,
Expand Down Expand Up @@ -115,6 +116,7 @@ def _pebble_layer(self) -> ops.pebble.LayerDict:
},
"checks": {
CHECK_READY_NAME: synapse.check_ready(),
CHECK_ALIVE_NAME: synapse.check_alive(),
},
}
return typing.cast(ops.pebble.LayerDict, layer)
Expand Down
3 changes: 2 additions & 1 deletion src/synapse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
"""Synapse package is used to interact with Synapse instance."""

# Exporting methods to be used for another modules
from .api import APIError, register_user # noqa: F401
from .api import APIError, get_version, register_user # noqa: F401
from .workload import ( # noqa: F401
ExecResult,
WorkloadError,
check_alive,
check_ready,
enable_metrics,
execute_migrate_config,
Expand Down
49 changes: 47 additions & 2 deletions src/synapse/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@
import hashlib
import hmac
import logging
import re
import typing

import requests
from requests import Session
from requests.adapters import HTTPAdapter
from urllib3.util import Retry

from user import User

logger = logging.getLogger(__name__)

SYNAPSE_URL = "http://localhost:8008"
REGISTER_URL = f"{SYNAPSE_URL}/_synapse/admin/v1/register"
VERSION_URL = f"{SYNAPSE_URL}/_synapse/admin/v1/server_version"


class APIError(Exception):
Expand Down Expand Up @@ -83,7 +88,7 @@ def register_user(registration_shared_secret: str, user: User) -> None:
requests.exceptions.Timeout,
requests.exceptions.HTTPError,
) as exc:
logger.error("Failed to request %s : %s", REGISTER_URL, exc)
logger.exception("Failed to request %s : %r", REGISTER_URL, exc)
raise NetworkError(f"Failed to request {REGISTER_URL}.") from exc


Expand Down Expand Up @@ -147,5 +152,45 @@ def _get_nonce() -> str:
requests.exceptions.Timeout,
requests.exceptions.HTTPError,
) as exc:
logger.error("Failed to request %s : %s", REGISTER_URL, exc)
logger.exception("Failed to request %s : %r", REGISTER_URL, exc)
raise NetworkError(f"Failed to request {REGISTER_URL}.") from exc


def get_version() -> str:
"""Get version.

Expected API output:
{
"server_version": "0.99.2rc1 (b=develop, abcdef123)",
"python_version": "3.7.8"
}

We're using retry here because after the config change, Synapse is restarted.

Returns:
The version returned by Synapse API.

Raises:
NetworkError: if there was an error fetching the version.
"""
try:
session = Session()
retries = Retry(
total=3,
backoff_factor=3,
)
session.mount("http://", HTTPAdapter(max_retries=retries))
res = session.get(VERSION_URL, timeout=10)
res.raise_for_status()
server_version = res.json()["server_version"]
amandahla marked this conversation as resolved.
Show resolved Hide resolved
version_match = re.search(r"^([^\s(]+)", server_version)
if version_match:
return version_match.group(1)
return server_version
except (
requests.exceptions.ConnectionError,
requests.exceptions.Timeout,
requests.exceptions.HTTPError,
) as exc:
logger.exception("Failed to request %s : %r", VERSION_URL, exc)
raise NetworkError(f"Failed to request {VERSION_URL}.") from exc
24 changes: 19 additions & 5 deletions src/synapse/workload.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,28 @@ class ExecResult(typing.NamedTuple):


def check_ready() -> typing.Dict:
"""Return the Synapse container check.
"""Return the Synapse container ready check.

Returns:
Dict: check object converted to its dict representation.
"""
check = Check(CHECK_READY_NAME)
check.override = "replace"
check.level = "ready"
check.http = {"url": f"http://localhost:{SYNAPSE_PORT}/_synapse/admin/v1/server_version"}
amandahla marked this conversation as resolved.
Show resolved Hide resolved
# _CheckDict cannot be imported
return check.to_dict() # type: ignore
amandahla marked this conversation as resolved.
Show resolved Hide resolved


def check_alive() -> typing.Dict:
"""Return the Synapse container alive check.

Returns:
Dict: check object converted to its dict representation.
"""
check = Check(CHECK_READY_NAME)
check.override = "replace"
check.level = "alive"
check.tcp = {"port": SYNAPSE_PORT}
# _CheckDict cannot be imported
return check.to_dict() # type: ignore
Expand Down Expand Up @@ -167,8 +181,8 @@ def reset_instance(container: ops.Container) -> None:
if "device or resource busy" in str(path_error):
pass
else:
logger.error(
"exception while erasing directory %s: %s", SYNAPSE_CONFIG_DIR, path_error
logger.exception(
"exception while erasing directory %s: %r", SYNAPSE_CONFIG_DIR, path_error
)
raise

Expand Down Expand Up @@ -275,8 +289,8 @@ def _get_configuration_field(container: ops.Container, fieldname: str) -> typing
SYNAPSE_CONFIG_PATH,
)
return None
logger.error(
"exception while reading configuration file %s: %s",
logger.exception(
"exception while reading configuration file %s: %r",
SYNAPSE_CONFIG_PATH,
path_error,
)
Expand Down
26 changes: 26 additions & 0 deletions tests/integration/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
# See LICENSE file for licensing details.

"""Integration tests for Synapse charm."""
import json
import logging
import re
import typing

import pytest
Expand Down Expand Up @@ -230,3 +232,27 @@ async def test_register_user_action(
)
assert response.status_code == 200
assert response.json()["access_token"]


async def test_workload_version(
ops_test: OpsTest,
synapse_app: Application,
get_unit_ips: typing.Callable[[str], typing.Awaitable[tuple[str, ...]]],
) -> None:
"""
arrange: a deployed Synapse charm.
act: get status from Juju.
assert: the workload version is set and match the one given by Synapse API request.
"""
_, status, _ = await ops_test.juju("status", "--format", "json")
status = json.loads(status)
juju_workload_version = status["applications"][synapse_app.name].get("version", "")
assert juju_workload_version
for unit_ip in await get_unit_ips(synapse_app.name):
res = requests.get(
f"http://{unit_ip}:{SYNAPSE_PORT}/_synapse/admin/v1/server_version", timeout=5
)
server_version = res.json()["server_version"]
version_match = re.search(r"^([^\s(]+)", server_version)
assert version_match
assert version_match.group(1) == juju_workload_version
3 changes: 2 additions & 1 deletion tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ def start_cmd_handler(argv: list[str]) -> synapse.ExecResult:


@pytest.fixture(name="harness_server_name_configured")
def harness_server_name_configured_fixture(harness: Harness) -> Harness:
def harness_server_name_configured_fixture(harness: Harness, monkeypatch) -> Harness:
"""Ops testing framework harness fixture with server_name already configured."""
monkeypatch.setattr(synapse, "get_version", lambda *_args, **_kwargs: "")
harness.disable_hooks()
harness.update_config({"server_name": TEST_SERVER_NAME})
harness.enable_hooks()
Expand Down
43 changes: 38 additions & 5 deletions tests/unit/test_synapse_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ def test_generate_mac():
@mock.patch("synapse.api.requests")
def test_get_nonce_success(mock_requests):
"""
arrange: set User parameters.
act: register the user.
assert: parameters are passed correctly.
arrange: none.
amandahla marked this conversation as resolved.
Show resolved Hide resolved
act: get nonce.
assert: _get_nonce return the correct value.
"""
nonce_value = "nonce_value"
mock_response = mock.MagicMock()
Expand All @@ -102,12 +102,45 @@ def test_get_nonce_success(mock_requests):

def test_get_nonce_error(monkeypatch: pytest.MonkeyPatch):
"""
arrange: set User parameters.
act: register the user.
arrange: none.
amandahla marked this conversation as resolved.
Show resolved Hide resolved
act: get nonce.
assert: NetworkError is raised.
"""
mock_response_error = requests.exceptions.ConnectionError("Connection error")
mock_response = mock.Mock(side_effect=mock_response_error)
monkeypatch.setattr("synapse.api.requests.get", mock_response)
with pytest.raises(synapse.APIError, match="Failed to request"):
synapse.api._get_nonce()


@mock.patch("synapse.api.Session")
def test_get_version_success(mock_session):
amandahla marked this conversation as resolved.
Show resolved Hide resolved
"""
arrange: none.
act: get version.
assert: get_version return the correct value.
"""
extracted_version = "0.99.2rc1"
mock_session_instance = mock_session.return_value
mock_response = mock.Mock()
mock_response.json.return_value = {
"server_version": f"{extracted_version} (b=develop, abcdef123)"
}
mock_session_instance.get.return_value = mock_response
assert synapse.api.get_version() == extracted_version


@mock.patch("synapse.api.Session")
def test_get_version_error(mock_session):
"""
arrange: none.
act: get version.
assert: NetworkError is raised.
"""
mock_session_instance = mock_session.return_value
mock_response = mock.Mock()
mock_response_error = requests.exceptions.ConnectionError("Connection error")
mock_response.json.side_effect = mock_response_error
mock_session_instance.get.return_value = mock_response
with pytest.raises(synapse.APIError, match="Failed to request"):
synapse.api.get_version()