diff --git a/tests/_create_bucket.py b/tests/_create_bucket.py deleted file mode 100644 index e0b55cb..0000000 --- a/tests/_create_bucket.py +++ /dev/null @@ -1,19 +0,0 @@ -import os - -import boto3 - - -def main() -> int: - resource = boto3.resource( - service_name="s3", - endpoint_url="http://minio:9000", - region_name="us-east-1", - aws_access_key_id=os.getenv("S3_ACCESS_KEY"), - aws_secret_access_key=os.getenv("S3_SECRET_KEY"), - ) - resource.create_bucket(Bucket="frappe") - return 0 - - -if __name__ == "__main__": - raise SystemExit(main()) diff --git a/tests/_wait_for_s3.py b/tests/_wait_for_s3.py new file mode 100644 index 0000000..fe1c9ce --- /dev/null +++ b/tests/_wait_for_s3.py @@ -0,0 +1,55 @@ +import os +import time + +import boto3 +from botocore.config import Config +from botocore.exceptions import ( + ClientError, + ConnectionClosedError, + ConnectTimeoutError, + EndpointConnectionError, + ReadTimeoutError, +) + + +def main() -> int: + endpoint_url = os.environ["S3_ENDPOINT_URL"] + bucket = os.environ["S3_BUCKET"] + client = boto3.client( + "s3", + endpoint_url=endpoint_url, + region_name="us-east-1", + config=Config( + connect_timeout=2, + read_timeout=2, + retries={"max_attempts": 0}, + s3={"addressing_style": "path"}, + ), + ) + deadline = time.monotonic() + 60 + while True: + try: + # Check credentials and bucket readiness from the backup client's network. + client.head_bucket(Bucket=bucket) + return 0 + except ClientError as exc: + status = exc.response["ResponseMetadata"]["HTTPStatusCode"] + if status != 404 and status < 500: + raise + error = exc + except ( + ConnectionClosedError, + ConnectTimeoutError, + EndpointConnectionError, + ReadTimeoutError, + ) as exc: + error = exc + if time.monotonic() >= deadline: + raise RuntimeError( + f"S3 bucket {bucket!r} at {endpoint_url} was not ready within 60 seconds" + ) from error + time.sleep(1) + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tests/conftest.py b/tests/conftest.py index 63169e3..450d42d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -126,43 +126,65 @@ def python_path(): @dataclass class S3ServiceResult: + endpoint_url: str + bucket: str access_key: str secret_key: str @pytest.fixture def s3_service(python_path: str, compose: Compose): + endpoint_url = "http://s3:8333" + bucket = "frappe" access_key = "AKIAIOSFODNN7EXAMPLE" secret_key = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" - cmd = ( - "docker", - "run", - "--name", - "minio", - "-d", - "-e", - f"MINIO_ACCESS_KEY={access_key}", - "-e", - f"MINIO_SECRET_KEY={secret_key}", - "--network", - f"{compose.project_name}_default", - "minio/minio", - "server", - "/data", - ) - subprocess.check_call(cmd) + container_name = f"{compose.project_name}-s3" + try: + subprocess.check_call( + ( + "docker", + "run", + "--detach", + "--pull=always", + "--name", + container_name, + "--network", + f"{compose.project_name}_default", + "--network-alias", + "s3", + "-e", + f"AWS_ACCESS_KEY_ID={access_key}", + "-e", + f"AWS_SECRET_ACCESS_KEY={secret_key}", + "-e", + f"S3_BUCKET={bucket}", + "chrislusf/seaweedfs:latest", + "mini", + "-dir=/data", + ) + ) + compose("cp", "tests/_wait_for_s3.py", "backend:/tmp") + compose.exec("backend", "bench", "pip", "install", "boto3~=1.34.143") + compose.exec( + "-e", + f"S3_ENDPOINT_URL={endpoint_url}", + "-e", + f"S3_BUCKET={bucket}", + "-e", + f"AWS_ACCESS_KEY_ID={access_key}", + "-e", + f"AWS_SECRET_ACCESS_KEY={secret_key}", + "backend", + python_path, + "/tmp/_wait_for_s3.py", + ) - compose("cp", "tests/_create_bucket.py", "backend:/tmp") - compose.exec("backend", "bench", "pip", "install", "boto3~=1.34.143") - compose.exec( - "-e", - f"S3_ACCESS_KEY={access_key}", - "-e", - f"S3_SECRET_KEY={secret_key}", - "backend", - python_path, - "/tmp/_create_bucket.py", - ) - - yield S3ServiceResult(access_key=access_key, secret_key=secret_key) - subprocess.call(("docker", "rm", "minio", "-f")) + yield S3ServiceResult( + endpoint_url=endpoint_url, + bucket=bucket, + access_key=access_key, + secret_key=secret_key, + ) + finally: + subprocess.call(("docker", "logs", "--tail", "100", container_name)) + subprocess.call(("docker", "rm", "-fv", container_name)) diff --git a/tests/test_frappe_docker.py b/tests/test_frappe_docker.py index 6eadeb5..ca4d2df 100644 --- a/tests/test_frappe_docker.py +++ b/tests/test_frappe_docker.py @@ -129,7 +129,7 @@ def test_push_backup( restic_password = "secret" compose.bench("--site", frappe_site, "backup", "--with-files") restic_args = [ - "--env=RESTIC_REPOSITORY=s3:http://minio:9000/frappe", + f"--env=RESTIC_REPOSITORY=s3:{s3_service.endpoint_url}/{s3_service.bucket}", f"--env=AWS_ACCESS_KEY_ID={s3_service.access_key}", f"--env=AWS_SECRET_ACCESS_KEY={s3_service.secret_key}", f"--env=RESTIC_PASSWORD={restic_password}", @@ -137,6 +137,7 @@ def test_push_backup( compose.exec(*restic_args, "backend", "restic", "init") compose.exec(*restic_args, "backend", "restic", "backup", "sites") compose.exec(*restic_args, "backend", "restic", "snapshots") + compose.exec(*restic_args, "backend", "restic", "check", "--read-data") def test_https(frappe_site: str, compose: Compose):