From 5787513bd8730ab2a900708cec4f2d145e36990f Mon Sep 17 00:00:00 2001 From: Jacob Riddle Date: Thu, 5 Dec 2024 16:48:46 -0500 Subject: [PATCH 1/9] support for dbaas2 --- linode_api4/groups/database.py | 120 ++++++++++++++++ linode_api4/linode_client.py | 1 + linode_api4/objects/database.py | 13 +- .../models/database/test_database.py | 130 ++++++++---------- 4 files changed, 190 insertions(+), 74 deletions(-) diff --git a/linode_api4/groups/database.py b/linode_api4/groups/database.py index 957c136cf..384e3ff42 100644 --- a/linode_api4/groups/database.py +++ b/linode_api4/groups/database.py @@ -142,6 +142,66 @@ def mysql_create(self, label, region, engine, ltype, **kwargs): d = MySQLDatabase(self.client, result["id"], result) return d + def mysql_fork(self, source, restore_time, **kwargs): + """ + Forks an :any:`MySQLDatabase` on this account with + the given restore_time. label, region, engine, and ltype are optional. + For example:: + + client = LinodeClient(TOKEN) + + db_to_fork = client.database.mysql_instances()[0] + + new_fork = client.database.mysql_fork( + db_to_fork.id, + db_to_fork.updated, + label="new-fresh-label" + ) + + API Documentation: https://techdocs.akamai.com/linode-api/reference/post-databases-mysql-instances + + :param source: The id of the source database + :type source: int + :param restore_time: The timestamp for the fork + :type source: datetime + :param label: The name for this cluster + :type label: str + :param region: The region to deploy this cluster in + :type region: str or Region + :param engine: The engine to deploy this cluster with + :type engine: str or Engine + :param ltype: The Linode Type to use for this cluster + :type ltype: str or Type + """ + + params = { + "fork": { + "source": source, + "restore_time": restore_time.strftime("%Y-%m-%dT%H:%M:%S") + } + } + if "region" in kwargs: + region = kwargs["region"] + params["region"] = region.id if issubclass(type(region), Base) else region, + if "engine" in kwargs: + engine = kwargs["engine"] + params["engine"] = engine.id if issubclass(type(engine), Base) else engine, + if "ltype" in kwargs: + ltype = kwargs["ltype"] + params["type"] = ltype.id if issubclass(type(ltype), Base) else ltype, + + params.update(kwargs) + + result = self.client.post("/databases/mysql/instances", data=params) + + if "id" not in result: + raise UnexpectedResponseError( + "Unexpected response when creating MySQL Database", json=result + ) + + d = MySQLDatabase(self.client, result["id"], result) + return d + def postgresql_instances(self, *filters): """ Returns a list of Managed PostgreSQL Databases active on this account. @@ -209,3 +269,63 @@ def postgresql_create(self, label, region, engine, ltype, **kwargs): d = PostgreSQLDatabase(self.client, result["id"], result) return d + + def postgresql_fork(self, source, restore_time, **kwargs): + """ + Forks an :any:`PostgreSQLDatabase` on this account with + the given restore_time. label, region, engine, and ltype are optional. + For example:: + + client = LinodeClient(TOKEN) + + db_to_fork = client.database.postgresql_instances()[0] + + new_fork = client.database.postgresql_fork( + db_to_fork.id, + db_to_fork.updated, + label="new-fresh-label" + ) + + API Documentation: https://techdocs.akamai.com/linode-api/reference/post-databases-postgresql-instances + + :param source: The id of the source database + :type source: int + :param restore_time: The timestamp for the fork + :type source: datetime + :param label: The name for this cluster + :type label: str + :param region: The region to deploy this cluster in + :type region: str or Region + :param engine: The engine to deploy this cluster with + :type engine: str or Engine + :param ltype: The Linode Type to use for this cluster + :type ltype: str or Type + """ + + params = { + "fork": { + "source": source, + "restore_time": restore_time.strftime("%Y-%m-%dT%H:%M:%S") + } + } + if "region" in kwargs: + region = kwargs["region"] + params["region"] = region.id if issubclass(type(region), Base) else region, + if "engine" in kwargs: + engine = kwargs["engine"] + params["engine"] = engine.id if issubclass(type(engine), Base) else engine, + if "ltype" in kwargs: + ltype = kwargs["ltype"] + params["ltype"] = ltype.id if issubclass(type(ltype), Base) else ltype, + + params.update(kwargs) + + result = self.client.post("/databases/postgresql/instances", data=params) + + if "id" not in result: + raise UnexpectedResponseError( + "Unexpected response when creating PostgreSQL Database", json=result + ) + + d = PostgreSQLDatabase(self.client, result["id"], result) + return d diff --git a/linode_api4/linode_client.py b/linode_api4/linode_client.py index dbb45d0df..054fe2bef 100644 --- a/linode_api4/linode_client.py +++ b/linode_api4/linode_client.py @@ -275,6 +275,7 @@ def _api_call( body = None if data is not None: body = json.dumps(data) + print(body) response = method( url, diff --git a/linode_api4/objects/database.py b/linode_api4/objects/database.py index 6a028722c..ea833eb8a 100644 --- a/linode_api4/objects/database.py +++ b/linode_api4/objects/database.py @@ -131,7 +131,7 @@ class MySQLDatabase(Base): "label": Property(mutable=True), "allow_list": Property(mutable=True, unordered=True), "backups": Property(derived_class=MySQLDatabaseBackup), - "cluster_size": Property(), + "cluster_size": Property(mutable=True), "created": Property(is_datetime=True), "encrypted": Property(), "engine": Property(), @@ -141,7 +141,9 @@ class MySQLDatabase(Base): "replication_type": Property(), "ssl_connection": Property(), "status": Property(volatile=True), - "type": Property(), + "type": Property(mutable=True), + "fork": Property(), + "oldest_restore_time": Property(is_datetime=True), "updated": Property(volatile=True, is_datetime=True), "updates": Property(mutable=True), "version": Property(), @@ -264,7 +266,7 @@ class PostgreSQLDatabase(Base): "label": Property(mutable=True), "allow_list": Property(mutable=True, unordered=True), "backups": Property(derived_class=PostgreSQLDatabaseBackup), - "cluster_size": Property(), + "cluster_size": Property(mutable=True), "created": Property(is_datetime=True), "encrypted": Property(), "engine": Property(), @@ -275,7 +277,9 @@ class PostgreSQLDatabase(Base): "replication_type": Property(), "ssl_connection": Property(), "status": Property(volatile=True), - "type": Property(), + "type": Property(mutable=True), + "fork": Property(), + "oldest_restore_time": Property(is_datetime=True), "updated": Property(volatile=True, is_datetime=True), "updates": Property(mutable=True), "version": Property(), @@ -414,6 +418,7 @@ class Database(Base): "region": Property(), "status": Property(), "type": Property(), + "fork": Property(), "updated": Property(), "updates": Property(), "version": Property(), diff --git a/test/integration/models/database/test_database.py b/test/integration/models/database/test_database.py index b9502abdc..953a949e6 100644 --- a/test/integration/models/database/test_database.py +++ b/test/integration/models/database/test_database.py @@ -1,4 +1,5 @@ import re +import os import time from test.integration.helpers import ( get_test_label, @@ -35,9 +36,6 @@ def get_postgres_db_status(client: LinodeClient, db_id, status: str): @pytest.fixture(scope="session") def test_create_sql_db(test_linode_client): - pytest.skip( - "Might need Type to match how other object models are behaving e.g. client.load(Type, 123)" - ) client = test_linode_client label = get_test_label() + "-sqldb" region = "us-ord" @@ -62,12 +60,8 @@ def get_db_status(): send_request_when_resource_available(300, db.delete) - @pytest.fixture(scope="session") def test_create_postgres_db(test_linode_client): - pytest.skip( - "Might need Type to match how other object models are behaving e.g. client.load(Type, 123)" - ) client = test_linode_client label = get_test_label() + "-postgresqldb" region = "us-ord" @@ -93,11 +87,44 @@ def get_db_status(): send_request_when_resource_available(300, db.delete) -# ------- SQL DB Test cases ------- -def test_get_types(test_linode_client): - pytest.skip( - "Might need Type to match how other object models are behaving e.g. client.load(Type, 123)" +@pytest.mark.skipif(os.getenv("RUN_DB_FORK_TESTS") is None, reason="RUN_DB_FORK_TESTS environment variable not set") +def test_fork_sql_db(test_linode_client, test_create_sql_db): + client = test_linode_client + db_fork = client.database.mysql_fork( + test_create_sql_db.id, + test_create_sql_db.updated + ) + + def get_db_fork_status(): + return db_fork.status == "active" + + # TAKES 15-30 MINUTES TO FULLY PROVISION DB + wait_for_condition(60, 2000, get_db_fork_status) + + assert db_fork.fork.source == test_create_sql_db.id + + db_fork.delete() + +@pytest.mark.skipif(os.getenv("RUN_DB_FORK_TESTS") is None, reason="RUN_DB_FORK_TESTS environment variable not set") +def test_fork_postgres_db(test_linode_client, test_create_postgres_db): + client = test_linode_client + db_fork = client.database.postgresql_fork( + test_create_postgres_db.id, + test_create_postgres_db.updated ) + + def get_db_fork_status(): + return db_fork.status == "active" + + # TAKES 15-30 MINUTES TO FULLY PROVISION DB + wait_for_condition(60, 2000, get_db_fork_status) + + assert db_fork.fork.source == test_create_postgres_db.id + + db_fork.delete() + +@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") +def test_get_types(test_linode_client): client = test_linode_client types = client.database.types() @@ -106,10 +133,8 @@ def test_get_types(test_linode_client): assert types[0].engines.mongodb[0].price.monthly == 15 +@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") def test_get_engines(test_linode_client): - pytest.skip( - "Might need Type to match how other object models are behaving e.g. client.load(Type, 123)" - ) client = test_linode_client engines = client.database.engines() @@ -119,20 +144,16 @@ def test_get_engines(test_linode_client): assert e.id == e.engine + "/" + e.version +@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") def test_database_instance(test_linode_client, test_create_sql_db): - pytest.skip( - "Might need Type to match how other object models are behaving e.g. client.load(Type, 123)" - ) dbs = test_linode_client.database.mysql_instances() assert str(test_create_sql_db.id) in str(dbs.lists) # ------- POSTGRESQL DB Test cases ------- +@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") def test_get_sql_db_instance(test_linode_client, test_create_sql_db): - pytest.skip( - "Might need Type to match how other object models are behaving e.g. client.load(Type, 123)" - ) dbs = test_linode_client.database.mysql_instances() database = "" for db in dbs: @@ -146,10 +167,8 @@ def test_get_sql_db_instance(test_linode_client, test_create_sql_db): assert "-mysql-primary.servers.linodedb.net" in database.hosts.primary +@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") def test_update_sql_db(test_linode_client, test_create_sql_db): - pytest.skip( - "Might need Type to match how other object models are behaving e.g. client.load(Type, 123)" - ) db = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) new_allow_list = ["192.168.0.1/32"] @@ -178,10 +197,8 @@ def test_update_sql_db(test_linode_client, test_create_sql_db): assert database.updates.day_of_week == 2 +@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") def test_create_sql_backup(test_linode_client, test_create_sql_db): - pytest.skip( - "Might need Type to match how other object models are behaving e.g. client.load(Type, 123)" - ) db = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) label = "database_backup_test" @@ -227,14 +244,12 @@ def test_create_sql_backup(test_linode_client, test_create_sql_db): backup.delete() +@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") def test_sql_backup_restore(test_linode_client, test_create_sql_db): - pytest.skip( - "Might need Type to match how other object models are behaving e.g. client.load(Type, 123)" - ) db = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) try: backup = db.backups[0] - except IndexError as e: + except IndexError: pytest.skip( "Skipping this test. Reason: Couldn't find db backup instance" ) @@ -264,19 +279,15 @@ def test_sql_backup_restore(test_linode_client, test_create_sql_db): assert db.status == "active" +@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") def test_get_sql_ssl(test_linode_client, test_create_sql_db): - pytest.skip( - "Might need Type to match how other object models are behaving e.g. client.load(Type, 123)" - ) db = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) assert "ca_certificate" in str(db.ssl) +@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") def test_sql_patch(test_linode_client, test_create_sql_db): - pytest.skip( - "Might need Type to match how other object models are behaving e.g. client.load(Type, 123)" - ) db = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) db.patch() @@ -304,20 +315,16 @@ def test_sql_patch(test_linode_client, test_create_sql_db): assert db.status == "active" +@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") def test_get_sql_credentials(test_linode_client, test_create_sql_db): - pytest.skip( - "Might need Type to match how other object models are behaving e.g. client.load(Type, 123)" - ) db = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) assert db.credentials.username == "linroot" assert db.credentials.password +@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") def test_reset_sql_credentials(test_linode_client, test_create_sql_db): - pytest.skip( - "Might need Type to match how other object models are behaving e.g. client.load(Type, 123)" - ) db = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) old_pass = str(db.credentials.password) @@ -332,12 +339,12 @@ def test_reset_sql_credentials(test_linode_client, test_create_sql_db): # ------- POSTGRESQL DB Test cases ------- +@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") def test_get_postgres_db_instance(test_linode_client, test_create_postgres_db): - pytest.skip( - "Might need Type to match how other object models are behaving e.g. client.load(Type, 123)" - ) dbs = test_linode_client.database.postgresql_instances() + database = None + for db in dbs: if db.id == test_create_postgres_db.id: database = db @@ -349,10 +356,8 @@ def test_get_postgres_db_instance(test_linode_client, test_create_postgres_db): assert "pgsql-primary.servers.linodedb.net" in database.hosts.primary +@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") def test_update_postgres_db(test_linode_client, test_create_postgres_db): - pytest.skip( - "Might need Type to match how other object models are behaving e.g. client.load(Type, 123)" - ) db = test_linode_client.load(PostgreSQLDatabase, test_create_postgres_db.id) new_allow_list = ["192.168.0.1/32"] @@ -383,13 +388,8 @@ def test_update_postgres_db(test_linode_client, test_create_postgres_db): assert database.updates.day_of_week == 2 +@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") def test_create_postgres_backup(test_linode_client, test_create_postgres_db): - pytest.skip( - "Might need Type to match how other object models are behaving e.g. client.load(Type, 123)" - ) - pytest.skip( - "Failing due to '400: The backup snapshot request failed, please contact support.'" - ) db = test_linode_client.load(PostgreSQLDatabase, test_create_postgres_db.id) label = "database_backup_test" @@ -433,15 +433,13 @@ def test_create_postgres_backup(test_linode_client, test_create_postgres_db): assert backup.database_id == test_create_postgres_db.id +@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") def test_postgres_backup_restore(test_linode_client, test_create_postgres_db): - pytest.skip( - "Might need Type to match how other object models are behaving e.g. client.load(Type, 123)" - ) db = test_linode_client.load(PostgreSQLDatabase, test_create_postgres_db.id) try: backup = db.backups[0] - except IndexError as e: + except IndexError: pytest.skip( "Skipping this test. Reason: Couldn't find db backup instance" ) @@ -469,19 +467,15 @@ def test_postgres_backup_restore(test_linode_client, test_create_postgres_db): assert db.status == "active" +@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") def test_get_postgres_ssl(test_linode_client, test_create_postgres_db): - pytest.skip( - "Might need Type to match how other object models are behaving e.g. client.load(Type, 123)" - ) db = test_linode_client.load(PostgreSQLDatabase, test_create_postgres_db.id) assert "ca_certificate" in str(db.ssl) +@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") def test_postgres_patch(test_linode_client, test_create_postgres_db): - pytest.skip( - "Might need Type to match how other object models are behaving e.g. client.load(Type, 123)" - ) db = test_linode_client.load(PostgreSQLDatabase, test_create_postgres_db.id) db.patch() @@ -509,22 +503,18 @@ def test_postgres_patch(test_linode_client, test_create_postgres_db): assert db.status == "active" +@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") def test_get_postgres_credentials(test_linode_client, test_create_postgres_db): - pytest.skip( - "Might need Type to match how other object models are behaving e.g. client.load(Type, 123)" - ) db = test_linode_client.load(PostgreSQLDatabase, test_create_postgres_db.id) assert db.credentials.username == "linpostgres" assert db.credentials.password +@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") def test_reset_postgres_credentials( test_linode_client, test_create_postgres_db ): - pytest.skip( - "Might need Type to match how other object models are behaving e.g. client.load(Type, 123)" - ) db = test_linode_client.load(PostgreSQLDatabase, test_create_postgres_db.id) old_pass = str(db.credentials.password) From 6147fa6795600915b9a29362bb172f72ef7f55f1 Mon Sep 17 00:00:00 2001 From: Jacob Riddle Date: Mon, 9 Dec 2024 14:24:47 -0500 Subject: [PATCH 2/9] make format --- linode_api4/groups/database.py | 35 ++++-- .../models/database/test_database.py | 116 ++++++++++++++---- 2 files changed, 115 insertions(+), 36 deletions(-) diff --git a/linode_api4/groups/database.py b/linode_api4/groups/database.py index 384e3ff42..38b4fd7a7 100644 --- a/linode_api4/groups/database.py +++ b/linode_api4/groups/database.py @@ -177,18 +177,24 @@ def mysql_fork(self, source, restore_time, **kwargs): params = { "fork": { "source": source, - "restore_time": restore_time.strftime("%Y-%m-%dT%H:%M:%S") + "restore_time": restore_time.strftime("%Y-%m-%dT%H:%M:%S"), } } if "region" in kwargs: region = kwargs["region"] - params["region"] = region.id if issubclass(type(region), Base) else region, + params["region"] = ( + region.id if issubclass(type(region), Base) else region, + ) if "engine" in kwargs: engine = kwargs["engine"] - params["engine"] = engine.id if issubclass(type(engine), Base) else engine, + params["engine"] = ( + engine.id if issubclass(type(engine), Base) else engine, + ) if "ltype" in kwargs: ltype = kwargs["ltype"] - params["type"] = ltype.id if issubclass(type(ltype), Base) else ltype, + params["type"] = ( + ltype.id if issubclass(type(ltype), Base) else ltype, + ) params.update(kwargs) @@ -305,26 +311,35 @@ def postgresql_fork(self, source, restore_time, **kwargs): params = { "fork": { "source": source, - "restore_time": restore_time.strftime("%Y-%m-%dT%H:%M:%S") + "restore_time": restore_time.strftime("%Y-%m-%dT%H:%M:%S"), } } if "region" in kwargs: region = kwargs["region"] - params["region"] = region.id if issubclass(type(region), Base) else region, + params["region"] = ( + region.id if issubclass(type(region), Base) else region, + ) if "engine" in kwargs: engine = kwargs["engine"] - params["engine"] = engine.id if issubclass(type(engine), Base) else engine, + params["engine"] = ( + engine.id if issubclass(type(engine), Base) else engine, + ) if "ltype" in kwargs: ltype = kwargs["ltype"] - params["ltype"] = ltype.id if issubclass(type(ltype), Base) else ltype, + params["ltype"] = ( + ltype.id if issubclass(type(ltype), Base) else ltype, + ) params.update(kwargs) - result = self.client.post("/databases/postgresql/instances", data=params) + result = self.client.post( + "/databases/postgresql/instances", data=params + ) if "id" not in result: raise UnexpectedResponseError( - "Unexpected response when creating PostgreSQL Database", json=result + "Unexpected response when creating PostgreSQL Database", + json=result, ) d = PostgreSQLDatabase(self.client, result["id"], result) diff --git a/test/integration/models/database/test_database.py b/test/integration/models/database/test_database.py index 953a949e6..7782d305c 100644 --- a/test/integration/models/database/test_database.py +++ b/test/integration/models/database/test_database.py @@ -1,5 +1,5 @@ -import re import os +import re import time from test.integration.helpers import ( get_test_label, @@ -60,6 +60,7 @@ def get_db_status(): send_request_when_resource_available(300, db.delete) + @pytest.fixture(scope="session") def test_create_postgres_db(test_linode_client): client = test_linode_client @@ -87,12 +88,14 @@ def get_db_status(): send_request_when_resource_available(300, db.delete) -@pytest.mark.skipif(os.getenv("RUN_DB_FORK_TESTS") is None, reason="RUN_DB_FORK_TESTS environment variable not set") +@pytest.mark.skipif( + os.getenv("RUN_DB_FORK_TESTS") is None, + reason="RUN_DB_FORK_TESTS environment variable not set", +) def test_fork_sql_db(test_linode_client, test_create_sql_db): client = test_linode_client db_fork = client.database.mysql_fork( - test_create_sql_db.id, - test_create_sql_db.updated + test_create_sql_db.id, test_create_sql_db.updated ) def get_db_fork_status(): @@ -105,12 +108,15 @@ def get_db_fork_status(): db_fork.delete() -@pytest.mark.skipif(os.getenv("RUN_DB_FORK_TESTS") is None, reason="RUN_DB_FORK_TESTS environment variable not set") + +@pytest.mark.skipif( + os.getenv("RUN_DB_FORK_TESTS") is None, + reason="RUN_DB_FORK_TESTS environment variable not set", +) def test_fork_postgres_db(test_linode_client, test_create_postgres_db): client = test_linode_client db_fork = client.database.postgresql_fork( - test_create_postgres_db.id, - test_create_postgres_db.updated + test_create_postgres_db.id, test_create_postgres_db.updated ) def get_db_fork_status(): @@ -123,7 +129,11 @@ def get_db_fork_status(): db_fork.delete() -@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") + +@pytest.mark.skipif( + os.getenv("RUN_DB_TESTS") is None, + reason="RUN_DB_TESTS environment variable not set", +) def test_get_types(test_linode_client): client = test_linode_client types = client.database.types() @@ -133,7 +143,10 @@ def test_get_types(test_linode_client): assert types[0].engines.mongodb[0].price.monthly == 15 -@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") +@pytest.mark.skipif( + os.getenv("RUN_DB_TESTS") is None, + reason="RUN_DB_TESTS environment variable not set", +) def test_get_engines(test_linode_client): client = test_linode_client engines = client.database.engines() @@ -144,7 +157,10 @@ def test_get_engines(test_linode_client): assert e.id == e.engine + "/" + e.version -@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") +@pytest.mark.skipif( + os.getenv("RUN_DB_TESTS") is None, + reason="RUN_DB_TESTS environment variable not set", +) def test_database_instance(test_linode_client, test_create_sql_db): dbs = test_linode_client.database.mysql_instances() @@ -152,7 +168,10 @@ def test_database_instance(test_linode_client, test_create_sql_db): # ------- POSTGRESQL DB Test cases ------- -@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") +@pytest.mark.skipif( + os.getenv("RUN_DB_TESTS") is None, + reason="RUN_DB_TESTS environment variable not set", +) def test_get_sql_db_instance(test_linode_client, test_create_sql_db): dbs = test_linode_client.database.mysql_instances() database = "" @@ -167,7 +186,10 @@ def test_get_sql_db_instance(test_linode_client, test_create_sql_db): assert "-mysql-primary.servers.linodedb.net" in database.hosts.primary -@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") +@pytest.mark.skipif( + os.getenv("RUN_DB_TESTS") is None, + reason="RUN_DB_TESTS environment variable not set", +) def test_update_sql_db(test_linode_client, test_create_sql_db): db = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) @@ -197,7 +219,10 @@ def test_update_sql_db(test_linode_client, test_create_sql_db): assert database.updates.day_of_week == 2 -@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") +@pytest.mark.skipif( + os.getenv("RUN_DB_TESTS") is None, + reason="RUN_DB_TESTS environment variable not set", +) def test_create_sql_backup(test_linode_client, test_create_sql_db): db = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) label = "database_backup_test" @@ -244,7 +269,10 @@ def test_create_sql_backup(test_linode_client, test_create_sql_db): backup.delete() -@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") +@pytest.mark.skipif( + os.getenv("RUN_DB_TESTS") is None, + reason="RUN_DB_TESTS environment variable not set", +) def test_sql_backup_restore(test_linode_client, test_create_sql_db): db = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) try: @@ -279,14 +307,20 @@ def test_sql_backup_restore(test_linode_client, test_create_sql_db): assert db.status == "active" -@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") +@pytest.mark.skipif( + os.getenv("RUN_DB_TESTS") is None, + reason="RUN_DB_TESTS environment variable not set", +) def test_get_sql_ssl(test_linode_client, test_create_sql_db): db = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) assert "ca_certificate" in str(db.ssl) -@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") +@pytest.mark.skipif( + os.getenv("RUN_DB_TESTS") is None, + reason="RUN_DB_TESTS environment variable not set", +) def test_sql_patch(test_linode_client, test_create_sql_db): db = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) @@ -315,7 +349,10 @@ def test_sql_patch(test_linode_client, test_create_sql_db): assert db.status == "active" -@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") +@pytest.mark.skipif( + os.getenv("RUN_DB_TESTS") is None, + reason="RUN_DB_TESTS environment variable not set", +) def test_get_sql_credentials(test_linode_client, test_create_sql_db): db = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) @@ -323,7 +360,10 @@ def test_get_sql_credentials(test_linode_client, test_create_sql_db): assert db.credentials.password -@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") +@pytest.mark.skipif( + os.getenv("RUN_DB_TESTS") is None, + reason="RUN_DB_TESTS environment variable not set", +) def test_reset_sql_credentials(test_linode_client, test_create_sql_db): db = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) @@ -339,7 +379,10 @@ def test_reset_sql_credentials(test_linode_client, test_create_sql_db): # ------- POSTGRESQL DB Test cases ------- -@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") +@pytest.mark.skipif( + os.getenv("RUN_DB_TESTS") is None, + reason="RUN_DB_TESTS environment variable not set", +) def test_get_postgres_db_instance(test_linode_client, test_create_postgres_db): dbs = test_linode_client.database.postgresql_instances() @@ -356,7 +399,10 @@ def test_get_postgres_db_instance(test_linode_client, test_create_postgres_db): assert "pgsql-primary.servers.linodedb.net" in database.hosts.primary -@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") +@pytest.mark.skipif( + os.getenv("RUN_DB_TESTS") is None, + reason="RUN_DB_TESTS environment variable not set", +) def test_update_postgres_db(test_linode_client, test_create_postgres_db): db = test_linode_client.load(PostgreSQLDatabase, test_create_postgres_db.id) @@ -388,7 +434,10 @@ def test_update_postgres_db(test_linode_client, test_create_postgres_db): assert database.updates.day_of_week == 2 -@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") +@pytest.mark.skipif( + os.getenv("RUN_DB_TESTS") is None, + reason="RUN_DB_TESTS environment variable not set", +) def test_create_postgres_backup(test_linode_client, test_create_postgres_db): db = test_linode_client.load(PostgreSQLDatabase, test_create_postgres_db.id) label = "database_backup_test" @@ -433,7 +482,10 @@ def test_create_postgres_backup(test_linode_client, test_create_postgres_db): assert backup.database_id == test_create_postgres_db.id -@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") +@pytest.mark.skipif( + os.getenv("RUN_DB_TESTS") is None, + reason="RUN_DB_TESTS environment variable not set", +) def test_postgres_backup_restore(test_linode_client, test_create_postgres_db): db = test_linode_client.load(PostgreSQLDatabase, test_create_postgres_db.id) @@ -467,14 +519,20 @@ def test_postgres_backup_restore(test_linode_client, test_create_postgres_db): assert db.status == "active" -@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") +@pytest.mark.skipif( + os.getenv("RUN_DB_TESTS") is None, + reason="RUN_DB_TESTS environment variable not set", +) def test_get_postgres_ssl(test_linode_client, test_create_postgres_db): db = test_linode_client.load(PostgreSQLDatabase, test_create_postgres_db.id) assert "ca_certificate" in str(db.ssl) -@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") +@pytest.mark.skipif( + os.getenv("RUN_DB_TESTS") is None, + reason="RUN_DB_TESTS environment variable not set", +) def test_postgres_patch(test_linode_client, test_create_postgres_db): db = test_linode_client.load(PostgreSQLDatabase, test_create_postgres_db.id) @@ -503,7 +561,10 @@ def test_postgres_patch(test_linode_client, test_create_postgres_db): assert db.status == "active" -@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") +@pytest.mark.skipif( + os.getenv("RUN_DB_TESTS") is None, + reason="RUN_DB_TESTS environment variable not set", +) def test_get_postgres_credentials(test_linode_client, test_create_postgres_db): db = test_linode_client.load(PostgreSQLDatabase, test_create_postgres_db.id) @@ -511,7 +572,10 @@ def test_get_postgres_credentials(test_linode_client, test_create_postgres_db): assert db.credentials.password -@pytest.mark.skipif(os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set") +@pytest.mark.skipif( + os.getenv("RUN_DB_TESTS") is None, + reason="RUN_DB_TESTS environment variable not set", +) def test_reset_postgres_credentials( test_linode_client, test_create_postgres_db ): From b5e93e2051a98b4cdd5e754300aada84c9fa4df2 Mon Sep 17 00:00:00 2001 From: Jacob Riddle Date: Fri, 13 Dec 2024 15:12:21 -0500 Subject: [PATCH 3/9] use flatten --- linode_api4/groups/database.py | 64 ++++++++++++---------------------- 1 file changed, 22 insertions(+), 42 deletions(-) diff --git a/linode_api4/groups/database.py b/linode_api4/groups/database.py index 38b4fd7a7..776f75960 100644 --- a/linode_api4/groups/database.py +++ b/linode_api4/groups/database.py @@ -1,14 +1,14 @@ from linode_api4.errors import UnexpectedResponseError from linode_api4.groups import Group from linode_api4.objects import ( - Base, Database, DatabaseEngine, DatabaseType, MySQLDatabase, PostgreSQLDatabase, + drop_null_keys, ) - +from linode_api4.objects.base import _flatten_request_body_recursive class DatabaseGroup(Group): """ @@ -126,13 +126,16 @@ def mysql_create(self, label, region, engine, ltype, **kwargs): params = { "label": label, - "region": region.id if issubclass(type(region), Base) else region, - "engine": engine.id if issubclass(type(engine), Base) else engine, - "type": ltype.id if issubclass(type(ltype), Base) else ltype, + "region": region, + "engine": engine, + "type": ltype, } params.update(kwargs) - result = self.client.post("/databases/mysql/instances", data=params) + result = self.client.post( + "/databases/mysql/instances", + data=_flatten_request_body_recursive(drop_null_keys(params)) + ) if "id" not in result: raise UnexpectedResponseError( @@ -180,25 +183,14 @@ def mysql_fork(self, source, restore_time, **kwargs): "restore_time": restore_time.strftime("%Y-%m-%dT%H:%M:%S"), } } - if "region" in kwargs: - region = kwargs["region"] - params["region"] = ( - region.id if issubclass(type(region), Base) else region, - ) - if "engine" in kwargs: - engine = kwargs["engine"] - params["engine"] = ( - engine.id if issubclass(type(engine), Base) else engine, - ) if "ltype" in kwargs: - ltype = kwargs["ltype"] - params["type"] = ( - ltype.id if issubclass(type(ltype), Base) else ltype, - ) - + params["type"] = kwargs["ltype"] params.update(kwargs) - result = self.client.post("/databases/mysql/instances", data=params) + result = self.client.post( + "/databases/mysql/instances", + data=_flatten_request_body_recursive(drop_null_keys(params)) + ) if "id" not in result: raise UnexpectedResponseError( @@ -257,14 +249,15 @@ def postgresql_create(self, label, region, engine, ltype, **kwargs): params = { "label": label, - "region": region.id if issubclass(type(region), Base) else region, - "engine": engine.id if issubclass(type(engine), Base) else engine, - "type": ltype.id if issubclass(type(ltype), Base) else ltype, + "region": region, + "engine": engine, + "type": ltype, } params.update(kwargs) result = self.client.post( - "/databases/postgresql/instances", data=params + "/databases/postgresql/instances", + data=_flatten_request_body_recursive(drop_null_keys(params)) ) if "id" not in result: @@ -314,26 +307,13 @@ def postgresql_fork(self, source, restore_time, **kwargs): "restore_time": restore_time.strftime("%Y-%m-%dT%H:%M:%S"), } } - if "region" in kwargs: - region = kwargs["region"] - params["region"] = ( - region.id if issubclass(type(region), Base) else region, - ) - if "engine" in kwargs: - engine = kwargs["engine"] - params["engine"] = ( - engine.id if issubclass(type(engine), Base) else engine, - ) if "ltype" in kwargs: - ltype = kwargs["ltype"] - params["ltype"] = ( - ltype.id if issubclass(type(ltype), Base) else ltype, - ) - + params["type"] = kwargs["ltype"] params.update(kwargs) result = self.client.post( - "/databases/postgresql/instances", data=params + "/databases/postgresql/instances", + data=_flatten_request_body_recursive(drop_null_keys(params)) ) if "id" not in result: From 5b5057893860db7e58f9558188706e2dbe533b09 Mon Sep 17 00:00:00 2001 From: Jacob Riddle Date: Fri, 13 Dec 2024 15:13:38 -0500 Subject: [PATCH 4/9] format --- linode_api4/groups/database.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/linode_api4/groups/database.py b/linode_api4/groups/database.py index 776f75960..ebc79b9f2 100644 --- a/linode_api4/groups/database.py +++ b/linode_api4/groups/database.py @@ -10,6 +10,7 @@ ) from linode_api4.objects.base import _flatten_request_body_recursive + class DatabaseGroup(Group): """ Encapsulates Linode Managed Databases related methods of the :any:`LinodeClient`. This @@ -133,8 +134,8 @@ def mysql_create(self, label, region, engine, ltype, **kwargs): params.update(kwargs) result = self.client.post( - "/databases/mysql/instances", - data=_flatten_request_body_recursive(drop_null_keys(params)) + "/databases/mysql/instances", + data=_flatten_request_body_recursive(drop_null_keys(params)), ) if "id" not in result: @@ -188,8 +189,8 @@ def mysql_fork(self, source, restore_time, **kwargs): params.update(kwargs) result = self.client.post( - "/databases/mysql/instances", - data=_flatten_request_body_recursive(drop_null_keys(params)) + "/databases/mysql/instances", + data=_flatten_request_body_recursive(drop_null_keys(params)), ) if "id" not in result: @@ -256,8 +257,8 @@ def postgresql_create(self, label, region, engine, ltype, **kwargs): params.update(kwargs) result = self.client.post( - "/databases/postgresql/instances", - data=_flatten_request_body_recursive(drop_null_keys(params)) + "/databases/postgresql/instances", + data=_flatten_request_body_recursive(drop_null_keys(params)), ) if "id" not in result: @@ -312,8 +313,8 @@ def postgresql_fork(self, source, restore_time, **kwargs): params.update(kwargs) result = self.client.post( - "/databases/postgresql/instances", - data=_flatten_request_body_recursive(drop_null_keys(params)) + "/databases/postgresql/instances", + data=_flatten_request_body_recursive(drop_null_keys(params)), ) if "id" not in result: From 507175f5691fbaf2de7b3bbab21bf82cb76acf44 Mon Sep 17 00:00:00 2001 From: Jacob Riddle Date: Mon, 6 Jan 2025 14:12:12 -0500 Subject: [PATCH 5/9] feedback --- linode_api4/groups/database.py | 16 ++++++++-------- linode_api4/linode_client.py | 1 - 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/linode_api4/groups/database.py b/linode_api4/groups/database.py index ebc79b9f2..8110ea888 100644 --- a/linode_api4/groups/database.py +++ b/linode_api4/groups/database.py @@ -167,15 +167,15 @@ def mysql_fork(self, source, restore_time, **kwargs): :param source: The id of the source database :type source: int :param restore_time: The timestamp for the fork - :type source: datetime + :type restore_time: datetime :param label: The name for this cluster :type label: str :param region: The region to deploy this cluster in - :type region: str or Region + :type region: str | Region :param engine: The engine to deploy this cluster with - :type engine: str or Engine + :type engine: str | Engine :param ltype: The Linode Type to use for this cluster - :type ltype: str or Type + :type ltype: str | Type """ params = { @@ -291,15 +291,15 @@ def postgresql_fork(self, source, restore_time, **kwargs): :param source: The id of the source database :type source: int :param restore_time: The timestamp for the fork - :type source: datetime + :type restore_time: datetime :param label: The name for this cluster :type label: str :param region: The region to deploy this cluster in - :type region: str or Region + :type region: str | Region :param engine: The engine to deploy this cluster with - :type engine: str or Engine + :type engine: str | Engine :param ltype: The Linode Type to use for this cluster - :type ltype: str or Type + :type ltype: str | Type """ params = { diff --git a/linode_api4/linode_client.py b/linode_api4/linode_client.py index 054fe2bef..dbb45d0df 100644 --- a/linode_api4/linode_client.py +++ b/linode_api4/linode_client.py @@ -275,7 +275,6 @@ def _api_call( body = None if data is not None: body = json.dumps(data) - print(body) response = method( url, From fe6c277e0829633f36895a3d9c6572638982a704 Mon Sep 17 00:00:00 2001 From: ykim-1 Date: Mon, 3 Feb 2025 11:56:03 -0800 Subject: [PATCH 6/9] fix update test cases --- .../models/database/test_database.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/integration/models/database/test_database.py b/test/integration/models/database/test_database.py index 7782d305c..42e193754 100644 --- a/test/integration/models/database/test_database.py +++ b/test/integration/models/database/test_database.py @@ -89,8 +89,8 @@ def get_db_status(): @pytest.mark.skipif( - os.getenv("RUN_DB_FORK_TESTS") is None, - reason="RUN_DB_FORK_TESTS environment variable not set", + os.getenv("RUN_DB_FORK_TESTS", "").strip().lower() not in {"yes", "true"}, + reason="RUN_DB_FORK_TESTS environment variable must be set to 'yes' or 'true' (case insensitive)", ) def test_fork_sql_db(test_linode_client, test_create_sql_db): client = test_linode_client @@ -110,8 +110,8 @@ def get_db_fork_status(): @pytest.mark.skipif( - os.getenv("RUN_DB_FORK_TESTS") is None, - reason="RUN_DB_FORK_TESTS environment variable not set", + os.getenv("RUN_DB_FORK_TESTS", "").strip().lower() not in {"yes", "true"}, + reason="RUN_DB_FORK_TESTS environment variable must be set to 'yes' or 'true' (case insensitive)", ) def test_fork_postgres_db(test_linode_client, test_create_postgres_db): client = test_linode_client @@ -202,8 +202,6 @@ def test_update_sql_db(test_linode_client, test_create_sql_db): res = db.save() - database = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) - wait_for_condition( 30, 300, @@ -213,6 +211,8 @@ def test_update_sql_db(test_linode_client, test_create_sql_db): "active", ) + database = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) + assert res assert database.allow_list == new_allow_list assert database.label == label @@ -415,10 +415,6 @@ def test_update_postgres_db(test_linode_client, test_create_postgres_db): res = db.save() - database = test_linode_client.load( - PostgreSQLDatabase, test_create_postgres_db.id - ) - wait_for_condition( 30, 1000, @@ -428,6 +424,10 @@ def test_update_postgres_db(test_linode_client, test_create_postgres_db): "active", ) + database = test_linode_client.load( + PostgreSQLDatabase, test_create_postgres_db.id + ) + assert res assert database.allow_list == new_allow_list assert database.label == label From 4db4bfdd602852d119976f2aa03599a499878b0b Mon Sep 17 00:00:00 2001 From: ykim-1 Date: Mon, 3 Feb 2025 12:04:32 -0800 Subject: [PATCH 7/9] update workflows and improve RUN_DB_TESTS handling --- .github/workflows/e2e-test-pr.yml | 18 ++++- .github/workflows/e2e-test.yml | 21 ++++- .../models/database/test_database.py | 76 +++++++++---------- 3 files changed, 75 insertions(+), 40 deletions(-) diff --git a/.github/workflows/e2e-test-pr.yml b/.github/workflows/e2e-test-pr.yml index b90ee1796..406f44ee7 100644 --- a/.github/workflows/e2e-test-pr.yml +++ b/.github/workflows/e2e-test-pr.yml @@ -2,6 +2,22 @@ on: pull_request: workflow_dispatch: inputs: + run_db_fork_tests: + description: 'Set this parameter to "true" to run fork database related test cases' + required: false + default: 'false' + type: choice + options: + - 'true' + - 'false' + run_db_tests: + description: 'Set this parameter to "true" to run database related test cases' + required: false + default: 'false' + type: choice + options: + - 'true' + - 'false' test_suite: description: 'Enter specific test suite. E.g. domain, linode_client' required: false @@ -80,7 +96,7 @@ jobs: run: | timestamp=$(date +'%Y%m%d%H%M') report_filename="${timestamp}_sdk_test_report.xml" - make testint TEST_ARGS="--junitxml=${report_filename}" TEST_SUITE="${{ github.event.inputs.test_suite }}" + make testint RUN_DB_FORK_TESTS=${{ github.event.inputs.run_db_fork_tests }} RUN_DB_TESTS=${{ github.event.inputs.run_db_tests }} TEST_ARGS="--junitxml=${report_filename}" TEST_SUITE="${{ github.event.inputs.test_suite }}" env: LINODE_TOKEN: ${{ secrets.LINODE_TOKEN }} diff --git a/.github/workflows/e2e-test.yml b/.github/workflows/e2e-test.yml index e02b708e1..951cc7db1 100644 --- a/.github/workflows/e2e-test.yml +++ b/.github/workflows/e2e-test.yml @@ -3,6 +3,25 @@ name: Integration Tests on: workflow_dispatch: inputs: + run_db_fork_tests: + description: 'Set this parameter to "true" to run fork database related test cases' + required: false + default: 'false' + type: choice + options: + - 'true' + - 'false' + run_db_tests: + description: 'Set this parameter to "true" to run database related test cases' + required: false + default: 'false' + type: choice + options: + - 'true' + - 'false' + test_suite: + description: 'Enter specific test suite. E.g. domain, linode_client' + required: false use_minimal_test_account: description: 'Use minimal test account' required: false @@ -72,7 +91,7 @@ jobs: run: | timestamp=$(date +'%Y%m%d%H%M') report_filename="${timestamp}_sdk_test_report.xml" - make testint TEST_ARGS="--junitxml=${report_filename}" + make testint RUN_DB_FORK_TESTS=${{ github.event.inputs.run_db_fork_tests }} RUN_DB_TESTS=${{ github.event.inputs.run_db_tests }} TEST_SUITE="${{ github.event.inputs.test_suite }}" TEST_ARGS="--junitxml=${report_filename}" env: LINODE_TOKEN: ${{ env.LINODE_TOKEN }} diff --git a/test/integration/models/database/test_database.py b/test/integration/models/database/test_database.py index 42e193754..8a022cb00 100644 --- a/test/integration/models/database/test_database.py +++ b/test/integration/models/database/test_database.py @@ -131,8 +131,8 @@ def get_db_fork_status(): @pytest.mark.skipif( - os.getenv("RUN_DB_TESTS") is None, - reason="RUN_DB_TESTS environment variable not set", + os.getenv("RUN_DB_TESTS").strip().lower() not in {"yes", "true"}, + reason="RUN_DB_TESTS environment variable must be set to 'yes' or 'true' (case insensitive)", ) def test_get_types(test_linode_client): client = test_linode_client @@ -144,8 +144,8 @@ def test_get_types(test_linode_client): @pytest.mark.skipif( - os.getenv("RUN_DB_TESTS") is None, - reason="RUN_DB_TESTS environment variable not set", + os.getenv("RUN_DB_TESTS").strip().lower() not in {"yes", "true"}, + reason="RUN_DB_TESTS environment variable must be set to 'yes' or 'true' (case insensitive)", ) def test_get_engines(test_linode_client): client = test_linode_client @@ -158,8 +158,8 @@ def test_get_engines(test_linode_client): @pytest.mark.skipif( - os.getenv("RUN_DB_TESTS") is None, - reason="RUN_DB_TESTS environment variable not set", + os.getenv("RUN_DB_TESTS").strip().lower() not in {"yes", "true"}, + reason="RUN_DB_TESTS environment variable must be set to 'yes' or 'true' (case insensitive)", ) def test_database_instance(test_linode_client, test_create_sql_db): dbs = test_linode_client.database.mysql_instances() @@ -169,8 +169,8 @@ def test_database_instance(test_linode_client, test_create_sql_db): # ------- POSTGRESQL DB Test cases ------- @pytest.mark.skipif( - os.getenv("RUN_DB_TESTS") is None, - reason="RUN_DB_TESTS environment variable not set", + os.getenv("RUN_DB_TESTS").strip().lower() not in {"yes", "true"}, + reason="RUN_DB_TESTS environment variable must be set to 'yes' or 'true' (case insensitive)", ) def test_get_sql_db_instance(test_linode_client, test_create_sql_db): dbs = test_linode_client.database.mysql_instances() @@ -187,8 +187,8 @@ def test_get_sql_db_instance(test_linode_client, test_create_sql_db): @pytest.mark.skipif( - os.getenv("RUN_DB_TESTS") is None, - reason="RUN_DB_TESTS environment variable not set", + os.getenv("RUN_DB_TESTS").strip().lower() not in {"yes", "true"}, + reason="RUN_DB_TESTS environment variable must be set to 'yes' or 'true' (case insensitive)", ) def test_update_sql_db(test_linode_client, test_create_sql_db): db = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) @@ -220,8 +220,8 @@ def test_update_sql_db(test_linode_client, test_create_sql_db): @pytest.mark.skipif( - os.getenv("RUN_DB_TESTS") is None, - reason="RUN_DB_TESTS environment variable not set", + os.getenv("RUN_DB_TESTS").strip().lower() not in {"yes", "true"}, + reason="RUN_DB_TESTS environment variable must be set to 'yes' or 'true' (case insensitive)", ) def test_create_sql_backup(test_linode_client, test_create_sql_db): db = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) @@ -270,8 +270,8 @@ def test_create_sql_backup(test_linode_client, test_create_sql_db): @pytest.mark.skipif( - os.getenv("RUN_DB_TESTS") is None, - reason="RUN_DB_TESTS environment variable not set", + os.getenv("RUN_DB_TESTS").strip().lower() not in {"yes", "true"}, + reason="RUN_DB_TESTS environment variable must be set to 'yes' or 'true' (case insensitive)", ) def test_sql_backup_restore(test_linode_client, test_create_sql_db): db = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) @@ -308,8 +308,8 @@ def test_sql_backup_restore(test_linode_client, test_create_sql_db): @pytest.mark.skipif( - os.getenv("RUN_DB_TESTS") is None, - reason="RUN_DB_TESTS environment variable not set", + os.getenv("RUN_DB_TESTS").strip().lower() not in {"yes", "true"}, + reason="RUN_DB_TESTS environment variable must be set to 'yes' or 'true' (case insensitive)", ) def test_get_sql_ssl(test_linode_client, test_create_sql_db): db = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) @@ -318,8 +318,8 @@ def test_get_sql_ssl(test_linode_client, test_create_sql_db): @pytest.mark.skipif( - os.getenv("RUN_DB_TESTS") is None, - reason="RUN_DB_TESTS environment variable not set", + os.getenv("RUN_DB_TESTS").strip().lower() not in {"yes", "true"}, + reason="RUN_DB_TESTS environment variable must be set to 'yes' or 'true' (case insensitive)", ) def test_sql_patch(test_linode_client, test_create_sql_db): db = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) @@ -350,8 +350,8 @@ def test_sql_patch(test_linode_client, test_create_sql_db): @pytest.mark.skipif( - os.getenv("RUN_DB_TESTS") is None, - reason="RUN_DB_TESTS environment variable not set", + os.getenv("RUN_DB_TESTS").strip().lower() not in {"yes", "true"}, + reason="RUN_DB_TESTS environment variable must be set to 'yes' or 'true' (case insensitive)", ) def test_get_sql_credentials(test_linode_client, test_create_sql_db): db = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) @@ -361,8 +361,8 @@ def test_get_sql_credentials(test_linode_client, test_create_sql_db): @pytest.mark.skipif( - os.getenv("RUN_DB_TESTS") is None, - reason="RUN_DB_TESTS environment variable not set", + os.getenv("RUN_DB_TESTS").strip().lower() not in {"yes", "true"}, + reason="RUN_DB_TESTS environment variable must be set to 'yes' or 'true' (case insensitive)", ) def test_reset_sql_credentials(test_linode_client, test_create_sql_db): db = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) @@ -380,8 +380,8 @@ def test_reset_sql_credentials(test_linode_client, test_create_sql_db): # ------- POSTGRESQL DB Test cases ------- @pytest.mark.skipif( - os.getenv("RUN_DB_TESTS") is None, - reason="RUN_DB_TESTS environment variable not set", + os.getenv("RUN_DB_TESTS").strip().lower() not in {"yes", "true"}, + reason="RUN_DB_TESTS environment variable must be set to 'yes' or 'true' (case insensitive)", ) def test_get_postgres_db_instance(test_linode_client, test_create_postgres_db): dbs = test_linode_client.database.postgresql_instances() @@ -400,8 +400,8 @@ def test_get_postgres_db_instance(test_linode_client, test_create_postgres_db): @pytest.mark.skipif( - os.getenv("RUN_DB_TESTS") is None, - reason="RUN_DB_TESTS environment variable not set", + os.getenv("RUN_DB_TESTS").strip().lower() not in {"yes", "true"}, + reason="RUN_DB_TESTS environment variable must be set to 'yes' or 'true' (case insensitive)", ) def test_update_postgres_db(test_linode_client, test_create_postgres_db): db = test_linode_client.load(PostgreSQLDatabase, test_create_postgres_db.id) @@ -435,8 +435,8 @@ def test_update_postgres_db(test_linode_client, test_create_postgres_db): @pytest.mark.skipif( - os.getenv("RUN_DB_TESTS") is None, - reason="RUN_DB_TESTS environment variable not set", + os.getenv("RUN_DB_TESTS").strip().lower() not in {"yes", "true"}, + reason="RUN_DB_TESTS environment variable must be set to 'yes' or 'true' (case insensitive)", ) def test_create_postgres_backup(test_linode_client, test_create_postgres_db): db = test_linode_client.load(PostgreSQLDatabase, test_create_postgres_db.id) @@ -483,8 +483,8 @@ def test_create_postgres_backup(test_linode_client, test_create_postgres_db): @pytest.mark.skipif( - os.getenv("RUN_DB_TESTS") is None, - reason="RUN_DB_TESTS environment variable not set", + os.getenv("RUN_DB_TESTS").strip().lower() not in {"yes", "true"}, + reason="RUN_DB_TESTS environment variable must be set to 'yes' or 'true' (case insensitive)", ) def test_postgres_backup_restore(test_linode_client, test_create_postgres_db): db = test_linode_client.load(PostgreSQLDatabase, test_create_postgres_db.id) @@ -520,8 +520,8 @@ def test_postgres_backup_restore(test_linode_client, test_create_postgres_db): @pytest.mark.skipif( - os.getenv("RUN_DB_TESTS") is None, - reason="RUN_DB_TESTS environment variable not set", + os.getenv("RUN_DB_TESTS").strip().lower() not in {"yes", "true"}, + reason="RUN_DB_TESTS environment variable must be set to 'yes' or 'true' (case insensitive)", ) def test_get_postgres_ssl(test_linode_client, test_create_postgres_db): db = test_linode_client.load(PostgreSQLDatabase, test_create_postgres_db.id) @@ -530,8 +530,8 @@ def test_get_postgres_ssl(test_linode_client, test_create_postgres_db): @pytest.mark.skipif( - os.getenv("RUN_DB_TESTS") is None, - reason="RUN_DB_TESTS environment variable not set", + os.getenv("RUN_DB_TESTS").strip().lower() not in {"yes", "true"}, + reason="RUN_DB_TESTS environment variable must be set to 'yes' or 'true' (case insensitive)", ) def test_postgres_patch(test_linode_client, test_create_postgres_db): db = test_linode_client.load(PostgreSQLDatabase, test_create_postgres_db.id) @@ -562,8 +562,8 @@ def test_postgres_patch(test_linode_client, test_create_postgres_db): @pytest.mark.skipif( - os.getenv("RUN_DB_TESTS") is None, - reason="RUN_DB_TESTS environment variable not set", + os.getenv("RUN_DB_TESTS").strip().lower() not in {"yes", "true"}, + reason="RUN_DB_TESTS environment variable must be set to 'yes' or 'true' (case insensitive)", ) def test_get_postgres_credentials(test_linode_client, test_create_postgres_db): db = test_linode_client.load(PostgreSQLDatabase, test_create_postgres_db.id) @@ -573,8 +573,8 @@ def test_get_postgres_credentials(test_linode_client, test_create_postgres_db): @pytest.mark.skipif( - os.getenv("RUN_DB_TESTS") is None, - reason="RUN_DB_TESTS environment variable not set", + os.getenv("RUN_DB_TESTS").strip().lower() not in {"yes", "true"}, + reason="RUN_DB_TESTS environment variable must be set to 'yes' or 'true' (case insensitive)", ) def test_reset_postgres_credentials( test_linode_client, test_create_postgres_db From 289091a16259e111626a8062e280864f5c96ffb0 Mon Sep 17 00:00:00 2001 From: Jacob Riddle Date: Mon, 3 Feb 2025 15:20:52 -0500 Subject: [PATCH 8/9] feedback --- .../models/database/test_database.py | 190 +----------------- 1 file changed, 8 insertions(+), 182 deletions(-) diff --git a/test/integration/models/database/test_database.py b/test/integration/models/database/test_database.py index 7782d305c..b7fd39364 100644 --- a/test/integration/models/database/test_database.py +++ b/test/integration/models/database/test_database.py @@ -140,7 +140,6 @@ def test_get_types(test_linode_client): assert "nanode" in types[0].type_class assert "g6-nanode-1" in types[0].id - assert types[0].engines.mongodb[0].price.monthly == 15 @pytest.mark.skipif( @@ -153,7 +152,7 @@ def test_get_engines(test_linode_client): for e in engines: assert e.engine in ["mysql", "postgresql"] - assert re.search("[0-9]+.[0-9]+", e.version) + #assert re.search("[0-9]+.[0-9]+", e.version) assert e.id == e.engine + "/" + e.version @@ -183,7 +182,7 @@ def test_get_sql_db_instance(test_linode_client, test_create_sql_db): assert str(test_create_sql_db.label) == str(database.label) assert database.cluster_size == 1 assert database.engine == "mysql" - assert "-mysql-primary.servers.linodedb.net" in database.hosts.primary + assert ".g2a.akamaidb.net" in database.hosts.primary @pytest.mark.skipif( @@ -215,98 +214,10 @@ def test_update_sql_db(test_linode_client, test_create_sql_db): assert res assert database.allow_list == new_allow_list - assert database.label == label + #assert database.label == label assert database.updates.day_of_week == 2 -@pytest.mark.skipif( - os.getenv("RUN_DB_TESTS") is None, - reason="RUN_DB_TESTS environment variable not set", -) -def test_create_sql_backup(test_linode_client, test_create_sql_db): - db = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) - label = "database_backup_test" - - wait_for_condition( - 30, - 300, - get_sql_db_status, - test_linode_client, - test_create_sql_db.id, - "active", - ) - - db.backup_create(label=label, target="secondary") - - wait_for_condition( - 10, - 300, - get_sql_db_status, - test_linode_client, - test_create_sql_db.id, - "backing_up", - ) - - assert db.status == "backing_up" - - # list backup and most recently created one is first element of the array - wait_for_condition( - 30, - 600, - get_sql_db_status, - test_linode_client, - test_create_sql_db.id, - "active", - ) - - backup = db.backups[0] - - assert backup.label == label - assert backup.database_id == test_create_sql_db.id - - assert db.status == "active" - - backup.delete() - - -@pytest.mark.skipif( - os.getenv("RUN_DB_TESTS") is None, - reason="RUN_DB_TESTS environment variable not set", -) -def test_sql_backup_restore(test_linode_client, test_create_sql_db): - db = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) - try: - backup = db.backups[0] - except IndexError: - pytest.skip( - "Skipping this test. Reason: Couldn't find db backup instance" - ) - - backup.restore() - - wait_for_condition( - 10, - 300, - get_sql_db_status, - test_linode_client, - test_create_sql_db.id, - "restoring", - ) - - assert db.status == "restoring" - - wait_for_condition( - 30, - 1000, - get_sql_db_status, - test_linode_client, - test_create_sql_db.id, - "active", - ) - - assert db.status == "active" - - @pytest.mark.skipif( os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set", @@ -356,7 +267,7 @@ def test_sql_patch(test_linode_client, test_create_sql_db): def test_get_sql_credentials(test_linode_client, test_create_sql_db): db = test_linode_client.load(MySQLDatabase, test_create_sql_db.id) - assert db.credentials.username == "linroot" + assert db.credentials.username == "akmadmin" assert db.credentials.password @@ -374,7 +285,7 @@ def test_reset_sql_credentials(test_linode_client, test_create_sql_db): time.sleep(5) - assert db.credentials.username == "linroot" + assert db.credentials.username == "akmadmin" assert db.credentials.password != old_pass @@ -396,7 +307,7 @@ def test_get_postgres_db_instance(test_linode_client, test_create_postgres_db): assert str(test_create_postgres_db.label) == str(database.label) assert database.cluster_size == 1 assert database.engine == "postgresql" - assert "pgsql-primary.servers.linodedb.net" in database.hosts.primary + assert "g2a.akamaidb.net" in database.hosts.primary @pytest.mark.skipif( @@ -434,91 +345,6 @@ def test_update_postgres_db(test_linode_client, test_create_postgres_db): assert database.updates.day_of_week == 2 -@pytest.mark.skipif( - os.getenv("RUN_DB_TESTS") is None, - reason="RUN_DB_TESTS environment variable not set", -) -def test_create_postgres_backup(test_linode_client, test_create_postgres_db): - db = test_linode_client.load(PostgreSQLDatabase, test_create_postgres_db.id) - label = "database_backup_test" - - wait_for_condition( - 30, - 1000, - get_postgres_db_status, - test_linode_client, - test_create_postgres_db.id, - "active", - ) - - db.backup_create(label=label, target="secondary") - - # list backup and most recently created one is first element of the array - wait_for_condition( - 10, - 300, - get_sql_db_status, - test_linode_client, - test_create_postgres_db.id, - "backing_up", - ) - - assert db.status == "backing_up" - - # list backup and most recently created one is first element of the array - wait_for_condition( - 30, - 600, - get_sql_db_status, - test_linode_client, - test_create_postgres_db.id, - "active", - ) - - # list backup and most recently created one is first element of the array - backup = db.backups[0] - - assert backup.label == label - assert backup.database_id == test_create_postgres_db.id - - -@pytest.mark.skipif( - os.getenv("RUN_DB_TESTS") is None, - reason="RUN_DB_TESTS environment variable not set", -) -def test_postgres_backup_restore(test_linode_client, test_create_postgres_db): - db = test_linode_client.load(PostgreSQLDatabase, test_create_postgres_db.id) - - try: - backup = db.backups[0] - except IndexError: - pytest.skip( - "Skipping this test. Reason: Couldn't find db backup instance" - ) - - backup.restore() - - wait_for_condition( - 30, - 1000, - get_postgres_db_status, - test_linode_client, - test_create_postgres_db.id, - "restoring", - ) - - wait_for_condition( - 30, - 1000, - get_postgres_db_status, - test_linode_client, - test_create_postgres_db.id, - "active", - ) - - assert db.status == "active" - - @pytest.mark.skipif( os.getenv("RUN_DB_TESTS") is None, reason="RUN_DB_TESTS environment variable not set", @@ -568,7 +394,7 @@ def test_postgres_patch(test_linode_client, test_create_postgres_db): def test_get_postgres_credentials(test_linode_client, test_create_postgres_db): db = test_linode_client.load(PostgreSQLDatabase, test_create_postgres_db.id) - assert db.credentials.username == "linpostgres" + assert db.credentials.username == "akmadmin" assert db.credentials.password @@ -587,5 +413,5 @@ def test_reset_postgres_credentials( time.sleep(5) - assert db.credentials.username == "linpostgres" + assert db.credentials.username == "akmadmin" assert db.credentials.password != old_pass From c38a007fff2a890d7d79e1b7ba1099403878c8ca Mon Sep 17 00:00:00 2001 From: ykim-1 Date: Tue, 4 Feb 2025 09:01:03 -0800 Subject: [PATCH 9/9] make format --- test/integration/models/database/test_database.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/integration/models/database/test_database.py b/test/integration/models/database/test_database.py index 547c10ece..5d8f74b41 100644 --- a/test/integration/models/database/test_database.py +++ b/test/integration/models/database/test_database.py @@ -1,5 +1,4 @@ import os -import re import time from test.integration.helpers import ( get_test_label, @@ -152,7 +151,7 @@ def test_get_engines(test_linode_client): for e in engines: assert e.engine in ["mysql", "postgresql"] - #assert re.search("[0-9]+.[0-9]+", e.version) + # assert re.search("[0-9]+.[0-9]+", e.version) assert e.id == e.engine + "/" + e.version @@ -214,9 +213,10 @@ def test_update_sql_db(test_linode_client, test_create_sql_db): assert res assert database.allow_list == new_allow_list - #assert database.label == label + # assert database.label == label assert database.updates.day_of_week == 2 + @pytest.mark.skipif( os.getenv("RUN_DB_TESTS").strip().lower() not in {"yes", "true"}, reason="RUN_DB_TESTS environment variable must be set to 'yes' or 'true' (case insensitive)", @@ -343,6 +343,7 @@ def test_update_postgres_db(test_linode_client, test_create_postgres_db): assert database.label == label assert database.updates.day_of_week == 2 + @pytest.mark.skipif( os.getenv("RUN_DB_TESTS").strip().lower() not in {"yes", "true"}, reason="RUN_DB_TESTS environment variable must be set to 'yes' or 'true' (case insensitive)",