forked from conda/conda
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for S3 channels using minio locally (conda#11644)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Jannis Leidel <[email protected]>
- Loading branch information
1 parent
383ddd3
commit bcd125d
Showing
9 changed files
with
209 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (C) 2012 Anaconda, Inc | ||
# SPDX-License-Identifier: BSD-3-Clause |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (C) 2012 Anaconda, Inc | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
import json | ||
import os | ||
import socket | ||
from pathlib import Path | ||
|
||
import pytest | ||
import boto3 | ||
from botocore.client import Config | ||
from xprocess import ProcessStarter | ||
|
||
from ...cli.find_commands import find_executable | ||
|
||
MINIO_EXE = find_executable("minio") | ||
|
||
|
||
def minio_s3_server(xprocess, tmp_path): | ||
""" | ||
Mock a local S3 server using `minio` | ||
This requires: | ||
- pytest-xprocess: runs the background process | ||
- minio: the executable must be in PATH | ||
Note, the given S3 server will be EMPTY! The test function needs | ||
to populate it. You can use | ||
`conda.testing.helpers.populate_s3_server` for that. | ||
""" | ||
|
||
class Minio: | ||
# The 'name' below will be the name of the S3 bucket containing | ||
# keys like `noarch/repodata.json` | ||
name = "minio_s3_server" | ||
port = 9000 | ||
|
||
def __init__(self): | ||
(Path(tmp_path) / self.name).mkdir() | ||
|
||
@property | ||
def server_url(self): | ||
return f"http://localhost:{self.port}/{self.name}" | ||
|
||
def populate_bucket(self, endpoint, bucket_name, channel_dir): | ||
"prepare the s3 connection for our minio instance" | ||
|
||
# Make the minio bucket public first | ||
# https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-example-bucket-policies.html#set-a-bucket-policy | ||
session = boto3.session.Session() | ||
client = session.client( | ||
"s3", | ||
endpoint_url=endpoint, | ||
aws_access_key_id="minioadmin", | ||
aws_secret_access_key="minioadmin", | ||
config=Config(signature_version="s3v4"), | ||
region_name="us-east-1", | ||
) | ||
bucket_policy = json.dumps( | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Sid": "AddPerm", | ||
"Effect": "Allow", | ||
"Principal": "*", | ||
"Action": ["s3:GetObject"], | ||
"Resource": f"arn:aws:s3:::{bucket_name}/*", | ||
} | ||
], | ||
} | ||
) | ||
client.put_bucket_policy(Bucket=bucket_name, Policy=bucket_policy) | ||
|
||
# Minio has to start with an empty directory; once available, | ||
# we can import all channel files by "uploading" them | ||
for current, _, files in os.walk(channel_dir): | ||
for f in files: | ||
path = Path(current, f) | ||
key = path.relative_to(channel_dir) | ||
client.upload_file( | ||
str(path), | ||
bucket_name, | ||
str(key), | ||
ExtraArgs={"ACL": "public-read"}, | ||
) | ||
|
||
print("Starting mock_s3_server") | ||
minio = Minio() | ||
|
||
class Starter(ProcessStarter): | ||
|
||
pattern = "https://docs.min.io" | ||
terminate_on_interrupt = True | ||
timeout = 10 | ||
args = [ | ||
MINIO_EXE, | ||
"server", | ||
f"--address=:{minio.port}", | ||
tmp_path, | ||
] | ||
|
||
def startup_check(self, port=minio.port): | ||
s = socket.socket() | ||
address = "localhost" | ||
error = False | ||
try: | ||
s.connect((address, port)) | ||
except Exception as e: | ||
print("something's wrong with %s:%d. Exception is %s" % (address, port, e)) | ||
error = True | ||
finally: | ||
s.close() | ||
|
||
return not error | ||
|
||
# ensure process is running and return its logfile | ||
pid, logfile = xprocess.ensure(minio.name, Starter) | ||
print(f"Server (PID: {pid}) log file can be found here: {logfile}") | ||
yield minio | ||
xprocess.getinfo(minio.name).terminate() | ||
|
||
|
||
if MINIO_EXE is not None: | ||
minio_s3_server = pytest.fixture()(minio_s3_server) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/bin/bash | ||
set -ex | ||
|
||
# Download the Minio server, needed for S3 tests | ||
curl -LO https://dl.minio.io/server/minio/release/darwin-amd64/minio | ||
chmod +x minio | ||
sudo mv minio /usr/local/bin/minio | ||
|
||
# restoring the default for changeps1 to have parity with dev | ||
conda config --set changeps1 true | ||
# make sure the caching works correctly | ||
conda config --set use_only_tar_bz2 true | ||
# install all test requirements | ||
conda install --name conda-test-env --yes --file tests/requirements.txt | ||
conda update openssl ca-certificates certifi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters