2022-11-14 12:04:15 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
import allure
|
|
|
|
import pytest
|
2023-05-15 09:59:33 +00:00
|
|
|
from frostfs_testlib.s3 import AwsCliClient, Boto3ClientWrapper, S3ClientWrapper, VersioningStatus
|
|
|
|
from frostfs_testlib.steps.cli.container import search_container_by_name
|
|
|
|
from frostfs_testlib.steps.s3 import s3_helper
|
|
|
|
from frostfs_testlib.steps.storage_policy import get_simple_object_copies
|
|
|
|
from frostfs_testlib.testing.cluster_test_base import ClusterTestBase
|
|
|
|
from frostfs_testlib.testing.test_control import expect_not_raises
|
|
|
|
from frostfs_testlib.utils.file_utils import generate_file
|
2022-11-14 12:04:15 +00:00
|
|
|
|
|
|
|
|
2023-05-15 09:59:33 +00:00
|
|
|
def pytest_generate_tests(metafunc: pytest.Metafunc):
|
2023-02-27 16:54:27 +00:00
|
|
|
policy = f"{os.getcwd()}/pytest_tests/resources/files/policy.json"
|
2022-11-14 12:04:15 +00:00
|
|
|
if "s3_client" in metafunc.fixturenames:
|
|
|
|
metafunc.parametrize(
|
2023-05-15 09:59:33 +00:00
|
|
|
"s3_client, s3_policy",
|
|
|
|
[(AwsCliClient, policy), (Boto3ClientWrapper, policy)],
|
2022-11-14 12:04:15 +00:00
|
|
|
indirect=True,
|
|
|
|
ids=["aws cli", "boto3"],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.s3_gate
|
2023-05-15 09:59:33 +00:00
|
|
|
class TestS3GatePolicy(ClusterTestBase):
|
2022-11-14 12:04:15 +00:00
|
|
|
@allure.title("Test S3: Verify bucket creation with retention policy applied")
|
2023-05-15 09:59:33 +00:00
|
|
|
def test_s3_bucket_location(
|
|
|
|
self, default_wallet: str, s3_client: S3ClientWrapper, simple_object_size: int
|
|
|
|
):
|
2022-12-07 12:38:56 +00:00
|
|
|
file_path_1 = generate_file(simple_object_size)
|
2023-05-15 09:59:33 +00:00
|
|
|
file_name_1 = s3_helper.object_key_from_file_path(file_path_1)
|
2022-12-07 12:38:56 +00:00
|
|
|
file_path_2 = generate_file(simple_object_size)
|
2023-05-15 09:59:33 +00:00
|
|
|
file_name_2 = s3_helper.object_key_from_file_path(file_path_2)
|
2022-11-14 12:04:15 +00:00
|
|
|
|
|
|
|
with allure.step("Create two buckets with different bucket configuration"):
|
2023-05-15 09:59:33 +00:00
|
|
|
bucket_1 = s3_client.create_bucket(location_constraint="complex")
|
|
|
|
s3_helper.set_bucket_versioning(s3_client, bucket_1, VersioningStatus.ENABLED)
|
|
|
|
bucket_2 = s3_client.create_bucket(location_constraint="rep-3")
|
|
|
|
s3_helper.set_bucket_versioning(s3_client, bucket_2, VersioningStatus.ENABLED)
|
|
|
|
list_buckets = s3_client.list_buckets()
|
2022-11-14 12:04:15 +00:00
|
|
|
assert (
|
|
|
|
bucket_1 in list_buckets and bucket_2 in list_buckets
|
|
|
|
), f"Expected two buckets {bucket_1, bucket_2}, got {list_buckets}"
|
|
|
|
|
2023-05-15 09:59:33 +00:00
|
|
|
with allure.step("Check head buckets"):
|
|
|
|
with expect_not_raises():
|
|
|
|
s3_client.head_bucket(bucket_1)
|
|
|
|
s3_client.head_bucket(bucket_2)
|
2022-11-14 12:04:15 +00:00
|
|
|
|
|
|
|
with allure.step("Put objects into buckets"):
|
2023-05-15 09:59:33 +00:00
|
|
|
version_id_1 = s3_client.put_object(bucket_1, file_path_1)
|
|
|
|
version_id_2 = s3_client.put_object(bucket_2, file_path_2)
|
|
|
|
s3_helper.check_objects_in_bucket(s3_client, bucket_1, [file_name_1])
|
|
|
|
s3_helper.check_objects_in_bucket(s3_client, bucket_2, [file_name_2])
|
2022-11-14 12:04:15 +00:00
|
|
|
|
|
|
|
with allure.step("Check bucket location"):
|
2023-05-15 09:59:33 +00:00
|
|
|
bucket_loc_1 = s3_client.get_bucket_location(bucket_1)
|
|
|
|
bucket_loc_2 = s3_client.get_bucket_location(bucket_2)
|
2022-11-14 12:04:15 +00:00
|
|
|
assert bucket_loc_1 == "complex"
|
|
|
|
assert bucket_loc_2 == "rep-3"
|
|
|
|
|
|
|
|
with allure.step("Check object policy"):
|
2022-12-05 22:31:45 +00:00
|
|
|
cid_1 = search_container_by_name(
|
2023-05-15 09:59:33 +00:00
|
|
|
default_wallet,
|
|
|
|
bucket_1,
|
|
|
|
shell=self.shell,
|
|
|
|
endpoint=self.cluster.default_rpc_endpoint,
|
2022-12-05 22:31:45 +00:00
|
|
|
)
|
2022-11-14 12:04:15 +00:00
|
|
|
copies_1 = get_simple_object_copies(
|
2023-05-15 09:59:33 +00:00
|
|
|
wallet=default_wallet,
|
2022-12-05 22:31:45 +00:00
|
|
|
cid=cid_1,
|
|
|
|
oid=version_id_1,
|
|
|
|
shell=self.shell,
|
|
|
|
nodes=self.cluster.storage_nodes,
|
2022-11-14 12:04:15 +00:00
|
|
|
)
|
|
|
|
assert copies_1 == 1
|
2022-12-05 22:31:45 +00:00
|
|
|
cid_2 = search_container_by_name(
|
2023-05-15 09:59:33 +00:00
|
|
|
default_wallet,
|
|
|
|
bucket_2,
|
|
|
|
shell=self.shell,
|
|
|
|
endpoint=self.cluster.default_rpc_endpoint,
|
2022-12-05 22:31:45 +00:00
|
|
|
)
|
2022-11-14 12:04:15 +00:00
|
|
|
copies_2 = get_simple_object_copies(
|
2023-05-15 09:59:33 +00:00
|
|
|
wallet=default_wallet,
|
2022-12-05 22:31:45 +00:00
|
|
|
cid=cid_2,
|
|
|
|
oid=version_id_2,
|
|
|
|
shell=self.shell,
|
|
|
|
nodes=self.cluster.storage_nodes,
|
2022-11-14 12:04:15 +00:00
|
|
|
)
|
|
|
|
assert copies_2 == 3
|
|
|
|
|
|
|
|
@allure.title("Test S3: bucket policy ")
|
2023-05-15 09:59:33 +00:00
|
|
|
def test_s3_bucket_policy(self, s3_client: S3ClientWrapper):
|
2022-11-14 12:04:15 +00:00
|
|
|
with allure.step("Create bucket with default policy"):
|
2023-05-15 09:59:33 +00:00
|
|
|
bucket = s3_client.create_bucket()
|
|
|
|
s3_helper.set_bucket_versioning(s3_client, bucket, VersioningStatus.ENABLED)
|
2022-11-14 12:04:15 +00:00
|
|
|
|
|
|
|
with allure.step("GetBucketPolicy"):
|
2023-05-15 09:59:33 +00:00
|
|
|
s3_client.get_bucket_policy(bucket)
|
2022-11-14 12:04:15 +00:00
|
|
|
|
|
|
|
with allure.step("Put new policy"):
|
2023-02-27 16:54:27 +00:00
|
|
|
custom_policy = f"file://{os.getcwd()}/pytest_tests/resources/files/bucket_policy.json"
|
2022-11-14 12:04:15 +00:00
|
|
|
custom_policy = {
|
|
|
|
"Version": "2008-10-17",
|
|
|
|
"Id": "aaaa-bbbb-cccc-dddd",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Sid": "AddPerm",
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Principal": {"AWS": "*"},
|
|
|
|
"Action": ["s3:GetObject"],
|
|
|
|
"Resource": [f"arn:aws:s3:::{bucket}/*"],
|
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
2023-05-15 09:59:33 +00:00
|
|
|
s3_client.put_bucket_policy(bucket, custom_policy)
|
2022-11-14 12:04:15 +00:00
|
|
|
with allure.step("GetBucketPolicy"):
|
2023-05-15 09:59:33 +00:00
|
|
|
policy_1 = s3_client.get_bucket_policy(bucket)
|
2022-11-14 12:04:15 +00:00
|
|
|
print(policy_1)
|
|
|
|
|
2023-05-15 09:59:33 +00:00
|
|
|
@allure.title("Test S3: bucket CORS")
|
|
|
|
def test_s3_cors(self, s3_client: S3ClientWrapper):
|
2022-11-14 12:04:15 +00:00
|
|
|
with allure.step("Create bucket without cors"):
|
2023-05-15 09:59:33 +00:00
|
|
|
bucket = s3_client.create_bucket()
|
|
|
|
s3_helper.set_bucket_versioning(s3_client, bucket, VersioningStatus.ENABLED)
|
2022-11-14 12:04:15 +00:00
|
|
|
|
|
|
|
with pytest.raises(Exception):
|
2023-05-15 09:59:33 +00:00
|
|
|
bucket_cors = s3_client.get_bucket_cors(bucket)
|
2022-11-14 12:04:15 +00:00
|
|
|
|
|
|
|
with allure.step("Put bucket cors"):
|
|
|
|
cors = {
|
|
|
|
"CORSRules": [
|
|
|
|
{
|
|
|
|
"AllowedOrigins": ["http://www.example.com"],
|
|
|
|
"AllowedHeaders": ["*"],
|
|
|
|
"AllowedMethods": ["PUT", "POST", "DELETE"],
|
|
|
|
"MaxAgeSeconds": 3000,
|
|
|
|
"ExposeHeaders": ["x-amz-server-side-encryption"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"AllowedOrigins": ["*"],
|
|
|
|
"AllowedHeaders": ["Authorization"],
|
|
|
|
"AllowedMethods": ["GET"],
|
|
|
|
"MaxAgeSeconds": 3000,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
2023-05-15 09:59:33 +00:00
|
|
|
s3_client.put_bucket_cors(bucket, cors)
|
|
|
|
bucket_cors = s3_client.get_bucket_cors(bucket)
|
2022-11-14 12:04:15 +00:00
|
|
|
assert bucket_cors == cors.get(
|
|
|
|
"CORSRules"
|
2023-05-15 09:59:33 +00:00
|
|
|
), f"Expected CORSRules must be {cors.get('CORSRules')}"
|
2022-11-14 12:04:15 +00:00
|
|
|
|
|
|
|
with allure.step("delete bucket cors"):
|
2023-05-15 09:59:33 +00:00
|
|
|
s3_client.delete_bucket_cors(bucket)
|
2022-11-14 12:04:15 +00:00
|
|
|
|
|
|
|
with pytest.raises(Exception):
|
2023-05-15 09:59:33 +00:00
|
|
|
bucket_cors = s3_client.get_bucket_cors(bucket)
|