Skip to content

Commit

Permalink
Apply suggestions from review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
weiiwang01 committed Jul 31, 2024
1 parent 1fd065b commit 18a6cd4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 64 deletions.
4 changes: 2 additions & 2 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ def _gen_pebble_plan(self) -> ops.pebble.LayerDict:
checks={
"backend-ready": {
"override": "replace",
"level": "ready",
"level": "alive",
"period": "30s",
"exec": {
# pylint: disable=line-too-long
"command": 'bash -c "pebble services backend | grep -q inactive || curl -f localhost:6060/readyz"' # noqa: E501
"command": 'bash -c "pebble services backend | grep -q inactive || curl -f -m 5 localhost:6060/readyz"' # noqa: E501
},
}
},
Expand Down
88 changes: 34 additions & 54 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,82 +6,62 @@
# pylint: disable=unused-argument

import collections
import json
import logging
import time

import boto3
import botocore.client
import kubernetes
import pytest
import pytest_asyncio
from pytest_operator.plugin import OpsTest

logger = logging.getLogger(__name__)


@pytest_asyncio.fixture(name="get_unit_ips", scope="module")
async def get_unit_ips_fixture(ops_test: OpsTest):
"""A function to get unit ips of a charm application."""

async def _get_unit_ips(name: str):
"""A function to get unit ips of a charm application.
Args:
name: The name of the charm application.
Returns:
A list of unit ips.
"""
_, status, _ = await ops_test.juju("status", "--format", "json")
status = json.loads(status)
units = status["applications"][name]["units"]
ip_list = []
for key in sorted(units.keys(), key=lambda n: int(n.split("/")[-1])):
ip_list.append(units[key]["address"])
return ip_list

return _get_unit_ips


@pytest.fixture(scope="module", name="load_kube_config")
def load_kube_config_fixture(pytestconfig: pytest.Config):
"""Load kubernetes config file."""
kube_config = pytestconfig.getoption("--kube-config")
kubernetes.config.load_kube_config(config_file=kube_config)


@pytest.fixture(scope="module")
def minio(load_kube_config, ops_test: OpsTest):
@pytest_asyncio.fixture(name="minio", scope="module")
async def minio_fixture(get_unit_ips, load_kube_config, ops_test: OpsTest):
"""Deploy test minio service."""
assert ops_test.model
namespace = ops_test.model.name
key = "minioadmin"
v1 = kubernetes.client.CoreV1Api()
pod = kubernetes.client.V1Pod(
api_version="v1",
kind="Pod",
metadata=kubernetes.client.V1ObjectMeta(
name="minio", namespace=namespace, labels={"app.kubernetes.io/name": "minio"}
),
spec=kubernetes.client.V1PodSpec(
containers=[
kubernetes.client.V1Container(
name="minio",
image="minio/minio",
args=["server", "/data"],
env=[
kubernetes.client.V1EnvVar(name="MINIO_ROOT_USER", value=key),
kubernetes.client.V1EnvVar(name="MINIO_ROOT_PASSWORD", value=key),
],
ports=[kubernetes.client.V1ContainerPort(container_port=9000)],
)
],
),
)
v1.create_namespaced_pod(namespace=namespace, body=pod)
service = kubernetes.client.V1Service(
api_version="v1",
kind="Service",
metadata=kubernetes.client.V1ObjectMeta(name="minio-service", namespace=namespace),
spec=kubernetes.client.V1ServiceSpec(
type="ClusterIP",
ports=[kubernetes.client.V1ServicePort(port=9000, target_port=9000)],
selector={"app.kubernetes.io/name": "minio"},
),
)
v1.create_namespaced_service(namespace=namespace, body=service)
deadline = time.time() + 300
while True:
if time.time() > deadline:
raise TimeoutError("timeout while waiting for minio pod")
try:
pod = v1.read_namespaced_pod(name="minio", namespace=namespace)
if pod.status.phase == "Running":
logger.info("minio running at %s", pod.status.pod_ip)
break
except kubernetes.client.ApiException:
pass
logger.info("waiting for minio pod")
time.sleep(1)
pod_ip = pod.status.pod_ip
assert ops_test.model
minio = await ops_test.model.deploy("minio", config={"access-key": key, "secret-key": key})
await ops_test.model.wait_for_idle(apps=[minio.name])
ip = (await get_unit_ips(minio.name))[0]
s3 = boto3.client(
"s3",
endpoint_url=f"http://{pod_ip}:9000",
endpoint_url=f"http://{ip}:9000",
aws_access_key_id=key,
aws_secret_access_key=key,
config=botocore.client.Config(signature_version="s3v4"),
Expand All @@ -90,7 +70,7 @@ def minio(load_kube_config, ops_test: OpsTest):
s3.create_bucket(Bucket=bucket)
S3Credential = collections.namedtuple("S3Credential", "endpoint bucket access_key secret_key")
return S3Credential(
endpoint=f"http://minio-service.{namespace}.svc.cluster.local:9000",
endpoint=f"http://minio-endpoints.{ops_test.model.name}.svc.cluster.local:9000",
bucket=bucket,
access_key=key,
secret_key=key,
Expand Down
20 changes: 14 additions & 6 deletions tests/integration/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,18 @@ async def test_create_profile(ops_test: OpsTest, ingress_address):
raise TimeoutError("timed out waiting for login success")
action = await unit.run_action("delete-profile", email=email)
await action.wait()
response = session.post(
f"http://{ingress_address}/api/rpc/command/login-with-password",
headers={"Host": "penpot.local"},
json={"~:email": email, "~:password": password},
timeout=10,
)
deadline = time.time() + 300
while time.time() < deadline:
response = session.post(
f"http://{ingress_address}/api/rpc/command/login-with-password",
headers={"Host": "penpot.local"},
json={"~:email": email, "~:password": password},
timeout=10,
)
if response.status_code == 400:
break
logger.info("penpot login status: %s", response.status_code)
time.sleep(5)
else:
raise TimeoutError("timed out waiting for login response")
assert response.status_code == 400
4 changes: 2 additions & 2 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ def test_penpot_pebble_layer(harness):
"backend-ready": {
"exec": {
# pylint: disable=line-too-long
"command": 'bash -c "pebble services backend | grep -q inactive || curl -f localhost:6060/readyz"' # noqa: E501
"command": 'bash -c "pebble services backend | grep -q inactive || curl -f -m 5 localhost:6060/readyz"' # noqa: E501
},
"level": "ready",
"level": "alive",
"override": "replace",
"period": "30s",
}
Expand Down

0 comments on commit 18a6cd4

Please sign in to comment.