Fix downloading of sql files for extension and libraries.

Rust code refactoring and C code fixes.

Add test for CREATE EXTENSION and LOAD 'library'
This commit is contained in:
Anastasia Lubennikova
2023-06-23 16:11:43 +03:00
parent 4201f4695f
commit 7cdcc8a500
8 changed files with 274 additions and 97 deletions

View File

@@ -82,12 +82,37 @@ def test_file_download(neon_env_builder: NeonEnvBuilder, remote_storage_kind: Re
)
cleanup_files.append(local_name)
# Rust will then download the control files from the bucket
# our rust code should obtain the same result as the following:
# env.remote_storage_client.get_object(
# Bucket=env.ext_remote_storage.bucket_name,
# Key=os.path.join(BUCKET_PREFIX, PUB_EXT_PATHS[0])
# )["Body"].read()
TEST_EXT_SQL_PATH = "v14/share/postgresql/extension/test_ext--1.0.sql"
test_ext_sql_file = BytesIO(
b"""
CREATE FUNCTION test_ext_add(integer, integer) RETURNS integer
AS 'select $1 + $2;'
LANGUAGE SQL
IMMUTABLE
RETURNS NULL ON NULL INPUT;
"""
)
env.remote_storage_client.upload_fileobj(
test_ext_sql_file,
env.ext_remote_storage.bucket_name,
os.path.join(BUCKET_PREFIX, TEST_EXT_SQL_PATH),
)
# upload some fake library file
TEST_LIB_PATH = "v14/lib/test_ext.so"
test_lib_file = BytesIO(
b"""
111
"""
)
env.remote_storage_client.upload_fileobj(
test_lib_file,
env.ext_remote_storage.bucket_name,
os.path.join(BUCKET_PREFIX, TEST_LIB_PATH),
)
tenant, _ = env.neon_cli.create_tenant()
env.neon_cli.create_timeline("test_file_download", tenant_id=tenant)
remote_ext_config = json.dumps(
{
@@ -99,17 +124,13 @@ def test_file_download(neon_env_builder: NeonEnvBuilder, remote_storage_kind: Re
)
endpoint = env.endpoints.create_start(
"test_file_download", tenant_id=tenant_id, remote_ext_config=remote_ext_config
"test_file_download",
tenant_id=tenant,
remote_ext_config=remote_ext_config,
config_lines=["log_min_messages=debug3"],
)
with closing(endpoint.connect()) as conn:
with conn.cursor() as cur:
# example query: insert some values and select them
cur.execute("CREATE TABLE t(key int primary key, value text)")
for i in range(100):
cur.execute(f"insert into t values({i}, {2*i})")
cur.execute("select * from t")
log.info(cur.fetchall())
# Test query: check that test_ext0 was successfully downloaded
cur.execute("SELECT * FROM pg_available_extensions")
all_extensions = [x[0] for x in cur.fetchall()]
@@ -118,10 +139,24 @@ def test_file_download(neon_env_builder: NeonEnvBuilder, remote_storage_kind: Re
assert f"test_ext{i}" in all_extensions
assert f"private_ext{i}" in all_extensions
# TODO: can create extension actually install an extension?
# cur.execute("CREATE EXTENSION test_ext0")
# log.info("**" * 100)
# log.info(cur.fetchall())
cur.execute("CREATE EXTENSION test_ext")
cur.execute("SELECT extname FROM pg_extension")
all_extensions = [x[0] for x in cur.fetchall()]
log.info(all_extensions)
assert "test_ext" in all_extensions
try:
cur.execute("LOAD 'test_ext.so'")
except Exception as e:
# expected to fail with
# could not load library ... test_ext.so: file too short
# because test_ext.so is not real library file
log.info("LOAD test_ext.so failed (expectedly): %s", e)
assert "file too short" in str(e)
# TODO add more test cases:
# - try to load non-existing library
# cleanup downloaded extensions (TODO: the file names are quesionable here)
for file in cleanup_files:
@@ -130,3 +165,4 @@ def test_file_download(neon_env_builder: NeonEnvBuilder, remote_storage_kind: Re
os.remove(file)
except FileNotFoundError:
log.info(f"{file} does not exist, so cannot be deleted")
assert "test_ext" in all_extensions