|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import boto3 |
| 3 | +import pytest |
| 4 | +import yatest |
| 5 | +import os |
| 6 | + |
| 7 | +from ydb.tests.oss.ydb_sdk_import import ydb |
| 8 | +from ydb.tests.library.compatibility.fixtures import MixedClusterFixture, RestartToAnotherVersionFixture, RollingUpgradeAndDowngradeFixture |
| 9 | + |
| 10 | +from ydb.export import ExportToS3Settings |
| 11 | + |
| 12 | + |
| 13 | +class ExternalDataTableTestBase: |
| 14 | + def setup_cluster(self): |
| 15 | + if min(self.versions) < (25, 1): |
| 16 | + pytest.skip("Only available since 25-1") |
| 17 | + |
| 18 | + output_path = yatest.common.test_output_path() |
| 19 | + self.output_f = open(os.path.join(output_path, "out.log"), "w") |
| 20 | + self.s3_config = self.setup_s3() |
| 21 | + s3_endpoint, s3_access_key, s3_secret_key, s3_bucket = self.s3_config |
| 22 | + self.settings = ( |
| 23 | + ExportToS3Settings() |
| 24 | + .with_endpoint(s3_endpoint) |
| 25 | + .with_access_key(s3_access_key) |
| 26 | + .with_secret_key(s3_secret_key) |
| 27 | + .with_bucket(s3_bucket) |
| 28 | + ) |
| 29 | + |
| 30 | + yield from super().setup_cluster( |
| 31 | + extra_feature_flags={ |
| 32 | + "enable_external_data_sources": True, |
| 33 | + } |
| 34 | + ) |
| 35 | + |
| 36 | + @staticmethod |
| 37 | + def setup_s3(): |
| 38 | + s3_endpoint = os.getenv("S3_ENDPOINT") |
| 39 | + s3_access_key = "minio" |
| 40 | + s3_secret_key = "minio123" |
| 41 | + s3_bucket = "test_bucket" |
| 42 | + |
| 43 | + resource = boto3.resource( |
| 44 | + "s3", endpoint_url=s3_endpoint, aws_access_key_id=s3_access_key, aws_secret_access_key=s3_secret_key |
| 45 | + ) |
| 46 | + |
| 47 | + bucket = resource.Bucket(s3_bucket) |
| 48 | + bucket.create() |
| 49 | + bucket.objects.all().delete() |
| 50 | + bucket.put_object(Key="file.txt", Body="Hello S3!") |
| 51 | + |
| 52 | + return s3_endpoint, s3_access_key, s3_secret_key, s3_bucket |
| 53 | + |
| 54 | + def create_external_data_source(self): |
| 55 | + s3_endpoint, s3_access_key, s3_secret_key, s3_bucket = self.s3_config |
| 56 | + |
| 57 | + with ydb.QuerySessionPool(self.driver) as session_pool: |
| 58 | + query = f""" |
| 59 | + CREATE OBJECT s3_access_key (TYPE SECRET) WITH value="{s3_access_key}"; |
| 60 | + CREATE OBJECT s3_secret_key (TYPE SECRET) WITH value="{s3_secret_key}"; |
| 61 | + """ |
| 62 | + session_pool.execute_with_retries(query) |
| 63 | + |
| 64 | + query = f""" |
| 65 | + CREATE EXTERNAL DATA SOURCE s3_source WITH ( |
| 66 | + SOURCE_TYPE = "ObjectStorage", |
| 67 | + LOCATION = "{s3_endpoint}/{s3_bucket}", |
| 68 | + AUTH_METHOD="AWS", |
| 69 | + AWS_ACCESS_KEY_ID_SECRET_NAME="s3_access_key", |
| 70 | + AWS_SECRET_ACCESS_KEY_SECRET_NAME="s3_secret_key", |
| 71 | + AWS_REGION="us-east-1" |
| 72 | + ); |
| 73 | + """ |
| 74 | + session_pool.execute_with_retries(query) |
| 75 | + |
| 76 | + def do_test_external_data(self): |
| 77 | + s3_endpoint, s3_access_key, s3_secret_key, s3_bucket = self.s3_config |
| 78 | + |
| 79 | + with ydb.QuerySessionPool(self.driver) as session_pool: |
| 80 | + query = """ |
| 81 | + SELECT * FROM s3_source.`file.txt` WITH ( |
| 82 | + FORMAT = "raw", |
| 83 | + SCHEMA = ( Data String ) |
| 84 | + ); |
| 85 | + """ |
| 86 | + result_sets = session_pool.execute_with_retries(query) |
| 87 | + data = result_sets[0].rows[0]['Data'] |
| 88 | + assert isinstance(data, bytes) and data.decode() == 'Hello S3!' |
| 89 | + |
| 90 | + |
| 91 | +class TestExternalDataTableMixedCluster(ExternalDataTableTestBase, MixedClusterFixture): |
| 92 | + @pytest.fixture(autouse=True, scope="function") |
| 93 | + def setup(self): |
| 94 | + yield from self.setup_cluster() |
| 95 | + |
| 96 | + def test_external_data_source(self): |
| 97 | + self.create_external_data_source() |
| 98 | + self.do_test_external_data() |
| 99 | + |
| 100 | + |
| 101 | +class TestExternalDataTableRestartToAnotherVersion(ExternalDataTableTestBase, RestartToAnotherVersionFixture): |
| 102 | + @pytest.fixture(autouse=True, scope="function") |
| 103 | + def setup(self): |
| 104 | + yield from self.setup_cluster() |
| 105 | + |
| 106 | + def test_external_data_source(self): |
| 107 | + self.create_external_data_source() |
| 108 | + self.change_cluster_version() |
| 109 | + self.do_test_external_data() |
| 110 | + |
| 111 | + |
| 112 | +class TestExternalDataTableRollingUpgradeAndDowngrade(ExternalDataTableTestBase, RollingUpgradeAndDowngradeFixture): |
| 113 | + @pytest.fixture(autouse=True, scope="function") |
| 114 | + def setup(self): |
| 115 | + yield from self.setup_cluster() |
| 116 | + |
| 117 | + def test_external_data_source(self): |
| 118 | + self.create_external_data_source() |
| 119 | + for _ in self.roll(): |
| 120 | + self.do_test_external_data() |
0 commit comments