def test_put_with_mixeddata_client_serializer_deserializer_with_spec_in_put(self): # Invoke put() for mixed data with class and instance serialziers # with a specification in put. Client one is called hostlist, user, password = TestBaseClass.get_hosts() method_config = {'hosts': hostlist, 'serialization': (client_serialize_function, client_deserialize_function)} if user == None and password == None: client = aerospike.client(method_config).connect() else: client = aerospike.client(method_config).connect(user, password) response = aerospike.set_serializer(serialize_function) response = aerospike.set_deserializer(deserialize_function) key = ('test', 'demo', 1) rec = { 'map': {"key": "asd';q;'1';", "pi": 3}, 'normal': 1234, 'special': '!@#@#$QSDAsd;as', 'list': ["nanslkdl", 1, bytearray("asd;as[d'as;d", "utf-8")], 'bytes': bytearray("asd;as[d'as;d", "utf-8"), 'nestedlist': ["nanslkdl", 1, bytearray("asd;as[d'as;d", "utf-8"), [1, bytearray("asd;as[d'as;d", "utf-8")]], 'nestedmap': { "key": "asd';q;'1';", "pi": 314, "nest": {"pi1": 312, "t": 1} }, } res = client.put(key, rec, {}, {}, aerospike.SERIALIZER_USER) assert res == 0 _, _, bins = client.get(key) assert bins == { 'map': {"key": "asd';q;'1';", "pi": 3}, 'normal': 1234, 'special': '!@#@#$QSDAsd;as', 'list': ["nanslkdl", 1, bytearray("asd;as[d'as;d", "utf-8")], 'bytes': bytearray("asd;as[d'as;d", "utf-8"), 'nestedlist': ["nanslkdl", 1, bytearray("asd;as[d'as;d", "utf-8"), [1, bytearray("asd;as[d'as;d", "utf-8")]], 'nestedmap': { "key": "asd';q;'1';", "pi": 314, "nest": {"pi1": 312, "t": 1} }, } client.close() self.delete_keys.append(key)
def setup_class(cls): print "setup class invoked..." hostlist, user, password = TestBaseClass.get_hosts() config = { 'hosts': hostlist } if user == None and password == None: TestLList.client = aerospike.client(config).connect() else: TestLList.client = aerospike.client(config).connect(user, password) TestLList.key1 = ('test', 'demo', 'integer_llist_ky') TestLList.llist_integer = TestLList.client.llist(TestLList.key1, 'integer_bin') TestLList.key2 = ('test', 'demo', 'string_llist_ky') TestLList.llist_string = TestLList.client.llist(TestLList.key2, 'string_bin') TestLList.key3 = ('test', 'demo', 'float_llist_ky') TestLList.llist_float = TestLList.client.llist(TestLList.key3, 'float_bin')
def setup_class(cls): hostlist, user, password = TestBaseClass.get_hosts() config = {'hosts': hostlist} if user == None and password == None: client = aerospike.client(config).connect() else: client = aerospike.client(config).connect(user, password) client.index_integer_create('test', 'demo', 'test_age', 'age_index') client.index_string_create('test', 'demo', 'addr', 'addr_index') client.index_integer_create('test', 'demo', 'age1', 'age_index1') client.index_list_create('test', 'demo', 'numeric_list', aerospike.INDEX_NUMERIC, 'numeric_list_index') client.index_list_create('test', 'demo', 'string_list', aerospike.INDEX_STRING, 'string_list_index') client.index_map_keys_create('test', 'demo', 'numeric_map', aerospike.INDEX_NUMERIC, 'numeric_map_index') client.index_map_keys_create('test', 'demo', 'string_map', aerospike.INDEX_STRING, 'string_map_index') client.index_map_values_create('test', 'demo', 'numeric_map', aerospike.INDEX_NUMERIC, 'numeric_map_values_index') client.index_map_values_create('test', 'demo', 'string_map', aerospike.INDEX_STRING, 'string_map_values_index') client.index_integer_create('test', None, 'test_age_none', 'age_index_none')
def test_with_class_serializer_and_instance_serializer(self): """ Invoke put() for mixed data record with class and instance serializer. """ aerospike.set_serializer((lambda v: json.dumps(v))) aerospike.set_deserializer((lambda v: json.loads(v))) hostlist, user, password = TestBaseClass.get_hosts() method_config = { 'hosts': hostlist, 'serialization': ((lambda v: marshal.dumps(v)), (lambda v: marshal.loads(v)))} if user == None and password == None: client = aerospike.client(method_config).connect() else: client = aerospike.client(method_config).connect(user, password) key = ('test', 'demo', 16) try: TestPythonSerializer.client.remove(key) except: pass rec = {'normal': 1234, 'tuple': (1,2,3)} res = client.put(key, rec, serializer=aerospike.SERIALIZER_USER) assert res == 0 _, _, bins = client.get(key) # tuples JSON-encode to a list, and we use this fact to check which # serializer ran: assert bins == {'normal': 1234, 'tuple': (1,2,3)} client.remove(key)
def setup_class(cls): hostlist, user, password = TestBaseClass.get_hosts() config = {'hosts': hostlist} if user == None and password == None: TestApply.client = aerospike.client(config).connect() else: TestApply.client = aerospike.client(config).connect(user, password) TestApply.skip_old_server = True versioninfo = TestApply.client.info('version') for keys in versioninfo: for value in versioninfo[keys]: if value != None: versionlist = value[value.find("build") + 6:value.find("\n")].split(".") if int(versionlist[0]) >= 3 and int(versionlist[1]) >= 6: TestApply.skip_old_server = False policy = {} TestApply.client.index_integer_create('test', 'demo', 'age', 'age_index', policy) policy = {} TestApply.client.index_integer_create('test', 'demo', 'age1', 'age_index1', policy) policy = {} filename = "sample.lua" udf_type = 0 status = TestApply.client.udf_put(filename, udf_type, policy) filename = "test_record_udf.lua" status = TestApply.client.udf_put(filename, udf_type, policy)
def test_with_class_serializer_and_instance_serializer_with_unset_serializer(self): """ Invoke put() for mixed data record with python serializer. """ aerospike.set_serializer((lambda v: json.dumps(v))) aerospike.set_deserializer((lambda v: json.loads(v))) hostlist, user, password = TestBaseClass.get_hosts() method_config = { 'hosts': hostlist } if user == None and password == None: client = aerospike.client(method_config).connect() else: client = aerospike.client(method_config).connect(user, password) key = ('test', 'demo', 16) try: TestPythonSerializer.client.remove(key) except: pass rec = {'normal': 1234, 'tuple': (1,2,3)} aerospike.unset_serializers() try: res = client.put(key, rec, serializer=aerospike.SERIALIZER_USER) except ClientError as exception: assert exception.code == -1 assert exception.msg == "No serializer callback registered"
def setup_class(cls): """ Setup method. """ hostlist, user, password = TestBaseClass.get_hosts() config = {'hosts': hostlist} if user == None and password == None: TestOperate.client = aerospike.client(config).connect() else: TestOperate.client = aerospike.client(config).connect(user, password) config_no_typechecks = {'hosts': hostlist, 'strict_types': False} if user == None and password == None: TestOperate.client_no_typechecks = aerospike.client(config_no_typechecks).connect() else: TestOperate.client_no_typechecks = aerospike.client(config_no_typechecks).connect(user, password) TestOperate.skip_old_server = True versioninfo = TestOperate.client.info('version') for keys in versioninfo: for value in versioninfo[keys]: if value != None: versionlist = value[value.find("build") + 6:value.find("\n")].split(".") if int(versionlist[0]) >= 3 and int(versionlist[1]) >= 6: TestOperate.skip_old_server = False
def test_instance_serializer_and_no_class_serializer(self): # Invoke put() for record with no class serializer. There is an # instance serializer hostlist, user, password = TestBaseClass.get_hosts() method_config = { 'hosts': hostlist, 'serialization': ((lambda v: json.dumps(v)), (lambda v: json.loads(v)))} if user == None and password == None: client = aerospike.client(method_config).connect() else: client = aerospike.client(method_config).connect(user, password) key = ('test', 'demo', 11) try: client.remove(key) except: pass rec = {'normal': 1234, 'tuple': (1,2,3)} res = client.put(key, rec, serializer=aerospike.SERIALIZER_USER) assert res == 0 _, _, bins = client.get(key) # tuples JSON-encode to a list, and we use this fact to check which # serializer ran: assert bins == {'normal': 1234, 'tuple': [1,2,3]} client.remove(key) client.close()
def test_with_unset_serializer_python_serializer(self): """ Invoke put() for mixed data record with python serializer and calling unset_serializers """ aerospike.set_serializer((lambda v: json.dumps(v))) aerospike.set_deserializer((lambda v: json.loads(v))) hostlist, user, password = TestBaseClass.get_hosts() method_config = { 'hosts': hostlist } if user == None and password == None: client = aerospike.client(method_config).connect() else: client = aerospike.client(method_config).connect(user, password) key = ('test', 'demo', 16) try: client.remove(key) except: pass rec = {'normal': 1234, 'tuple': (1,2,3)} aerospike.unset_serializers() res = client.put(key, rec, serializer=aerospike.SERIALIZER_PYTHON) assert res == 0 _, _, bins = client.get(key) # tuples JSON-encode to a list, and we use this fact to check which # serializer ran: assert bins == {'normal': 1234, 'tuple': (1,2,3)} client.remove(key)
def test_builtin_with_instance_serializer_and_no_class_serializer(self): # Invoke put() for data record with builtin serializer and an # instance serializer set hostlist, user, password = TestBaseClass.get_hosts() method_config = { 'hosts': hostlist, 'serialization': ((lambda v: json.dumps(v)), (lambda v: json.loads(v)))} if user == None and password == None: client = aerospike.client(method_config).connect() else: client = aerospike.client(method_config).connect(user, password) key = ('test', 'demo', 12) try: client.remove(key) except: pass rec = {'normal': 1234, 'tuple': (1,2,3)} res = client.put(key, rec, serializer=aerospike.SERIALIZER_PYTHON) assert res == 0 _, _, bins = client.get(key) assert bins == {'normal': 1234, 'tuple': (1,2,3)} client.remove(key) client.close()
def setup_class(cls): hostlist, user, password = TestBaseClass.get_hosts() config = { 'hosts': hostlist, 'lua':{'user_path': '/tmp/', 'system_path':'../aerospike-client-c/lua/'}} if user == None and password == None: client = aerospike.client(config).connect() else: client = aerospike.client(config).connect(user, password) TestAggregate.skip_old_server = True versioninfo = client.info('version') for keys in versioninfo: for value in versioninfo[keys]: if value != None: versionlist = value[value.find("build") + 6:value.find("\n")].split(".") if int(versionlist[0]) >= 3 and int(versionlist[1]) >= 6: TestAggregate.skip_old_server = False client.index_integer_create('test', 'demo', 'test_age', 'test_demo_test_age_idx') client.index_integer_create('test', 'demo', 'age1', 'test_demo_age1_idx') time.sleep(2) filename = "stream_example.lua" udf_type = aerospike.UDF_TYPE_LUA status = client.udf_put(filename, udf_type) shutil.copyfile(filename, config['lua']['user_path'] + 'stream_example.lua') client.close()
def setup_method(self, method): """ Setup method. """ hostlist, user, password = TestBaseClass.get_hosts() config = { 'hosts': hostlist, 'lua':{'user_path': '/tmp/', 'system_path':'../aerospike-client-c/lua/'}} if TestBaseClass.user == None and TestBaseClass.password == None: self.client = aerospike.client(config).connect() else: self.client = aerospike.client(config).connect( TestBaseClass.user, TestBaseClass.password) for i in xrange(5): key = ('test', 'demo', i) rec = { 'name': 'name%s' % (str(i)), 'addr': 'name%s' % (str(i)), 'test_age': i, 'no': i } self.client.put(key, rec)
def setup_method(self, method): """ Setup method. """ hostlist, user, password = TestBaseClass.get_hosts() config = {'hosts': hostlist} if user == None and password == None: self.client = aerospike.client(config).connect() else: self.client = aerospike.client(config).connect(user, password) for i in xrange(19): key = ('test', u'demo', i) rec = {'name': 'name%s' % (str(i)), 'age': i} self.client.put(key, rec) key = ('test', u'demo', 122) list = [{"op": aerospike.OPERATOR_APPEND, "bin": bytearray("asd;adk\0kj", "utf-8"), "val": u"john"}]; self.client.operate(key, list) key = ('test', u'demo', 'ldt_key') self.llist_bin = self.client.llist(key, 'llist_key') self.llist_bin.add(10)
def setup_class(cls): """ Setup method. """ hostlist, user, password = TestBaseClass.get_hosts() config = {'hosts': hostlist} if user == None and password == None: TestExists.client = aerospike.client(config).connect() else: TestExists.client = aerospike.client(config).connect(user, password)
def setup_class(cls): """ Setup method. """ TestGetMany.hostlist, TestGetMany.user, TestGetMany.password = TestBaseClass.get_hosts() config = {"hosts": TestGetMany.hostlist} if TestGetMany.user == None and TestGetMany.password == None: TestGetMany.client = aerospike.client(config).connect() else: TestGetMany.client = aerospike.client(config).connect(TestGetMany.user, TestGetMany.password)
def setup_class(cls): """ Setup class """ hostlist, user, password = TestBaseClass.get_hosts() config = {"hosts": hostlist} if user == None and password == None: TestPut.client = aerospike.client(config).connect() else: TestPut.client = aerospike.client(config).connect(user, password)
def setup_class(cls): """ Setup method. """ hostlist, user, password = TestBaseClass.get_hosts() config = {'hosts': hostlist} if user == None and password == None: TestGeospatial.client = aerospike.client(config).connect() else: TestGeospatial.client = aerospike.client(config).connect(user, password) TestGeospatial.client.index_geo2dsphere_create("test", "demo", "loc", "loc_index")
def test_isconnected_positive(self): """ Invoke is_connected() positive. """ hostlist, user, password = TestBaseClass.get_hosts() config = {'hosts': hostlist} if user == None and password == None: self.client = aerospike.client(config).connect() else: self.client = aerospike.client(config).connect(user, password) assert self.client.is_connected() == True self.client.close()
def setup_class(cls): """ Setup class """ hostlist, user, password = TestBaseClass.get_hosts() config = {'hosts': hostlist} if user == None and password == None: TestUserSerializer.client = aerospike.client(config).connect() else: TestUserSerializer.client = aerospike.client(config).connect(user, password) response = aerospike.set_serializer(serialize_function) response = aerospike.set_deserializer(deserialize_function)
def setup_class(cls): """ Setup class. """ TestInfo.hostlist, user, password = TestBaseClass.get_hosts() config = { 'hosts': TestInfo.hostlist } TestInfo.config = config if user == None and password == None: TestInfo.client = aerospike.client(config).connect() else: TestInfo.client = aerospike.client(config).connect(user, password)
def setup_class(cls): """ Setup class. """ hostlist, user, password = TestBaseClass.get_hosts() config = {'hosts': hostlist} if user == None and password == None: TestGetRegistered.client = aerospike.client(config).connect() else: TestGetRegistered.client = aerospike.client(config).connect( user, password) policy = {'timeout': 5000} TestGetRegistered.client.udf_put(u"bin_lua.lua", 0, policy)
def setup_class(cls): """ Setup class """ hostlist, user, password = TestBaseClass.get_hosts() config = {"hosts": hostlist} if user == None and password == None: TestUdfList.client = aerospike.client(config).connect() else: TestUdfList.client = aerospike.client(config).connect(user, password) TestUdfList.client.udf_put("example.lua", 0, {})
def setup_class(cls): """ Setup class """ hostlist, user, password = TestBaseClass.get_hosts() config = {'hosts': hostlist} if user == None and password == None: TestPythonSerializer.client = aerospike.client(config).connect() else: TestPythonSerializer.client = aerospike.client(config).connect(user, password) # Unset previously set class serializers if set aerospike.unset_serializers()
def setup_class(cls): print "setup class invoked..." hostlist, user, password = TestBaseClass.get_hosts() config = {'hosts': hostlist} if user == None and password == None: TestLSet.client = aerospike.client(config).connect() else: TestLSet.client = aerospike.client(config).connect(user, password) TestLSet.key = ('test', 'demo', 'lset_add_key') TestLSet.lset = TestLSet.client.lset(TestLSet.key, 'lset_add_bin')
def setup_method(self, method): """ Setup method. """ hostlist, user, password = TestBaseClass.get_hosts() config = {'hosts': hostlist} if user == None and password == None: self.client = aerospike.client(config).connect() else: self.client = aerospike.client(config).connect(user, password) for i in xrange(20): key = ('test', u'demo', i) rec = {'name': 'name%s' % (str(i)), 'age': i} self.client.put(key, rec)
def setup_method(self, method): """ Setup method. """ hostlist, user, password = TestBaseClass.get_hosts() config = {"hosts": hostlist} if user == None and password == None: self.client = aerospike.client(config).connect() else: self.client = aerospike.client(config).connect(user, password) for i in xrange(5): key = ("test", "demo", i) rec = {"name": "name%s" % (str(i)), "age": i} self.client.put(key, rec) policy = {} self.client.udf_put(u"bin_lua.lua", 0, policy)
def teardown_class(cls): hostlist, user, password = TestBaseClass.get_hosts() config = { 'hosts': hostlist } if user == None and password == None: client = aerospike.client(config).connect() else: client = aerospike.client(config).connect(user, password) policy = {} client.index_remove('test', 'age_index', policy); client.index_remove('test', 'age_index1', policy); policy = { } module = "stream_example.lua" status = client.udf_remove( module, policy ) client.close()
def setup_class(cls): """ Setup class """ hostlist, user, password = TestBaseClass.get_hosts() config = {'hosts': hostlist} if user == None and password == None: TestUserSerializer.client = aerospike.client(config).connect() else: TestUserSerializer.client = aerospike.client(config).connect(user, password) TestUserSerializer.skip_old_server = True versioninfo = TestUserSerializer.client.info('version') for keys in versioninfo: for value in versioninfo[keys]: if value != None: versionlist = value[value.find("build") + 6:value.find("\n")].split(".") if int(versionlist[0]) >= 3 and int(versionlist[1]) >= 6: TestUserSerializer.skip_old_server = False
def teardown_class(cls): return hostlist, user, password = TestBaseClass.get_hosts() config = { 'hosts': hostlist, 'lua':{'user_path': '/tmp/', 'system_path':'../aerospike-client-c/lua/'}} if user == None and password == None: client = aerospike.client(config).connect() else: client = aerospike.client(config).connect(user, password) client.index_remove('test', 'test_demo_test_age_idx') client.index_remove('test', 'test_demo_age1_idx') module = "stream_example.lua" status = client.udf_remove(module) os.remove(config['lua']['user_path'] + 'stream_example.lua') client.close()
def setup_method(self, method): """ Setup method. """ hostlist, user, password = TestBaseClass.get_hosts() config = {'hosts': hostlist} if user == None and password == None: self.client = aerospike.client(config).connect() else: self.client = aerospike.client(config).connect(user, password) for i in xrange(5): key = ('test', 'demo', i) rec = {'name': 'name%s' % (str(i)), 'age': i} self.client.put(key, rec) policy = {} self.client.udf_put("bin_lua.lua", 0, policy) self.job_id = self.client.scan_apply("test", "demo", "bin_lua", "mytransform", ['age', 2])
def setup_method(self, method): """ Setup method """ hostlist, user, password = TestBaseClass().get_hosts() config = {"hosts": hostlist} TestQueryUsers.Me = self self.client = aerospike.client(config).connect(user, password) try: self.client.admin_drop_user("example-test") except: pass policy = {} user = "******" password = "******" roles = ["read-write", "sys-admin", "read"] status = self.client.admin_create_user(user, password, roles, policy) self.delete_users = []
def setup_class(cls): """ Setup method. """ hostlist, user, password = TestBaseClass.get_hosts() config = {'hosts': hostlist} if user == None and password == None: TestGeospatial.client = aerospike.client(config).connect() else: TestGeospatial.client = aerospike.client(config).connect(user, password) TestGeospatial.client.index_geo2dsphere_create("test", "demo", "loc", "loc_index") TestGeospatial.client.index_geo2dsphere_create("test", "demo", "loc_polygon", "loc_polygon_index") TestGeospatial.client.index_geo2dsphere_create("test", "demo", "loc_circle", "loc_circle_index") TestGeospatial.skip_old_server = True versioninfo = TestGeospatial.client.info('version') for keys in versioninfo: for value in versioninfo[keys]: if value != None: versionlist = value[value.find("build") + 6:value.find("\n")].split(".") if int(versionlist[0]) >= 3 and int(versionlist[1]) >= 7: TestGeospatial.skip_old_server = False
def setup_method(self, method): """ Setup method. """ hostlist, user, password = TestBaseClass.get_hosts() config = {'hosts': hostlist} if user == None and password == None: self.client = aerospike.client(config).connect() else: self.client = aerospike.client(config).connect(user, password) for i in xrange(20): key = ('test', u'demo', i) rec = {'name': 'name%s' % (str(i)), 'age': i} self.client.put(key, rec) key = ('test', u'demo', 122) list = [{ "op": aerospike.OPERATOR_APPEND, "bin": bytearray("asd;adk\0kj", "utf-8"), "val": u"john" }] self.client.operate(key, list)
def setup_method(self, method): """ Setup method """ hostlist, user, password = TestBaseClass().get_hosts() config = { "hosts": hostlist } self.client = aerospike.client(config).connect( user, password ) try: self.client.admin_drop_role("usr-sys-admin") except: pass usr_sys_admin_privs = [ {"code": aerospike.PRIV_USER_ADMIN}, {"code": aerospike.PRIV_SYS_ADMIN}] try: self.client.admin_drop_role("usr-sys-admin-test") except: pass self.client.admin_create_role("usr-sys-admin-test", usr_sys_admin_privs) self.delete_users = [] time.sleep(1)
def setup_class(cls): hostlist, user, password = TestBaseClass.get_hosts() config = {'hosts': hostlist} if user == None and password == None: client = aerospike.client(config).connect() else: client = aerospike.client(config).connect(user, password) client.index_integer_create('test', 'demo', 'test_age', 'age_index') client.index_string_create('test', 'demo', 'addr', 'addr_index') client.index_integer_create('test', 'demo', 'age1', 'age_index1') client.index_list_create('test', 'demo', 'numeric_list', aerospike.INDEX_NUMERIC, 'numeric_list_index') client.index_list_create('test', 'demo', 'string_list', aerospike.INDEX_STRING, 'string_list_index') client.index_map_keys_create('test', 'demo', 'numeric_map', aerospike.INDEX_NUMERIC, 'numeric_map_index') client.index_map_keys_create('test', 'demo', 'string_map', aerospike.INDEX_STRING, 'string_map_index') client.index_map_values_create('test', 'demo', 'numeric_map', aerospike.INDEX_NUMERIC, 'numeric_map_values_index') client.index_map_values_create('test', 'demo', 'string_map', aerospike.INDEX_STRING, 'string_map_values_index') client.index_integer_create('test', None, 'test_age_none', 'age_index_none') client.index_integer_create('test', 'demo', bytearray("sal\0kj", "utf-8"), 'sal_index')
class TestGrantRoles(TestBaseClass): pytestmark = pytest.mark.skipif( TestBaseClass().get_hosts()[1] == None, reason="No user specified, may be not secured cluster.") def setup_method(self, method): """ Setup method """ hostlist, user, password = TestBaseClass().get_hosts() config = {"hosts": hostlist} TestGrantRoles.Me = self self.client = aerospike.client(config).connect(user, password) try: self.client.admin_drop_user("example-test") except: pass policy = {} user = "******" password = "******" roles = ["read-write"] status = self.client.admin_create_user( user, password, roles, policy ) self.delete_users = [] def teardown_method(self, method): """ Teardown method """ policy = {} self.client.admin_drop_user( "example-test", policy ) self.client.close() def test_grant_roles_without_any_parameters(self): with pytest.raises(TypeError) as typeError: self.client.admin_grant_roles() assert "Required argument 'user' (pos 1) not found" in typeError.value def test_grant_roles_with_proper_parameters(self): policy = {'timeout': 1000} user = "******" roles = ["read", "read-write", "sys-admin"] status = self.client.admin_grant_roles(user, roles, policy) assert status == 0 time.sleep(2) user_details = self.client.admin_query_user( user, policy ) assert user_details == ['read', 'read-write', 'sys-admin'] def test_grant_roles_with_proper_parameters_without_policy(self): policy = {'timeout': 1000} user = "******" roles = ["read", "read-write", "sys-admin"] status = self.client.admin_grant_roles(user, roles) assert status == 0 time.sleep(2) user_details = self.client.admin_query_user( user, policy ) assert user_details == ['read', 'read-write', 'sys-admin'] def test_grant_roles_with_invalid_timeout_policy_value(self): policy = {"timeout": 0.1} user = "******" roles = ['sys-admin'] try: status = self.client.admin_grant_roles( user, roles, policy ) except ParamError as exception: assert exception.code == -2 assert exception.msg == "timeout is invalid" def test_grant_roles_with_proper_timeout_policy_value(self): policy = {'timeout': 20} user = "******" roles = ["read-write", "sys-admin"] status = self.client.admin_grant_roles( user, roles ) time.sleep(2) assert status == 0 user_details = self.client.admin_query_user( user ) assert user_details == ['read-write', 'sys-admin'] def test_grant_roles_with_none_username(self): policy = {'timeout': 20} user = None roles = ["sys-admin"] try: status = self.client.admin_grant_roles( user, roles, policy ) except ParamError as exception: assert exception.code == -2 assert exception.msg == "Username should be a string" def test_grant_roles_with_empty_username(self): policy = {'timeout': 1000} user = "" roles = ["read-write"] try: status = self.client.admin_grant_roles( user, roles, policy ) except InvalidUser as exception: assert exception.code == 60 assert exception.msg == "AEROSPIKE_INVALID_USER" def test_grant_roles_with_special_characters_in_username(self): policy = {'timeout': 1000} user = "******" password = "******" roles = ["read-write"] status = self.client.admin_create_user( user, password, roles, policy ) assert status == 0 roles = ["read"] status = self.client.admin_grant_roles( user, roles , policy ) time.sleep(2) assert status == 0 user_details = self.client.admin_query_user( user ) assert user_details == ['read', 'read-write'] status = self.client.admin_drop_user( "!#Q#AEQ@#$%&^*((^&*~~~````[" ) assert status == 0 def test_grant_roles_with_empty_roles_list(self): policy = {'timeout': 1000} user = "******" roles = [] try: status = self.client.admin_grant_roles( user, roles, policy ) except InvalidRole as exception: assert exception.code == 70 assert exception.msg == "AEROSPIKE_INVALID_ROLE"
class TestLList(TestBaseClass): pytestmark = pytest.mark.skipif( TestBaseClass.has_ldt_support() == False, reason="LDTs are not enabled for namespace 'test'") llist_integer = None llist_string = None client = None key1 = None key2 = None def setup_class(self): print "setup class invoked..." hostlist, user, password = TestBaseClass.get_hosts() config = {'hosts': hostlist} if user == None and password == None: self.client = aerospike.client(config).connect() else: self.client = aerospike.client(config).connect(user, password) TestLList.key1 = ('test', 'demo', 'integer_llist_ky') TestLList.llist_integer = TestLList.client.llist(TestLList.key1, 'integer_bin') TestLList.key2 = ('test', 'demo', 'string_llist_ky') TestLList.llist_string = TestLList.client.llist(TestLList.key2, 'string_bin') TestLList.key3 = ('test', 'demo', 'float_llist_ky') TestLList.llist_float = TestLList.client.llist(TestLList.key3, 'float_bin') def teardown_class(self): print "teardown class invoked..." try: TestLList.llist_integer.destroy() TestLList.llist_string.destroy() TestLList.list_float.destroy() except: pass self.client.close() #Add() - Add an object to the llist. #Get() - Get an object from the llist. #Size() - Get the current item count of the llist. def test_llist_add_get_size_positive(self): """ Invoke add() an object to LList. """ assert 0 == TestLList.llist_integer.add(11) assert [11] == TestLList.llist_integer.get(11) assert 0 == TestLList.llist_string.add("abc") assert ['abc'] == TestLList.llist_string.get('abc') assert 1 == TestLList.llist_integer.size() #Add() - Add() unsupported type data to llist. def test_llist_add_float_positive(self): """ Invoke add() float type data. """ rec = {"pi": 3.14} try: TestLList.llist_float.add(rec) except LDTKeyFunctionNotFound as exception: assert exception.code == 1433 assert exception.msg == "LDT-Key Field Not Found" #Add() - Add() without any mandatory parameters. def test_llist_no_parameter_negative(self): """ Invoke add() without any mandatory parameters. """ with pytest.raises(TypeError) as typeError: TestLList.llist_integer.add() #Add_many() - Add a list of objects to the set. def test_llist_add_many_positive(self): """ Invoke add_many() to add a list of objects to the set. """ policy = {'timeout': 7000} assert 0 == TestLList.llist_integer.add_many([122, 56, 871], policy) assert [122] == TestLList.llist_integer.get(122) assert [56] == TestLList.llist_integer.get(56) assert [871] == TestLList.llist_integer.get(871) #Get() - Get without any mandatory parameters. def test_llist_get_element_negative(self): """ Invoke get() without any mandatory parameters. """ with pytest.raises(TypeError) as typeError: TestLList.llist_integer.get() #Remove() and Get()- Remove an object from the set and get non-existent element. def test_llist_remove_positive(self): """ Invoke remove() to remove element. """ assert 0 == TestLList.llist_string.add('remove') assert 0 == TestLList.llist_string.remove('remove') try: TestLList.llist_string.get('remove') except UDFError as exception: assert exception.code == 100L except LargeItemNotFound as exception: assert exception.code == 125L #Remove() - Remove non-existent object from the llist. def test_llist_remove_element_negative(self): """ Invoke remove() to remove non-existent element. """ try: TestLList.llist_string.remove('kk') except UDFError as exception: assert exception.code == 100L except LargeItemNotFound as exception: assert exception.code == 125L #Destroy() - Delete the entire LList(LDT Remove). def test_llist_destroy_positive(self): """ Invoke destroy() to delete entire LDT. """ key = ('test', 'demo', 'remove') llist = self.client.llist(key, 'llist_add') try: llist.add(876) except: pass assert 0 == llist.destroy() def test_llist_ldt_initialize_negative(self): """ Initialize ldt with wrong key. """ key = ('test', 'demo', 12.3) try: llist = self.client.llist(key, 'ldt_stk') except ParamError as exception: assert exception.code == -2 assert exception.msg == "Parameters are incorrect" def test_llist_find_first_positive_without_policy(self): """ Invoke find_first() to access elements """ elements_list = TestLList.llist_integer.find_first(2) assert elements_list == [11, 56] def test_llist_find_first_positive(self): """ Invoke find_first() to access elements """ elements_list = TestLList.llist_integer.find_first(2, {'timeout': 1000}) assert elements_list == [11, 56] def test_llist_find_first_count_large_positive(self): """ Invoke find_first() to access elements with a larger count """ elements_list = TestLList.llist_integer.find_first(10, {'timeout': 1000}) assert elements_list == [11, 56, 122, 871] def test_llist_find_first_count_negative(self): """ Invoke find_first() to access elements with a negative count """ elements_list = TestLList.llist_integer.find_first(-8, {'timeout': 1000}) assert elements_list == [11, 56, 122, 871] def test_llist_find_last_positive_without_policy(self): """ Invoke find_last() to access elements """ elements_list = TestLList.llist_integer.find_last(2) assert elements_list == [871, 122] def test_llist_find_last_positive(self): """ Invoke find_last() to access elements """ elements_list = TestLList.llist_integer.find_last(2, {'timeout': 1000}) assert elements_list == [871, 122] def test_llist_find_last_count_large(self): """ Invoke find_last() to access elements """ elements_list = TestLList.llist_integer.find_last(15, {'timeout': 1000}) assert elements_list == [871, 122, 56, 11] def test_llist_find_last_count_negative(self): """ Invoke find_last() to access elements """ elements_list = TestLList.llist_integer.find_last(-2, {'timeout': 1000}) assert elements_list == [871, 122, 56, 11] def test_llist_find_last_no_params(self): """ Invoke find_last() to access elements """ with pytest.raises(TypeError) as typeError: TestLList.llist_integer.find_last() assert "Required argument 'count' (pos 1) not found" in typeError.value def test_llist_find_last_no_parameters_negative(self): """ Invoke find_last() to access elements """ with pytest.raises(TypeError) as typeError: TestLList.llist_integer.find_last() assert "Required argument 'count' (pos 1) not found" in typeError.value def test_llist_find_from_positive_without_policy(self): """ Invoke find_from() to access elements from a given key """ elements_list = TestLList.llist_integer.find_from(56, 2) assert elements_list == [56, 122] def test_llist_find_from_positive(self): """ Invoke find_from() to access elements from a given key """ elements_list = TestLList.llist_integer.find_from(56, 2, {'timeout': 1000}) assert elements_list == [56, 122] def test_llist_find_from_positive_non_existent_key(self): """ Invoke find_from() to access elements from a non-existent key """ elements_list = TestLList.llist_integer.find_from(21, 2, {'timeout': 1000}) assert elements_list == [56, 122] def test_llist_range_limit_positive_without_policy(self): """ Invoke range_limit() to access elements """ elements_list = TestLList.llist_integer.range_limit(56, 871, 2, None, None) assert elements_list == [56, 122, 871] def test_llist_range_limit_positive(self): """ Invoke range_limit() to access elements """ elements_list = TestLList.llist_integer.range_limit(56, 871, 2, None, None, {'timeout': 1000}) assert elements_list == [56, 122, 871] def test_llist_range_limit_negative_keys(self): """ Invoke range_limit() to access elements with negative keys """ elements_list = TestLList.llist_integer.range_limit(-56, -871, 2, None, None, {'timeout': 1000}) assert elements_list == [] def test_llist_range_limit_larger_count_positive(self): """ Invoke range_limit() to access elements with larger count than list size """ elements_list = TestLList.llist_integer.range_limit(56, 871, 8, None, None, {'timeout': 1000}) assert elements_list == [56, 122, 871] def test_llist_range_limit_count_negative(self): """ Invoke range_limit() to access elements """ elements_list = TestLList.llist_integer.range_limit(56, 871, -2, None, None, {'timeout': 1000}) assert elements_list == [56, 122, 871] def test_llist_set_page_size_without_policy(self): #Invoke set_page_size() to set page size of ldt bin. assert 0 == TestLList.llist_integer.set_page_size(8192) def test_llist_set_page_size(self): #Invoke set_page_size() to set page size of ldt bin. assert 0 == TestLList.llist_integer.set_page_size(8192, {'timeout': 0}) def test_llist_set_page_size_string_negative(self): with pytest.raises(TypeError) as typeError: TestLList.llist_integer.set_page_size("8192", {'timeout': 0}) assert "an integer is required" in typeError.value """ Causes db to shutdown
class TestGeospatial(TestBaseClass): pytestmark = pytest.mark.skipif( TestBaseClass.has_geo_support() == False, reason="Server does not support geospatial data.") def setup_class(cls): """ Setup method. """ hostlist, user, password = TestBaseClass.get_hosts() config = {'hosts': hostlist} if user == None and password == None: TestGeospatial.client = aerospike.client(config).connect() else: TestGeospatial.client = aerospike.client(config).connect(user, password) TestGeospatial.client.index_geo2dsphere_create("test", "demo", "loc", "loc_index") TestGeospatial.client.index_geo2dsphere_create("test", "demo", "loc_polygon", "loc_polygon_index") TestGeospatial.client.index_geo2dsphere_create("test", "demo", "loc_circle", "loc_circle_index") TestGeospatial.skip_old_server = True versioninfo = TestGeospatial.client.info('version') for keys in versioninfo: for value in versioninfo[keys]: if value != None: versionlist = value[value.find("build") + 6:value.find("\n")].split(".") if int(versionlist[0]) >= 3 and int(versionlist[1]) >= 7: TestGeospatial.skip_old_server = False def teardown_class(cls): TestGeospatial.client.index_remove('test', 'loc_index') TestGeospatial.client.index_remove('test', 'loc_polygon_index') TestGeospatial.client.index_remove('test', 'loc_circle_index') TestGeospatial.client.close() def setup_method(self, method): self.keys = [] pre = '{"type": "Point", "coordinates"' suf = ']}' for i in xrange(10): lng = 1220 - (2 * i) lat = 375 + (2 * i) key = ('test', 'demo', i) s = "{0}: [-{1}.{2}, {3}.{4}{5}".format(pre, (lng/10), (lng%10), (lat/10), (lat%10), suf) self.geo_object = aerospike.geojson(s) TestGeospatial.client.put(key, {"loc": self.geo_object}) self.keys.append(key) key = ('test', 'demo', 'polygon') self.geo_object_polygon = aerospike.GeoJSON({"type": "Polygon", "coordinates": [[[-122.500000, 37.000000],[-121.000000, 37.000000], [-121.000000, 38.080000],[-122.500000, 38.080000], [-122.500000, 37.000000]]]}) TestGeospatial.client.put(key, {"loc_polygon": self.geo_object_polygon}) self.keys.append(key) if not TestGeospatial.skip_old_server: key = ('test', 'demo', 'circle') geo_circle = aerospike.GeoJSON({"type": "AeroCircle", "coordinates": [[-122.0, 37.5], 250.2]}) TestGeospatial.client.put(key, {"loc_circle": geo_circle}) self.keys.append(key) def teardown_method(self, method): """ Teardown method. """ for key in self.keys: TestGeospatial.client.remove(key) def test_geospatial_put_get_positive(self): """ Perform a get and put with multiple bins including geospatial bin """ key = ('test', 'demo', 'single_geo_put') geo_object_single = aerospike.GeoJSON({"type": "Point", "coordinates": [42.34, 58.62] }) geo_object_dict = aerospike.GeoJSON({"type": "Point", "coordinates": [56.34, 69.62] }) TestGeospatial.client.put(key, {"loc": geo_object_single, "int_bin": 2, "string_bin": "str", "dict_bin": {"a": 1, "b":2, "geo": geo_object_dict}}) key, meta, bins = TestGeospatial.client.get(key) expected = {'loc': {'coordinates': [42.34, 58.62], 'type': 'Point'}, "int_bin": 2, "string_bin": "str", "dict_bin": {"a": 1, "b": 2, "geo": {'coordinates': [56.34, 69.62], 'type': 'Point'}}} for b in bins: assert b in expected TestGeospatial.client.remove(key) def test_geospatial_positive_query(self): """ Perform a positive geospatial query for a polygon """ records = [] query = TestGeospatial.client.query("test", "demo") geo_object2 = aerospike.GeoJSON({"type": "Polygon", "coordinates": [[[-122.500000, 37.000000],[-121.000000, 37.000000], [-121.000000, 38.080000],[-122.500000, 38.080000], [-122.500000, 37.000000]]]}) query.where(p.geo_within_geojson_region("loc", geo_object2.dumps())) def callback((key, metadata, record)): records.append(record) query.foreach(callback) assert len(records) == 3 expected = [{"coordinates": [-122.0, 37.5], "type": "Point"}, {"coordinates": [-121.8, 37.7], "type": "Point"}, {"coordinates": [-121.6, 37.9], "type": "Point"}] for r in records: assert r['loc'].unwrap() in expected def test_geospatial_positive_query_outside_shape(self): """ Perform a positive geospatial query for polygon where all points are outside polygon """ records = [] query = TestGeospatial.client.query("test", "demo") geo_object2 = aerospike.GeoJSON({"type": "Polygon", "coordinates": [[[-126.500000, 37.000000],[-124.000000, 37.000000], [-124.000000, 38.080000],[-126.500000, 38.080000], [-126.500000, 37.000000]]]}) query.where(p.geo_within_geojson_region("loc", geo_object2.dumps())) def callback((key, metadata, record)): records.append(record) query.foreach(callback) assert len(records) == 0 def test_geospatial_positive_query_without_set(self): """ Perform a positive geospatial query for a polygon without a set """ keys = [] pre = '{"type": "Point", "coordinates"' suf = ']}' for i in xrange(1, 10): lng = 1220 - (2 * i) lat = 375 + (2 * i) key = ('test', None, i) s = "{0}: [-{1}.{2}, {3}.{4}{5}".format(pre, (lng/10), (lng%10), (lat/10), (lat%10), suf) geo_object = aerospike.geojson(s) TestGeospatial.client.put(key, {"loc": geo_object}) keys.append(key) TestGeospatial.client.index_geo2dsphere_create("test", None, "loc", "loc_index_no_set") records = [] query = TestGeospatial.client.query("test", None) geo_object2 = aerospike.GeoJSON({"type": "Polygon", "coordinates": [[[-122.500000, 37.000000],[-121.000000, 37.000000], [-121.000000, 38.080000],[-122.500000, 38.080000], [-122.500000, 37.000000]]]}) query.where(p.geo_within_geojson_region("loc", geo_object2.dumps())) def callback((key, metadata, record)): records.append(record) query.foreach(callback) TestGeospatial.client.index_remove('test', 'loc_index_no_set') for key in keys: TestGeospatial.client.remove(key) assert len(records) == 2 expected = [{'coordinates': [-121.8, 37.7], 'type': 'Point'}, {'coordinates': [-121.6, 37.9], 'type': 'Point'}] for r in records: assert r['loc'].unwrap() in expected def test_geospatial_positive_query_for_circle(self): """ Perform a positive geospatial query for a circle """ if TestGeospatial.skip_old_server == True: pytest.skip("Server does not support apply on AeroCircle for GeoJSON") records = [] query = TestGeospatial.client.query("test", "demo") geo_object2 = aerospike.GeoJSON({"type": "AeroCircle", "coordinates": [[-122.0, 37.5], 250.2]}) query.where(p.geo_within_geojson_region("loc", geo_object2.dumps())) def callback((key, metadata, record)): records.append(record) query.foreach(callback) assert len(records) == 1 expected = [{'coordinates': [-122.0, 37.5], 'type': 'Point'}] for r in records: assert r['loc'].unwrap() in expected def test_geospatial_positive_query_for_circle_with_within_radius_helper(self): """ Perform a positive geospatial query for a circle with helper """ if TestGeospatial.skip_old_server == True: pytest.skip("Server does not support apply on AeroCircle for GeoJSON") records = [] query = TestGeospatial.client.query("test", "demo") query.where(p.geo_within_radius("loc", -122.0, 37.5, 250.2)) def callback((key, metadata, record)): records.append(record) query.foreach(callback) assert len(records) == 1 expected = [{'coordinates': [-122.0, 37.5], 'type': 'Point'}] for r in records: assert r['loc'].unwrap() in expected def test_geospatial_operate_positive(self): """ Perform an operate operation with geospatial bin """ geo_object_operate = aerospike.GeoJSON({"type": "Point", "coordinates": [43.45, 56.75] }) key = ('test', 'demo', 'single_geo_operate') list = [{ "op": aerospike.OPERATOR_WRITE, "bin": "write_bin", "val": {"no": geo_object_operate} }, {"op": aerospike.OPERATOR_READ, "bin": "write_bin"}] key, meta, bins = TestGeospatial.client.operate(key, list) assert bins['write_bin']['no'].unwrap() == {'coordinates': [43.45, 56.75], 'type': 'Point'} TestGeospatial.client.remove(key) def test_geospatial_wrap_positive(self): """ Perform a positive wrap on geospatial data """ self.geo_object.wrap({"type": "Polygon", "coordinates": [[[-122.500000, 37.000000],[-121.000000, 37.000000], [-121.000000, 38.080000],[-122.500000, 38.080000], [-122.500000, 37.000000]]]}) assert self.geo_object.unwrap() == {'coordinates': [[[-122.5, 37.0], [-121.0, 37.0], [-121.0, 38.08], [-122.5, 38.08], [-122.5, 37.0]]], 'type': 'Polygon'} def test_geospatial_wrap_positive_with_query(self): """ Perform a positive wrap on geospatial data followed by a query """ geo_object_wrap = aerospike.GeoJSON({"type": "Polygon", "coordinates": [[[-124.500000, 37.000000],[-125.000000, 37.000000], [-121.000000, 38.080000],[-122.500000, 38.080000], [-124.500000, 37.000000]]]}) geo_object_wrap.wrap({"type": "Polygon", "coordinates": [[[-122.500000, 37.000000],[-121.000000, 37.000000], [-121.000000, 38.080000],[-122.500000, 38.080000], [-122.500000, 37.000000]]]}) assert geo_object_wrap.unwrap() == {'coordinates': [[[-122.5, 37.0], [-121.0, 37.0], [-121.0, 38.08], [-122.5, 38.08], [-122.5, 37.0]]], 'type': 'Polygon'} records = [] query = TestGeospatial.client.query("test", "demo") query.where(p.geo_within_geojson_region("loc", geo_object_wrap.dumps())) def callback((key, metadata, record)): records.append(record) query.foreach(callback) assert len(records) == 3 expected = [{'coordinates': [-122.0, 37.5], 'type': 'Point'}, {'coordinates': [-121.8, 37.7], 'type': 'Point'}, {'coordinates': [-121.6, 37.9], 'type': 'Point'}] for r in records: assert r['loc'].unwrap() in expected def test_geospatial_loads_positive(self): """ Perform a positive loads on geoJSON raw string """ self.geo_object.loads('{"type": "Polygon", "coordinates": [[[-122.500000, 37.000000], [-121.000000, 37.000000], [-121.000000, 38.080000],[-122.500000, 38.080000], [-122.500000, 37.000000]]]}') assert self.geo_object.unwrap() == {'coordinates': [[[-122.5, 37.0], [-121.0, 37.0], [-121.0, 38.08], [-122.5, 38.08], [-122.5, 37.0]]], 'type': 'Polygon'} def test_geospatial_loads_positive_with_query(self): """ Perform a positive loads on geoJSON raw string followed by a query """ geo_object_loads = aerospike.GeoJSON({"type": "Polygon", "coordinates": [[[-124.500000, 37.000000],[-125.000000, 37.000000], [-121.000000, 38.080000],[-122.500000, 38.080000], [-124.500000, 37.000000]]]}) geo_object_loads.loads('{"type": "Polygon", "coordinates": [[[-122.500000, 37.000000], [-121.000000, 37.000000], [-121.000000, 38.080000],[-122.500000, 38.080000], [-122.500000, 37.000000]]]}') assert geo_object_loads.unwrap() == {'coordinates': [[[-122.5, 37.0], [-121.0, 37.0], [-121.0, 38.08], [-122.5, 38.08], [-122.5, 37.0]]], 'type': 'Polygon'} records = [] query = TestGeospatial.client.query("test", "demo") query.where(p.geo_within_geojson_region("loc", geo_object_loads.dumps())) def callback((key, metadata, record)): records.append(record) query.foreach(callback) assert len(records) == 3 expected = [{'coordinates': [-122.0, 37.5], 'type': 'Point'}, {'coordinates': [-121.8, 37.7], 'type': 'Point'}, {'coordinates': [-121.6, 37.9], 'type': 'Point'}] for r in records: assert r['loc'].unwrap() in expected def test_geospatial_dumps_positive(self): """ Perform a positive dumps. Verify using str """ geojson_str = self.geo_object.dumps() assert type(geojson_str) == str obj = aerospike.geojson(geojson_str) assert obj.unwrap() == self.geo_object.unwrap() geojson_str = str(self.geo_object) assert type(geojson_str) == str obj = aerospike.geojson(geojson_str) assert obj.unwrap() == self.geo_object.unwrap() def test_geospatial_repr_positive(self): """ Perform a positive repr. Verify using eval() """ geojson_str = eval(repr(self.geo_object)) assert type(geojson_str) == str obj = aerospike.geojson(geojson_str) assert obj.unwrap() == self.geo_object.unwrap() def test_geospatial_put_get_positive_with_geodata(self): """ Perform a get and put with multiple bins including geospatial bin using geodata method """ key = ('test', 'demo', 'single_geo_put') geo_object_single = aerospike.geodata({"type": "Point", "coordinates": [42.34, 58.62] }) geo_object_dict = aerospike.geodata({"type": "Point", "coordinates": [56.34, 69.62] }) TestGeospatial.client.put(key, {"loc": geo_object_single, "int_bin": 2, "string_bin": "str", "dict_bin": {"a": 1, "b":2, "geo": geo_object_dict}}) key, meta, bins = TestGeospatial.client.get(key) expected = {'loc': {'coordinates': [42.34, 58.62], 'type': 'Point'}, "int_bin": 2, "string_bin": "str", "dict_bin": {"a": 1, "b": 2, "geo": {'coordinates': [56.34, 69.62], 'type': 'Point'}}} for b in bins: assert b in expected TestGeospatial.client.remove(key) def test_geospatial_put_get_positive_with_geojson(self): """ Perform a get and put with multiple bins including geospatial bin using geodata method """ key = ('test', 'demo', 'single_geo_put') geo_object_single = aerospike.geojson('{"type": "Point", "coordinates": [42.34, 58.62] }') geo_object_dict = aerospike.geojson('{"type": "Point", "coordinates": [56.34, 69.62] }') TestGeospatial.client.put(key, {"loc": geo_object_single, "int_bin": 2, "string_bin": "str", "dict_bin": {"a": 1, "b":2, "geo": geo_object_dict}}) key, meta, bins = TestGeospatial.client.get(key) expected = {'loc': {'coordinates': [42.34, 58.62], 'type': 'Point'}, "int_bin": 2, "string_bin": "str", "dict_bin": {"a": 1, "b": 2, "geo": {'coordinates': [56.34, 69.62], 'type': 'Point'}}} for b in bins: assert b in expected TestGeospatial.client.remove(key) def test_geospatial_positive_query_with_geodata(self): """ Perform a positive geospatial query for a polygon with geodata """ records = [] query = TestGeospatial.client.query("test", "demo") geo_object2 = aerospike.geodata({"type": "Polygon", "coordinates": [[[-122.500000, 37.000000],[-121.000000, 37.000000], [-121.000000, 38.080000],[-122.500000, 38.080000], [-122.500000, 37.000000]]]}) query.where(p.geo_within_geojson_region("loc", geo_object2.dumps())) def callback((key, metadata, record)): records.append(record) query.foreach(callback) assert len(records) == 3 expected = [{'coordinates': [-122.0, 37.5], 'type': 'Point'}, {'coordinates': [-121.8, 37.7], 'type': 'Point'}, {'coordinates': [-121.6, 37.9], 'type': 'Point'}] for r in records: assert r['loc'].unwrap() in expected def test_geospatial_positive_query_with_geojson(self): """ Perform a positive geospatial query for a polygon with geojson """ records = [] query = TestGeospatial.client.query("test", "demo") geo_object2 = aerospike.geojson('{"type": "Polygon", "coordinates": [[[-122.500000, 37.000000], [-121.000000, 37.000000], [-121.000000, 38.080000],[-122.500000, 38.080000], [-122.500000, 37.000000]]]}') query.where(p.geo_within_geojson_region("loc", geo_object2.dumps())) def callback((key, metadata, record)): records.append(record) query.foreach(callback) assert len(records) == 3 expected = [{'coordinates': [-122.0, 37.5], 'type': 'Point'}, {'coordinates': [-121.8, 37.7], 'type': 'Point'},{'coordinates': [-121.6, 37.9], 'type': 'Point'}] for r in records: assert r['loc'].unwrap() in expected def test_geospatial_2dindex_positive(self): """ Perform a positive 2d index creation """ status = TestGeospatial.client.index_remove('test', 'loc_index') assert status == 0 status = TestGeospatial.client.index_geo2dsphere_create("test", "demo", "loc", "loc_index") assert status == 0 def test_geospatial_2dindex_positive_with_policy(self): """ Perform a positive 2d index creation with policy """ status = TestGeospatial.client.index_remove('test', 'loc_index') assert status == 0 status = TestGeospatial.client.index_geo2dsphere_create("test", "demo", "loc", "loc_index", {"timeout": 2000}) assert status == 0 def test_geospatial_positive_query_with_point(self): """ Perform a positive geospatial query for a point """ records = [] query = TestGeospatial.client.query("test", "demo") geo_object2 = aerospike.GeoJSON({"type": "Point", "coordinates": [-121.700000, 37.200000]}) query.where(p.geo_contains_geojson_point("loc_polygon", geo_object2.dumps())) def callback((key, metadata, record)): records.append(record) query.foreach(callback) assert len(records) == 1 expected = [{'coordinates': [[[-122.500000, 37.000000],[-121.000000, 37.000000], [-121.000000, 38.080000],[-122.500000, 38.080000], [-122.500000, 37.000000]]], 'type': 'Polygon'}] for r in records: assert r['loc_polygon'].unwrap() in expected def test_geospatial_positive_query_with_point_outside_polygon(self): """ Perform a positive geospatial query for a point outside polygon """ records = [] query = TestGeospatial.client.query("test", "demo") geo_object2 = aerospike.GeoJSON({"type": "Point", "coordinates": [-123.700000, 37.200000]}) query.where(p.geo_contains_geojson_point("loc_polygon", geo_object2.dumps())) def callback((key, metadata, record)): records.append(record) query.foreach(callback) assert len(records) == 0 def test_geospatial_positive_query_with_point_in_aerocircle(self): """ Perform a positive geospatial query for a point in aerocircle """ if TestGeospatial.skip_old_server == True: pytest.skip("Server does not support apply on AeroCircle for GeoJSON") records = [] query = TestGeospatial.client.query("test", "demo") geo_object2 = aerospike.GeoJSON({"type": "Point", "coordinates": [-122.000000, 37.500000]}) query.where(p.geo_contains_geojson_point("loc_circle", geo_object2.dumps())) def callback((key, metadata, record)): records.append(record) query.foreach(callback) assert len(records) == 1 expected = [{'coordinates': [[-122.0, 37.5], 250.2], 'type': 'AeroCircle'}] for r in records: assert r['loc_circle'].unwrap() in expected def test_geospatial_positive_query_with_point_outside_aerocircle(self): """ Perform a positive geospatial query for a point in aerocircle """ if TestGeospatial.skip_old_server == True: pytest.skip("Server does not support apply on AeroCircle for GeoJSON") records = [] query = TestGeospatial.client.query("test", "demo") geo_object2 = aerospike.GeoJSON({"type": "Point", "coordinates": [-122.000000, 450.200]}) query.where(p.geo_contains_geojson_point("loc_circle", geo_object2.dumps())) def callback((key, metadata, record)): records.append(record) query.foreach(callback) assert len(records) == 0 def test_geospatial_positive_query_with_point_helper_method(self): """ Perform a positive geospatial query for a point with helper method """ records = [] query = TestGeospatial.client.query("test", "demo") query.where(p.geo_contains_point("loc_polygon", -121.7, 37.2)) def callback((key, metadata, record)): records.append(record) query.foreach(callback) assert len(records) == 1 expected = [{'coordinates': [[[-122.500000, 37.000000],[-121.000000, 37.000000], [-121.000000, 38.080000],[-122.500000, 38.080000], [-122.500000, 37.000000]]], 'type': 'Polygon'}] for r in records: assert r['loc_polygon'].unwrap() in expected def test_geospatial_object_not_dict_or_string(self): """ The geospatial object is not a dictionary or string """ try: geo_object_wrong = aerospike.GeoJSON(1) except ParamError as exception: assert exception.code == -2 assert exception.msg == 'Geospatial data should be a dictionary or raw GeoJSON string' def test_geospatial_object_non_json_serializable_string(self): """ The geospatial object is not a json serializable string """ try: geo_object_wrong = aerospike.GeoJSON("abc") except ClientError as exception: assert exception.code == -1 assert exception.msg == 'String is not GeoJSON serializable' def test_geospatial_object_wrap_non_dict(self): """ The geospatial object provided to wrap() is not a dictionary """ try: self.geo_object.wrap("abc") except ParamError as exception: assert exception.code == -2 assert exception.msg == 'Geospatial data should be a dictionary or raw GeoJSON string' def test_geospatial_object_loads_non_dict(self): """ The geospatial object provided to loads() is not a dictionary """ try: self.geo_object.loads('{"abc"}') except ClientError as exception: assert exception.code == -1 assert exception.msg == 'String is not GeoJSON serializable' def test_geospatial_2dindex_set_length_extra(self): """ Perform a 2d creation with set length exceeding limit """ set_name = 'a' for i in xrange(100): set_name = set_name + 'a' status = TestGeospatial.client.index_remove('test', 'loc_index') assert status == 0 try: status = TestGeospatial.client.index_geo2dsphere_create("test", set_name, "loc", "loc_index") except InvalidRequest as exception: assert exception.code == 4 def test_geospatial_query_circle_incorrect_param_within_radius_helper_method(self): """ Perform a positive geospatial query for a circle with incorrect params for helper method """ records = [] query = TestGeospatial.client.query("test", "demo") try: query.where(p.geo_contains_point("loc_polygon", -122.0, 37.5, 250)) except ParamError as exception: assert exception.code == -2L assert exception.msg == "predicate is invalid." def test_geospatial_query_point_incorrect_param_helper_method(self): """ Perform a positive geospatial query for a point with incorrect params for helper method """ records = [] query = TestGeospatial.client.query("test", "demo") try: query.where(p.geo_contains_point("loc_polygon", -121.7, 37)) except ParamError as exception: assert exception.code == -2L
class TestDropUser(TestBaseClass): pytestmark = pytest.mark.skipif( TestBaseClass().get_hosts()[1] == None, reason="No user specified, may be not secured cluster.") def setup_method(self, method): """ Setup method. """ hostlist, user, password = TestBaseClass().get_hosts() config = {'hosts': hostlist} TestDropUser.Me = self self.client = aerospike.client(config).connect(user, password) try: self.client.admin_drop_user("foo-test") except: pass def teardown_method(self, method): """ Teardoen method. """ self.client.close() def test_drop_user_with_no_parameters(self): """ Invoke drop_user() without any mandatory parameters. """ with pytest.raises(TypeError) as typeError: self.client.admin_drop_user() assert "Required argument 'user' (pos 1) not found" in typeError.value def test_drop_user_with_policy_none(self): """ Invoke drop_user() with policy none """ policy = None user = "******" password = "******" roles = ["read", "read-write", "sys-admin"] status = self.client.admin_create_user( user, password, roles, policy ) time.sleep(2) assert status == 0 user_details = self.client.admin_query_user( user, policy ) assert user_details == ['read', 'read-write', 'sys-admin'] status = self.client.admin_drop_user( user, policy ) assert status == 0 try: user_details = self.client.admin_query_user( user ) except InvalidUser as exception: assert exception.code == 60L assert exception.msg == 'AEROSPIKE_INVALID_USER' def test_drop_user_with_user_none(self): """ Invoke drop_user() with policy none """ policy = {'timeout': 1000} try: self.client.admin_drop_user( None, policy ) except ParamError as exception: assert exception.code == -2L assert exception.msg == 'Username should be a string' def test_drop_user_positive(self): """ Invoke drop_user() with correct arguments. """ policy = {'timeout': 1000} user = "******" password = "******" roles = ["read", "read-write", "sys-admin"] status = self.client.admin_create_user( user, password, roles, policy ) time.sleep(1) assert status == 0 user_details = self.client.admin_query_user( user, policy ) assert user_details == ['read', 'read-write', 'sys-admin'] status = self.client.admin_drop_user( user, policy ) assert status == 0 time.sleep(1) try: user_details = self.client.admin_query_user( user, policy ) except InvalidUser as exception: assert exception.code == 60L assert exception.msg == 'AEROSPIKE_INVALID_USER' def test_drop_user_positive_without_policy(self): """ Invoke drop_user() with correct arguments. """ policy = { 'timeout': 1000 } user = "******" password = "******" roles = ["read", "read-write", "sys-admin"] status = self.client.admin_create_user( user, password, roles, policy ) time.sleep(1) assert status == 0 user_details = self.client.admin_query_user( user, policy ) assert user_details == ['read', 'read-write', 'sys-admin'] status = self.client.admin_drop_user( user ) assert status == 0 time.sleep(1) try: user_details = self.client.admin_query_user( user, policy ) except InvalidUser as exception: assert exception.code == 60L assert exception.msg == 'AEROSPIKE_INVALID_USER' def test_drop_user_negative(self): """ Invoke drop_user() with non-existent user. """ policy = {} user = "******" password = "******" roles = ["read", "read-write", "sys-admin"] try: user_details = self.client.admin_query_user( user, policy ) except InvalidUser as exception: assert exception.code == 60L assert exception.msg == 'AEROSPIKE_INVALID_USER' try: status = self.client.admin_drop_user( user ) except InvalidUser as exception: assert exception.code == 60L assert exception.msg == 'AEROSPIKE_INVALID_USER' def test_drop_user_policy_incorrect(self): """ Invoke drop_user() with policy incorrect """ policy = {'timeout': 1000} user = "******" password = "******" roles = ["read", "read-write", "sys-admin"] status = self.client.admin_create_user( user, password, roles, policy ) time.sleep(1) assert status == 0 user_details = self.client.admin_query_user( user, policy ) assert user_details == ['read', 'read-write', 'sys-admin'] policy = { 'timeout': 0.2 } try: status = self.client.admin_drop_user( user, policy ) except ParamError as exception: assert exception.code == -2L assert exception.msg == 'timeout is invalid' status = self.client.admin_drop_user( user ) def test_drop_user_with_extra_argument(self): """ Invoke drop_user() with extra argument. """ policy = {'timeout': 1000} with pytest.raises(TypeError) as typeError: self.client.admin_drop_user( "foo-test", policy, "" ) assert "admin_drop_user() takes at most 2 arguments (3 given)" in typeError.value def test_drop_user_with_too_long_username(self): policy = {} user = "******" * 1000 password = "******" roles = ["sys-admin"] try: status = self.client.admin_create_user( user, password, roles, policy ) except InvalidUser as exception: assert exception.code == 60 assert exception.msg == "AEROSPIKE_INVALID_USER" try: status = self.client.admin_drop_user( user, policy ) except InvalidUser as exception: assert exception.code == 60 assert exception.msg == "AEROSPIKE_INVALID_USER" def test_drop_user_with_special_characters_in_username(self): policy = {} user = "******" password = "******" roles = ["read-write"] try: status = self.client.admin_create_user( user, password, roles, policy ) assert status == 0 except: pass status = self.client.admin_drop_user( user ) assert status == 0
class TestCreateRole(TestBaseClass): pytestmark = pytest.mark.skipif( TestBaseClass().get_hosts()[1] == None, reason="No user specified, may be not secured cluster.") def setup_method(self, method): """ Setup method """ hostlist, user, password = TestBaseClass().get_hosts() config = { "hosts": hostlist} self.client = aerospike.client(config).connect( user, password ) try: self.client.admin_drop_user("testcreaterole") except: pass # do nothing, EAFP self.delete_users = [] def teardown_method(self, method): """ Teardown method """ policy = {} for user in self.delete_users: self.client.admin_drop_user( user, policy ) self.client.close() def test_create_role_without_any_parameters(self): with pytest.raises(TypeError) as typeError: self.client.admin_create_role() assert "Required argument 'role' (pos 1) not found" in typeError.value def test_create_role_positive_with_policy(self): """ Create role positive """ try: self.client.admin_query_role("usr-sys-admin-test") # role exists, clear it out. self.client.admin_drop_role("usr-sys-admin-test") except 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': 1000}) time.sleep(1) roles = self.client.admin_query_role("usr-sys-admin-test") assert roles == [{'code': 10, 'ns': 'test', 'set': 'demo'}] status = self.client.admin_create_user("testcreaterole", "createrole", ["usr-sys-admin-test"]) assert status == 0 time.sleep(1) users = self.client.admin_query_user("testcreaterole") assert users == ["usr-sys-admin-test"] status = self.client.admin_drop_role("usr-sys-admin-test") assert status == 0 users = self.client.admin_query_user("testcreaterole") assert users == [] self.client.admin_drop_user("testcreaterole") def test_create_role_positive(self): """ Create role positive """ try: self.client.admin_query_role("usr-sys-admin-test") # role exists, clear it out. self.client.admin_drop_role("usr-sys-admin-test") except 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}]) time.sleep(1) roles = self.client.admin_query_role("usr-sys-admin-test") assert roles == [{"code": 0, 'ns': '', 'set': ''}, {"code": 1, 'ns': '', 'set': ''}] status = self.client.admin_create_user("testcreaterole", "createrole", ["usr-sys-admin-test"]) assert status == 0 time.sleep(1) users = self.client.admin_query_user("testcreaterole") assert users == ["usr-sys-admin-test"] status = self.client.admin_drop_role("usr-sys-admin-test") assert status == 0 users = self.client.admin_query_user("testcreaterole") assert users == [] self.client.admin_drop_user("testcreaterole") def test_create_role_incorrect_role_type(self): """ role name not string """ try: self.client.admin_create_role(1, [{"code": aerospike.PRIV_USER_ADMIN}]) except ParamError as exception: assert exception.code == -2 assert exception.msg == "Role name should be a string" def test_create_role_unknown_privilege_type(self): """ privilege type unknown """ try: self.client.admin_query_role("usr-sys-admin-test") # role exists, clear it out. self.client.admin_drop_role("usr-sys-admin-test") except InvalidRole: pass # we are good, no such role exists try: self.client.admin_create_role("usr-sys-admin-test", [{"code": 64}]) except InvalidPrivilege as exception: assert exception.code == 72 def test_create_role_incorrect_privilege_type(self): """ privilege type incorrect """ try: self.client.admin_create_role("usr-sys-admin-test", None) except ParamError as exception: assert exception.code == -2 assert exception.msg == "Privileges should be a list" def test_create_role_existing_role(self): """ create an already existing role """ try: self.client.admin_query_role("usr-sys-admin-test") # role exists, clear it out. self.client.admin_drop_role("usr-sys-admin-test") except 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}]) try: self.client.admin_create_role("usr-sys-admin-test", [{"code": aerospike.PRIV_USER_ADMIN}, {"code": aerospike.PRIV_SYS_ADMIN}]) except 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") assert status == 0 def test_create_role_positive_with_special_characters(self): """ Create role positive with special characters in role name """ role_name = "!#Q#AEQ@#$%&^*((^&*~~~````" try: self.client.admin_drop_role(role_name) # clear out if it exists except: pass # EAFP status = self.client.admin_create_role(role_name, [{"code": aerospike.PRIV_READ, "ns": "test", "set":"demo"}], {'timeout': 1000}) assert status == 0 time.sleep(1) roles = self.client.admin_query_role(role_name) assert roles == [{"code": aerospike.PRIV_READ, "ns": "test", "set": "demo"}] status = self.client.admin_create_user("testcreaterole", "createrole", [role_name]) assert status == 0 time.sleep(1) users = self.client.admin_query_user("testcreaterole") assert users == [role_name] status = self.client.admin_drop_role(role_name) assert status == 0 users = self.client.admin_query_user("testcreaterole") assert users == [] self.client.admin_drop_user("testcreaterole") def test_create_role_positive_with_too_long_role_name(self): """ Create role positive with too long role name """ role_name = "role$"*1000 try: self.client.admin_create_role(role_name, [{"code": aerospike.PRIV_READ, "ns": "test", "set":"demo"}], {'timeout': 1000}) except InvalidRole as exception: assert exception.code == 70 assert exception.msg == "AEROSPIKE_INVALID_ROLE"
class TestQueryRoles(TestBaseClass): pytestmark = pytest.mark.skipif( TestBaseClass().get_hosts()[1] == None, reason="No user specified, may be not secured cluster.") def setup_method(self, method): """ Setup method """ hostlist, user, password = TestBaseClass().get_hosts() config = { "hosts": hostlist } self.client = aerospike.client(config).connect( user, password ) try: self.client.admin_drop_role("usr-sys-admin") except: pass usr_sys_admin_privs = [ {"code": aerospike.PRIV_USER_ADMIN}, {"code": aerospike.PRIV_SYS_ADMIN}] try: self.client.admin_drop_role("usr-sys-admin-test") except: pass self.client.admin_create_role("usr-sys-admin-test", usr_sys_admin_privs) self.delete_users = [] time.sleep(1) def teardown_method(self, method): """ Teardown method """ policy = {} self.client.admin_drop_role("usr-sys-admin-test") self.client.close() def test_admin_query_roles_positive(self): """ Query roles positive """ roles = self.client.admin_query_roles() flag = 0 assert roles['usr-sys-admin-test'] == [{'code': 0, 'ns': '', 'set': ''}, {'code': 1, 'ns': '', 'set': ''}] def test_admin_query_roles_positive_with_policy(self): """ Query roles positive policy """ roles = self.client.admin_query_roles({'timeout': 1000}) flag = 0 assert roles['usr-sys-admin-test'] == [{'code': 0, 'ns': '', 'set': ''}, {'code': 1, 'ns': '', 'set': ''}] def test_admin_query_roles_incorrect_policy(self): """ Query roles incorrect policy """ try: roles = self.client.admin_query_roles({'timeout': 0.2}) except ParamError as exception: assert exception.code == -2 assert exception.msg == 'timeout is invalid'
class TestGeospatial(TestBaseClass): pytestmark = pytest.mark.skipif( TestBaseClass.has_geo_support() == False, reason="Server does not support geospatial data.") def setup_class(cls): """ Setup method. """ hostlist, user, password = TestBaseClass.get_hosts() config = {'hosts': hostlist} if user == None and password == None: TestGeospatial.client = aerospike.client(config).connect() else: TestGeospatial.client = aerospike.client(config).connect( user, password) TestGeospatial.client.index_geo2dsphere_create("test", "demo", "loc", "loc_index") def teardown_class(cls): TestGeospatial.client.index_remove('test', 'loc_index') TestGeospatial.client.close() def setup_method(self, method): self.keys = [] for i in xrange(10): key = ('test', 'demo', i) lng = -122 + (0.2 * i) lat = 37.5 + (0.2 * i) self.geo_object = aerospike.GeoJSON({ "type": "Point", "coordinates": [lng, lat] }) TestGeospatial.client.put(key, {"loc": self.geo_object}) self.keys.append(key) def teardown_method(self, method): """ Teardown method. """ for key in self.keys: TestGeospatial.client.remove(key) def test_geospatial_put_get_positive(self): """ Perform a get and put with multiple bins including geospatial bin """ key = ('test', 'demo', 'single_geo_put') geo_object_single = aerospike.GeoJSON({ "type": "Point", "coordinates": [42.34, 58.62] }) geo_object_dict = aerospike.GeoJSON({ "type": "Point", "coordinates": [56.34, 69.62] }) TestGeospatial.client.put( key, { "loc": geo_object_single, "int_bin": 2, "string_bin": "str", "dict_bin": { "a": 1, "b": 2, "geo": geo_object_dict } }) key, meta, bins = TestGeospatial.client.get(key) assert bins == { 'loc': { 'coordinates': [42.34, 58.62], 'type': 'Point' }, "int_bin": 2, "string_bin": "str", "dict_bin": { "a": 1, "b": 2, "geo": { 'coordinates': [56.34, 69.62], 'type': 'Point' } } } TestGeospatial.client.remove(key) def test_geospatial_positive_query(self): """ Perform a positive geospatial query for a polygon """ records = [] query = TestGeospatial.client.query("test", "demo") geo_object2 = aerospike.GeoJSON({ "type": "Polygon", "coordinates": [[[-122.500000, 37.000000], [-121.000000, 37.000000], [-121.000000, 38.080000], [-122.500000, 38.080000], [-122.500000, 37.000000]]] }) query.where(p.geo_within("loc", geo_object2.dumps())) def callback((key, metadata, record)): records.append(record) query.foreach(callback) assert len(records) == 3 assert records == [{ 'loc': { 'coordinates': [-122.0, 37.5], 'type': 'Point' } }, { 'loc': { 'coordinates': [-121.8, 37.7], 'type': 'Point' } }, { 'loc': { 'coordinates': [-121.6, 37.9], 'type': 'Point' } }] def test_geospatial_positive_query_outside_shape(self): """ Perform a positive geospatial query for polygon where all points are outside polygon """ records = [] query = TestGeospatial.client.query("test", "demo") geo_object2 = aerospike.GeoJSON({ "type": "Polygon", "coordinates": [[[-126.500000, 37.000000], [-124.000000, 37.000000], [-124.000000, 38.080000], [-126.500000, 38.080000], [-126.500000, 37.000000]]] }) query.where(p.geo_within("loc", geo_object2.dumps())) def callback((key, metadata, record)): records.append(record) query.foreach(callback) assert len(records) == 0 def test_geospatial_positive_query_without_set(self): """ Perform a positive geospatial query for a polygon without a set """ keys = [] for i in xrange(1, 10): key = ('test', None, i) lng = -122 + (0.2 * i) lat = 37.5 + (0.2 * i) geo_object = aerospike.GeoJSON({ "type": "Point", "coordinates": [lng, lat] }) TestGeospatial.client.put(key, {"loc": geo_object}) keys.append(key) TestGeospatial.client.index_geo2dsphere_create("test", None, "loc", "loc_index_no_set") records = [] query = TestGeospatial.client.query("test", None) geo_object2 = aerospike.GeoJSON({ "type": "Polygon", "coordinates": [[[-122.500000, 37.000000], [-121.000000, 37.000000], [-121.000000, 38.080000], [-122.500000, 38.080000], [-122.500000, 37.000000]]] }) query.where(p.geo_within("loc", geo_object2.dumps())) def callback((key, metadata, record)): records.append(record) query.foreach(callback) TestGeospatial.client.index_remove('test', 'loc_index_no_set') for key in keys: TestGeospatial.client.remove(key) assert len(records) == 2 assert records == [{ 'loc': { 'coordinates': [-121.8, 37.7], 'type': 'Point' } }, { 'loc': { 'coordinates': [-121.6, 37.9], 'type': 'Point' } }] def test_geospatial_positive_query_for_circle(self): """ Perform a positive geospatial query for a circle """ records = [] query = TestGeospatial.client.query("test", "demo") geo_object2 = aerospike.GeoJSON({ "type": "AeroCircle", "coordinates": [[-122.0, 37.5], 250.2] }) query.where(p.geo_within("loc", geo_object2.dumps())) def callback((key, metadata, record)): records.append(record) query.foreach(callback) assert len(records) == 1 assert records == [{ 'loc': { 'coordinates': [-122.0, 37.5], 'type': 'Point' } }] def test_geospatial_operate_positive(self): """ Perform an operate operation with geospatial bin """ geo_object_operate = aerospike.GeoJSON({ "type": "Point", "coordinates": [43.45, 56.75] }) key = ('test', 'demo', 'single_geo_operate') list = [{ "op": aerospike.OPERATOR_WRITE, "bin": "write_bin", "val": { "no": geo_object_operate } }, { "op": aerospike.OPERATOR_READ, "bin": "write_bin" }] key, meta, bins = TestGeospatial.client.operate(key, list) assert bins == { 'write_bin': { 'no': { 'coordinates': [43.45, 56.75], 'type': 'Point' } } } TestGeospatial.client.remove(key) def test_geospatial_object_not_dict_or_string(self): """ The geospatial object is not a dictionary or string """ try: geo_object_wrong = aerospike.GeoJSON(1) except ParamError as exception: assert exception.code == -2 assert exception.msg == 'Geospatial data should be a dictionary or raw GeoJSON string' def test_geospatial_object_non_json_serialziable_string(self): """ The geospatial object is not a json serializable string """ try: geo_object_wrong = aerospike.GeoJSON("abc") except ClientError as exception: assert exception.code == -1 assert exception.msg == 'String is not GeoJSON serializable' def test_geospatial_wrap_positive(self): """ Perform a positive wrap on geospatial data """ self.geo_object.wrap({ "type": "Polygon", "coordinates": [[[-122.500000, 37.000000], [-121.000000, 37.000000], [-121.000000, 38.080000], [-122.500000, 38.080000], [-122.500000, 37.000000]]] }) assert self.geo_object.unwrap() == { 'coordinates': [[[-122.5, 37.0], [-121.0, 37.0], [-121.0, 38.08], [-122.5, 38.08], [-122.5, 37.0]]], 'type': 'Polygon' } def test_geospatial_object_wrap_non_dict(self): """ The geospatial object provided to wrap() is not a dictionary """ try: self.geo_object.wrap("abc") except ParamError as exception: assert exception.code == -2 assert exception.msg == 'Geospatial data should be a dictionary or raw GeoJSON string' def test_geospatial_wrap_positive_with_query(self): """ Perform a positive wrap on geospatial data followed by a query """ geo_object_wrap = aerospike.GeoJSON({ "type": "Polygon", "coordinates": [[[-124.500000, 37.000000], [-125.000000, 37.000000], [-121.000000, 38.080000], [-122.500000, 38.080000], [-124.500000, 37.000000]]] }) geo_object_wrap.wrap({ "type": "Polygon", "coordinates": [[[-122.500000, 37.000000], [-121.000000, 37.000000], [-121.000000, 38.080000], [-122.500000, 38.080000], [-122.500000, 37.000000]]] }) assert geo_object_wrap.unwrap() == { 'coordinates': [[[-122.5, 37.0], [-121.0, 37.0], [-121.0, 38.08], [-122.5, 38.08], [-122.5, 37.0]]], 'type': 'Polygon' } records = [] query = TestGeospatial.client.query("test", "demo") query.where(p.geo_within("loc", geo_object_wrap.dumps())) def callback((key, metadata, record)): records.append(record) query.foreach(callback) assert len(records) == 3 assert records == [{ 'loc': { 'coordinates': [-122.0, 37.5], 'type': 'Point' } }, { 'loc': { 'coordinates': [-121.8, 37.7], 'type': 'Point' } }, { 'loc': { 'coordinates': [-121.6, 37.9], 'type': 'Point' } }] def test_geospatial_loads_positive(self): """ Perform a positive loads on geoJSON raw string """ self.geo_object.loads( '{"type": "Polygon", "coordinates": [[[-122.500000, 37.000000], [-121.000000, 37.000000], [-121.000000, 38.080000],[-122.500000, 38.080000], [-122.500000, 37.000000]]]}' ) assert self.geo_object.unwrap() == { 'coordinates': [[[-122.5, 37.0], [-121.0, 37.0], [-121.0, 38.08], [-122.5, 38.08], [-122.5, 37.0]]], 'type': 'Polygon' } def test_geospatial_object_loads_non_dict(self): """ The geospatial object provided to loads() is not a dictionary """ try: self.geo_object.loads('{"abc"}') except ClientError as exception: assert exception.code == -1 assert exception.msg == 'String is not GeoJSON serializable' def test_geospatial_loads_positive_with_query(self): """ Perform a positive loads on geoJSON raw string followed by a query """ geo_object_loads = aerospike.GeoJSON({ "type": "Polygon", "coordinates": [[[-124.500000, 37.000000], [-125.000000, 37.000000], [-121.000000, 38.080000], [-122.500000, 38.080000], [-124.500000, 37.000000]]] }) geo_object_loads.loads( '{"type": "Polygon", "coordinates": [[[-122.500000, 37.000000], [-121.000000, 37.000000], [-121.000000, 38.080000],[-122.500000, 38.080000], [-122.500000, 37.000000]]]}' ) assert geo_object_loads.unwrap() == { 'coordinates': [[[-122.5, 37.0], [-121.0, 37.0], [-121.0, 38.08], [-122.5, 38.08], [-122.5, 37.0]]], 'type': 'Polygon' } records = [] query = TestGeospatial.client.query("test", "demo") query.where(p.geo_within("loc", geo_object_loads.dumps())) def callback((key, metadata, record)): records.append(record) query.foreach(callback) assert len(records) == 3 assert records == [{ 'loc': { 'coordinates': [-122.0, 37.5], 'type': 'Point' } }, { 'loc': { 'coordinates': [-121.8, 37.7], 'type': 'Point' } }, { 'loc': { 'coordinates': [-121.6, 37.9], 'type': 'Point' } }] def test_geospatial_dumps_positive(self): """ Perform a positive dumps. Verify using str """ assert self.geo_object.dumps( ) == '{"type": "Point", "coordinates": [-120.2, 39.3]}' assert str(self.geo_object ) == '{"type": "Point", "coordinates": [-120.2, 39.3]}' def test_geospatial_repr_positive(self): """ Perform a positive repr. Verify using eval() """ assert repr(self.geo_object ) == '\'{"type": "Point", "coordinates": [-120.2, 39.3]}\'' assert eval(repr(self.geo_object) ) == '{"type": "Point", "coordinates": [-120.2, 39.3]}' def test_geospatial_put_get_positive_with_geodata(self): """ Perform a get and put with multiple bins including geospatial bin using geodata method """ key = ('test', 'demo', 'single_geo_put') geo_object_single = aerospike.geodata({ "type": "Point", "coordinates": [42.34, 58.62] }) geo_object_dict = aerospike.geodata({ "type": "Point", "coordinates": [56.34, 69.62] }) TestGeospatial.client.put( key, { "loc": geo_object_single, "int_bin": 2, "string_bin": "str", "dict_bin": { "a": 1, "b": 2, "geo": geo_object_dict } }) key, meta, bins = TestGeospatial.client.get(key) assert bins == { 'loc': { 'coordinates': [42.34, 58.62], 'type': 'Point' }, "int_bin": 2, "string_bin": "str", "dict_bin": { "a": 1, "b": 2, "geo": { 'coordinates': [56.34, 69.62], 'type': 'Point' } } } TestGeospatial.client.remove(key) def test_geospatial_put_get_positive_with_geojson(self): """ Perform a get and put with multiple bins including geospatial bin using geodata method """ key = ('test', 'demo', 'single_geo_put') geo_object_single = aerospike.geojson( '{"type": "Point", "coordinates": [42.34, 58.62] }') geo_object_dict = aerospike.geojson( '{"type": "Point", "coordinates": [56.34, 69.62] }') TestGeospatial.client.put( key, { "loc": geo_object_single, "int_bin": 2, "string_bin": "str", "dict_bin": { "a": 1, "b": 2, "geo": geo_object_dict } }) key, meta, bins = TestGeospatial.client.get(key) assert bins == { 'loc': { 'coordinates': [42.34, 58.62], 'type': 'Point' }, "int_bin": 2, "string_bin": "str", "dict_bin": { "a": 1, "b": 2, "geo": { 'coordinates': [56.34, 69.62], 'type': 'Point' } } } TestGeospatial.client.remove(key) def test_geospatial_positive_query_with_geodata(self): """ Perform a positive geospatial query for a polygon with geodata """ records = [] query = TestGeospatial.client.query("test", "demo") geo_object2 = aerospike.geodata({ "type": "Polygon", "coordinates": [[[-122.500000, 37.000000], [-121.000000, 37.000000], [-121.000000, 38.080000], [-122.500000, 38.080000], [-122.500000, 37.000000]]] }) query.where(p.geo_within("loc", geo_object2.dumps())) def callback((key, metadata, record)): records.append(record) query.foreach(callback) assert len(records) == 3 assert records == [{ 'loc': { 'coordinates': [-122.0, 37.5], 'type': 'Point' } }, { 'loc': { 'coordinates': [-121.8, 37.7], 'type': 'Point' } }, { 'loc': { 'coordinates': [-121.6, 37.9], 'type': 'Point' } }] def test_geospatial_positive_query_with_geojson(self): """ Perform a positive geospatial query for a polygon with geojson """ records = [] query = TestGeospatial.client.query("test", "demo") geo_object2 = aerospike.geojson( '{"type": "Polygon", "coordinates": [[[-122.500000, 37.000000], [-121.000000, 37.000000], [-121.000000, 38.080000],[-122.500000, 38.080000], [-122.500000, 37.000000]]]}' ) query.where(p.geo_within("loc", geo_object2.dumps())) def callback((key, metadata, record)): records.append(record) query.foreach(callback) assert len(records) == 3 assert records == [{ 'loc': { 'coordinates': [-122.0, 37.5], 'type': 'Point' } }, { 'loc': { 'coordinates': [-121.8, 37.7], 'type': 'Point' } }, { 'loc': { 'coordinates': [-121.6, 37.9], 'type': 'Point' } }] def test_geospatial_2dindex_positive(self): """ Perform a positive 2d index creation """ status = TestGeospatial.client.index_remove('test', 'loc_index') assert status == 0 status = TestGeospatial.client.index_geo2dsphere_create( "test", "demo", "loc", "loc_index") assert status == 0 def test_geospatial_2dindex_positive_with_policy(self): """ Perform a positive 2d index creation with policy """ status = TestGeospatial.client.index_remove('test', 'loc_index') assert status == 0 status = TestGeospatial.client.index_geo2dsphere_create( "test", "demo", "loc", "loc_index", {"timeout": 2000}) assert status == 0 def test_geospatial_2dindex_set_length_extra(self): """ Perform a 2d creation with set length exceeding limit """ set_name = 'a' for i in xrange(100): set_name = set_name + 'a' status = TestGeospatial.client.index_remove('test', 'loc_index') assert status == 0 try: status = TestGeospatial.client.index_geo2dsphere_create( "test", set_name, "loc", "loc_index") except InvalidRequest as exception: assert exception.code == 4 assert exception.msg == "Invalid Set Name"
class TestQueryRole(TestBaseClass): pytestmark = pytest.mark.skipif( TestBaseClass().get_hosts()[1] == None, reason="No user specified, may be not secured cluster.") def setup_method(self, method): """ Setup method """ hostlist, user, password = TestBaseClass().get_hosts() config = {"hosts": hostlist} self.client = aerospike.client(config).connect(user, password) try: self.client.admin_drop_role("usr-sys-admin") except: pass usr_sys_admin_privs = [{ "code": aerospike.PRIV_USER_ADMIN }, { "code": aerospike.PRIV_SYS_ADMIN }] try: self.client.admin_drop_role("usr-sys-admin-test") except: pass self.client.admin_create_role("usr-sys-admin-test", usr_sys_admin_privs) self.delete_users = [] time.sleep(1) def teardown_method(self, method): """ Teardown method """ policy = {} self.client.admin_drop_role("usr-sys-admin-test") self.client.close() def test_admin_query_role_no_parameters(self): """ Query role with no parameters """ with pytest.raises(TypeError) as typeError: self.client.admin_query_role() assert "Required argument 'role' (pos 1) not found" in typeError.value 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': '' }] def test_admin_query_role_positive_with_policy(self): """ Query role positive policy """ roles = self.client.admin_query_role("usr-sys-admin-test", {'timeout': 1000}) assert roles == [{ 'code': 0, 'ns': '', 'set': '' }, { 'code': 1, 'ns': '', 'set': '' }] def test_admin_query_role_incorrect_role_name(self): """ Incorrect role name """ try: self.client.admin_query_role("usr-sys-admin-test-non-existent", {'timeout': 1000}) except InvalidRole as exception: assert exception.code == 70 assert exception.msg == "AEROSPIKE_INVALID_ROLE" def test_admin_query_role_incorrect_role_type(self): """ Incorrect role type """ try: self.client.admin_query_role(None, {'timeout': 1000}) except ParamError as exception: assert exception.code == -2 assert exception.msg == "Role name should be a string"
class TestCreateUser(TestBaseClass): pytestmark = pytest.mark.skipif( TestBaseClass().get_hosts()[1] == None, reason="No user specified, may be not secured cluster.") def setup_method(self, method): """ Setup method """ hostlist, user, password = TestBaseClass().get_hosts() config = {"hosts": hostlist} self.client = aerospike.client(config).connect(user, password) self.delete_users = [] def teardown_method(self, method): """ Teardown method """ policy = {} for user in self.delete_users: self.client.admin_drop_user(user, policy) self.client.close() def test_create_user_without_any_parameters(self): with pytest.raises(TypeError) as typeError: self.client.admin_create_user() assert "Required argument 'user' (pos 1) not found" in typeError.value def test_create_user_with_proper_parameters(self): policy = {"timeout": 1000} user = "******" password = "******" roles = ["read", "read-write", "sys-admin"] try: self.client.admin_drop_user(user, policy) except: pass status = self.client.admin_create_user(user, password, roles, policy) time.sleep(2) assert status == 0 user_details = self.client.admin_query_user(user, policy) assert user_details == ['read', 'read-write', 'sys-admin'] self.delete_users.append('user1-test') def test_create_user_with_proper_parameters_without_policy(self): policy = {"timeout": 1000} user = "******" password = "******" roles = ["read", "read-write", "sys-admin"] try: self.client.admin_drop_user(user, policy) except: pass status = self.client.admin_create_user(user, password, roles) time.sleep(2) assert status == 0 user_details = self.client.admin_query_user(user, policy) assert user_details == ['read', 'read-write', 'sys-admin'] self.delete_users.append('user1-test') def test_create_user_with_invalid_timeout_policy_value(self): policy = {"timeout": 0.1} user = "******" password = "******" roles = ['sys-admin'] try: self.client.admin_drop_user(user, policy) except: pass try: status = self.client.admin_create_user(user, password, roles, policy) except ParamError as exception: assert exception.code == -2 assert exception.msg == "timeout is invalid" def test_create_user_with_proper_timeout_policy_value(self): policy = {'timeout': 20} user = "******" password = "******" roles = ["read-write", "sys-admin"] try: self.client.admin_drop_user(user, policy) except: pass status = self.client.admin_create_user(user, password, roles, policy) time.sleep(2) assert status == 0 user_details = self.client.admin_query_user(user) assert user_details == ['read-write', 'sys-admin'] self.delete_users.append('user2-test') def test_create_user_with_none_username(self): policy = {'timeout': 20} user = None password = "******" roles = ["sys-admin"] try: status = self.client.admin_create_user(user, password, roles, policy) except ParamError as exception: assert exception.code == -2 assert exception.msg == "Username should be a string" def test_create_user_with_empty_username(self): policy = {} user = "" password = "******" roles = ["read-write"] try: status = self.client.admin_create_user(user, password, roles, policy) except InvalidUser as exception: assert exception.code == 60 assert exception.msg == "AEROSPIKE_INVALID_USER" def test_create_user_with_special_characters_in_username(self): policy = {} user = "******" password = "******" roles = ["read-write"] try: self.client.admin_drop_user(user, policy) except: pass status = self.client.admin_create_user(user, password, roles, policy) assert status == 0 self.delete_users.append(user) def test_create_user_with_none_password(self): policy = {} user = "******" password = None roles = ["sys-admin"] try: status = self.client.admin_create_user(user, password, roles, policy) except ParamError as exception: assert exception.code == -2 assert exception.msg == "Password should be a string" def test_create_user_with_empty_string_as_password(self): policy = {} user = "******" password = "" roles = ["read-write"] try: self.client.admin_drop_user(user, policy) except: pass status = self.client.admin_create_user(user, password, roles, policy) assert status == 0 time.sleep(2) self.delete_users.append(user) def test_create_user_with_special_characters_in_password(self): policy = {} user = "******" password = "******" roles = ["sys-admin"] try: self.client.admin_drop_user(user, policy) except: pass status = self.client.admin_create_user(user, password, roles, policy) assert status == 0 self.delete_users.append(user) def test_create_user_with_too_long_username(self): policy = {} user = "******" * 1000 password = "******" roles = ["sys-admin"] try: self.client.admin_drop_user(user, policy) except: pass try: status = self.client.admin_create_user(user, password, roles, policy) except InvalidUser as exception: assert exception.code == 60 assert exception.msg == "AEROSPIKE_INVALID_USER" def test_create_user_with_too_long_password(self): policy = {'timeout': 1000} user = "******" password = "******" * 1000 roles = ["read-write"] try: self.client.admin_drop_user(user, policy) except: pass status = self.client.admin_create_user(user, password, roles, policy) assert status == 0 time.sleep(1) user_details = self.client.admin_query_user(user, policy) assert user_details == ['read-write'] self.delete_users.append(user) def test_create_user_with_empty_roles_list(self): policy = {} user = "******" password = "******" roles = [] try: self.client.admin_drop_user(user, policy) except: pass try: status = self.client.admin_create_user(user, password, roles, policy) except InvalidRole as exception: assert exception.code == 70 assert exception.msg == "AEROSPIKE_INVALID_ROLE" def test_create_user_with_non_user_admin_user(self): policy = {} user = "******" password = "******" roles = ["read-write"] try: self.client.admin_drop_user(user, policy) except: pass status = self.client.admin_create_user(user, password, roles, policy) assert status == 0 config = {"hosts": TestCreateUser.hostlist} non_admin_client = None try: non_admin_client = aerospike.client(config).connect( "non_admin_test", "non_admin_test") status = non_admin_client.admin_create_user( "user78", password, roles, policy) if non_admin_client: non_admin_client.close() except RoleViolation as exception: assert exception.code == 81 self.delete_users.append("non_admin_test")
class TestQueryUser(TestBaseClass): pytestmark = pytest.mark.skipif( TestBaseClass().get_hosts()[1] == None, reason="No user specified, may be not secured cluster.") def setup_method(self, method): """ Setup method """ hostlist, user, password = TestBaseClass().get_hosts() config = {"hosts": hostlist} TestQueryUser.Me = self self.client = aerospike.client(config).connect(user, password) try: self.client.admin_drop_user("example-test") except: pass policy = {} user = "******" password = "******" roles = ["read-write", "sys-admin", "read"] status = self.client.admin_create_user(user, password, roles, policy) self.delete_users = [] def teardown_method(self, method): """ Teardown method """ policy = {} self.client.admin_drop_user("example-test", policy) self.client.close() def test_query_user_without_any_parameters(self): with pytest.raises(TypeError) as typeError: self.client.admin_query_user() assert "Required argument 'user' (pos 1) not found" in typeError.value def test_query_user_with_proper_parameters(self): policy = {} user = "******" time.sleep(2) user_details = self.client.admin_query_user(user) assert user_details == ['read', 'read-write', 'sys-admin'] def test_query_user_with_invalid_timeout_policy_value(self): policy = {"timeout": 0.1} user = "******" try: status = self.client.admin_query_user(user, policy) except ParamError as exception: assert exception.code == -2 assert exception.msg == "timeout is invalid" def test_query_user_with_proper_timeout_policy_value(self): policy = {'timeout': 30} user = "******" time.sleep(2) user_details = self.client.admin_query_user(user, policy) assert user_details == ['read', 'read-write', 'sys-admin'] def test_query_user_with_none_username(self): policy = {'timeout': 30} user = None try: user_details = self.client.admin_query_user(user, policy) except ParamError as exception: assert exception.code == -2 assert exception.msg == "Username should be a string" def test_query_user_with_empty_username(self): policy = {} user = "" try: status = self.client.admin_query_user(user, policy) except InvalidUser as exception: assert exception.code == 60 assert exception.msg == "AEROSPIKE_INVALID_USER" def test_query_user_with_nonexistent_username(self): policy = {} user = "******" try: status = self.client.admin_query_user(user, policy) except InvalidUser as exception: assert exception.code == 60 assert exception.msg == "AEROSPIKE_INVALID_USER" def test_query_user_with_no_roles(self): policy = {} user = "******" roles = ["sys-admin", "read", "read-write"] status = self.client.admin_revoke_roles(user, roles, policy) assert status == 0 time.sleep(2) user_details = self.client.admin_query_user(user) assert user_details == [] def test_query_user_with_extra_argument(self): """ Invoke query_user() with extra argument. """ policy = {'timeout': 1000} with pytest.raises(TypeError) as typeError: self.client.admin_query_user("foo", policy, "") assert "admin_query_user() takes at most 2 arguments (3 given)" in typeError.value def test_query_user_with_policy_as_string(self): """ Invoke query_user() with policy as string """ policy = "" try: self.client.admin_query_user("foo", policy) except AerospikeError as exception: assert exception.code == -2L assert exception.msg == "policy must be a dict"
def setup_class(cls): KVTestCase.hostlist, KVTestCase.user, KVTestCase.password = TestBaseClass.get_hosts( )
class TestChangePassword(TestBaseClass): pytestmark = pytest.mark.skipif( TestBaseClass().get_hosts()[1] == None, reason="No user specified, may be not secured cluster.") def setup_method(self, method): """ Setup method """ hostlist, user, password = TestBaseClass().get_hosts() config = {"hosts": hostlist} self.client = aerospike.client(config).connect(user, password) try: self.client.admin_create_user( "testchangepassworduser", "aerospike", ["read"], {}) time.sleep(2) except UserExistsError: pass # we are good, no such role exists self.delete_users = [] def teardown_method(self, method): """ Teardown method """ self.client.admin_drop_user( "testchangepassworduser" ) self.client.close() def test_change_password_without_any_parameters(self): with pytest.raises(TypeError) as typeError: status = self.client.admin_change_password() assert "Required argument 'user' (pos 1) not found" in typeError.value def test_change_password_with_proper_parameters(self): user = "******" config = {"hosts": TestChangePassword.hostlist} self.clientreaduser = aerospike.client(config).connect(user, "aerospike") policy = {} password = "******" status = self.clientreaduser.admin_change_password( user, password ) assert status == 0 config = { "hosts": TestChangePassword.hostlist } try: self.clientreaduserwrong = aerospike.client(config).connect( user, "aerospike" ) except InvalidPassword as exception: assert exception.code == 62 assert exception.msg == None except ClientError as exception: assert exception.code == -1 assert exception.msg == "Failed to seed cluster" self.clientreaduserright = aerospike.client(config).connect( user, "newpassword") assert self.clientreaduserright != None self.clientreaduserright.close() self.clientreaduser.close() def test_change_password_with_invalid_timeout_policy_value(self): policy = {'timeout': 0.1} user = "******" password = "******" try: status = self.client.admin_change_password( user, password, policy ) except ParamError as exception: assert exception.code == -2 assert exception.msg == "timeout is invalid" def test_change_password_with_proper_timeout_policy_value(self): user = "******" config = {"hosts": TestChangePassword.hostlist} self.clientreaduser = aerospike.client(config).connect(user, "aerospike") policy = {'timeout': 100} password = "******" status = self.clientreaduser.admin_change_password( user, password, policy ) assert status == 0 config = { "hosts": TestChangePassword.hostlist } try: self.clientreaduserwrong = aerospike.client(config).connect( user, "aerospike" ) except InvalidPassword as exception: assert exception.code == 62 assert exception.msg == None except ClientError as exception: assert exception.code == -1 assert exception.msg == "Failed to seed cluster" self.clientreaduserright = aerospike.client(config).connect( user, "newpassword") assert self.clientreaduserright != None self.clientreaduserright.close() self.clientreaduser.close() def test_change_password_with_none_username(self): policy = {} user = None password = "******" try: status = self.client.admin_change_password( user, password, policy ) except ParamError as exception: assert exception.code == -2 assert exception.msg == "Username should be a string" def test_change_password_with_none_password(self): policy = {} user = "******" password = None try: status = self.client.admin_change_password( user, password, policy ) except ParamError as exception: assert exception.code == -2 assert exception.msg == "Password should be a string" def test_change_password_with_non_existent_user(self): policy = {} user = "******" password = "******" try: status = self.client.admin_change_password( user, password, policy ) except InvalidUser as exception: assert exception.code == 60 assert exception.msg == "AEROSPIKE_INVALID_USER" def test_change_password_with_too_long_password(self): user = "******" config = {"hosts": TestChangePassword.hostlist} self.clientreaduser = aerospike.client(config).connect(user, "aerospike") policy = {'timeout': 100} password = "******" * 1000 status = self.clientreaduser.admin_change_password( user, password, policy ) assert status == 0 config = { "hosts": TestChangePassword.hostlist } try: self.clientreaduserwrong = aerospike.client(config).connect( user, "aerospike" ) except InvalidPassword as exception: assert exception.code == 62 assert exception.msg == None except ClientError as exception: assert exception.code == -1 assert exception.msg == "Failed to seed cluster" self.clientreaduserright = aerospike.client(config).connect(user, password) assert self.clientreaduserright != None self.clientreaduserright.close() self.clientreaduser.close()
class TestDropRole(TestBaseClass): pytestmark = pytest.mark.skipif( TestBaseClass().get_hosts()[1] == None, reason="No user specified, may be not secured cluster.") def setup_method(self, method): """ Setup method """ hostlist, user, password = TestBaseClass().get_hosts() config = {"hosts": hostlist} self.client = aerospike.client(config).connect(user, password) self.delete_users = [] def teardown_method(self, method): """ Teardown method """ policy = {} for user in self.delete_users: self.client.admin_drop_user(user, policy) self.client.close() def test_drop_role_without_any_parameters(self): with pytest.raises(TypeError) as typeError: self.client.admin_drop_role() assert "Required argument 'role' (pos 1) not found" in typeError.value def test_drop_role_positive_with_policy(self): """ Drop role positive with policy """ try: self.client.admin_query_role("usr-sys-admin-test") # role exists, clear it out. self.client.admin_drop_role("usr-sys-admin-test") except 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': 1000}) time.sleep(1) roles = self.client.admin_query_role("usr-sys-admin-test") assert roles == [{"code": 10, "ns": "test", "set": "demo"}] try: self.client.admin_query_user("testcreaterole") # user exists, clear it out. self.client.admin_drop_user("testcreaterole") except AdminError: pass # we are good, no such user exists status = self.client.admin_create_user("testcreaterole", "createrole", ["usr-sys-admin-test"]) assert status == 0 time.sleep(1) users = self.client.admin_query_user("testcreaterole") assert users == ["usr-sys-admin-test"] status = self.client.admin_drop_role("usr-sys-admin-test", {'timeout': 1000}) assert status == 0 users = self.client.admin_query_user("testcreaterole") assert users == [] self.client.admin_drop_user("testcreaterole") def test_drop_role_positive(self): """ Drop role positive """ try: self.client.admin_query_role("usr-sys-admin-test") # role exists, clear it out. self.client.admin_drop_role("usr-sys-admin-test") except InvalidRole: pass # we are good, no such role exists status = self.client.admin_create_role( "usr-sys-admin-test", [{ "code": aerospike.PRIV_USER_ADMIN }, { "code": aerospike.PRIV_SYS_ADMIN }]) time.sleep(1) roles = self.client.admin_query_role("usr-sys-admin-test") assert roles == [{ "code": 0, "ns": "", "set": "" }, { "code": 1, "ns": "", "set": "" }] try: self.client.admin_query_user("testcreaterole") # user exists, clear it out. self.client.admin_drop_user("testcreaterole") except AdminError: pass # we are good, no such user exists status = self.client.admin_create_user("testcreaterole", "createrole", ["usr-sys-admin-test"]) assert status == 0 time.sleep(1) users = self.client.admin_query_user("testcreaterole") assert users == ["usr-sys-admin-test"] status = self.client.admin_drop_role("usr-sys-admin-test") assert status == 0 users = self.client.admin_query_user("testcreaterole") assert users == [] self.client.admin_drop_user("testcreaterole") def test_drop_non_existent_role(self): """ Drop non-existent role """ try: self.client.admin_drop_role("usr-sys-admin-test") except InvalidRole as exception: assert exception.code == 70 assert exception.msg == "AEROSPIKE_INVALID_ROLE" def test_drop_role_rolename_None(self): """ Drop role with role name None """ try: self.client.admin_drop_role(None) except ParamError as exception: assert exception.code == -2 assert exception.msg == "Role name should be a string" 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 }]) assert status == 0 try: self.client.admin_drop_role("usr-sys-admin-test", {"timeout": 0.2}) except ParamError as exception: assert exception.code == -2 assert exception.msg == 'timeout is invalid' self.client.admin_drop_role("usr-sys-admin-test")
def setup_class(cls): hostlist, user, password = TestBaseClass.get_hosts() config = {'hosts': hostlist} TestConnect.config = config
class TestRevokePrivilege(TestBaseClass): pytestmark = pytest.mark.skipif( TestBaseClass().get_hosts()[1] == None, reason="No user specified, may be not secured cluster.") def setup_method(self, method): """ Setup method """ hostlist, user, password = TestBaseClass().get_hosts() config = {"hosts": hostlist} self.client = aerospike.client(config).connect(user, password) try: self.client.admin_drop_role("usr-sys-admin-test") except: pass self.client.admin_create_role("usr-sys-admin-test", [{ "code": aerospike.PRIV_USER_ADMIN }, { "code": aerospike.PRIV_SYS_ADMIN }]) self.delete_users = [] def teardown_method(self, method): """ Teardown method """ policy = {} self.client.admin_drop_role("usr-sys-admin-test") self.client.close() def test_admin_revoke_privileges_no_parameters(self): """ Revoke privileges with no parameters """ with pytest.raises(TypeError) as typeError: self.client.admin_revoke_privileges() assert "Required argument 'role' (pos 1) not found" in typeError.value def test_admin_revoke_privileges_positive(self): """ revoke privileges positive """ status = self.client.admin_grant_privileges( "usr-sys-admin-test", [{ "code": aerospike.PRIV_READ }]) 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': '' }, { 'code': 10, 'ns': '', 'set': '' }] status = self.client.admin_revoke_privileges( "usr-sys-admin-test", [{ "code": aerospike.PRIV_READ }]) 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': '' }] def test_admin_revoke_privileges_positive_with_policy(self): """ Revoke privileges positive with policy """ status = self.client.admin_grant_privileges( "usr-sys-admin-test", [{ "code": aerospike.PRIV_READ }], {'timeout': 1000}) 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': '' }, { 'code': 10, 'ns': '', 'set': '' }] status = self.client.admin_revoke_privileges( "usr-sys-admin-test", [{ "code": aerospike.PRIV_READ }], {'timeout': 1000}) time.sleep(1) assert status == 0 roles = self.client.admin_query_role("usr-sys-admin-test") assert roles == [{ 'code': 0, 'ns': '', 'set': '' }, { 'code': 1, 'ns': '', 'set': '' }] def test_admin_revoke_privileges_positive_with_ns_set(self): """ Revoke privileges positive with ns and set """ status = self.client.admin_grant_privileges( "usr-sys-admin-test", [{ "code": aerospike.PRIV_READ, "ns": "test", "set": "demo" }]) 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': '' }, { 'code': 10, 'ns': 'test', 'set': 'demo' }] status = self.client.admin_revoke_privileges( "usr-sys-admin-test", [{ "code": aerospike.PRIV_READ, "ns": "test", "set": "demo" }]) time.sleep(1) assert status == 0 roles = self.client.admin_query_role("usr-sys-admin-test") assert roles == [{ 'code': 0, 'ns': '', 'set': '' }, { 'code': 1, 'ns': '', 'set': '' }] def test_revoke_privileges_incorrect_role_type(self): """ role name not string """ try: self.client.admin_revoke_privileges( 1, [{ "code": aerospike.PRIV_USER_ADMIN }]) except ParamError as exception: assert exception.code == -2 assert exception.msg == "Role name should be a string" def test_revoke_privileges_unknown_privilege_type(self): """ privilege type unknown """ try: self.client.admin_revoke_privileges("usr-sys-admin-test", [{ "code": 64 }]) except InvalidPrivilege as exception: assert exception.code == 72 def test_revoke_privileges_incorrect_privilege_type(self): """ privilege type incorrect """ try: self.client.admin_revoke_privileges("usr-sys-admin-test", None) except ParamError as exception: assert exception.code == -2 assert exception.msg == "Privileges should be a list" def test_revoke_privileges_empty_list_privileges(self): """ privilege type is an empty list """ try: self.client.admin_revoke_privileges("usr-sys-admin-test", []) except InvalidPrivilege as exception: assert exception.code == 72 assert exception.msg == 'AEROSPIKE_INVALID_PRIVILEGE'
class TestSetPassword(TestBaseClass): pytestmark = pytest.mark.skipif( TestBaseClass().get_hosts()[1] == None, reason="No user specified, may be not secured cluster.") def setup_method(self, method): """ Setup method """ hostlist, user, password = TestBaseClass().get_hosts() config = {"hosts": hostlist} TestSetPassword.Me = self self.client = aerospike.client(config).connect(user, password) try: self.client.admin_drop_user("testsetpassworduser") except: pass self.client.admin_create_user( "testsetpassworduser", "aerospike", ["read"], {}) self.delete_users = [] def teardown_method(self, method): """ Teardown method """ self.client.admin_drop_user( "testsetpassworduser" ) self.client.close() def test_set_password_without_any_parameters(self): with pytest.raises(TypeError) as typeError: status = self.client.admin_set_password() assert "Required argument 'user' (pos 1) not found" in typeError.value def test_set_password_with_proper_parameters(self): policy = {'timeout': 50} user = "******" password = "******" status = self.client.admin_set_password( user, password ) assert status == 0 def test_set_password_with_invalid_timeout_policy_value(self): policy = {'timeout': 0.1} user = "******" password = "******" try: status = self.client.admin_set_password( user, password, policy ) except ParamError as exception: assert exception.code == -2 assert exception.msg == "timeout is invalid" def test_set_password_with_proper_timeout_policy_value(self): policy = {'timeout': 50} user = "******" password = "******" status = self.client.admin_set_password( user, password, policy ) assert status == 0 def test_set_password_with_none_username(self): policy = {} user = None password = "******" try: status = self.client.admin_set_password( user, password ) except ParamError as exception: assert exception.code == -2 assert exception.msg == "Username should be a string" def test_set_password_with_none_password(self): policy = {} user = "******" password = None try: status = self.client.admin_set_password( user, password ) except ParamError as exception: assert exception.code == -2 assert exception.msg == "Password should be a string" def test_set_password_with_non_existent_user(self): policy = {} user = "******" password = "******" try: status = self.client.admin_set_password( user, password, policy ) except InvalidUser as exception: assert exception.code == 60 assert exception.msg == "AEROSPIKE_INVALID_USER" def test_set_password_with_too_long_password(self): policy = {} user = "******" password = "******" * 1000 status = self.client.admin_set_password( user, password, policy ) assert status == 0
def setup_class(cls): hostlist, user, password = TestBaseClass.get_hosts()