diff --git a/test/new_tests/conftest.py b/test/new_tests/conftest.py index 0eea8f0993..c58076cd5d 100644 --- a/test/new_tests/conftest.py +++ b/test/new_tests/conftest.py @@ -4,11 +4,13 @@ import sys import pytest - from . import invalid_data from .test_base_class import TestBaseClass import aerospike +from aerospike import exception as e +import logging + # Comment this out because nowhere in the repository is using it ''' @@ -241,3 +243,92 @@ def invalid_key(request): # aerospike.set_log_level(aerospike.LOG_LEVEL_DEBUG) # aerospike.set_log_handler(None) + +HARD_LIMIT_SECS = 3 +# Server team states that this time interval is a safe amount +POLL_INTERVAL_SECS = 0.1 + +def poll_until_role_exists(role_name: str, client: aerospike.Client): + start = time.time() + while time.time() - start < HARD_LIMIT_SECS: + try: + client.admin_query_role(role=role_name) + except e.InvalidRole: + time.sleep(POLL_INTERVAL_SECS) + continue + logging.debug("Role now exists. Return early") + return + logging.debug("poll_until_role_exists timeout") + +def poll_until_role_doesnt_exist(role_name: str, client: aerospike.Client): + start = time.time() + try: + while time.time() - start < HARD_LIMIT_SECS: + client.admin_query_role(role=role_name) + time.sleep(POLL_INTERVAL_SECS) + except e.InvalidRole: + logging.debug("Role no longer exists. Return early") + return + logging.debug("poll_until_role_doesnt_exist timeout") + +def poll_until_user_exists(username: str, client: aerospike.Client, roles: list[str]): + start = time.time() + while time.time() - start < HARD_LIMIT_SECS: + try: + user_dict = client.admin_query_user_info(user=username) + if user_dict["roles"] != roles: + continue + except e.InvalidUser: + time.sleep(POLL_INTERVAL_SECS) + continue + logging.debug("User now exists with expected roles.") + return + logging.debug("poll_until_user_exists timeout") + +def poll_until_user_doesnt_exist(username: str, client: aerospike.Client): + start = time.time() + try: + while time.time() - start < HARD_LIMIT_SECS: + client.admin_query_user_info(user=username) + time.sleep(POLL_INTERVAL_SECS) + except e.InvalidUser: + logging.debug("User no longer exists. Return early") + return + logging.debug("poll_until_user_doesnt_exist timeout") + +def wait_for_job_completion(as_connection, job_id, job_module: int = aerospike.JOB_QUERY, time_limit_secs: float = float("inf")): + """ + Blocks until the job has completed + """ + start = time.time() + while time.time() - start < time_limit_secs: + response = as_connection.job_info(job_id, job_module) + if response["status"] != aerospike.JOB_STATUS_INPROGRESS: + break + time.sleep(0.1) + +# Monkeypatching the client class or instance isn't possible since it's immutable + +def admin_create_role_and_poll(client: aerospike.Client, role: str, *args, **kwargs): + retval = client.admin_create_role(role, *args, **kwargs) + poll_until_role_exists(role, client) + return retval + +def admin_create_user_and_poll(client: aerospike.Client, username: str, password: str, roles: list, *args, **kwargs): + retval = client.admin_create_user(username, password, roles, *args, **kwargs) + + # The server creates a user and adds roles to it asynchronously, since security is a type of smd operation + # So client.admin_create_user() can return before all th + poll_until_user_exists(username, client, roles) + + return retval + +def admin_drop_user_and_poll(client: aerospike.Client, username: str, *args, **kwargs): + retval = client.admin_drop_user(username, *args, **kwargs) + poll_until_user_doesnt_exist(username, client) + return retval + +def admin_drop_role_and_poll(client: aerospike.Client, role: str, *args, **kwargs): + retval = client.admin_drop_role(role, *args, **kwargs) + poll_until_role_doesnt_exist(role, client) + return retval diff --git a/test/new_tests/test_admin_change_password.py b/test/new_tests/test_admin_change_password.py index 82e3cee210..14b9d577cc 100644 --- a/test/new_tests/test_admin_change_password.py +++ b/test/new_tests/test_admin_change_password.py @@ -3,25 +3,32 @@ import pytest import time from .test_base_class import TestBaseClass +from .conftest import admin_drop_user_and_poll, poll_until_user_doesnt_exist import aerospike @pytest.mark.usefixtures("connection_config") -class TestChangePassword(object): +class TestChangePassword: def setup_method(self, method): """ Setup method """ if TestBaseClass.auth_in_use() is False: - pytest.skip("No user specified, may not be a secured cluster", allow_module_level=True) + pytest.skip( + "No user specified, may not be a secured cluster", + allow_module_level=True, + ) config = TestBaseClass.get_connection_config() - self.client = aerospike.client(config).connect(config["user"], config["password"]) + self.client = aerospike.client(config).connect( + config["user"], config["password"] + ) try: - self.client.admin_create_user("testchangepassworduser", "aerospike", ["read"]) - time.sleep(2) + self.client.admin_create_user( + "testchangepassworduser", "aerospike", ["read"] + ) except aerospike.exception.UserExistsError: pass # we are good, no such role exists self.delete_users = [] @@ -32,7 +39,7 @@ def teardown_method(self, method): """ try: - self.client.admin_drop_user("testchangepassworduser") + admin_drop_user_and_poll(self.client, "testchangepassworduser") except Exception: pass @@ -56,13 +63,15 @@ def test_change_password_with_proper_parameters(self): status = self.clientreaduser.admin_change_password(user, password) assert status == 0 - - time.sleep(2) config = self.connection_config # Assert that connecting to the server with the old password fails - with pytest.raises((aerospike.exception.InvalidPassword, aerospike.exception.InvalidCredential)): - self.clientreaduserwrong = aerospike.client(config).connect(user, "aerospike") + with pytest.raises( + (aerospike.exception.InvalidPassword, aerospike.exception.InvalidCredential) + ): + self.clientreaduserwrong = aerospike.client(config).connect( + user, "aerospike" + ) self.clientreaduserright = aerospike.client(config).connect(user, "newpassword") @@ -101,8 +110,12 @@ def test_change_password_with_proper_timeout_policy_value(self): time.sleep(2) config = self.connection_config - with pytest.raises((aerospike.exception.InvalidPassword, aerospike.exception.InvalidCredential)): - self.clientreaduserwrong = aerospike.client(config).connect(user, "aerospike") + with pytest.raises( + (aerospike.exception.InvalidPassword, aerospike.exception.InvalidCredential) + ): + self.clientreaduserwrong = aerospike.client(config).connect( + user, "aerospike" + ) self.clientreaduserright = aerospike.client(config).connect(user, "newpassword") diff --git a/test/new_tests/test_admin_create_role.py b/test/new_tests/test_admin_create_role.py index 4ce5164381..6ff573989c 100644 --- a/test/new_tests/test_admin_create_role.py +++ b/test/new_tests/test_admin_create_role.py @@ -4,6 +4,7 @@ import time from .test_base_class import TestBaseClass from aerospike import exception as e +from .conftest import admin_drop_role_and_poll, admin_drop_user_and_poll, poll_until_role_doesnt_exist, poll_until_user_doesnt_exist, admin_create_user_and_poll, admin_create_role_and_poll import aerospike @@ -24,8 +25,7 @@ def setup_method(self, method): config = self.config self.client = aerospike.client(config).connect(config["user"], config["password"]) try: - self.client.admin_drop_user("testcreaterole") - time.sleep(2) + admin_drop_user_and_poll(self.client, "testcreaterole") except Exception: pass # do nothing, EAFP @@ -38,7 +38,7 @@ def teardown_method(self, method): for user in self.delete_users: try: - self.client.admin_drop_user(user) + admin_drop_user_and_poll(self.client, user) except Exception: pass @@ -58,15 +58,13 @@ def test_create_role_positive_with_policy(self): try: self.client.admin_get_role("usr-sys-admin-test") # role exists, clear it out. - self.client.admin_drop_role("usr-sys-admin-test") - time.sleep(2) + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except e.InvalidRole: pass # we are good, no such role exists - self.client.admin_create_role( + admin_create_role_and_poll(self.client, "usr-sys-admin-test", [{"code": aerospike.PRIV_READ, "ns": "test", "set": "demo"}], {"timeout": 180000} ) - time.sleep(1) roles = self.client.admin_get_role("usr-sys-admin-test") assert roles == { "privileges": [{"code": 10, "ns": "test", "set": "demo"}], @@ -76,18 +74,17 @@ def test_create_role_positive_with_policy(self): } try: - status = self.client.admin_create_user("testcreaterole", "createrole", ["usr-sys-admin-test"]) + status = admin_create_user_and_poll(self.client, "testcreaterole", "createrole", ["usr-sys-admin-test"]) except e.QuotasNotEnabled: pytest.mark.skip(reason="Got QuotasNotEnabled, skipping quota test.") pytest.skip() assert status == 0 - time.sleep(1) user = self.client.admin_query_user_info("testcreaterole") assert user["roles"] == ["usr-sys-admin-test"] - self.client.admin_drop_user("testcreaterole") + admin_drop_user_and_poll(self.client, "testcreaterole") @pytest.mark.parametrize( "priv_name, privs", @@ -114,31 +111,28 @@ def test_create_role_all_privs_positive(self, priv_name, privs): try: self.client.admin_get_role(role_name) # role exists, clear it out. - self.client.admin_drop_role(role_name) - time.sleep(2) + admin_drop_role_and_poll(self.client, role_name) except e.InvalidRole: pass # we are good, no such role exists - self.client.admin_create_role(role_name, privs) - time.sleep(1) + admin_create_role_and_poll(self.client, role_name, privs) roles = self.client.admin_get_role(role_name) assert roles == {"privileges": privs, "whitelist": [], "read_quota": 0, "write_quota": 0} try: - status = self.client.admin_create_user("testcreaterole", "createrole", [role_name]) + status = admin_create_user_and_poll(self.client, "testcreaterole", "createrole", [role_name]) except e.QuotasNotEnabled: pytest.mark.skip(reason="Got QuotasNotEnabled, skipping quota test.") pytest.skip() assert status == 0 - time.sleep(1) user = self.client.admin_query_user_info("testcreaterole") assert user["roles"] == [role_name] - self.client.admin_drop_user("testcreaterole") + admin_drop_user_and_poll(self.client, "testcreaterole") - self.client.admin_drop_role(role_name) + admin_drop_role_and_poll(self.client, role_name) def test_create_role_positive_with_policy_write(self): """ @@ -147,15 +141,13 @@ def test_create_role_positive_with_policy_write(self): try: self.client.admin_get_role("usr-sys-admin-test") # role exists, clear it out. - self.client.admin_drop_role("usr-sys-admin-test") - time.sleep(2) + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except e.InvalidRole: pass # we are good, no such role exists - self.client.admin_create_role( + admin_create_role_and_poll(self.client, "usr-sys-admin-test", [{"code": aerospike.PRIV_WRITE, "ns": "test", "set": "demo"}] ) - time.sleep(1) roles = self.client.admin_get_role("usr-sys-admin-test") assert roles == { "privileges": [{"code": 13, "ns": "test", "set": "demo"}], @@ -165,18 +157,17 @@ def test_create_role_positive_with_policy_write(self): } try: - status = self.client.admin_create_user("testcreaterole", "createrole", ["usr-sys-admin-test"]) + status = admin_create_user_and_poll(self.client, "testcreaterole", "createrole", ["usr-sys-admin-test"]) except e.QuotasNotEnabled: pytest.mark.skip(reason="Got QuotasNotEnabled, skipping quota test.") pytest.skip() assert status == 0 - time.sleep(1) user = self.client.admin_query_user_info("testcreaterole") assert user["roles"] == ["usr-sys-admin-test"] - self.client.admin_drop_user("testcreaterole") + admin_drop_user_and_poll(self.client, "testcreaterole") def test_create_role_positive(self): """ @@ -185,16 +176,13 @@ def test_create_role_positive(self): try: self.client.admin_get_role("usr-sys-admin-test") # role exists, clear it out. - self.client.admin_drop_role("usr-sys-admin-test") - # Give some time for the role removal to take place - time.sleep(2) + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except e.InvalidRole: pass # we are good, no such role exists - self.client.admin_create_role( + admin_create_role_and_poll(self.client, "usr-sys-admin-test", [{"code": aerospike.PRIV_USER_ADMIN}, {"code": aerospike.PRIV_SYS_ADMIN}] ) - time.sleep(1) roles = self.client.admin_get_role("usr-sys-admin-test") assert roles == { @@ -204,7 +192,7 @@ def test_create_role_positive(self): "write_quota": 0, } - self.client.admin_drop_role("usr-sys-admin-test") + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") def test_create_role_whitelist_quota_positive(self): """ @@ -213,14 +201,12 @@ def test_create_role_whitelist_quota_positive(self): try: self.client.admin_get_role("usr-sys-admin-test") # role exists, clear it out. - self.client.admin_drop_role("usr-sys-admin-test") - # Give some time for the role removal to take place - time.sleep(2) + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except e.InvalidRole: pass # we are good, no such role exists try: - self.client.admin_create_role( + admin_create_role_and_poll(self.client, "usr-sys-admin-test", [{"code": aerospike.PRIV_USER_ADMIN}, {"code": aerospike.PRIV_SYS_ADMIN}], whitelist=["127.0.0.1", "10.1.2.0/24"], @@ -231,9 +217,7 @@ def test_create_role_whitelist_quota_positive(self): pytest.mark.skip(reason="Got QuotasNotEnabled, skipping quota test.") pytest.skip() - time.sleep(1) roles = self.client.admin_get_role("usr-sys-admin-test") - assert roles == { "privileges": [ {"code": aerospike.PRIV_USER_ADMIN, "ns": "", "set": ""}, @@ -244,14 +228,14 @@ def test_create_role_whitelist_quota_positive(self): "write_quota": 30, } - self.client.admin_drop_role("usr-sys-admin-test") + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") def test_create_role_incorrect_role_type(self): """ role name not string """ try: - self.client.admin_create_role(1, [{"code": aerospike.PRIV_USER_ADMIN}]) + admin_create_role_and_poll(self.client, 1, [{"code": aerospike.PRIV_USER_ADMIN}]) except e.ParamError as exception: assert exception.code == -2 assert "Role name should be a string" in exception.msg @@ -263,13 +247,12 @@ def test_create_role_unknown_privilege_type(self): try: self.client.admin_get_role("usr-sys-admin-test") # role exists, clear it out. - self.client.admin_drop_role("usr-sys-admin-test") - time.sleep(2) + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except e.InvalidRole: pass # we are good, no such role exists try: - self.client.admin_create_role("usr-sys-admin-test", [{"code": 64}]) + admin_create_role_and_poll(self.client, "usr-sys-admin-test", [{"code": 64}]) except e.InvalidPrivilege as exception: assert exception.code == 72 @@ -278,7 +261,7 @@ def test_create_role_incorrect_privilege_type(self): privilege type incorrect """ try: - self.client.admin_create_role("usr-sys-admin-test", None) + admin_create_role_and_poll(self.client, "usr-sys-admin-test", None) except e.ParamError as exception: assert exception.code == -2 @@ -291,25 +274,22 @@ def test_create_role_existing_role(self): try: self.client.admin_get_role("usr-sys-admin-test") # role exists, clear it out. - self.client.admin_drop_role("usr-sys-admin-test") - time.sleep(2) + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except e.InvalidRole: pass # we are good, no such role exists - self.client.admin_create_role( + admin_create_role_and_poll(self.client, "usr-sys-admin-test", [{"code": aerospike.PRIV_USER_ADMIN}, {"code": aerospike.PRIV_SYS_ADMIN}] ) try: - self.client.admin_create_role( + admin_create_role_and_poll(self.client, "usr-sys-admin-test", [{"code": aerospike.PRIV_USER_ADMIN}, {"code": aerospike.PRIV_SYS_ADMIN}] ) - except e.RoleExistsError as exception: assert exception.code == 71 assert exception.msg == "AEROSPIKE_ROLE_ALREADY_EXISTS" - time.sleep(1) - status = self.client.admin_drop_role("usr-sys-admin-test") + status = admin_drop_role_and_poll(self.client, "usr-sys-admin-test") assert status == 0 @@ -319,16 +299,14 @@ def test_create_role_positive_with_special_characters(self): """ role_name = "!#Q#AEQ@#$%&^*((^&*~~~````" try: - self.client.admin_drop_role(role_name) # clear out if it exists - time.sleep(2) + admin_drop_role_and_poll(self.client, role_name) # clear out if it exists except Exception: pass # EAFP - status = self.client.admin_create_role( + status = admin_create_role_and_poll(self.client, role_name, [{"code": aerospike.PRIV_READ, "ns": "test", "set": "demo"}] ) assert status == 0 - time.sleep(1) roles = self.client.admin_get_role(role_name) assert roles == { @@ -340,23 +318,20 @@ def test_create_role_positive_with_special_characters(self): "write_quota": 0, } - status = self.client.admin_create_user("testcreaterole", "createrole", [role_name]) + status = admin_create_user_and_poll(self.client, "testcreaterole", "createrole", [role_name]) assert status == 0 - time.sleep(1) users = self.client.admin_query_user_info("testcreaterole") assert users["roles"] == [role_name] - self.client.admin_drop_role(role_name) - - time.sleep(1) + admin_drop_role_and_poll(self.client, role_name) users = self.client.admin_query_user_info("testcreaterole") assert users["roles"] == [] - self.client.admin_drop_user("testcreaterole") + admin_drop_user_and_poll(self.client, "testcreaterole") def test_create_role_positive_with_too_long_role_name(self): """ @@ -365,7 +340,7 @@ def test_create_role_positive_with_too_long_role_name(self): role_name = "role$" * 1000 try: - self.client.admin_create_role( + admin_create_role_and_poll(self.client, role_name, [{"code": aerospike.PRIV_READ, "ns": "test", "set": "demo"}] ) diff --git a/test/new_tests/test_admin_create_user.py b/test/new_tests/test_admin_create_user.py index 5bb4812155..ee708253e0 100644 --- a/test/new_tests/test_admin_create_user.py +++ b/test/new_tests/test_admin_create_user.py @@ -6,23 +6,24 @@ from aerospike import exception as e from contextlib import nullcontext import aerospike +from .conftest import admin_drop_user_and_poll, poll_until_user_doesnt_exist, admin_create_user_and_poll, poll_until_user_exists @pytest.mark.usefixtures("connection_config") class TestCreateUser(object): user = "user7" - pytestmark = pytest.mark.skipif( - not TestBaseClass.auth_in_use(), reason="No user specified, may be not secured cluster." - ) - def setup_method(self, method): """ Setup method """ config = TestBaseClass.get_connection_config() + if not TestBaseClass.auth_in_use(): + pytest.skip("No user specified, may be not secured cluster.") - self.client = aerospike.client(config).connect(config["user"], config["password"]) + self.client = aerospike.client(config).connect( + config["user"], config["password"] + ) self.delete_users = [] @@ -33,10 +34,9 @@ def teardown_method(self, method): for user in self.delete_users: try: - self.client.admin_drop_user(user) + admin_drop_user_and_poll(self.client, user) except Exception: pass - time.sleep(2) self.client.close() def test_create_user_without_any_parameters(self): @@ -54,14 +54,11 @@ def test_create_user_with_proper_parameters(self): roles = ["read", "read-write", "sys-admin"] try: - self.client.admin_drop_user(user, policy) - time.sleep(2) + admin_drop_user_and_poll(self.client, user, policy) except Exception: pass - status = self.client.admin_create_user(user, password, roles, policy) - - time.sleep(2) + status = admin_create_user_and_poll(self.client, user, password, roles, policy) assert status == 0 @@ -78,14 +75,11 @@ def test_create_user_with_proper_parameters_without_policy(self): roles = ["read", "read-write", "sys-admin"] try: - self.client.admin_drop_user(user) - time.sleep(2) + admin_drop_user_and_poll(self.client, user) except Exception: pass - status = self.client.admin_create_user(user, password, roles) - - time.sleep(2) + status = admin_create_user_and_poll(self.client, user, password, roles) assert status == 0 @@ -103,13 +97,12 @@ def test_create_user_with_invalid_timeout_policy_value(self): roles = ["sys-admin"] try: - self.client.admin_drop_user(user, policy) - time.sleep(2) + admin_drop_user_and_poll(self.client, user, policy) except Exception: pass try: - self.client.admin_create_user(user, password, roles, policy) + admin_create_user_and_poll(self.client, user, password, roles, policy) except e.ParamError as exception: assert exception.code == -2 @@ -123,15 +116,11 @@ def test_create_user_with_proper_timeout_policy_value(self): roles = ["read-write", "sys-admin"] try: - self.client.admin_drop_user(user, policy) - time.sleep(2) + admin_drop_user_and_poll(self.client, user, policy) except Exception: pass - status = self.client.admin_create_user(user, password, roles, policy) - - time.sleep(2) - + status = admin_create_user_and_poll(self.client, user, password, roles, policy) assert status == 0 user = self.client.admin_query_user_info(user) @@ -147,7 +136,7 @@ def test_create_user_with_none_username(self): roles = ["sys-admin"] try: - self.client.admin_create_user(user, password, roles) + admin_create_user_and_poll(self.client, user, password, roles) except e.ParamError as exception: assert exception.code == -2 @@ -160,7 +149,7 @@ def test_create_user_with_empty_username(self): roles = ["read-write"] try: - self.client.admin_create_user(user, password, roles) + admin_create_user_and_poll(self.client, user, password, roles) except e.InvalidUser as exception: assert exception.code == 60 @@ -173,12 +162,11 @@ def test_create_user_with_special_characters_in_username(self): roles = ["read-write"] try: - self.client.admin_drop_user(user) - time.sleep(2) + admin_drop_user_and_poll(self.client, user) except Exception: pass - status = self.client.admin_create_user(user, password, roles) + status = admin_create_user_and_poll(self.client, user, password, roles) assert status == 0 @@ -191,7 +179,7 @@ def test_create_user_with_none_password(self): roles = ["sys-admin"] try: - self.client.admin_create_user(user, password, roles) + admin_create_user_and_poll(self.client, user, password, roles) except e.ParamError as exception: assert exception.code == -2 @@ -204,15 +192,13 @@ def test_create_user_with_empty_string_as_password(self): roles = ["read-write"] try: - self.client.admin_drop_user(user) - time.sleep(2) + admin_drop_user_and_poll(self.client, user) except Exception: pass - status = self.client.admin_create_user(user, password, roles) + status = admin_create_user_and_poll(self.client, user, password, roles) assert status == 0 - time.sleep(2) self.delete_users.append(user) def test_create_user_with_special_characters_in_password(self): @@ -222,12 +208,11 @@ def test_create_user_with_special_characters_in_password(self): roles = ["sys-admin"] try: - self.client.admin_drop_user(user) - time.sleep(2) + admin_drop_user_and_poll(self.client, user) except Exception: pass - status = self.client.admin_create_user(user, password, roles) + status = admin_create_user_and_poll(self.client, user, password, roles) assert status == 0 @@ -240,13 +225,12 @@ def test_create_user_with_too_long_username(self): roles = ["sys-admin"] try: - self.client.admin_drop_user(user) - time.sleep(2) + admin_drop_user_and_poll(self.client, user) except Exception: pass try: - self.client.admin_create_user(user, password, roles) + admin_create_user_and_poll(self.client, user, password, roles) except e.InvalidUser as exception: assert exception.code == 60 @@ -262,13 +246,12 @@ def test_create_user_with_too_long_password(self): roles = ["read-write"] try: - self.client.admin_drop_user(user) - time.sleep(2) + admin_drop_user_and_poll(self.client, user) except Exception: pass with pytest.raises(e.ClientError): - self.client.admin_create_user(user, password, roles) + admin_create_user_and_poll(self.client, user, password, roles) def test_create_user_with_empty_roles_list(self): @@ -277,13 +260,12 @@ def test_create_user_with_empty_roles_list(self): roles = [] try: - self.client.admin_drop_user(user) - time.sleep(2) + admin_drop_user_and_poll(self.client, user) except Exception: pass try: - self.client.admin_create_user(user, password, roles) + admin_create_user_and_poll(self.client, user, password, roles) except e.InvalidRole as exception: assert exception.code == 70 @@ -296,13 +278,11 @@ def test_create_user_with_non_user_admin_user(self): roles = ["read-write"] try: - self.client.admin_drop_user(user) - time.sleep(2) + admin_drop_user_and_poll(self.client, user) except Exception: pass - status = self.client.admin_create_user(user, password, roles) - time.sleep(2) + status = admin_create_user_and_poll(self.client, user, password, roles) assert status == 0 @@ -331,12 +311,12 @@ def test_create_user_with_non_list_roles(self, roles): user = "user7" password = "user7" try: - self.client.admin_drop_user(user) + admin_drop_user_and_poll(self.client, user) except Exception: pass with pytest.raises(e.ParamError): - self.client.admin_create_user(user, password, roles) + admin_create_user_and_poll(self.client, user, password, roles) @pytest.mark.parametrize("list_item", [{}, (), 5, []]) def test_create_user_with_invalid_roles_types(self, list_item): @@ -345,31 +325,29 @@ def test_create_user_with_invalid_roles_types(self, list_item): password = "user7" roles = ["read-write", list_item] try: - self.client.admin_drop_user(user) + admin_drop_user_and_poll(self.client, user) except Exception: pass with pytest.raises(e.ClientError): - self.client.admin_create_user(user, password, roles) + admin_create_user_and_poll(self.client, user, password, roles) def test_create_user_with_very_long_role_name(self): password = "user7" roles = ["read-write", "abc" * 50] try: - self.client.admin_drop_user(self.user) - time.sleep(2) + admin_drop_user_and_poll(self.client, self.user) except Exception: pass with pytest.raises(e.ClientError): - self.client.admin_create_user(self.user, password, roles) + admin_create_user_and_poll(self.client, self.user, password, roles) # Need as_connection to get server version def test_create_pki_user(self, as_connection): try: - self.client.admin_drop_user(self.user) - time.sleep(2) + admin_drop_user_and_poll(self.client, self.user) except Exception: pass @@ -391,10 +369,13 @@ def test_create_pki_user(self, as_connection): roles = ["read-write"] admin_policy = {} with context: - self.client.admin_create_pki_user(user=self.user, roles=roles, policy=admin_policy) + self.client.admin_create_pki_user( + user=self.user, roles=roles, policy=admin_policy + ) if type(context) == nullcontext: + poll_until_user_exists(self.user, self.client, roles) + print("Check that the PKI user was created.") - time.sleep(2) userDict = self.client.admin_query_user_info(self.user) assert userDict["roles"] == ["read-write"] diff --git a/test/new_tests/test_admin_drop_role.py b/test/new_tests/test_admin_drop_role.py index b32bb0d286..0210decf69 100644 --- a/test/new_tests/test_admin_drop_role.py +++ b/test/new_tests/test_admin_drop_role.py @@ -5,6 +5,7 @@ from .test_base_class import TestBaseClass from aerospike import exception as e +from .conftest import admin_drop_user_and_poll, admin_drop_role_and_poll, admin_create_role_and_poll import aerospike @@ -12,7 +13,8 @@ class TestDropRole(object): pytestmark = pytest.mark.skipif( - not TestBaseClass.auth_in_use(), reason="No user specified, may be not secured cluster." + not TestBaseClass.auth_in_use(), + reason="No user specified, may be not secured cluster.", ) def setup_method(self, method): @@ -20,7 +22,9 @@ def setup_method(self, method): Setup method """ config = TestBaseClass.get_connection_config() - self.client = aerospike.client(config).connect(config["user"], config["password"]) + self.client = aerospike.client(config).connect( + config["user"], config["password"] + ) self.delete_users = [] @@ -31,8 +35,7 @@ def teardown_method(self, method): for user in self.delete_users: try: - self.client.admin_drop_user(user) - time.sleep(2) + admin_drop_user_and_poll(self.client, user) except Exception: pass @@ -52,20 +55,19 @@ def test_drop_role_positive_with_policy(self): try: self.client.admin_query_role("usr-sys-admin-test") # role exists, clear it out. - self.client.admin_drop_role("usr-sys-admin-test") - time.sleep(1) + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except e.InvalidRole: pass # we are good, no such role exists - self.client.admin_create_role( - "usr-sys-admin-test", [{"code": aerospike.PRIV_READ, "ns": "test", "set": "demo"}], {"timeout": 180000} + admin_create_role_and_poll(self.client, + "usr-sys-admin-test", + [{"code": aerospike.PRIV_READ, "ns": "test", "set": "demo"}], + {"timeout": 180000}, ) - time.sleep(1) - status = self.client.admin_drop_role("usr-sys-admin-test", {"timeout": 180000}) + status = admin_drop_role_and_poll(self.client, "usr-sys-admin-test", {"timeout": 180000}) assert status == 0 - time.sleep(1) with pytest.raises(e.InvalidRole): self.client.admin_query_role("usr-sys-admin-test") @@ -77,20 +79,18 @@ def test_drop_role_positive_with_policy_write(self): try: self.client.admin_query_role("usr-sys-admin-test") # role exists, clear it out. - self.client.admin_drop_role("usr-sys-admin-test") - time.sleep(1) + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except e.InvalidRole: pass # we are good, no such role exists - self.client.admin_create_role( - "usr-sys-admin-test", [{"code": aerospike.PRIV_WRITE, "ns": "test", "set": "demo"}] + admin_create_role_and_poll(self.client, + "usr-sys-admin-test", + [{"code": aerospike.PRIV_WRITE, "ns": "test", "set": "demo"}], ) - time.sleep(1) - status = self.client.admin_drop_role("usr-sys-admin-test") + status = admin_drop_role_and_poll(self.client, "usr-sys-admin-test") assert status == 0 - time.sleep(1) with pytest.raises(e.InvalidRole): self.client.admin_query_role("usr-sys-admin-test") @@ -102,21 +102,22 @@ def test_drop_role_positive(self): try: self.client.admin_query_role("usr-sys-admin-test") # role exists, clear it out. - self.client.admin_drop_role("usr-sys-admin-test") - time.sleep(1) + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except e.InvalidRole: pass # we are good, no such role exists - self.client.admin_create_role( - "usr-sys-admin-test", [{"code": aerospike.PRIV_USER_ADMIN}, {"code": aerospike.PRIV_SYS_ADMIN}] + admin_create_role_and_poll(self.client, + "usr-sys-admin-test", + [{"code": aerospike.PRIV_USER_ADMIN}, {"code": aerospike.PRIV_SYS_ADMIN}], ) - time.sleep(1) privs = self.client.admin_query_role("usr-sys-admin-test") - assert privs == [{"code": 0, "ns": "", "set": ""}, {"code": 1, "ns": "", "set": ""}] + assert privs == [ + {"code": 0, "ns": "", "set": ""}, + {"code": 1, "ns": "", "set": ""}, + ] - self.client.admin_drop_role("usr-sys-admin-test") - time.sleep(1) + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") with pytest.raises(e.InvalidRole): self.client.admin_query_role("usr-sys-admin-test") @@ -126,7 +127,7 @@ def test_drop_non_existent_role(self): Drop non-existent role """ try: - self.client.admin_drop_role("usr-sys-admin-test") + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except e.InvalidRole as exception: assert exception.code == 70 @@ -147,17 +148,18 @@ def test_drop_role_with_incorrect_policy(self): """ Drop role with incorrect policy """ - status = self.client.admin_create_role("usr-sys-admin-test", [{"code": aerospike.PRIV_USER_ADMIN}]) + status = admin_create_role_and_poll(self.client, + "usr-sys-admin-test", [{"code": aerospike.PRIV_USER_ADMIN}] + ) assert status == 0 - time.sleep(3) try: - self.client.admin_drop_role("usr-sys-admin-test", {"timeout": 0.2}) + admin_drop_role_and_poll(self.client, "usr-sys-admin-test", {"timeout": 0.2}) except e.ParamError as exception: assert exception.code == -2 assert exception.msg == "timeout is invalid" try: - self.client.admin_drop_role("usr-sys-admin-test") + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except Exception: pass diff --git a/test/new_tests/test_admin_drop_user.py b/test/new_tests/test_admin_drop_user.py index 1765a90a46..5c9e45db9c 100644 --- a/test/new_tests/test_admin_drop_user.py +++ b/test/new_tests/test_admin_drop_user.py @@ -6,13 +6,15 @@ from aerospike import exception as e import aerospike +from .conftest import admin_drop_user_and_poll, poll_until_user_doesnt_exist, admin_create_user_and_poll @pytest.mark.usefixtures("connection_config") class TestDropUser(object): pytestmark = pytest.mark.skipif( - not TestBaseClass.auth_in_use(), reason="No user specified, may be not secured cluster." + not TestBaseClass.auth_in_use(), + reason="No user specified, may be not secured cluster.", ) def setup_method(self, method): @@ -21,10 +23,11 @@ def setup_method(self, method): """ config = TestBaseClass.get_connection_config() TestDropUser.Me = self - self.client = aerospike.client(config).connect(config["user"], config["password"]) + self.client = aerospike.client(config).connect( + config["user"], config["password"] + ) try: - self.client.admin_drop_user("foo-test") - time.sleep(2) + admin_drop_user_and_poll(self.client, "foo-test") except Exception: pass @@ -51,23 +54,13 @@ def test_drop_user_with_policy_none(self): user = "foo-test" password = "foo1" roles = ["read", "read-write", "sys-admin"] + admin_create_user_and_poll(self.client, user, password, roles, policy) - status = self.client.admin_create_user(user, password, roles, policy) - - time.sleep(2) - - assert status == 0 - user_info = self.client.admin_query_user_info(user, policy) - - assert user_info["roles"] == ["read", "read-write", "sys-admin"] - - status = self.client.admin_drop_user(user, policy) - + status = admin_drop_user_and_poll(self.client, user, policy) assert status == 0 try: self.client.admin_query_user_info(user) - except e.InvalidUser as exception: assert exception.code == 60 assert exception.msg == "AEROSPIKE_INVALID_USER" @@ -77,7 +70,7 @@ def test_drop_user_with_user_none(self): Invoke drop_user() with policy none """ try: - self.client.admin_drop_user(None) + admin_drop_user_and_poll(self.client, None) except e.ParamError as exception: assert exception.code == -2 @@ -91,22 +84,14 @@ def test_drop_user_positive(self): password = "foo1" roles = ["read", "read-write", "sys-admin"] - status = self.client.admin_create_user(user, password, roles) - - time.sleep(1) - + status = admin_create_user_and_poll(self.client, user, password, roles) assert status == 0 - user_info = self.client.admin_query_user_info(user) - assert user_info["roles"] == ["read", "read-write", "sys-admin"] - status = self.client.admin_drop_user(user) + status = admin_drop_user_and_poll(self.client, user) assert status == 0 - time.sleep(2) - try: self.client.admin_query_user_info(user) - except e.InvalidUser as exception: assert exception.code == 60 assert exception.msg == "AEROSPIKE_INVALID_USER" @@ -119,22 +104,13 @@ def test_drop_user_positive_without_policy(self): password = "foo1" roles = ["read", "read-write", "sys-admin"] - status = self.client.admin_create_user(user, password, roles) - - time.sleep(1) - - assert status == 0 - user_info = self.client.admin_query_user_info(user) + status = admin_create_user_and_poll(self.client, user, password, roles) - assert user_info["roles"] == ["read", "read-write", "sys-admin"] - status = self.client.admin_drop_user(user) + status = admin_drop_user_and_poll(self.client, user) assert status == 0 - time.sleep(1) - try: self.client.admin_query_user_info(user) - except e.InvalidUser as exception: assert exception.code == 60 assert exception.msg == "AEROSPIKE_INVALID_USER" @@ -152,7 +128,7 @@ def test_drop_user_negative(self): assert exception.msg == "AEROSPIKE_INVALID_USER" try: - self.client.admin_drop_user(user) + admin_drop_user_and_poll(self.client, user) except e.InvalidUser as exception: assert exception.code == 60 @@ -166,9 +142,7 @@ def test_drop_user_policy_incorrect(self): password = "foo1" roles = ["read", "read-write", "sys-admin"] - status = self.client.admin_create_user(user, password, roles) - - time.sleep(1) + status = admin_create_user_and_poll(self.client, user, password, roles) assert status == 0 user_details = self.client.admin_query_user_info(user) @@ -176,24 +150,28 @@ def test_drop_user_policy_incorrect(self): assert user_details["roles"] == ["read", "read-write", "sys-admin"] policy = {"timeout": 0.2} try: - status = self.client.admin_drop_user(user, policy) + status = admin_drop_user_and_poll(self.client, user, policy) except e.ParamError as exception: assert exception.code == -2 assert exception.msg == "timeout is invalid" - status = self.client.admin_drop_user(user) + status = admin_drop_user_and_poll(self.client, user) def test_drop_user_with_extra_argument(self): """ Invoke drop_user() with extra argument. """ with pytest.raises(TypeError) as typeError: - self.client.admin_drop_user("foo-test", None, "") + admin_drop_user_and_poll(self.client, "foo-test", None, "") - assert "admin_drop_user() takes at most 2 arguments (3 given)" in str(typeError.value) + assert "admin_drop_user() takes at most 2 arguments (3 given)" in str( + typeError.value + ) - @pytest.mark.xfail(reason="It is no longer possible to create a user with" "a name too long") + @pytest.mark.xfail( + reason="It is no longer possible to create a user with" "a name too long" + ) def test_drop_user_with_too_long_username(self): user = "user$" * 1000 @@ -201,14 +179,14 @@ def test_drop_user_with_too_long_username(self): roles = ["sys-admin"] try: - self.client.admin_create_user(user, password, roles) + admin_create_user_and_poll(self.client, user, password, roles) except e.InvalidUser as exception: assert exception.code == 60 assert exception.msg == "AEROSPIKE_INVALID_USER" try: - self.client.admin_drop_user(user) + admin_drop_user_and_poll(self.client, user) except e.InvalidUser as exception: assert exception.code == 60 @@ -221,12 +199,11 @@ def test_drop_user_with_special_characters_in_username(self): roles = ["read-write"] try: - status = self.client.admin_create_user(user, password, roles) + status = admin_create_user_and_poll(self.client, user, password, roles) assert status == 0 - time.sleep(1) except Exception: pass - status = self.client.admin_drop_user(user) + status = admin_drop_user_and_poll(self.client, user) assert status == 0 diff --git a/test/new_tests/test_admin_get_role.py b/test/new_tests/test_admin_get_role.py index 4379ad5768..aa31f5b97b 100644 --- a/test/new_tests/test_admin_get_role.py +++ b/test/new_tests/test_admin_get_role.py @@ -4,6 +4,7 @@ import time from .test_base_class import TestBaseClass from aerospike import exception as e +from .conftest import admin_drop_role_and_poll, poll_until_role_doesnt_exist, admin_create_role_and_poll import aerospike @@ -11,7 +12,8 @@ class TestGetRole(TestBaseClass): pytestmark = pytest.mark.skipif( - not TestBaseClass.auth_in_use(), reason="No user specified, may be not secured cluster." + not TestBaseClass.auth_in_use(), + reason="No user specified, may be not secured cluster.", ) def setup_method(self, method): @@ -19,29 +21,31 @@ def setup_method(self, method): Setup method """ config = TestBaseClass.get_connection_config() - self.client = aerospike.client(config).connect(config["user"], config["password"]) + self.client = aerospike.client(config).connect( + config["user"], config["password"] + ) try: - self.client.admin_drop_role("usr-sys-admin") - time.sleep(2) + admin_drop_role_and_poll(self.client, "usr-sys-admin") except Exception: pass - usr_sys_admin_privs = [{"code": aerospike.PRIV_USER_ADMIN}, {"code": aerospike.PRIV_SYS_ADMIN}] + usr_sys_admin_privs = [ + {"code": aerospike.PRIV_USER_ADMIN}, + {"code": aerospike.PRIV_SYS_ADMIN}, + ] try: - self.client.admin_drop_role("usr-sys-admin-test") - time.sleep(2) + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except Exception: pass - self.client.admin_create_role("usr-sys-admin-test", usr_sys_admin_privs) + admin_create_role_and_poll(self.client, "usr-sys-admin-test", usr_sys_admin_privs) self.delete_users = [] - time.sleep(1) def teardown_method(self, method): """ Teardown method """ try: - self.client.admin_drop_role("usr-sys-admin-test") + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except Exception: pass self.client.close() @@ -59,7 +63,10 @@ def test_admin_get_role_positive(self): """ roles = self.client.admin_get_role("usr-sys-admin-test") assert roles == { - "privileges": [{"ns": "", "set": "", "code": 0}, {"ns": "", "set": "", "code": 1}], + "privileges": [ + {"ns": "", "set": "", "code": 0}, + {"ns": "", "set": "", "code": 1}, + ], "whitelist": [], "read_quota": 0, "write_quota": 0, @@ -71,7 +78,10 @@ def test_admin_get_role_positive_with_policy(self): """ roles = self.client.admin_get_role("usr-sys-admin-test", {"timeout": 180000}) assert roles == { - "privileges": [{"ns": "", "set": "", "code": 0}, {"ns": "", "set": "", "code": 1}], + "privileges": [ + {"ns": "", "set": "", "code": 0}, + {"ns": "", "set": "", "code": 1}, + ], "whitelist": [], "read_quota": 0, "write_quota": 0, diff --git a/test/new_tests/test_admin_get_roles.py b/test/new_tests/test_admin_get_roles.py index 06a455e31d..8a4a3a2921 100644 --- a/test/new_tests/test_admin_get_roles.py +++ b/test/new_tests/test_admin_get_roles.py @@ -6,7 +6,7 @@ from aerospike import exception as e import aerospike - +from .conftest import admin_drop_role_and_poll, poll_until_role_doesnt_exist, admin_create_role_and_poll class TestGetRoles(TestBaseClass): @@ -21,26 +21,23 @@ def setup_method(self, method): config = TestBaseClass.get_connection_config() self.client = aerospike.client(config).connect(config["user"], config["password"]) try: - self.client.admin_drop_role("usr-sys-admin") + admin_drop_role_and_poll(self.client, "usr-sys-admin") except Exception: pass - time.sleep(2) usr_sys_admin_privs = [{"code": aerospike.PRIV_USER_ADMIN}, {"code": aerospike.PRIV_SYS_ADMIN}] try: - self.client.admin_drop_role("usr-sys-admin-test") + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except Exception: pass - time.sleep(2) - self.client.admin_create_role("usr-sys-admin-test", usr_sys_admin_privs) + admin_create_role_and_poll(self.client, "usr-sys-admin-test", usr_sys_admin_privs) self.delete_users = [] - time.sleep(2) def teardown_method(self, method): """ Teardown method """ try: - self.client.admin_drop_role("usr-sys-admin-test") + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except Exception: pass self.client.close() diff --git a/test/new_tests/test_admin_grant_privileges.py b/test/new_tests/test_admin_grant_privileges.py index b4d5990430..243de81423 100644 --- a/test/new_tests/test_admin_grant_privileges.py +++ b/test/new_tests/test_admin_grant_privileges.py @@ -6,6 +6,7 @@ from aerospike import exception as e import aerospike +from .conftest import admin_drop_role_and_poll, poll_until_role_doesnt_exist, admin_create_role_and_poll class TestGrantPrivileges(object): @@ -13,7 +14,8 @@ class TestGrantPrivileges(object): config = TestBaseClass.get_connection_config() pytestmark = pytest.mark.skipif( - not TestBaseClass.auth_in_use(), reason="No user specified, may be not secured cluster." + not TestBaseClass.auth_in_use(), + reason="No user specified, may be not secured cluster.", ) def setup_method(self, method): @@ -21,18 +23,19 @@ def setup_method(self, method): Setup method """ config = self.config - self.client = aerospike.client(config).connect(config["user"], config["password"]) + self.client = aerospike.client(config).connect( + config["user"], config["password"] + ) try: - self.client.admin_drop_role("usr-sys-admin-test") - time.sleep(1) + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except e.InvalidRole: pass - self.client.admin_create_role( - "usr-sys-admin-test", [{"code": aerospike.PRIV_USER_ADMIN}, {"code": aerospike.PRIV_SYS_ADMIN}] + admin_create_role_and_poll(self.client, + "usr-sys-admin-test", + [{"code": aerospike.PRIV_USER_ADMIN}, {"code": aerospike.PRIV_SYS_ADMIN}], ) self.delete_users = [] - time.sleep(1) def teardown_method(self, method): """ @@ -40,8 +43,8 @@ def teardown_method(self, method): """ try: - self.client.admin_drop_role("usr-sys-admin-test") - time.sleep(1) + # TODO: is this necessary if we already drop the role at the beginning of each test? + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except e.InvalidRole: pass self.client.close() @@ -57,7 +60,9 @@ def test_admin_grant_privileges_positive(self): """ Grant privileges positive """ - status = self.client.admin_grant_privileges("usr-sys-admin-test", [{"code": aerospike.PRIV_READ}]) + status = self.client.admin_grant_privileges( + "usr-sys-admin-test", [{"code": aerospike.PRIV_READ}] + ) assert status == 0 time.sleep(1) @@ -68,7 +73,9 @@ def test_admin_grant_privileges_positive(self): {"code": 10, "ns": "", "set": ""}, ] - status = self.client.admin_revoke_privileges("usr-sys-admin-test", [{"code": aerospike.PRIV_READ}]) + status = self.client.admin_revoke_privileges( + "usr-sys-admin-test", [{"code": aerospike.PRIV_READ}] + ) assert status == 0 @@ -94,7 +101,11 @@ def test_admin_grant_privileges_all_positive(self, privs): assert status == 0 time.sleep(1) roles = self.client.admin_query_role("usr-sys-admin-test") - assert roles == [{"code": 0, "ns": "", "set": ""}, {"code": 1, "ns": "", "set": ""}, *privs] + assert roles == [ + {"code": 0, "ns": "", "set": ""}, + {"code": 1, "ns": "", "set": ""}, + *privs, + ] status = self.client.admin_revoke_privileges("usr-sys-admin-test", privs) @@ -104,7 +115,9 @@ def test_admin_grant_privileges_positive_write(self): """ Grant write privileges positive """ - status = self.client.admin_grant_privileges("usr-sys-admin-test", [{"code": aerospike.PRIV_WRITE}]) + status = self.client.admin_grant_privileges( + "usr-sys-admin-test", [{"code": aerospike.PRIV_WRITE}] + ) assert status == 0 time.sleep(1) @@ -115,7 +128,9 @@ def test_admin_grant_privileges_positive_write(self): {"code": 13, "ns": "", "set": ""}, ] - status = self.client.admin_revoke_privileges("usr-sys-admin-test", [{"code": aerospike.PRIV_WRITE}]) + status = self.client.admin_revoke_privileges( + "usr-sys-admin-test", [{"code": aerospike.PRIV_WRITE}] + ) assert status == 0 @@ -136,7 +151,9 @@ def test_admin_grant_privileges_positive_with_policy(self): {"code": 10, "ns": "", "set": ""}, ] - status = self.client.admin_revoke_privileges("usr-sys-admin-test", [{"code": aerospike.PRIV_READ}]) + status = self.client.admin_revoke_privileges( + "usr-sys-admin-test", [{"code": aerospike.PRIV_READ}] + ) assert status == 0 @@ -145,7 +162,8 @@ def test_admin_grant_privileges_positive_with_ns_set(self): Grant privileges positive with ns and set """ status = self.client.admin_grant_privileges( - "usr-sys-admin-test", [{"code": aerospike.PRIV_READ, "ns": "test", "set": "demo"}] + "usr-sys-admin-test", + [{"code": aerospike.PRIV_READ, "ns": "test", "set": "demo"}], ) assert status == 0 @@ -158,7 +176,8 @@ def test_admin_grant_privileges_positive_with_ns_set(self): ] status = self.client.admin_revoke_privileges( - "usr-sys-admin-test", [{"code": aerospike.PRIV_READ, "ns": "test", "set": "demo"}] + "usr-sys-admin-test", + [{"code": aerospike.PRIV_READ, "ns": "test", "set": "demo"}], ) assert status == 0 diff --git a/test/new_tests/test_admin_grant_roles.py b/test/new_tests/test_admin_grant_roles.py index c009904f9e..02c82d9d5a 100644 --- a/test/new_tests/test_admin_grant_roles.py +++ b/test/new_tests/test_admin_grant_roles.py @@ -6,6 +6,7 @@ from aerospike import exception as e import aerospike +from .conftest import admin_drop_user_and_poll, poll_until_user_doesnt_exist, admin_create_user_and_poll class TestGrantRoles(TestBaseClass): @@ -23,8 +24,7 @@ def setup_method(self, method): self.client = aerospike.client(config).connect(config["user"], config["password"]) try: - self.client.admin_drop_user("example-test") - time.sleep(1) + admin_drop_user_and_poll(self.client, "example-test") except e.InvalidUser: pass user = "example-test" @@ -32,8 +32,7 @@ def setup_method(self, method): roles = ["read-write"] try: - self.client.admin_create_user(user, password, roles) - time.sleep(1) + admin_create_user_and_poll(self.client, user, password, roles) except e.UserExistsError: pass self.delete_users = [] @@ -44,8 +43,7 @@ def teardown_method(self, method): """ try: - self.client.admin_drop_user("example-test") - time.sleep(1) + admin_drop_user_and_poll(self.client, "example-test") except e.InvalidUser: pass self.client.close() @@ -141,8 +139,7 @@ def test_grant_roles_with_special_characters_in_username(self): roles = ["read-write"] try: - self.client.admin_create_user(user, password, roles) - time.sleep(1) + admin_create_user_and_poll(self.client, user, password, roles) except e.UserExistsError: pass @@ -157,7 +154,7 @@ def test_grant_roles_with_special_characters_in_username(self): assert set(user_details["roles"]) == set(["read", "read-write"]) - self.client.admin_drop_user(user) + admin_drop_user_and_poll(self.client, user) def test_grant_roles_with_empty_roles_list(self): diff --git a/test/new_tests/test_admin_query_role.py b/test/new_tests/test_admin_query_role.py index 0510ae3677..e5f5e0bb1e 100644 --- a/test/new_tests/test_admin_query_role.py +++ b/test/new_tests/test_admin_query_role.py @@ -6,42 +6,44 @@ from aerospike import exception as e import aerospike +from .conftest import admin_drop_role_and_poll, poll_until_role_doesnt_exist, admin_create_role_and_poll -class TestQueryRole(TestBaseClass): - - pytestmark = pytest.mark.skipif( - not TestBaseClass.auth_in_use(), reason="No user specified, may be not secured cluster." - ) +@pytest.mark.usefixtures("connection_config") +class TestQueryRole: def setup_method(self, method): """ Setup method """ + if not TestBaseClass.auth_in_use(): + pytest.skip(reason="No user specified, may be not secured cluster.") + config = TestBaseClass.get_connection_config() - self.client = aerospike.client(config).connect(config["user"], config["password"]) + self.client = aerospike.client(config).connect( + config["user"], config["password"] + ) try: - self.client.admin_drop_role("usr-sys-admin") - time.sleep(2) + admin_drop_role_and_poll(self.client, "usr-sys-admin") except Exception: pass - usr_sys_admin_privs = [{"code": aerospike.PRIV_USER_ADMIN}, {"code": aerospike.PRIV_SYS_ADMIN}] + usr_sys_admin_privs = [ + {"code": aerospike.PRIV_USER_ADMIN}, + {"code": aerospike.PRIV_SYS_ADMIN}, + ] try: - self.client.admin_drop_role("usr-sys-admin-test") - time.sleep(2) + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except Exception: pass - self.client.admin_create_role("usr-sys-admin-test", usr_sys_admin_privs) - + admin_create_role_and_poll(self.client, "usr-sys-admin-test", usr_sys_admin_privs) self.delete_users = [] - time.sleep(1) def teardown_method(self, method): """ Teardown method """ try: - self.client.admin_drop_role("usr-sys-admin-test") + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except Exception: pass self.client.close() @@ -58,14 +60,20 @@ def test_admin_query_role_positive(self): Query role positive """ roles = self.client.admin_query_role("usr-sys-admin-test") - assert roles == [{"code": 0, "ns": "", "set": ""}, {"code": 1, "ns": "", "set": ""}] + assert roles == [ + {"code": 0, "ns": "", "set": ""}, + {"code": 1, "ns": "", "set": ""}, + ] def test_admin_query_role_positive_with_policy(self): """ Query role positive policy """ roles = self.client.admin_query_role("usr-sys-admin-test", {"timeout": 180000}) - assert roles == [{"code": 0, "ns": "", "set": ""}, {"code": 1, "ns": "", "set": ""}] + assert roles == [ + {"code": 0, "ns": "", "set": ""}, + {"code": 1, "ns": "", "set": ""}, + ] def test_admin_query_role_incorrect_role_name(self): """ diff --git a/test/new_tests/test_admin_query_roles.py b/test/new_tests/test_admin_query_roles.py index 803d34e9e9..a68c14eafd 100644 --- a/test/new_tests/test_admin_query_roles.py +++ b/test/new_tests/test_admin_query_roles.py @@ -6,6 +6,7 @@ from aerospike import exception as e import aerospike +from .conftest import admin_drop_role_and_poll, poll_until_role_doesnt_exist, admin_create_role_and_poll class TestQueryRoles(TestBaseClass): @@ -21,26 +22,23 @@ def setup_method(self, method): config = TestBaseClass.get_connection_config() self.client = aerospike.client(config).connect(config["user"], config["password"]) try: - self.client.admin_drop_role("usr-sys-admin") + admin_drop_role_and_poll(self.client, "usr-sys-admin") except Exception: pass - time.sleep(2) usr_sys_admin_privs = [{"code": aerospike.PRIV_USER_ADMIN}, {"code": aerospike.PRIV_SYS_ADMIN}] try: - self.client.admin_drop_role("usr-sys-admin-test") + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except Exception: pass - time.sleep(2) - self.client.admin_create_role("usr-sys-admin-test", usr_sys_admin_privs) + admin_create_role_and_poll(self.client, "usr-sys-admin-test", usr_sys_admin_privs) self.delete_users = [] - time.sleep(2) def teardown_method(self, method): """ Teardown method """ try: - self.client.admin_drop_role("usr-sys-admin-test") + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except Exception: pass self.client.close() diff --git a/test/new_tests/test_admin_query_user_info.py b/test/new_tests/test_admin_query_user_info.py index b56a3e70f6..5c0b5f5ad3 100644 --- a/test/new_tests/test_admin_query_user_info.py +++ b/test/new_tests/test_admin_query_user_info.py @@ -6,6 +6,7 @@ from aerospike import exception as e import aerospike +from .conftest import admin_drop_user_and_poll, poll_until_user_doesnt_exist, admin_create_user_and_poll class TestQueryUserInfo(TestBaseClass): @@ -23,16 +24,14 @@ def setup_method(self, method): self.user = "example-test" self.client = aerospike.client(config).connect(config["user"], config["password"]) try: - self.client.admin_drop_user(self.user) - time.sleep(1) + admin_drop_user_and_poll(self.client, self.user) except e.InvalidUser: pass password = "foo2" roles = ["read-write", "sys-admin", "read"] try: - self.client.admin_create_user(self.user, password, roles) - time.sleep(1) + admin_create_user_and_poll(self.client, self.user, password, roles) except e.UserExistsError: pass self.delete_users = [] @@ -43,8 +42,7 @@ def teardown_method(self, method): """ try: - self.client.admin_drop_user(self.user) - time.sleep(1) + admin_drop_user_and_poll(self.client, self.user) except e.InvalidUser: pass @@ -57,7 +55,6 @@ def test_query_user_info_without_any_parameters(self): def test_query_user_info_with_proper_parameters(self): - time.sleep(2) user_details = self.client.admin_query_user_info(self.user) assert user_details.get("roles") == [ "read", diff --git a/test/new_tests/test_admin_query_users_info.py b/test/new_tests/test_admin_query_users_info.py index 11db38ef38..9bbc067790 100644 --- a/test/new_tests/test_admin_query_users_info.py +++ b/test/new_tests/test_admin_query_users_info.py @@ -6,12 +6,14 @@ from aerospike import exception as e import aerospike +from .conftest import admin_drop_user_and_poll, poll_until_user_doesnt_exist, admin_create_user_and_poll class TestQueryUsersInfo(TestBaseClass): pytestmark = pytest.mark.skipif( - not TestBaseClass.auth_in_use(), reason="No user specified, may not be secured cluster." + not TestBaseClass.auth_in_use(), + reason="No user specified, may not be secured cluster.", ) def setup_method(self, method): @@ -20,11 +22,12 @@ def setup_method(self, method): """ config = TestBaseClass.get_connection_config() TestQueryUsersInfo.Me = self - self.client = aerospike.client(config).connect(config["user"], config["password"]) + self.client = aerospike.client(config).connect( + config["user"], config["password"] + ) try: - self.client.admin_drop_user("example-test") - time.sleep(2) + admin_drop_user_and_poll(self.client, "example-test") except e.InvalidUser: pass user = "example-test" @@ -32,8 +35,7 @@ def setup_method(self, method): roles = ["read-write", "sys-admin", "read"] try: - self.client.admin_create_user(user, password, roles) - time.sleep(2) + admin_create_user_and_poll(self.client, user, password, roles) except e.UserExistsError: pass self.delete_users = [] @@ -44,14 +46,13 @@ def teardown_method(self, method): """ try: - self.client.admin_drop_user("example-test") + admin_drop_user_and_poll(self.client, "example-test") except Exception: pass self.client.close() def test_query_users_info_with_proper_parameters(self): - time.sleep(2) user_details = self.client.admin_query_users_info() # Usage test; doesn't actually test if the server records user data @@ -78,7 +79,11 @@ def test_query_users_info_with_proper_timeout_policy_value(self): user_details = self.client.admin_query_users_info(policy) - assert user_details.get("example-test").get("roles") == ["read", "read-write", "sys-admin"] + assert user_details.get("example-test").get("roles") == [ + "read", + "read-write", + "sys-admin", + ] def test_query_users_info_with_no_roles(self): @@ -100,7 +105,9 @@ def test_query_users_info_with_extra_argument(self): with pytest.raises(TypeError) as typeError: self.client.admin_query_users_info(None, "") - assert "admin_query_users_info() takes at most 1 argument (2 given)" in str(typeError.value) + assert "admin_query_users_info() takes at most 1 argument (2 given)" in str( + typeError.value + ) def test_query_users_info_with_policy_as_string(self): """ diff --git a/test/new_tests/test_admin_revoke_privileges.py b/test/new_tests/test_admin_revoke_privileges.py index e5bfef9c24..ceb52da490 100644 --- a/test/new_tests/test_admin_revoke_privileges.py +++ b/test/new_tests/test_admin_revoke_privileges.py @@ -6,6 +6,7 @@ from aerospike import exception as e import aerospike +from .conftest import admin_drop_role_and_poll, poll_until_role_doesnt_exist, admin_create_role_and_poll class TestRevokePrivilege(TestBaseClass): @@ -23,14 +24,12 @@ def setup_method(self, method): config = self.config self.client = aerospike.client(config).connect(config["user"], config["password"]) try: - self.client.admin_drop_role("usr-sys-admin-test") - time.sleep(2) + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except e.InvalidRole: pass - self.client.admin_create_role( + admin_create_role_and_poll(self.client, "usr-sys-admin-test", [{"code": aerospike.PRIV_USER_ADMIN}, {"code": aerospike.PRIV_SYS_ADMIN}] ) - time.sleep(2) self.delete_users = [] def teardown_method(self, method): @@ -38,7 +37,7 @@ def teardown_method(self, method): Teardown method """ try: - self.client.admin_drop_role("usr-sys-admin-test") + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except e.InvalidRole: pass self.client.close() diff --git a/test/new_tests/test_admin_revoke_roles.py b/test/new_tests/test_admin_revoke_roles.py index 10616159bf..5f171a2631 100644 --- a/test/new_tests/test_admin_revoke_roles.py +++ b/test/new_tests/test_admin_revoke_roles.py @@ -6,6 +6,7 @@ from aerospike import exception as e import aerospike +from .conftest import admin_drop_user_and_poll, poll_until_user_doesnt_exist, admin_create_user_and_poll class TestRevokeRoles(TestBaseClass): @@ -22,8 +23,7 @@ def setup_method(self, method): TestRevokeRoles.Me = self self.client = aerospike.client(config).connect(config["user"], config["password"]) try: - self.client.admin_drop_user("example-test") - time.sleep(1) + admin_drop_user_and_poll(self.client, "example-test") except e.InvalidUser: pass user = "example-test" @@ -31,8 +31,7 @@ def setup_method(self, method): roles = ["read-write", "sys-admin", "read"] try: - self.client.admin_create_user(user, password, roles) - time.sleep(1) + admin_create_user_and_poll(self.client, user, password, roles) except e.UserExistsError: pass @@ -44,8 +43,7 @@ def teardown_method(self, method): """ try: - self.client.admin_drop_user("example-test") - time.sleep(1) + admin_drop_user_and_poll(self.client, "example-test") except e.InvalidUser: pass self.client.close() @@ -73,7 +71,6 @@ def test_revoke_all_roles_with_proper_parameters(self): user = "example-test" roles = ["read", "sys-admin", "read-write"] - time.sleep(2) status = self.client.admin_revoke_roles(user, roles) assert status == 0 time.sleep(2) @@ -165,8 +162,7 @@ def test_revoke_roles_with_special_characters_in_username(self): password = "abcd" roles = ["read-write"] - status = self.client.admin_create_user(user, password, roles) - time.sleep(2) + status = admin_create_user_and_poll(self.client, user, password, roles) assert status == 0 status = self.client.admin_revoke_roles(user, roles) @@ -179,7 +175,7 @@ def test_revoke_roles_with_special_characters_in_username(self): assert user_details["roles"] == [] - status = self.client.admin_drop_user("!#Q#AEQ@#$%&^*((^&*~~~````[[") + status = admin_drop_user_and_poll(self.client, "!#Q#AEQ@#$%&^*((^&*~~~````[[") assert status == 0 def test_revoke_roles_nonpossessed(self): @@ -188,8 +184,7 @@ def test_revoke_roles_nonpossessed(self): password = "abcd" roles = ["read-write"] - status = self.client.admin_create_user(user, password, roles) - time.sleep(2) + status = admin_create_user_and_poll(self.client, user, password, roles) assert status == 0 roles = ["read"] @@ -202,7 +197,7 @@ def test_revoke_roles_nonpossessed(self): assert user_details["roles"] == ["read-write"] assert status == 0 - status = self.client.admin_drop_user(user) + status = admin_drop_user_and_poll(self.client, user) assert status == 0 def test_revoke_roles_with_roles_exceeding_max_length(self): diff --git a/test/new_tests/test_admin_set_password.py b/test/new_tests/test_admin_set_password.py index 0e0d5d400e..76f9c9c39f 100644 --- a/test/new_tests/test_admin_set_password.py +++ b/test/new_tests/test_admin_set_password.py @@ -6,6 +6,7 @@ from aerospike import exception as e import aerospike +from .conftest import admin_drop_user_and_poll, poll_until_user_doesnt_exist, admin_create_user_and_poll class TestSetPassword(TestBaseClass): @@ -22,17 +23,15 @@ def setup_method(self, method): TestSetPassword.Me = self self.client = aerospike.client(config).connect(config["user"], config["password"]) try: - self.client.admin_drop_user("testsetpassworduser") - time.sleep(2) + admin_drop_user_and_poll(self.client, "testsetpassworduser") except e.InvalidUser: pass try: - self.client.admin_create_user("testsetpassworduser", "aerospike", ["read"]) + admin_create_user_and_poll(self.client, "testsetpassworduser", "aerospike", ["read"]) except e.UserExistsError: pass - time.sleep(2) self.delete_users = [] def teardown_method(self, method): @@ -41,8 +40,7 @@ def teardown_method(self, method): """ try: - self.client.admin_drop_user("testsetpassworduser") - time.sleep(2) + admin_drop_user_and_poll(self.client, "testsetpassworduser") except e.InvalidUser: pass self.client.close() diff --git a/test/new_tests/test_admin_set_quotas.py b/test/new_tests/test_admin_set_quotas.py index 4f7e21316d..40b8c3a69a 100644 --- a/test/new_tests/test_admin_set_quotas.py +++ b/test/new_tests/test_admin_set_quotas.py @@ -6,6 +6,7 @@ from aerospike import exception as e import aerospike +from .conftest import admin_drop_role_and_poll, poll_until_role_doesnt_exist, admin_create_role_and_poll class TestSetQuotas(TestBaseClass): @@ -19,24 +20,22 @@ def setup_method(self, method): usr_sys_admin_privs = [{"code": aerospike.PRIV_USER_ADMIN}, {"code": aerospike.PRIV_SYS_ADMIN}] try: - self.client.admin_drop_role("usr-sys-admin-test") - time.sleep(2) + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except Exception: pass try: - self.client.admin_create_role("usr-sys-admin-test", usr_sys_admin_privs, write_quota=4500) + admin_create_role_and_poll(self.client, "usr-sys-admin-test", usr_sys_admin_privs, write_quota=4500) except e.QuotasNotEnabled: pytest.skip(reason="Got QuotasNotEnabled, skipping quota test.") - time.sleep(1) def teardown_method(self, method): """ Teardown method """ try: - self.client.admin_drop_role("usr-sys-admin-test") + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except Exception: pass diff --git a/test/new_tests/test_admin_set_whitelist.py b/test/new_tests/test_admin_set_whitelist.py index 1577129a34..e82f051689 100644 --- a/test/new_tests/test_admin_set_whitelist.py +++ b/test/new_tests/test_admin_set_whitelist.py @@ -6,6 +6,7 @@ from aerospike import exception as e import aerospike +from .conftest import admin_drop_role_and_poll, admin_drop_user_and_poll, poll_until_role_doesnt_exist, admin_create_role_and_poll class TestSetWhitelist(TestBaseClass): @@ -13,7 +14,8 @@ class TestSetWhitelist(TestBaseClass): config = TestBaseClass.get_connection_config() pytestmark = pytest.mark.skipif( - not TestBaseClass.auth_in_use(), reason="No user specified, may be not secured cluster." + not TestBaseClass.auth_in_use(), + reason="No user specified, may be not secured cluster.", ) def setup_method(self, method): @@ -21,25 +23,30 @@ def setup_method(self, method): Setup method """ config = self.config - self.client = aerospike.client(config).connect(config["user"], config["password"]) - usr_sys_admin_privs = [{"code": aerospike.PRIV_USER_ADMIN}, {"code": aerospike.PRIV_SYS_ADMIN}] + self.client = aerospike.client(config).connect( + config["user"], config["password"] + ) + usr_sys_admin_privs = [ + {"code": aerospike.PRIV_USER_ADMIN}, + {"code": aerospike.PRIV_SYS_ADMIN}, + ] whitelist = ["127.0.0.1"] try: - self.client.admin_drop_role("usr-sys-admin-test") - time.sleep(2) + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except Exception: pass - self.client.admin_create_role("usr-sys-admin-test", usr_sys_admin_privs, whitelist=whitelist) + admin_create_role_and_poll(self.client, + "usr-sys-admin-test", usr_sys_admin_privs, whitelist=whitelist + ) self.delete_users = [] - time.sleep(1) def teardown_method(self, method): """ Teardown method """ try: - self.client.admin_drop_role("usr-sys-admin-test") + admin_drop_role_and_poll(self.client, "usr-sys-admin-test") except Exception: pass self.client.close() @@ -59,7 +66,10 @@ def test_admin_set_whitelist_empty_whitelist_positive(self): time.sleep(1) roles = self.client.admin_get_role("usr-sys-admin-test") assert roles == { - "privileges": [{"ns": "", "set": "", "code": 0}, {"ns": "", "set": "", "code": 1}], + "privileges": [ + {"ns": "", "set": "", "code": 0}, + {"ns": "", "set": "", "code": 1}, + ], "whitelist": [], "read_quota": 0, "write_quota": 0, @@ -73,7 +83,10 @@ def test_admin_set_whitelist_none_whitelist_positive(self): time.sleep(1) roles = self.client.admin_get_role("usr-sys-admin-test") assert roles == { - "privileges": [{"ns": "", "set": "", "code": 0}, {"ns": "", "set": "", "code": 1}], + "privileges": [ + {"ns": "", "set": "", "code": 0}, + {"ns": "", "set": "", "code": 1}, + ], "whitelist": [], "read_quota": 0, "write_quota": 0, @@ -83,11 +96,17 @@ def test_admin_set_whitelist_one_whitelist_positive(self): """ Set whitelist with whitelist. """ - self.client.admin_set_whitelist(role="usr-sys-admin-test", whitelist=["10.0.2.0/24", "127.0.0.1", "127.0.0.2"]) + self.client.admin_set_whitelist( + role="usr-sys-admin-test", + whitelist=["10.0.2.0/24", "127.0.0.1", "127.0.0.2"], + ) time.sleep(1) roles = self.client.admin_get_role("usr-sys-admin-test") assert roles == { - "privileges": [{"ns": "", "set": "", "code": 0}, {"ns": "", "set": "", "code": 1}], + "privileges": [ + {"ns": "", "set": "", "code": 0}, + {"ns": "", "set": "", "code": 1}, + ], "whitelist": ["10.0.2.0/24", "127.0.0.1", "127.0.0.2"], "read_quota": 0, "write_quota": 0, @@ -101,7 +120,10 @@ def test_admin_set_quota_empty_positive(self): time.sleep(1) roles = self.client.admin_get_role("usr-sys-admin-test") assert roles == { - "privileges": [{"ns": "", "set": "", "code": 0}, {"ns": "", "set": "", "code": 1}], + "privileges": [ + {"ns": "", "set": "", "code": 0}, + {"ns": "", "set": "", "code": 1}, + ], "whitelist": [], "read_quota": 0, "write_quota": 0, @@ -112,12 +134,17 @@ def test_admin_set_whitelist_positive_with_policy(self): Set whitelist positive policy """ self.client.admin_set_whitelist( - role="usr-sys-admin-test", whitelist=["10.0.2.0/24", "127.0.0.1"], policy={"timeout": 180000} + role="usr-sys-admin-test", + whitelist=["10.0.2.0/24", "127.0.0.1"], + policy={"timeout": 180000}, ) time.sleep(1) roles = self.client.admin_get_role("usr-sys-admin-test") assert roles == { - "privileges": [{"ns": "", "set": "", "code": 0}, {"ns": "", "set": "", "code": 1}], + "privileges": [ + {"ns": "", "set": "", "code": 0}, + {"ns": "", "set": "", "code": 1}, + ], "whitelist": ["10.0.2.0/24", "127.0.0.1"], "read_quota": 0, "write_quota": 0, @@ -128,7 +155,9 @@ def test_admin_set_whitelist_incorrect_role_name(self): Incorrect role name """ try: - self.client.admin_set_whitelist(role="bad-role-name", whitelist=["10.0.2.0/24"]) + self.client.admin_set_whitelist( + role="bad-role-name", whitelist=["10.0.2.0/24"] + ) except e.InvalidRole as exception: assert exception.code == 70 @@ -150,7 +179,9 @@ def test_admin_set_whitelist_incorrect_whitelist(self): Incorrect role name """ try: - self.client.admin_set_whitelist(role="usr-sys-admin-test", whitelist=["bad_IP"]) + self.client.admin_set_whitelist( + role="usr-sys-admin-test", whitelist=["bad_IP"] + ) except e.InvalidWhitelist as exception: assert exception.code == 73 assert exception.msg == "AEROSPIKE_INVALID_WHITELIST" @@ -170,16 +201,22 @@ def test_admin_set_whitelist_forbiden_host(self): """ Forbiden host """ - self.client.admin_set_whitelist(role="usr-sys-admin-test", whitelist=["123.4.5.6"]) + self.client.admin_set_whitelist( + role="usr-sys-admin-test", whitelist=["123.4.5.6"] + ) - self.client.admin_create_user("test_whitelist_user", "123", ["usr-sys-admin-test"]) + self.client.admin_create_user( + "test_whitelist_user", "123", ["usr-sys-admin-test"] + ) config = TestBaseClass.get_connection_config() - new_client = aerospike.client(config).connect(config["user"], config["password"]) + new_client = aerospike.client(config).connect( + config["user"], config["password"] + ) try: new_client.connect("test_whitelist_user", "123") except e.NotWhitelisted as exception: assert exception.code == 82 assert exception.msg == "Failed to connect" finally: - self.client.admin_drop_user("test_whitelist_user") + admin_drop_user_and_poll(self.client, "test_whitelist_user") diff --git a/test/new_tests/test_batch_read.py b/test/new_tests/test_batch_read.py index ba9879a376..00aff3e1d3 100644 --- a/test/new_tests/test_batch_read.py +++ b/test/new_tests/test_batch_read.py @@ -9,21 +9,21 @@ class TestBatchRead(TestBaseClass): - @pytest.fixture(autouse=True) - def setup(self, request, connection_with_config_funcs): + @pytest.fixture(autouse=True, scope="class") + def setup(self, connection_with_config_funcs): as_connection = connection_with_config_funcs - if self.server_version < [6, 0]: + if self.__class__.server_version < [6, 0]: pytest.mark.xfail(reason="Servers older than 6.0 do not support batch read.") pytest.xfail() - self.test_ns = "test" - self.test_set = "demo" - self.keys = [] - self.keys_to_expected_bins = {} - self.batch_size = 5 + self.__class__.test_ns = "test" + self.__class__.test_set = "demo" + self.__class__.keys = [] + self.__class__.keys_to_expected_bins = {} + self.__class__.batch_size = 5 - for i in range(self.batch_size): + for i in range(self.__class__.batch_size): key = ("test", "demo", i) rec = { "count": i, @@ -40,15 +40,14 @@ def setup(self, request, connection_with_config_funcs): }, } as_connection.put(key, rec) - self.keys.append(key) - self.keys_to_expected_bins[key] = rec + self.__class__.keys.append(key) + self.__class__.keys_to_expected_bins[key] = rec - def teardown(): - for i in range(self.batch_size): - key = ("test", "demo", i) - as_connection.remove(key) + yield - request.addfinalizer(teardown) + for i in range(self.__class__.batch_size): + key = ("test", "demo", i) + as_connection.remove(key) @pytest.mark.parametrize("bins", [ None, diff --git a/test/new_tests/test_cdt_compators.py b/test/new_tests/test_cdt_compators.py index 9cd9a0fe29..04459afcaa 100644 --- a/test/new_tests/test_cdt_compators.py +++ b/test/new_tests/test_cdt_compators.py @@ -17,12 +17,12 @@ def get_list_result_from_operation(client, key, operation, binname): class TestNewRelativeCDTValues(object): - @pytest.fixture(autouse=True) - def setup(self, request, as_connection): + @pytest.fixture(autouse=True, scope="class") + def setup(self, as_connection): """ Setup Method """ - self.keys = [] + self.__class__.keys = [] cdt_list_val = [[0, "a"], [1, "b"], [1, "c"], [1, "d", "e"], [2, "f"], [2, "two"], [3, "g"]] cdt_map_val = { "a": [0, "a"], @@ -34,22 +34,22 @@ def setup(self, request, as_connection): "g": [3, "g"], } - self.cdt_key = ("test", "cdt_values", "wildcard") - self.cdt_list_bin = "cdt_list_bin" - self.cdt_map_bin = "cdt_map_bin" + self.__class__.cdt_key = ("test", "cdt_values", "wildcard") + self.__class__.cdt_list_bin = "cdt_list_bin" + self.__class__.cdt_map_bin = "cdt_map_bin" - self.as_connection.put(self.cdt_key, {self.cdt_list_bin: cdt_list_val, self.cdt_map_bin: cdt_map_val}) + as_connection.put(self.__class__.cdt_key, {self.__class__.cdt_list_bin: cdt_list_val, self.__class__.cdt_map_bin: cdt_map_val}) # Make sure the list is ordered, in order to get expected return order. - ops = [lo.list_sort(self.cdt_list_bin, 0), lo.list_set_order(self.cdt_list_bin, aerospike.LIST_ORDERED)] - self.as_connection.operate(self.cdt_key, ops) + ops = [lo.list_sort(self.__class__.cdt_list_bin, 0), lo.list_set_order(self.__class__.cdt_list_bin, aerospike.LIST_ORDERED)] + as_connection.operate(self.__class__.cdt_key, ops) - self.keys.append(self.cdt_key) + self.__class__.keys.append(self.__class__.cdt_key) yield - for rec_key in self.keys: + for rec_key in self.__class__.keys: try: - self.as_connection.remove(rec_key) + as_connection.remove(rec_key) except e.AerospikeError: pass diff --git a/test/new_tests/test_new_constructor.py b/test/new_tests/test_new_constructor.py index d9233c5b1e..d0b4a74fa2 100644 --- a/test/new_tests/test_new_constructor.py +++ b/test/new_tests/test_new_constructor.py @@ -7,7 +7,7 @@ from aerospike_helpers.operations import operations from aerospike_helpers.batch.records import Write, BatchRecords from aerospike_helpers.metrics import MetricsPolicy -from .test_scan_execute_background import wait_for_job_completion +from .conftest import wait_for_job_completion import copy from contextlib import nullcontext import time @@ -465,7 +465,7 @@ def test_setting_scan_ttl(self, config_ttl_setup): scan.add_ops(ops) job_id = scan.execute_background() - wait_for_job_completion(self.client, job_id) + wait_for_job_completion(self.client, job_id, aerospike.JOB_SCAN) self.check_ttl() @@ -483,7 +483,7 @@ def test_query_client_default_ttl(self, config_ttl_setup): query.add_ops(ops) job_id = query.execute_background() - wait_for_job_completion(self.client, job_id) + wait_for_job_completion(self.client, job_id, aerospike.JOB_QUERY) self.check_ttl() diff --git a/test/new_tests/test_query.py b/test/new_tests/test_query.py index c9c710f7d6..19e7e4dd68 100644 --- a/test/new_tests/test_query.py +++ b/test/new_tests/test_query.py @@ -62,8 +62,9 @@ def add_ctx_op(ctx_type, value): class TestQuery(TestBaseClass): # TODO: This fixture should be split up to speed up this test class - def setup_class(cls): - client = TestBaseClass.get_new_connection() + @pytest.fixture(autouse=True, scope="class") + def setup(cls, as_connection): + client = as_connection try: client.index_integer_create("test", "demo", "test_age", "age_index") @@ -169,10 +170,43 @@ def setup_class(cls): except e.IndexFoundError: pass - client.close() + for i in range(5): + key = ("test", "demo", i) + # 5x5 box, then 10x10 box, ... until 25x25 box + box_coordinates = [[0, 0], [5 * (i + 1), 0], [5 * (i + 1), 5 * (i + 1)], [0, 5 * (i + 1)], [0, 0]] + rec = { + "geo_point": aerospike.GeoJSON({"type": "Point", "coordinates": [i, i]}), + "geo_polygon": aerospike.GeoJSON({"type": "Polygon", "coordinates": [box_coordinates]}), + "name": "name%s" % (str(i)), + "addr": "name%s" % (str(i)), + "numeric_list": [i, i + 1, i + 2], + "string_list": ["str" + str(i), "str" + str(i + 1), "str" + str(i + 2)], + "numeric_map": {"a": i, "b": i + 1, "c": i + 2}, + "string_map": {"a": "a" + str(i), "b": "b" + str(i + 1), "c": "c" + str(i + 2)}, + "blob_list": [i.to_bytes(length=1, byteorder='big')], + "blob_map": { + i.to_bytes(length=1, byteorder='big'): i.to_bytes(length=1, byteorder='big') + }, + "test_age_none": 1, + "test_age": i, + "no": i, + "blob": i.to_bytes(length=1, byteorder='big') + } + client.put(key, rec) + for i in range(5, 10): + key = ("test", "demo", i) + rec = {"name": "name%s" % (str(i)), "addr": "name%s" % (str(i)), "test_age": i, "no": i} + client.put(key, rec) + + key = ("test", "demo", 122) + llist = [{"op": aerospike.OPERATOR_WRITE, "bin": bytearray("sal\0kj", "utf-8"), "val": 80000}] + client.operate(key, llist) + + key = ("test", None, 145) + rec = {"test_age_none": 1} + client.put(key, rec) - def teardown_class(cls): - client = TestBaseClass.get_new_connection() + yield policy = {} try: @@ -251,63 +285,14 @@ def teardown_class(cls): except e.IndexNotFound: pass - client.close() - - @pytest.fixture(autouse=True) - def setup_method(self, request, as_connection): - """ - Setup method. - """ - for i in range(5): + for i in range(10): key = ("test", "demo", i) - # 5x5 box, then 10x10 box, ... until 25x25 box - box_coordinates = [[0, 0], [5 * (i + 1), 0], [5 * (i + 1), 5 * (i + 1)], [0, 5 * (i + 1)], [0, 0]] - rec = { - "geo_point": aerospike.GeoJSON({"type": "Point", "coordinates": [i, i]}), - "geo_polygon": aerospike.GeoJSON({"type": "Polygon", "coordinates": [box_coordinates]}), - "name": "name%s" % (str(i)), - "addr": "name%s" % (str(i)), - "numeric_list": [i, i + 1, i + 2], - "string_list": ["str" + str(i), "str" + str(i + 1), "str" + str(i + 2)], - "numeric_map": {"a": i, "b": i + 1, "c": i + 2}, - "string_map": {"a": "a" + str(i), "b": "b" + str(i + 1), "c": "c" + str(i + 2)}, - "blob_list": [i.to_bytes(length=1, byteorder='big')], - "blob_map": { - i.to_bytes(length=1, byteorder='big'): i.to_bytes(length=1, byteorder='big') - }, - "test_age_none": 1, - "test_age": i, - "no": i, - "blob": i.to_bytes(length=1, byteorder='big') - } - as_connection.put(key, rec) - for i in range(5, 10): - key = ("test", "demo", i) - rec = {"name": "name%s" % (str(i)), "addr": "name%s" % (str(i)), "test_age": i, "no": i} - as_connection.put(key, rec) + client.remove(key) key = ("test", "demo", 122) - llist = [{"op": aerospike.OPERATOR_WRITE, "bin": bytearray("sal\0kj", "utf-8"), "val": 80000}] - as_connection.operate(key, llist) - + client.remove(key) key = ("test", None, 145) - rec = {"test_age_none": 1} - as_connection.put(key, rec) - - def teardown(): - """ - Teardown method. - """ - for i in range(10): - key = ("test", "demo", i) - as_connection.remove(key) - - key = ("test", "demo", 122) - as_connection.remove(key) - key = ("test", None, 145) - as_connection.remove(key) - - request.addfinalizer(teardown) + client.remove(key) def test_query_with_correct_parameters_hi(self): """ diff --git a/test/new_tests/test_query_execute_background.py b/test/new_tests/test_query_execute_background.py index 756699d1e9..a9c1e3d35b 100644 --- a/test/new_tests/test_query_execute_background.py +++ b/test/new_tests/test_query_execute_background.py @@ -6,6 +6,7 @@ from aerospike_helpers import expressions as exp from aerospike_helpers.operations import operations from aerospike import exception, predicates +from .conftest import wait_for_job_completion TEST_NS = "test" TEST_SET = "background_q_e" @@ -18,16 +19,6 @@ long = int -def wait_for_job_completion(as_connection, job_id): - """ - Blocks until the job has completed - """ - while True: - response = as_connection.job_info(job_id, aerospike.JOB_QUERY) - if response["status"] != aerospike.JOB_STATUS_INPROGRESS: - break - time.sleep(0.1) - def add_indexes_to_client(client): try: @@ -119,11 +110,11 @@ def test_background_execute_no_predicate(self, clean_test_background): query = self.as_connection.query(TEST_NS, TEST_SET) query.apply(TEST_UDF_MODULE, TEST_UDF_FUNCTION, [test_bin]) - query.execute_background() + job_id = query.execute_background() # Give time for the query to finish - time.sleep(5) - # wait_for_job_completion(self.as_connection, job_id) + # time.sleep(5) + wait_for_job_completion(self.as_connection, job_id) validate_records(self.as_connection, keys, lambda rec: rec[test_bin] == "aerospike") @@ -149,10 +140,10 @@ def test_background_execute_exp_everywhere(self, clean_test_background): query = self.as_connection.query(TEST_NS, TEST_SET) # query.where(number_predicate) query.apply(TEST_UDF_MODULE, TEST_UDF_FUNCTION, [test_bin]) - query.execute_background(policy) + job_id = query.execute_background(policy) # Give time for the query to finish - time.sleep(5) - # wait_for_job_completion(self.as_connection, job_id) + # time.sleep(5) + wait_for_job_completion(self.as_connection, job_id) for key in keys: _, _, bins = self.as_connection.get(key) @@ -179,9 +170,10 @@ def test_background_execute_exp_and_predicate(self, clean_test_background): query = self.as_connection.query(TEST_NS, TEST_SET) query.where(number_predicate) query.apply(TEST_UDF_MODULE, TEST_UDF_FUNCTION, [test_bin]) - query.execute_background(policy) + job_id = query.execute_background(policy) # Give time for the query to finish - time.sleep(5) + # time.sleep(5) + wait_for_job_completion(self.as_connection, job_id) for key in keys: _, _, bins = self.as_connection.get(key) @@ -212,9 +204,9 @@ def test_background_execute_with_ops_and_exp(self, clean_test_background): policy = {"expressions": expr.compile()} query.add_ops(ops) - query.execute_background(policy=policy) + job_id = query.execute_background(policy=policy) # Give time for the query to finish - time.sleep(5) + wait_for_job_completion(self.as_connection, job_id) for key in keys: _, _, bins = self.as_connection.get(key) @@ -236,9 +228,10 @@ def test_background_execute_with_ops(self, clean_test_background): ops = [operations.write(test_bin, "new_val")] query.add_ops(ops) - query.execute_background() + job_id = query.execute_background() # Give time for the query to finish - time.sleep(5) + # time.sleep(5) + wait_for_job_completion(self.as_connection, job_id) validate_records(self.as_connection, keys, lambda rec: rec[test_bin] == "new_val") @@ -257,9 +250,10 @@ def test_background_execute_with_ops_and_preds(self, clean_test_background): query.add_ops(ops) query.where(number_predicate) - query.execute_background() + job_id = query.execute_background() # Give time for the query to finish - time.sleep(5) + # time.sleep(5) + wait_for_job_completion(self.as_connection, job_id) _, _, num_5_record = self.as_connection.get((TEST_NS, TEST_SET, 5)) assert num_5_record.get(test_bin) is None @@ -289,9 +283,9 @@ def test_background_execute_sindex_predicate(self, clean_test_background): query = self.as_connection.query(TEST_NS, TEST_SET) query.where(number_predicate) query.apply(TEST_UDF_MODULE, TEST_UDF_FUNCTION, [test_bin]) - query.execute_background() + job_id = query.execute_background() + wait_for_job_completion(self.as_connection, job_id) # Give time for the query to finish - time.sleep(5) keys = [(TEST_NS, TEST_SET, i) for i in range(500) if i != 5] validate_records(self.as_connection, keys, lambda rec: test_bin not in rec) _, _, num_5_record = self.as_connection.get((TEST_NS, TEST_SET, 5)) @@ -317,9 +311,10 @@ def test_background_execute_sindex_exp(self, clean_test_background): query = self.as_connection.query(TEST_NS, TEST_SET) query.apply(TEST_UDF_MODULE, TEST_UDF_FUNCTION, [test_bin]) - query.execute_background(policy=policy) + job_id = query.execute_background(policy=policy) + wait_for_job_completion(self.as_connection, job_id) # Give time for the query to finish - time.sleep(5) + # time.sleep(5) # Records with number > 10 should not have had the UDF applied validate_records(self.as_connection, keys[10:], lambda rec: test_bin not in rec) diff --git a/test/new_tests/test_query_get_partitions_status.py b/test/new_tests/test_query_get_partitions_status.py index f054cdac29..ac49b777e8 100644 --- a/test/new_tests/test_query_get_partitions_status.py +++ b/test/new_tests/test_query_get_partitions_status.py @@ -4,72 +4,73 @@ from .test_base_class import TestBaseClass -class TestQueryGetPartitionsStatus(TestBaseClass): - @pytest.fixture(autouse=True) - def setup(self, request, as_connection): - if self.server_version < [6, 0]: - pytest.mark.xfail(reason="Servers older than 6.0 do not support partition queries.") - pytest.xfail() - - self.test_ns = "test" - self.test_set = "demo" - - self.partition_1000_count = 0 - self.partition_1001_count = 0 - self.partition_1002_count = 0 - self.partition_1003_count = 0 - - as_connection.truncate(self.test_ns, None, 0) - +@pytest.fixture(autouse=True, scope="class") +def setup(request, as_connection): + if request.cls.server_version < [6, 0]: + pytest.mark.xfail(reason="Servers older than 6.0 do not support partition queries.") + pytest.xfail() + + request.cls.test_ns = "test" + request.cls.test_set = "demo" + + request.cls.partition_1000_count = 0 + request.cls.partition_1001_count = 0 + request.cls.partition_1002_count = 0 + request.cls.partition_1003_count = 0 + + as_connection.truncate(request.cls.test_ns, None, 0) + + for i in range(1, 100000): + put = 0 + key = (request.cls.test_ns, request.cls.test_set, str(i)) + rec_partition = as_connection.get_key_partition_id(request.cls.test_ns, request.cls.test_set, str(i)) + + if rec_partition == 1000: + request.cls.partition_1000_count += 1 + put = 1 + if rec_partition == 1001: + request.cls.partition_1001_count += 1 + put = 1 + if rec_partition == 1002: + request.cls.partition_1002_count += 1 + put = 1 + if rec_partition == 1003: + request.cls.partition_1003_count += 1 + put = 1 + if put: + rec = { + "i": i, + "s": "xyz", + "l": [2, 4, 8, 16, 32, None, 128, 256], + "m": {"partition": rec_partition, "b": 4, "c": 8, "d": 16}, + } + as_connection.put(key, rec) + + def teardown(): for i in range(1, 100000): put = 0 - key = (self.test_ns, self.test_set, str(i)) - rec_partition = as_connection.get_key_partition_id(self.test_ns, self.test_set, str(i)) + key = ("test", "demo", str(i)) + rec_partition = as_connection.get_key_partition_id(request.cls.test_ns, request.cls.test_set, str(i)) if rec_partition == 1000: - self.partition_1000_count += 1 + request.cls.partition_1000_count += 1 put = 1 if rec_partition == 1001: - self.partition_1001_count += 1 + request.cls.partition_1001_count += 1 put = 1 if rec_partition == 1002: - self.partition_1002_count += 1 + request.cls.partition_1002_count += 1 put = 1 if rec_partition == 1003: - self.partition_1003_count += 1 + request.cls.partition_1003_count += 1 put = 1 if put: - rec = { - "i": i, - "s": "xyz", - "l": [2, 4, 8, 16, 32, None, 128, 256], - "m": {"partition": rec_partition, "b": 4, "c": 8, "d": 16}, - } - as_connection.put(key, rec) - - def teardown(): - for i in range(1, 100000): - put = 0 - key = ("test", "demo", str(i)) - rec_partition = as_connection.get_key_partition_id(self.test_ns, self.test_set, str(i)) - - if rec_partition == 1000: - self.partition_1000_count += 1 - put = 1 - if rec_partition == 1001: - self.partition_1001_count += 1 - put = 1 - if rec_partition == 1002: - self.partition_1002_count += 1 - put = 1 - if rec_partition == 1003: - self.partition_1003_count += 1 - put = 1 - if put: - as_connection.remove(key) - - request.addfinalizer(teardown) + as_connection.remove(key) + + request.addfinalizer(teardown) + +class TestQueryGetPartitionsStatus(TestBaseClass): def test_query_get_partitions_status_no_tracking(self): query_obj = self.as_connection.query(self.test_ns, self.test_set) diff --git a/test/new_tests/test_query_pagination.py b/test/new_tests/test_query_pagination.py index 4eb9770075..85e9b29d9f 100644 --- a/test/new_tests/test_query_pagination.py +++ b/test/new_tests/test_query_pagination.py @@ -8,76 +8,76 @@ import math -class TestQueryPagination(TestBaseClass): - @pytest.fixture(autouse=True) - def setup(self, request, as_connection): - if self.server_version < [6, 0]: - pytest.mark.xfail(reason="Servers older than 6.0 do not support paginated queries.") - pytest.xfail() - self.test_ns = "test" - self.test_set = "demo" - - self.partition_1000_count = 0 - self.partition_1001_count = 0 - self.partition_1002_count = 0 - self.partition_1003_count = 0 - - as_connection.truncate(self.test_ns, None, 0) - +@pytest.fixture(autouse=True, scope="class") +def setup(request, as_connection): + if request.cls.server_version < [6, 0]: + pytest.mark.xfail(reason="Servers older than 6.0 do not support paginated queries.") + pytest.xfail() + request.cls.test_ns = "test" + request.cls.test_set = "demo" + + request.cls.partition_1000_count = 0 + request.cls.partition_1001_count = 0 + request.cls.partition_1002_count = 0 + request.cls.partition_1003_count = 0 + + as_connection.truncate(request.cls.test_ns, None, 0) + + for i in range(1, 100000): + put = 0 + rec_partition = as_connection.get_key_partition_id(request.cls.test_ns, request.cls.test_set, str(i)) + + if rec_partition == 1000: + request.cls.partition_1000_count += 1 + put = 1 + if rec_partition == 1001: + request.cls.partition_1001_count += 1 + put = 1 + if rec_partition == 1002: + request.cls.partition_1002_count += 1 + put = 1 + if rec_partition == 1003: + request.cls.partition_1003_count += 1 + put = 1 + if put: + rec = { + "i": i, + "s": "xyz", + "l": [2, 4, 8, 16, 32, None, 128, 256], + "m": {"partition": rec_partition, "b": 4, "c": 8, "d": 16}, + } + key = { + "ns": request.cls.test_ns, + "set": request.cls.test_set, + "key": str(i), + "digest": aerospike.calc_digest(request.cls.test_ns, request.cls.test_set, str(i)), + } + as_connection.put(key, rec) + + def teardown(): for i in range(1, 100000): put = 0 - rec_partition = as_connection.get_key_partition_id(self.test_ns, self.test_set, str(i)) + key = ("test", "demo", str(i)) + rec_partition = as_connection.get_key_partition_id(request.cls.test_ns, request.cls.test_set, str(i)) if rec_partition == 1000: - self.partition_1000_count += 1 + request.cls.partition_1000_count += 1 put = 1 if rec_partition == 1001: - self.partition_1001_count += 1 + request.cls.partition_1001_count += 1 put = 1 if rec_partition == 1002: - self.partition_1002_count += 1 + request.cls.partition_1002_count += 1 put = 1 if rec_partition == 1003: - self.partition_1003_count += 1 + request.cls.partition_1003_count += 1 put = 1 if put: - rec = { - "i": i, - "s": "xyz", - "l": [2, 4, 8, 16, 32, None, 128, 256], - "m": {"partition": rec_partition, "b": 4, "c": 8, "d": 16}, - } - key = { - "ns": self.test_ns, - "set": self.test_set, - "key": str(i), - "digest": aerospike.calc_digest(self.test_ns, self.test_set, str(i)), - } - as_connection.put(key, rec) - - def teardown(): - for i in range(1, 100000): - put = 0 - key = ("test", "demo", str(i)) - rec_partition = as_connection.get_key_partition_id(self.test_ns, self.test_set, str(i)) - - if rec_partition == 1000: - self.partition_1000_count += 1 - put = 1 - if rec_partition == 1001: - self.partition_1001_count += 1 - put = 1 - if rec_partition == 1002: - self.partition_1002_count += 1 - put = 1 - if rec_partition == 1003: - self.partition_1003_count += 1 - put = 1 - if put: - as_connection.remove(key) - - request.addfinalizer(teardown) + as_connection.remove(key) + + request.addfinalizer(teardown) +class TestQueryPagination(TestBaseClass): @pytest.mark.xfail(reason="Might fail, server may return less than what asked for.") def test_query_pagination_with_existent_ns_and_set(self): diff --git a/test/new_tests/test_query_partition.py b/test/new_tests/test_query_partition.py index 05a5ffbe9a..e5e3c851ab 100644 --- a/test/new_tests/test_query_partition.py +++ b/test/new_tests/test_query_partition.py @@ -32,82 +32,78 @@ def remove_sindex(client): pass -class TestQueryPartition(TestBaseClass): - def setup_class(cls): - # Register setup and teardown functions - cls.connection_setup_functions = [add_sindex] - cls.connection_teardown_functions = [remove_sindex] - - @pytest.fixture(autouse=True) - def setup(self, request, connection_with_config_funcs): - if self.server_version < [6, 0]: - pytest.mark.xfail(reason="Servers older than 6.0 do not support partition queries.") - pytest.xfail() - as_connection = connection_with_config_funcs - - self.test_ns = "test" - self.test_set = "demo" - - self.partition_1000_count = 0 - self.partition_1001_count = 0 - self.partition_1002_count = 0 - self.partition_1003_count = 0 - - as_connection.truncate(self.test_ns, None, 0) - - for i in range(1, 100000): - put = 0 - key = (self.test_ns, self.test_set, str(i)) - rec_partition = as_connection.get_key_partition_id(self.test_ns, self.test_set, str(i)) - - if rec_partition == 1000: - self.partition_1000_count += 1 - put = 1 - if rec_partition == 1001: - self.partition_1001_count += 1 - put = 1 - if rec_partition == 1002: - self.partition_1002_count += 1 - put = 1 - if rec_partition == 1003: - self.partition_1003_count += 1 - put = 1 - if put: - rec = { - "i": i, - "s": "xyz", - "l": [2, 4, 8, 16, 32, None, 128, 256], - "m": {"partition": rec_partition, "b": 4, "c": 8, "d": 16}, - } - as_connection.put(key, rec) - # print(f"{self.partition_1000_count} records are put in partition 1000, \ - # {self.partition_1001_count} records are put in partition 1001, \ - # {self.partition_1002_count} records are put in partition 1002, \ - # {self.partition_1003_count} records are put in partition 1003") - - def teardown(): - for i in range(1, 100000): - put = 0 - key = ("test", "demo", str(i)) - rec_partition = as_connection.get_key_partition_id(self.test_ns, self.test_set, str(i)) - - if rec_partition == 1000: - self.partition_1000_count += 1 - put = 1 - if rec_partition == 1001: - self.partition_1001_count += 1 - put = 1 - if rec_partition == 1002: - self.partition_1002_count += 1 - put = 1 - if rec_partition == 1003: - self.partition_1003_count += 1 - put = 1 - if put: - as_connection.remove(key) - - request.addfinalizer(teardown) +@pytest.fixture(autouse=True, scope="class") +def setup(request, as_connection): + if request.cls.server_version < [6, 0]: + pytest.mark.xfail(reason="Servers older than 6.0 do not support partition queries.") + pytest.xfail() + + add_sindex(as_connection) + request.cls.test_ns = "test" + request.cls.test_set = "demo" + + request.cls.partition_1000_count = 0 + request.cls.partition_1001_count = 0 + request.cls.partition_1002_count = 0 + request.cls.partition_1003_count = 0 + + as_connection.truncate(request.cls.test_ns, None, 0) + + for i in range(1, 100000): + put = 0 + key = (request.cls.test_ns, request.cls.test_set, str(i)) + rec_partition = as_connection.get_key_partition_id(request.cls.test_ns, request.cls.test_set, str(i)) + + if rec_partition == 1000: + request.cls.partition_1000_count += 1 + put = 1 + if rec_partition == 1001: + request.cls.partition_1001_count += 1 + put = 1 + if rec_partition == 1002: + request.cls.partition_1002_count += 1 + put = 1 + if rec_partition == 1003: + request.cls.partition_1003_count += 1 + put = 1 + if put: + rec = { + "i": i, + "s": "xyz", + "l": [2, 4, 8, 16, 32, None, 128, 256], + "m": {"partition": rec_partition, "b": 4, "c": 8, "d": 16}, + } + as_connection.put(key, rec) + # print(f"{request.cls.partition_1000_count} records are put in partition 1000, \ + # {request.cls.partition_1001_count} records are put in partition 1001, \ + # {request.cls.partition_1002_count} records are put in partition 1002, \ + # {request.cls.partition_1003_count} records are put in partition 1003") + + yield + + for i in range(1, 100000): + put = 0 + key = ("test", "demo", str(i)) + rec_partition = as_connection.get_key_partition_id(request.cls.test_ns, request.cls.test_set, str(i)) + + if rec_partition == 1000: + request.cls.partition_1000_count += 1 + put = 1 + if rec_partition == 1001: + request.cls.partition_1001_count += 1 + put = 1 + if rec_partition == 1002: + request.cls.partition_1002_count += 1 + put = 1 + if rec_partition == 1003: + request.cls.partition_1003_count += 1 + put = 1 + if put: + as_connection.remove(key) + + remove_sindex(as_connection) +class TestQueryPartition(TestBaseClass): def test_query_partition_with_existent_ns_and_set(self): records = [] @@ -232,6 +228,7 @@ def callback(part_id, input_tuple): assert len(records) == self.partition_1000_count + # TODO: this doesn't have timeout policy? def test_query_partition_with_timeout_policy(self): records = [] diff --git a/test/new_tests/test_scan.py b/test/new_tests/test_scan.py index e57383d1a8..8c3eb6661f 100644 --- a/test/new_tests/test_scan.py +++ b/test/new_tests/test_scan.py @@ -9,34 +9,35 @@ import aerospike -class TestScan(TestBaseClass): - @pytest.fixture(autouse=True) - def setup(self, request, as_connection): - self.test_ns = "test" - self.test_set = "demo" - +@pytest.fixture(autouse=True, scope="class") +def setup(request, as_connection): + request.cls.test_ns = "test" + request.cls.test_set = "demo" + + for i in range(19): + key = ("test", "demo", i) + rec = {"name": "name%s" % (str(i)), "age": i} + as_connection.put(key, rec) + + key = ("test", "demo", 122) + llist = [{"op": aerospike.OPERATOR_APPEND, "bin": bytearray("asd;adk\0kj", "utf-8"), "val": "john"}] + # Creates a record with the key 122, with one bytearray key. + request.cls.bytearray_bin = bytearray("asd;adk\0kj", "utf-8") + as_connection.operate(key, llist) + request.cls.record_count = 20 + + def teardown(): for i in range(19): key = ("test", "demo", i) - rec = {"name": "name%s" % (str(i)), "age": i} - as_connection.put(key, rec) + as_connection.remove(key) key = ("test", "demo", 122) - llist = [{"op": aerospike.OPERATOR_APPEND, "bin": bytearray("asd;adk\0kj", "utf-8"), "val": "john"}] - # Creates a record with the key 122, with one bytearray key. - self.bytearray_bin = bytearray("asd;adk\0kj", "utf-8") - as_connection.operate(key, llist) - self.record_count = 20 - - def teardown(): - for i in range(19): - key = ("test", "demo", i) - as_connection.remove(key) - - key = ("test", "demo", 122) - as_connection.remove(key) + as_connection.remove(key) + + request.addfinalizer(teardown) - request.addfinalizer(teardown) +class TestScan(TestBaseClass): def test_scan_with_existent_ns_and_set(self): records = [] diff --git a/test/new_tests/test_scan_apply.py b/test/new_tests/test_scan_apply.py index 015ad508c1..d649b3ea78 100644 --- a/test/new_tests/test_scan_apply.py +++ b/test/new_tests/test_scan_apply.py @@ -5,22 +5,11 @@ from aerospike_helpers import expressions as exp from .test_base_class import TestBaseClass from aerospike import exception as e +from .conftest import wait_for_job_completion import aerospike -def wait_for_job_completion(as_connection, job_id): - """ - Blocks until the job has completed - """ - time.sleep(0.1) - while True: - response = as_connection.job_info(job_id, aerospike.JOB_SCAN) - if response["status"] != aerospike.JOB_STATUS_INPROGRESS: - break - time.sleep(0.1) - - class TestScanApply(object): def setup_class(cls): cls.udf_to_load = "bin_lua.lua" @@ -53,7 +42,7 @@ def test_scan_apply_with_correct_parameters_with_set(self): """ scan_id = self.as_connection.scan_apply("test", "demo", "bin_lua", "mytransform", ["age", 2]) - wait_for_job_completion(self.as_connection, scan_id) + wait_for_job_completion(self.as_connection, scan_id, aerospike.JOB_SCAN) for i in range(5): key = ("test", "demo", i) @@ -70,7 +59,7 @@ def test_scan_apply_with_correct_policy(self): policy = {"socket_timeout": 180000} scan_id = self.as_connection.scan_apply("test", "demo", "bin_lua", "mytransform", ["age", 2], policy) - wait_for_job_completion(self.as_connection, scan_id) + wait_for_job_completion(self.as_connection, scan_id, aerospike.JOB_SCAN) for i in range(5): key = ("test", "demo", i) @@ -90,7 +79,7 @@ def test_scan_apply_with_correct_policy_and_expressions(self): policy = {"total_timeout": 180000, "expressions": expr.compile()} scan_id = self.as_connection.scan_apply("test", None, "bin_lua", "mytransform", ["age", 2], policy) - wait_for_job_completion(self.as_connection, scan_id) + wait_for_job_completion(self.as_connection, scan_id, aerospike.JOB_SCAN) for i in range(5): key = ("test", "demo", i) @@ -118,7 +107,7 @@ def test_scan_apply_with_none_set(self): policy = {} scan_id = self.as_connection.scan_apply("test", None, "bin_lua", "mytransform", ["age", 2], policy) - wait_for_job_completion(self.as_connection, scan_id) + wait_for_job_completion(self.as_connection, scan_id, aerospike.JOB_SCAN) for i in range(5): key = ("test", "demo", i) @@ -138,7 +127,7 @@ def test_scan_apply_with_none_set_and_expressions(self): policy = {"expressions": expr.compile()} scan_id = self.as_connection.scan_apply("test", None, "bin_lua", "mytransform", ["age", 2], policy) - wait_for_job_completion(self.as_connection, scan_id) + wait_for_job_completion(self.as_connection, scan_id, aerospike.JOB_SCAN) for i in range(5): key = ("test", "demo", i) @@ -157,7 +146,7 @@ def test_scan_apply_with_extra_call_to_lua(self): """ scan_id = self.as_connection.scan_apply("test", "demo", "bin_lua", "mytransform", ["age", 2, 3]) - wait_for_job_completion(self.as_connection, scan_id) + wait_for_job_completion(self.as_connection, scan_id, aerospike.JOB_SCAN) for i in range(5): key = ("test", "demo", i) @@ -170,7 +159,7 @@ def test_scan_apply_with_extra_parameter_in_lua(self): """ scan_id = self.as_connection.scan_apply("test", "demo", "bin_lua", "mytransformextra", ["age", 2]) - wait_for_job_completion(self.as_connection, scan_id) + wait_for_job_completion(self.as_connection, scan_id, aerospike.JOB_SCAN) for i in range(5): key = ("test", "demo", i) @@ -184,7 +173,7 @@ def test_scan_apply_with_less_parameter_in_lua(self): """ scan_id = self.as_connection.scan_apply("test", "demo", "bin_lua", "mytransformless", ["age", 2]) - wait_for_job_completion(self.as_connection, scan_id) + wait_for_job_completion(self.as_connection, scan_id, aerospike.JOB_SCAN) for i in range(5): key = ("test", "demo", i) @@ -198,7 +187,7 @@ def test_scan_apply_with_no_function_args(self): """ scan_id = self.as_connection.scan_apply("test", "demo", "bin_lua", "mytransformless") - wait_for_job_completion(self.as_connection, scan_id) + wait_for_job_completion(self.as_connection, scan_id, aerospike.JOB_SCAN) for i in range(5): key = ("test", "demo", i) @@ -213,7 +202,7 @@ def test_scan_apply_with_options_positive(self): options = {"concurrent": False} scan_id = self.as_connection.scan_apply("test", "demo", "bin_lua", "mytransform", ["age", 2], policy, options) - wait_for_job_completion(self.as_connection, scan_id) + wait_for_job_completion(self.as_connection, scan_id, aerospike.JOB_SCAN) for i in range(5): key = ("test", "demo", i) @@ -226,7 +215,7 @@ def test_scan_apply_unicode_input(self): """ scan_id = self.as_connection.scan_apply("test", "demo", "bin_lua", "mytransform", ["age", 2]) - wait_for_job_completion(self.as_connection, scan_id) + wait_for_job_completion(self.as_connection, scan_id, aerospike.JOB_SCAN) for i in range(5): key = ("test", "demo", i) @@ -239,7 +228,7 @@ def test_scan_apply_with_incorrect_module_name(self): """ scan_id = self.as_connection.scan_apply("test", "demo", "bin_lua_incorrect", "mytransform", ["age", 2]) - wait_for_job_completion(self.as_connection, scan_id) + wait_for_job_completion(self.as_connection, scan_id, aerospike.JOB_SCAN) for i in range(5): key = ("test", "demo", i) @@ -252,7 +241,7 @@ def test_scan_apply_with_incorrect_function_name(self): """ scan_id = self.as_connection.scan_apply("test", "demo", "bin_lua", "mytransform_incorrect", ["age", 2]) - wait_for_job_completion(self.as_connection, scan_id) + wait_for_job_completion(self.as_connection, scan_id, aerospike.JOB_SCAN) for i in range(5): key = ("test", "demo", i) @@ -265,7 +254,7 @@ def test_scan_apply_with_argument_is_none(self): """ scan_id = self.as_connection.scan_apply("test", "demo", "bin_lua", "mytransform", None) - wait_for_job_completion(self.as_connection, scan_id) + wait_for_job_completion(self.as_connection, scan_id, aerospike.JOB_SCAN) # The function application should have failed and not changed # any bin values diff --git a/test/new_tests/test_scan_execute_background.py b/test/new_tests/test_scan_execute_background.py index 06da2635a3..c2497ee1be 100644 --- a/test/new_tests/test_scan_execute_background.py +++ b/test/new_tests/test_scan_execute_background.py @@ -6,6 +6,7 @@ from aerospike import exception, predicates from aerospike_helpers.operations import operations, map_operations from aerospike_helpers import expressions as exp +from .conftest import wait_for_job_completion TEST_NS = "test" TEST_SET = "background_scan1" @@ -20,18 +21,6 @@ long = int -def wait_for_job_completion(as_connection, job_id): - """ - Blocks until the job has completed - """ - time.sleep(0.1) - while True: - response = as_connection.job_info(job_id, aerospike.JOB_SCAN) - if response["status"] != aerospike.JOB_STATUS_INPROGRESS: - break - time.sleep(0.1) - - def add_test_udf(client): policy = {} client.udf_put("bin_lua.lua", 0, policy) @@ -92,7 +81,7 @@ def test_background_execute_no_predicate(self): scan = self.as_connection.scan(TEST_NS, TEST_SET) scan.apply(TEST_UDF_MODULE, TEST_UDF_FUNCTION, [test_bin, 1]) job_id = scan.execute_background() - wait_for_job_completion(self.as_connection, job_id) + wait_for_job_completion(self.as_connection, job_id, aerospike.JOB_SCAN) for i, key in enumerate(keys): _, _, bins = self.as_connection.get(key) @@ -112,7 +101,7 @@ def test_background_execute_expressions_everywhere(self): scan = self.as_connection.scan(TEST_NS, TEST_SET) scan.apply(TEST_UDF_MODULE, TEST_UDF_FUNCTION, [test_bin, 1]) job_id = scan.execute_background(policy) - wait_for_job_completion(self.as_connection, job_id) + wait_for_job_completion(self.as_connection, job_id, aerospike.JOB_SCAN) for i, key in enumerate(keys): _, _, bins = self.as_connection.get(key) @@ -140,7 +129,7 @@ def test_background_execute_expressions_and_predicate(self): scan.where(number_predicate) scan.apply(TEST_UDF_MODULE, TEST_UDF_FUNCTION, [test_bin]) job_id = scan.execute_background(policy) - wait_for_job_completion(self.as_connection, job_id) + wait_for_job_completion(self.as_connection, job_id, aerospike.JOB_SCAN) for key in keys: _, _, bins = self.as_connection.get(key) @@ -167,7 +156,7 @@ def test_background_execute_with_ops_and_expressions(self): scan.add_ops(ops) job_id = scan.execute_background(policy) - wait_for_job_completion(self.as_connection, job_id) + wait_for_job_completion(self.as_connection, job_id, aerospike.JOB_SCAN) for key in keys: _, _, bins = self.as_connection.get(key) @@ -194,7 +183,7 @@ def test_background_execute_with_ops_and_expressions_None_set(self): scan.add_ops(ops) job_id = scan.execute_background(policy) - wait_for_job_completion(self.as_connection, job_id) + wait_for_job_completion(self.as_connection, job_id, aerospike.JOB_SCAN) for key in keys: _, _, bins = self.as_connection.get(key) @@ -224,7 +213,7 @@ def test_background_execute_with_ops(self): scan.add_ops(ops) job_id = scan.execute_background() - wait_for_job_completion(self.as_connection, job_id) + wait_for_job_completion(self.as_connection, job_id, aerospike.JOB_SCAN) validate_records(self.as_connection, keys, lambda rec: rec[test_bin] == "new_val") @@ -241,7 +230,7 @@ def test_background_execute_with_map_ops(self): scan.add_ops(ops) job_id = scan.execute_background() - wait_for_job_completion(self.as_connection, job_id) + wait_for_job_completion(self.as_connection, job_id, aerospike.JOB_SCAN) brs = self.as_connection.batch_read(keys) # Sort batch records by user key @@ -269,7 +258,7 @@ def test_background_execute_with_ops_and_preds(self): scan.add_ops(ops) scan.where(number_predicate) job_id = scan.execute_background() - wait_for_job_completion(self.as_connection, job_id) + wait_for_job_completion(self.as_connection, job_id, aerospike.JOB_SCAN) _, _, num_5_record = self.as_connection.get((TEST_NS, TEST_SET, 5)) assert num_5_record[test_bin] == "aerospike" @@ -282,7 +271,7 @@ def test_background_execute_with_ops_and_preds(self): scan.add_ops(ops) job_id = scan.execute_background() - wait_for_job_completion(self.as_connection, job_id) + wait_for_job_completion(self.as_connection, job_id, aerospike.JOB_SCAN) validate_records(self.as_connection, keys, lambda rec: rec[test_bin] == "aerospike") @@ -300,7 +289,7 @@ def test_background_execute_sindex_predicate(self): scan.where(number_predicate) scan.apply(TEST_UDF_MODULE, TEST_UDF_FUNCTION, [test_bin]) job_id = scan.execute_background() - wait_for_job_completion(self.as_connection, job_id) + wait_for_job_completion(self.as_connection, job_id, aerospike.JOB_SCAN) keys = [(TEST_NS, TEST_SET, i) for i in range(50) if i != 5] validate_records(self.as_connection, keys, lambda rec: test_bin not in rec) diff --git a/test/new_tests/test_scan_get_partitions_status.py b/test/new_tests/test_scan_get_partitions_status.py index 52d6589140..86bd725570 100644 --- a/test/new_tests/test_scan_get_partitions_status.py +++ b/test/new_tests/test_scan_get_partitions_status.py @@ -4,68 +4,69 @@ from .test_base_class import TestBaseClass -class TestScanGetPartitionsStatus(TestBaseClass): - @pytest.fixture(autouse=True) - def setup(self, request, as_connection): - self.test_ns = "test" - self.test_set = "demo" - - self.partition_1000_count = 0 - self.partition_1001_count = 0 - self.partition_1002_count = 0 - self.partition_1003_count = 0 - - as_connection.truncate(self.test_ns, None, 0) - +@pytest.fixture(autouse=True, scope="class") +def setup(request, as_connection): + request.cls.test_ns = "test" + request.cls.test_set = "demo" + + request.cls.partition_1000_count = 0 + request.cls.partition_1001_count = 0 + request.cls.partition_1002_count = 0 + request.cls.partition_1003_count = 0 + + as_connection.truncate(request.cls.test_ns, None, 0) + + for i in range(1, 100000): + put = 0 + key = (request.cls.test_ns, request.cls.test_set, str(i)) + rec_partition = as_connection.get_key_partition_id(request.cls.test_ns, request.cls.test_set, str(i)) + + if rec_partition == 1000: + request.cls.partition_1000_count += 1 + put = 1 + if rec_partition == 1001: + request.cls.partition_1001_count += 1 + put = 1 + if rec_partition == 1002: + request.cls.partition_1002_count += 1 + put = 1 + if rec_partition == 1003: + request.cls.partition_1003_count += 1 + put = 1 + if put: + rec = { + "i": i, + "s": "xyz", + "l": [2, 4, 8, 16, 32, None, 128, 256], + "m": {"partition": rec_partition, "b": 4, "c": 8, "d": 16}, + } + as_connection.put(key, rec) + + def teardown(): for i in range(1, 100000): put = 0 - key = (self.test_ns, self.test_set, str(i)) - rec_partition = as_connection.get_key_partition_id(self.test_ns, self.test_set, str(i)) + key = ("test", "demo", str(i)) + rec_partition = as_connection.get_key_partition_id(request.cls.test_ns, request.cls.test_set, str(i)) if rec_partition == 1000: - self.partition_1000_count += 1 + request.cls.partition_1000_count += 1 put = 1 if rec_partition == 1001: - self.partition_1001_count += 1 + request.cls.partition_1001_count += 1 put = 1 if rec_partition == 1002: - self.partition_1002_count += 1 + request.cls.partition_1002_count += 1 put = 1 if rec_partition == 1003: - self.partition_1003_count += 1 + request.cls.partition_1003_count += 1 put = 1 if put: - rec = { - "i": i, - "s": "xyz", - "l": [2, 4, 8, 16, 32, None, 128, 256], - "m": {"partition": rec_partition, "b": 4, "c": 8, "d": 16}, - } - as_connection.put(key, rec) - - def teardown(): - for i in range(1, 100000): - put = 0 - key = ("test", "demo", str(i)) - rec_partition = as_connection.get_key_partition_id(self.test_ns, self.test_set, str(i)) - - if rec_partition == 1000: - self.partition_1000_count += 1 - put = 1 - if rec_partition == 1001: - self.partition_1001_count += 1 - put = 1 - if rec_partition == 1002: - self.partition_1002_count += 1 - put = 1 - if rec_partition == 1003: - self.partition_1003_count += 1 - put = 1 - if put: - as_connection.remove(key) - - request.addfinalizer(teardown) + as_connection.remove(key) + request.addfinalizer(teardown) + + +class TestScanGetPartitionsStatus(TestBaseClass): def test_scan_get_partitions_status_no_tracking(self): scan_obj = self.as_connection.scan(self.test_ns, self.test_set) diff --git a/test/new_tests/test_scan_pagination.py b/test/new_tests/test_scan_pagination.py index bf3c505108..1a4ed87d51 100644 --- a/test/new_tests/test_scan_pagination.py +++ b/test/new_tests/test_scan_pagination.py @@ -7,77 +7,78 @@ from .as_status_codes import AerospikeStatus -class TestScanPagination(TestBaseClass): - @pytest.fixture(autouse=True) - def setup(self, request, as_connection): - self.test_ns = "test" - self.test_set = "demo" - - self.partition_1000_count = 0 - self.partition_1001_count = 0 - self.partition_1002_count = 0 - self.partition_1003_count = 0 - - as_connection.truncate(self.test_ns, None, 0) - +@pytest.fixture(autouse=True, scope="class") +def setup(request, as_connection): + request.cls.test_ns = "test" + request.cls.test_set = "demo" + + request.cls.partition_1000_count = 0 + request.cls.partition_1001_count = 0 + request.cls.partition_1002_count = 0 + request.cls.partition_1003_count = 0 + + as_connection.truncate(request.cls.test_ns, None, 0) + + for i in range(1, 100000): + put = 0 + rec_partition = as_connection.get_key_partition_id(request.cls.test_ns, request.cls.test_set, str(i)) + + if rec_partition == 1000: + request.cls.partition_1000_count += 1 + put = 1 + if rec_partition == 1001: + request.cls.partition_1001_count += 1 + put = 1 + if rec_partition == 1002: + request.cls.partition_1002_count += 1 + put = 1 + if rec_partition == 1003: + request.cls.partition_1003_count += 1 + put = 1 + if put: + rec = { + "i": i, + "s": "xyz", + "l": [2, 4, 8, 16, 32, None, 128, 256], + "m": {"partition": rec_partition, "b": 4, "c": 8, "d": 16}, + } + key = { + "ns": request.cls.test_ns, + "set": request.cls.test_set, + "key": str(i), + "digest": aerospike.calc_digest(request.cls.test_ns, request.cls.test_set, str(i)), + } + as_connection.put(key, rec) + # print(f"{request.cls.partition_1000_count} records are put in partition 1000, \ + # {request.cls.partition_1001_count} records are put in partition 1001, \ + # {request.cls.partition_1002_count} records are put in partition 1002, \ + # {request.cls.partition_1003_count} records are put in partition 1003") + + def teardown(): for i in range(1, 100000): put = 0 - rec_partition = as_connection.get_key_partition_id(self.test_ns, self.test_set, str(i)) + key = ("test", "demo", str(i)) + rec_partition = as_connection.get_key_partition_id(request.cls.test_ns, request.cls.test_set, str(i)) if rec_partition == 1000: - self.partition_1000_count += 1 + request.cls.partition_1000_count += 1 put = 1 if rec_partition == 1001: - self.partition_1001_count += 1 + request.cls.partition_1001_count += 1 put = 1 if rec_partition == 1002: - self.partition_1002_count += 1 + request.cls.partition_1002_count += 1 put = 1 if rec_partition == 1003: - self.partition_1003_count += 1 + request.cls.partition_1003_count += 1 put = 1 if put: - rec = { - "i": i, - "s": "xyz", - "l": [2, 4, 8, 16, 32, None, 128, 256], - "m": {"partition": rec_partition, "b": 4, "c": 8, "d": 16}, - } - key = { - "ns": self.test_ns, - "set": self.test_set, - "key": str(i), - "digest": aerospike.calc_digest(self.test_ns, self.test_set, str(i)), - } - as_connection.put(key, rec) - # print(f"{self.partition_1000_count} records are put in partition 1000, \ - # {self.partition_1001_count} records are put in partition 1001, \ - # {self.partition_1002_count} records are put in partition 1002, \ - # {self.partition_1003_count} records are put in partition 1003") - - def teardown(): - for i in range(1, 100000): - put = 0 - key = ("test", "demo", str(i)) - rec_partition = as_connection.get_key_partition_id(self.test_ns, self.test_set, str(i)) - - if rec_partition == 1000: - self.partition_1000_count += 1 - put = 1 - if rec_partition == 1001: - self.partition_1001_count += 1 - put = 1 - if rec_partition == 1002: - self.partition_1002_count += 1 - put = 1 - if rec_partition == 1003: - self.partition_1003_count += 1 - put = 1 - if put: - as_connection.remove(key) - - request.addfinalizer(teardown) + as_connection.remove(key) + request.addfinalizer(teardown) + + +class TestScanPagination(TestBaseClass): @pytest.mark.xfail(reason="Might fail, server may return less than what asked for.") def test_scan_pagination_with_existent_ns_and_set(self): diff --git a/test/new_tests/test_scan_partition.py b/test/new_tests/test_scan_partition.py index 7381d7785e..bb26381642 100644 --- a/test/new_tests/test_scan_partition.py +++ b/test/new_tests/test_scan_partition.py @@ -6,72 +6,73 @@ from .as_status_codes import AerospikeStatus -class TestScanPartition(TestBaseClass): - @pytest.fixture(autouse=True) - def setup(self, request, as_connection): - self.test_ns = "test" - self.test_set = "demo" - - self.partition_1000_count = 0 - self.partition_1001_count = 0 - self.partition_1002_count = 0 - self.partition_1003_count = 0 - - as_connection.truncate(self.test_ns, None, 0) - +@pytest.fixture(autouse=True, scope="class") +def setup(request, as_connection): + request.cls.test_ns = "test" + request.cls.test_set = "demo" + + request.cls.partition_1000_count = 0 + request.cls.partition_1001_count = 0 + request.cls.partition_1002_count = 0 + request.cls.partition_1003_count = 0 + + as_connection.truncate(request.cls.test_ns, None, 0) + + for i in range(1, 100000): + put = 0 + key = (request.cls.test_ns, request.cls.test_set, str(i)) + rec_partition = as_connection.get_key_partition_id(request.cls.test_ns, request.cls.test_set, str(i)) + + if rec_partition == 1000: + request.cls.partition_1000_count += 1 + put = 1 + if rec_partition == 1001: + request.cls.partition_1001_count += 1 + put = 1 + if rec_partition == 1002: + request.cls.partition_1002_count += 1 + put = 1 + if rec_partition == 1003: + request.cls.partition_1003_count += 1 + put = 1 + if put: + rec = { + "i": i, + "s": "xyz", + "l": [2, 4, 8, 16, 32, None, 128, 256], + "m": {"partition": rec_partition, "b": 4, "c": 8, "d": 16}, + } + as_connection.put(key, rec) + # print(f"{request.cls.partition_1000_count} records are put in partition 1000, \ + # {request.cls.partition_1001_count} records are put in partition 1001, \ + # {request.cls.partition_1002_count} records are put in partition 1002, \ + # {request.cls.partition_1003_count} records are put in partition 1003") + + def teardown(): for i in range(1, 100000): put = 0 - key = (self.test_ns, self.test_set, str(i)) - rec_partition = as_connection.get_key_partition_id(self.test_ns, self.test_set, str(i)) + key = ("test", "demo", str(i)) + rec_partition = as_connection.get_key_partition_id(request.cls.test_ns, request.cls.test_set, str(i)) if rec_partition == 1000: - self.partition_1000_count += 1 + request.cls.partition_1000_count += 1 put = 1 if rec_partition == 1001: - self.partition_1001_count += 1 + request.cls.partition_1001_count += 1 put = 1 if rec_partition == 1002: - self.partition_1002_count += 1 + request.cls.partition_1002_count += 1 put = 1 if rec_partition == 1003: - self.partition_1003_count += 1 + request.cls.partition_1003_count += 1 put = 1 if put: - rec = { - "i": i, - "s": "xyz", - "l": [2, 4, 8, 16, 32, None, 128, 256], - "m": {"partition": rec_partition, "b": 4, "c": 8, "d": 16}, - } - as_connection.put(key, rec) - # print(f"{self.partition_1000_count} records are put in partition 1000, \ - # {self.partition_1001_count} records are put in partition 1001, \ - # {self.partition_1002_count} records are put in partition 1002, \ - # {self.partition_1003_count} records are put in partition 1003") - - def teardown(): - for i in range(1, 100000): - put = 0 - key = ("test", "demo", str(i)) - rec_partition = as_connection.get_key_partition_id(self.test_ns, self.test_set, str(i)) - - if rec_partition == 1000: - self.partition_1000_count += 1 - put = 1 - if rec_partition == 1001: - self.partition_1001_count += 1 - put = 1 - if rec_partition == 1002: - self.partition_1002_count += 1 - put = 1 - if rec_partition == 1003: - self.partition_1003_count += 1 - put = 1 - if put: - as_connection.remove(key) - - request.addfinalizer(teardown) + as_connection.remove(key) + request.addfinalizer(teardown) + + +class TestScanPartition(TestBaseClass): def test_scan_partition_with_existent_ns_and_set(self): records = []