Skip to content

Commit

Permalink
configure database path (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
Abuelodelanada committed Mar 26, 2024
1 parent a708cb7 commit 6caa776
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
DATABASE = "database"
PEER = "grafana"
PORT = 3000
DATABASE_PATH = "/var/lib/grafana/grafana.db"

# Template for storing trusted certificate in a file.
TRUSTED_CA_TEMPLATE = string.Template(
Expand Down Expand Up @@ -378,7 +379,7 @@ def _configure_replication(self) -> None:
):
restart = True

litestream_config = {"addr": ":9876", "dbs": [{"path": "/var/lib/grafana/grafana.db"}]}
litestream_config = {"addr": ":9876", "dbs": [{"path": DATABASE_PATH}]}

if not leader:
litestream_config["dbs"][0].update({"upstream": {"url": "http://${LITESTREAM_UPSTREAM_URL}"}}) # type: ignore
Expand Down Expand Up @@ -754,6 +755,16 @@ def _generate_grafana_config(self) -> str:
configs = []
if self.has_db:
configs.append(self._generate_database_config())
else:
with StringIO() as data:
config_ini = configparser.ConfigParser()
config_ini["database"] = {
"type": "sqlite3",
"path": DATABASE_PATH,
}
config_ini.write(data)
data.seek(0)
configs.append(data.read())

return "\n".join(configs)

Expand Down Expand Up @@ -874,7 +885,7 @@ def restart_grafana(self) -> None:
pragma = self.containers["workload"].exec(
[
"/usr/local/bin/sqlite3",
"/var/lib/grafana/grafana.db",
DATABASE_PATH,
"pragma journal_mode=wal;",
]
)
Expand Down
38 changes: 38 additions & 0 deletions tests/integration/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,44 @@ async def check_grafana_is_ready(ops_test: OpsTest, app_name: str, unit_num: int
return is_ready


async def create_org(ops_test: OpsTest, app_name: str, unit_num: int, org_name: str) -> dict:
"""Create Organisation.
Args:
ops_test: pytest-operator plugin
app_name: string name of Grafana application
unit_num: integer number of a Grafana juju unit
org_name: string name of Org.
Returns:
Oranisation created.
"""
host = await unit_address(ops_test, app_name, unit_num)
pw = await grafana_password(ops_test, app_name)
grafana = Grafana(host=host, pw=pw)
org = await grafana.create_org(name=org_name)
return org


async def get_org(ops_test: OpsTest, app_name: str, unit_num: int, org_name: str) -> dict:
"""Fetch Organisation.
Args:
ops_test: pytest-operator plugin
app_name: string name of Grafana application
unit_num: integer number of a Grafana juju unit
org_name: string name of Org.
Returns:
Oranisation.
"""
host = await unit_address(ops_test, app_name, unit_num)
pw = await grafana_password(ops_test, app_name)
grafana = Grafana(host=host, pw=pw)
org = await grafana.fetch_org(name=org_name)
return org


async def get_grafana_settings(ops_test: OpsTest, app_name: str, unit_num: int) -> dict:
"""Fetch Grafana settings.
Expand Down
8 changes: 8 additions & 0 deletions tests/integration/test_kubectl_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import pytest
from helpers import (
check_grafana_is_ready,
create_org,
get_config_values,
get_dashboard_by_search,
get_datasource_for,
get_grafana_datasources,
get_org,
oci_image,
uk8s_group,
)
Expand Down Expand Up @@ -84,6 +86,9 @@ async def test_create_and_check_datasource_and_dashboard_before_delete(ops_test)


async def test_config_values_are_retained_after_pod_deleted_and_restarted(ops_test):
org_name = "D10S"
await create_org(ops_test, grafana_app_name, 0, org_name)

pod_name = f"{grafana_app_name}-0"

cmd = [
Expand All @@ -108,6 +113,9 @@ async def test_config_values_are_retained_after_pod_deleted_and_restarted(ops_te
await check_grafana_is_ready(ops_test, grafana_app_name, 0)
assert (await get_config_values(ops_test, grafana_app_name)).items() >= config.items()

org = await get_org(ops_test, grafana_app_name, 0, "D10S")
assert org["name"] == org_name


async def test_dashboards_and_datasources_are_retained_after_pod_deleted_and_restarted(ops_test):
await check_grafana_is_ready(ops_test, grafana_app_name, 0)
Expand Down
26 changes: 26 additions & 0 deletions tests/integration/workload.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,29 @@ async def fetch_dashboard(self, dashboard_uid: str) -> dict:
async with session.get(uri) as response:
result = await response.json()
return result if response.status == 200 else {}

async def fetch_org(self, name: str) -> dict:
"""Get the JSON representation of orgs.
Returns:
Organisation.
"""
api_path = f"/api/orgs/name/{name}"
uri = f"{self.base_uri}{api_path}"
async with aiohttp.ClientSession(headers=self.headers) as session:
async with session.get(uri) as response:
result = await response.json()
return result if response.status == 200 else {}

async def create_org(self, name: str) -> dict:
"""Create org.
Returns:
Dict containing the orgId.
"""
api_path = "/api/orgs"
uri = f"{self.base_uri}{api_path}"
async with aiohttp.ClientSession(headers=self.headers) as session:
async with session.post(uri, json={"name": name}) as response:
result = await response.json()
return result if response.status == 200 else {}

0 comments on commit 6caa776

Please sign in to comment.