def test_hosts_get_with_key_value_filters(self): project_id = self.make_project('project_1', foo='P1', zoo='P2') cloud_id = self.make_cloud(project_id, 'cloud_1') region_id = self.make_region(project_id, cloud_id, 'region_1', foo='R1') host1 = self.make_host(project_id, cloud_id, region_id, 'www.example.xyz', IPAddress(u'10.1.2.101'), 'server') variables = {"key1": "example1", "key2": "Tom"} dbapi.variables_update_by_resource_id( self.context, "hosts", host1, variables ) # Second host with own variables host2 = self.make_host(project_id, cloud_id, region_id, 'www.example2.xyz', IPAddress(u'10.1.2.102'), 'server') variables = {"key1": "example2", "key2": "Tom"} dbapi.variables_update_by_resource_id( self.context, "hosts", host2, variables ) filters = {"vars": "key1:example2"} res, _ = dbapi.hosts_get_all(self.context, filters, default_pagination) self.assertEqual(len(res), 1) self.assertEqual('www.example2.xyz', res[0].name) filters = {"vars": "key2:Tom"} res, _ = dbapi.hosts_get_all(self.context, filters, default_pagination) self.assertEqual(len(res), 2)
def test_cells_get_all_with_filters_noexist(self): res = dbapi.cells_create(self.context, cell1) variables = {"key1": "value1", "key2": "value2"} dbapi.variables_update_by_resource_id(self.context, "cells", res.id, variables) filters = {} filters["vars"] = "key2:value5" res, _ = dbapi.cells_get_all(self.context, filters, default_pagination) self.assertEqual(len(res), 0)
def test_project_variables_update_does_update_variables(self): create_res = dbapi.projects_create(self.context, project1) res = dbapi.projects_get_by_id(self.context, create_res.id) self.assertEqual(res.variables, {}) variables = {"key1": "value1", "key2": "value2"} res = dbapi.variables_update_by_resource_id(self.context, "projects", res.id, variables) self.assertEqual(res.variables, variables) new_variables = {"key1": "tom", "key2": "cat"} res = dbapi.variables_update_by_resource_id(self.context, "projects", res.id, new_variables) self.assertEqual(res.variables, new_variables)
def test_cells_get_all_with_filters(self): res = dbapi.cells_create(self.context, cell1) variables = {"key1": "value1", "key2": "value2"} dbapi.variables_update_by_resource_id(self.context, "cells", res.id, variables) filters = { "vars": "key2:value2", "region_id": cell1["region_id"], } res, _ = dbapi.cells_get_all(self.context, filters, default_pagination) self.assertEqual(len(res), 1) self.assertEqual(res[0]['name'], 'cell1')
def test_regions_get_all_with_var_filters_noexist(self): res = dbapi.regions_create(self.context, region1) variables = {"key1": "value1", "key2": "value2"} dbapi.variables_update_by_resource_id( self.context, "regions", res.id, variables ) filters = {} filters["vars"] = "key1:value12" regions, _ = dbapi.regions_get_all( self.context, filters, default_pagination, ) self.assertEqual(len(regions), 0)
def test_clouds_get_all_with_var_filters(self): res = dbapi.clouds_create(self.context, cloud1) variables = {"key1": "value1", "key2": "value2"} dbapi.variables_update_by_resource_id(self.context, "clouds", res.id, variables) filters = {} filters["vars"] = "key1:value1" clouds, _ = dbapi.clouds_get_all( self.context, filters, default_pagination, ) self.assertEqual(len(clouds), 1) self.assertEqual(clouds[0].name, cloud1['name'])
def test_variables_update_by_resource_id_modify_existing(self): existing_variables = { "key1": "value1", "key2": "value2", "key3": "value3", } update_variables = { "key3": "newvalue3", "key4": "value4", } result_variables = deepcopy(existing_variables) result_variables.update(deepcopy(update_variables)) resource_id = self.setup_resource(existing_variables) test = dbapi.variables_update_by_resource_id( context=self.context, resources=self.resources_type, resource_id=resource_id, data=deepcopy(update_variables)) self.assertEqual(resource_id, test.id) self.assertEqual(result_variables, test.variables) validate = dbapi.resource_get_by_id(self.context, self.resources_type, resource_id) self.assertEqual(resource_id, validate.id) self.assertEqual(result_variables, validate.variables)
def test_hosts_get_all_with_filters_noexist(self): project_id = self.make_project('project_1', foo='P1', zoo='P2') cloud_id = self.make_cloud(project_id, 'cloud_1') region_id = self.make_region(project_id, cloud_id, 'region_1', foo='R1') host_id = self.make_host(project_id, cloud_id, region_id, 'www.example.xyz', IPAddress(u'10.1.2.101'), 'server') variables = {"key1": "value1", "key2": "value2"} dbapi.variables_update_by_resource_id(self.context, "hosts", host_id, variables) filters = { "region_id": "region_1", "vars": "key1:value5", } res, _ = dbapi.hosts_get_all(self.context, filters, default_pagination) self.assertEqual(len(res), 0)
def test_project_variables_delete(self): create_res = dbapi.projects_create(self.context, project1) res = dbapi.projects_get_by_id(self.context, create_res.id) self.assertEqual(res.variables, {}) variables = {"key1": "value1", "key2": "value2"} res = dbapi.variables_update_by_resource_id(self.context, "projects", res.id, variables) self.assertEqual(res.variables, variables) # NOTE(sulo): we delete variables by their key res = dbapi.variables_delete_by_resource_id(self.context, "projects", res.id, {"key1": "key1"}) self.assertEqual(res.variables, {"key2": "value2"})
def test_variables_update_by_resource_id_existing_empty(self): existing_variables = {} resource_id = self.setup_resource(existing_variables) variables = { "key1": "value1", "key2": "value2", "key3": "value3", } test = dbapi.variables_update_by_resource_id(self.context, self.resources_type, resource_id, deepcopy(variables)) self.assertEqual(resource_id, test.id) self.assertEqual(variables, test.variables) validate = dbapi.resource_get_by_id(self.context, self.resources_type, resource_id) self.assertEqual(resource_id, validate.id) self.assertEqual(variables, validate.variables)