|
| 1 | +import xarray as xr |
| 2 | + |
| 3 | +import json |
| 4 | +import time |
| 5 | + |
| 6 | +import pytest |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture(scope="session") |
| 11 | +def container(): |
| 12 | + import docker |
| 13 | + |
| 14 | + client = docker.from_env() |
| 15 | + port = 9000 |
| 16 | + minio_container = client.containers.run( |
| 17 | + "quay.io/minio/minio", |
| 18 | + "server /data", |
| 19 | + detach=True, |
| 20 | + ports={f"{port}/tcp": port}, |
| 21 | + environment={ |
| 22 | + "MINIO_ACCESS_KEY": "minioadmin", |
| 23 | + "MINIO_SECRET_KEY": "minioadmin", |
| 24 | + }, |
| 25 | + ) |
| 26 | + time.sleep(3) # give it time to boot |
| 27 | + # enter |
| 28 | + yield { |
| 29 | + "port": port, |
| 30 | + "endpoint": f"http://localhost:{port}", |
| 31 | + "username": "minioadmin", |
| 32 | + "password": "minioadmin", |
| 33 | + } |
| 34 | + # exit |
| 35 | + minio_container.stop() |
| 36 | + minio_container.remove() |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture(scope="session") |
| 40 | +def minio_bucket(container): |
| 41 | + # Setup with guidance from https://medium.com/@sant1/using-minio-with-docker-and-python-cbbad397cb5d |
| 42 | + from minio import Minio |
| 43 | + |
| 44 | + bucket = "my-bucket" |
| 45 | + filename = "test.nc" |
| 46 | + # Initialize MinIO client |
| 47 | + client = Minio( |
| 48 | + "localhost:9000", |
| 49 | + access_key=container["username"], |
| 50 | + secret_key=container["password"], |
| 51 | + secure=False, |
| 52 | + ) |
| 53 | + client.make_bucket(bucket) |
| 54 | + policy = { |
| 55 | + "Version": "2012-10-17", |
| 56 | + "Statement": [ |
| 57 | + { |
| 58 | + "Effect": "Allow", |
| 59 | + "Principal": {"AWS": "*"}, |
| 60 | + "Action": ["s3:GetBucketLocation", "s3:ListBucket"], |
| 61 | + "Resource": "arn:aws:s3:::my-bucket", |
| 62 | + }, |
| 63 | + { |
| 64 | + "Effect": "Allow", |
| 65 | + "Principal": {"AWS": "*"}, |
| 66 | + "Action": [ |
| 67 | + "s3:GetObject", |
| 68 | + "s3:GetObjectRetention", |
| 69 | + "s3:GetObjectLegalHold", |
| 70 | + ], |
| 71 | + "Resource": "arn:aws:s3:::my-bucket/*", |
| 72 | + }, |
| 73 | + ], |
| 74 | + } |
| 75 | + client.set_bucket_policy(bucket, json.dumps(policy)) |
| 76 | + yield { |
| 77 | + "port": container["port"], |
| 78 | + "endpoint": container["endpoint"], |
| 79 | + "username": container["username"], |
| 80 | + "password": container["password"], |
| 81 | + "bucket": bucket, |
| 82 | + "file": filename, |
| 83 | + "client": client, |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | +@pytest.fixture |
| 88 | +def local_netcdf4_file(tmp_path: Path) -> str: |
| 89 | + """Create a NetCDF4 file with data in multiple groups.""" |
| 90 | + filepath = tmp_path / "test.nc" |
| 91 | + ds1 = xr.DataArray([1, 2, 3], name="foo").to_dataset() |
| 92 | + ds1.to_netcdf(filepath) |
| 93 | + return str(filepath) |
0 commit comments