Skip to content

Commit

Permalink
Tweak backup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dragomirp committed Oct 28, 2023
1 parent 6eaa292 commit df36e8b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 54 deletions.
53 changes: 0 additions & 53 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -1,62 +1,9 @@
#!/usr/bin/env python3
# Copyright 2022 Canonical Ltd.
# See LICENSE file for licensing details.
import os
import uuid

import boto3
import pytest
from pytest_operator.plugin import OpsTest

from tests.integration.helpers import construct_endpoint

AWS = "AWS"
GCP = "GCP"


@pytest.fixture(scope="module")
async def cloud_configs(ops_test: OpsTest) -> None:
# Define some configurations and credentials.
configs = {
AWS: {
"endpoint": "https://s3.amazonaws.com",
"bucket": "data-charms-testing",
"path": f"/postgresql-vm/{uuid.uuid1()}",
"region": "us-east-1",
},
GCP: {
"endpoint": "https://storage.googleapis.com",
"bucket": "data-charms-testing",
"path": f"/postgresql-vm/{uuid.uuid1()}",
"region": "",
},
}
credentials = {
AWS: {
"access-key": os.environ.get("AWS_ACCESS_KEY"),
"secret-key": os.environ.get("AWS_SECRET_KEY"),
},
GCP: {
"access-key": os.environ.get("GCP_ACCESS_KEY"),
"secret-key": os.environ.get("GCP_SECRET_KEY"),
},
}
yield configs, credentials
# Delete the previously created objects.
for cloud, config in configs.items():
session = boto3.session.Session(
aws_access_key_id=credentials[cloud]["access-key"],
aws_secret_access_key=credentials[cloud]["secret-key"],
region_name=config["region"],
)
s3 = session.resource(
"s3", endpoint_url=construct_endpoint(config["endpoint"], config["region"])
)
bucket = s3.Bucket(config["bucket"])
# GCS doesn't support batch delete operation, so delete the objects one by one.
for bucket_object in bucket.objects.filter(Prefix=config["path"].lstrip("/")):
bucket_object.delete()


@pytest.fixture(scope="module")
async def charm(ops_test: OpsTest):
Expand Down
50 changes: 49 additions & 1 deletion tests/integration/test_backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import uuid
from typing import Dict, Tuple

import boto3
import pytest as pytest
from pytest_operator.plugin import OpsTest
from tenacity import Retrying, stop_after_attempt, wait_exponential

from tests.integration.conftest import AWS
from tests.integration.helpers import (
CHARM_SERIES,
DATABASE_APP_NAME,
construct_endpoint,
db_connect,
get_password,
get_primary,
Expand All @@ -30,6 +31,53 @@

logger = logging.getLogger(__name__)

AWS = "AWS"
GCP = "GCP"


@pytest.fixture(scope="module")
async def cloud_configs(ops_test: OpsTest, github_secrets) -> None:
# Define some configurations and credentials.
configs = {
AWS: {
"endpoint": "https://s3.amazonaws.com",
"bucket": "data-charms-testing",
"path": f"/postgresql-vm/{uuid.uuid1()}",
"region": "us-east-1",
},
GCP: {
"endpoint": "https://storage.googleapis.com",
"bucket": "data-charms-testing",
"path": f"/postgresql-vm/{uuid.uuid1()}",
"region": "",
},
}
credentials = {
AWS: {
"access-key": github_secrets["AWS_ACCESS_KEY"],
"secret-key": github_secrets["AWS_SECRET_KEY"],
},
GCP: {
"access-key": github_secrets["GCP_ACCESS_KEY"],
"secret-key": github_secrets["GCP_SECRET_KEY"],
},
}
yield configs, credentials
# Delete the previously created objects.
for cloud, config in configs.items():
session = boto3.session.Session(
aws_access_key_id=credentials[cloud]["access-key"],
aws_secret_access_key=credentials[cloud]["secret-key"],
region_name=config["region"],
)
s3 = session.resource(
"s3", endpoint_url=construct_endpoint(config["endpoint"], config["region"])
)
bucket = s3.Bucket(config["bucket"])
# GCS doesn't support batch delete operation, so delete the objects one by one.
for bucket_object in bucket.objects.filter(Prefix=config["path"].lstrip("/")):
bucket_object.delete()


@pytest.mark.abort_on_fail
async def test_backup(ops_test: OpsTest, cloud_configs: Tuple[Dict, Dict]) -> None:
Expand Down

0 comments on commit df36e8b

Please sign in to comment.