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

CSS-6116 Add backup and restore integration test to Ranger charm #22

Merged
merged 6 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,44 @@ juju relate ranger-k8s nginx-ingress-integrator

Once deployed, the hostname will default to the name of the application (ranger-k8s), and can be configured using the external-hostname configuration on the ranger operator.

## Backup and restore
### Setting up storage
Apache Ranger is a stateless application, all of the metadata is stored in the PostgreSQL relation. Therefore backup and restore is achieved through backup and restoration of this data. A requirement for this is an [AWS S3 bucket](https://aws.amazon.com/s3/) for use with the [S3 integrator charm](https://charmhub.io/s3-integrator).

```
# Deploy the s3-integrator charm
juju deploy s3-integrator
# Provide S3 credentials
juju run s3-integrator/leader sync-s3-credentials access-key=<your_key> secret-key=<your_secret_key>
# Configure the s3-integrator
juju config s3-integrator \
endpoint="https://s3.eu-west-2.amazonaws.com" \
bucket="ranger-backup-bucket-1" \
path="/ranger-backup" \
region="eu-west-2"
# Relate postgres
juju relate s3-integratior postgresql-k8s
```

More details and configuration values can be found in the [documentation for the PostgreSQL K8s charm](https://charmhub.io/postgresql-k8s/docs/h-configure-s3-aws)

### Create and list backups
```
# Create a backup
juju run postgresql-k8s/leader create-backup --wait 5m
# List backups
juju run postgresql-k8s/leader list-backups
```
More details found [here](https://charmhub.io/postgresql-k8s/docs/h-create-and-list-backups).

### Restore a backup
```
# Check available backups
juju run postgresql-k8s/leader list-backups
# Restore backup by ID
juju run postgresql-k8s/leader restore backup-id=YYYY-MM-DDTHH:MM:SSZ --wait 5m
```
More details found [here](https://charmhub.io/postgresql-k8s/docs/h-restore-backup).

## Contributing

Expand Down
32 changes: 32 additions & 0 deletions tests/integration/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

"""Charm integration test helpers."""

import json
import logging
from pathlib import Path

import requests
import yaml
from pytest_operator.plugin import OpsTest

Expand Down Expand Up @@ -60,3 +62,33 @@ async def get_unit_url(
f"{application}/{unit}"
]["address"]
return f"{protocol}://{address}:{port}"


async def get_memberships(ops_test: OpsTest, url):
AmberCharitos marked this conversation as resolved.
Show resolved Hide resolved
"""Return membership from Ranger.

Args:
ops_test: PyTest object.
url: Ranger unit address.

Returns:
membership: Ranger membership.

Raises:
Exception: requests exception.
"""
url = f"{url}/service/xusers/groupusers"
try:
response = requests.get(
url, headers=HEADERS, auth=RANGER_AUTH, timeout=20
)
except requests.exceptions.RequestException:
logger.exception(
"An exception has occurred while getting Ranger memberships:"
)
raise
data = json.loads(response.text)
group = data["vXGroupUsers"][0].get("name")
user_id = data["vXGroupUsers"][0].get("userId")
membership = (group, user_id)
return membership
68 changes: 59 additions & 9 deletions tests/integration/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
# See LICENSE file for licensing details.

"""Charm integration tests."""
import json
import logging

import pytest
import requests
from apache_ranger.client import ranger_client
from conftest import deploy # noqa: F401, pylint: disable=W0611
from helpers import APP_NAME, HEADERS, RANGER_AUTH, TRINO_SERVICE, get_unit_url
from helpers import (
APP_NAME,
METADATA,
POSTGRES_NAME,
RANGER_AUTH,
TRINO_SERVICE,
get_memberships,
get_unit_url,
)
from pytest_operator.plugin import OpsTest

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -49,13 +56,56 @@ async def test_group_membership(self, ops_test: OpsTest):
url = await get_unit_url(
ops_test, application=APP_NAME, unit=0, port=6080
)
url = f"{url}/service/xusers/groupusers"
response = requests.get(
url, headers=HEADERS, auth=RANGER_AUTH, timeout=20
membership = await get_memberships(ops_test, url)

assert membership == ("commercial-systems", 8)

async def test_simulate_crash(self, ops_test: OpsTest):
"""Simulate the crash of the Ranger charm.

Args:
ops_test: PyTest object.
"""
# Destroy charm
await ops_test.model.applications[APP_NAME].destroy()
await ops_test.model.block_until(
lambda: APP_NAME not in ops_test.model.applications
)
data = json.loads(response.text)
group = data["vXGroupUsers"][0].get("name")
user_id = data["vXGroupUsers"][0].get("userId")
membership = (group, user_id)

# Deploy charm again
charm = await ops_test.build_charm(".")
resources = {
"ranger-image": METADATA["resources"]["ranger-image"][
"upstream-source"
]
}
await ops_test.model.deploy(
charm,
resources=resources,
application_name=APP_NAME,
num_units=1,
)
await ops_test.model.wait_for_idle(
apps=[APP_NAME],
status="blocked",
raise_on_blocked=False,
timeout=1000,
)

await ops_test.model.integrate(APP_NAME, POSTGRES_NAME)

await ops_test.model.wait_for_idle(
apps=[APP_NAME, POSTGRES_NAME],
status="active",
raise_on_blocked=False,
timeout=1500,
)
url = await get_unit_url(
ops_test, application=APP_NAME, unit=0, port=6080
)
response = requests.get(url, timeout=300, verify=False) # nosec
assert response.status_code == 200

membership = await get_memberships(ops_test, url)
logger.info(f"Ranger memberships: {membership}")
assert membership == ("commercial-systems", 8)
Loading