def test_list_with_all_tenants_sec_name_and_admin_context(self): project_id = '0af70a4d22cf4652824ddc1f2435dd85' search_opts = {'all_tenants': 1} security_group_names = ['secgroup_ssh'] security_groups_list = {'security_groups': []} admin_context = context.RequestContext('user1', project_id, True) with mock.patch.object( self.mocked_client, 'list_security_groups', return_value=security_groups_list) as mock_list_secgroup: sg_api = neutron_driver.SecurityGroupAPI() sg_api.list(admin_context, project=project_id, names=security_group_names, search_opts=search_opts) mock_list_secgroup.assert_called_once_with( name=security_group_names, tenant_id=project_id)
def test_create_security_group_rules_bad_request(self): vals = {'protocol': 'icmp', 'cidr': '0.0.0.0/0', 'parent_group_id': '7ae75663-277e-4a0e-8f87-56ea4e70cb47', 'group_id': None, 'to_port': 255} body = {'security_group_rules': [{'remote_group_id': None, 'direction': 'ingress', 'protocol': 'icmp', 'ethertype': 'IPv4', 'port_range_max': 255, 'security_group_id': '7ae75663-277e-4a0e-8f87-56ea4e70cb47', 'remote_ip_prefix': '0.0.0.0/0'}]} name = 'test-security-group' message = "ICMP code (port-range-max) 255 is provided but ICMP type" \ " (port-range-min) is missing" self.moxed_client.create_security_group_rule( body).AndRaise(n_exc.NeutronClientException(status_code=400, message=message)) self.mox.ReplayAll() sg_api = neutron_driver.SecurityGroupAPI() self.assertRaises(exception.Invalid, sg_api.add_rules, self.context, None, name, [vals])
def _test_instances_security_group_bindings_scale(self, num_servers): max_query = 150 sg1_id = '2f7ce969-1a73-4ef9-bbd6-c9a91780ecd4' sg2_id = '20c89ce5-9388-4046-896e-64ffbd3eb584' sg1 = {'id': sg1_id, 'name': 'wol'} sg2 = {'id': sg2_id, 'name': 'eor'} security_groups_list = {'security_groups': [sg1, sg2]} servers = [] device_ids = [] ports = [] sg_bindings = {} for i in range(0, num_servers): server_id = "server-%d" % i port_id = "port-%d" % i servers.append({'id': server_id}) device_ids.append(server_id) ports.append({'id': port_id, 'device_id': server_id, 'security_groups': [sg1_id, sg2_id]}) sg_bindings[server_id] = [{'name': 'wol'}, {'name': 'eor'}] expected_args = [] return_values = [] for x in range(0, num_servers, max_query): expected_args.append( mock.call(device_id=device_ids[x:x + max_query])) return_values.append({'ports': ports[x:x + max_query]}) self.mocked_client.list_security_groups.return_value = ( security_groups_list) self.mocked_client.list_ports.side_effect = return_values sg_api = neutron_driver.SecurityGroupAPI() result = sg_api.get_instances_security_groups_bindings( self.context, servers) self.assertEqual(sg_bindings, result) self.mocked_client.list_security_groups.assert_called_once_with( id=mock.ANY) self.assertEqual(sorted([sg1_id, sg2_id]), sorted(self.mocked_client.list_security_groups.call_args[1]['id'])) self.assertEqual(expected_args, self.mocked_client.list_ports.call_args_list)
def test_get_with_invalid_name(self): sg_name = 'invalid_name' expected_sg_id = '85cc3048-abc3-43cc-89b3-377341426ac5' list_security_groups = {'security_groups': [{'name': sg_name, 'id': expected_sg_id, 'tenant_id': self.context.tenant, 'description': 'server', 'rules': []} ]} self.moxed_client.list_security_groups(name=sg_name, fields='id', tenant_id=self.context.tenant).AndReturn(list_security_groups) self.moxed_client.show_security_group(expected_sg_id).AndRaise( TypeError) self.mox.ReplayAll() sg_api = neutron_driver.SecurityGroupAPI() self.assertRaises(exception.SecurityGroupNotFound, sg_api.get, self.context, name=sg_name)
def test_instances_security_group_bindings_with_hidden_sg(self): servers = [{'id': 'server_1'}] ports = [{'id': '1', 'device_id': 'dev_1', 'security_groups': ['1']}, {'id': '2', 'device_id': 'dev_1', 'security_groups': ['2']}] port_list = {'ports': ports} sg1 = {'id': '1', 'name': 'wol'} # User doesn't have access to sg2 security_groups_list = {'security_groups': [sg1]} sg_bindings = {'dev_1': [{'name': 'wol'}]} self.moxed_client.list_ports(device_id=['server_1']).AndReturn( port_list) self.moxed_client.list_security_groups(id=['1', '2']).AndReturn( security_groups_list) self.mox.ReplayAll() sg_api = neutron_driver.SecurityGroupAPI() result = sg_api.get_instances_security_groups_bindings( self.context, servers) self.assertEqual(result, sg_bindings)
def test_instances_security_group_bindings(self): server_id = 'c5a20e8d-c4b0-47cf-9dca-ebe4f758acb1' port1_id = '4c505aec-09aa-47bc-bcc0-940477e84dc0' port2_id = 'b3b31a53-6e29-479f-ae5c-00b7b71a6d44' sg1_id = '2f7ce969-1a73-4ef9-bbd6-c9a91780ecd4' sg2_id = '20c89ce5-9388-4046-896e-64ffbd3eb584' servers = [{'id': server_id}] ports = [{ 'id': port1_id, 'device_id': server_id, 'security_groups': [sg1_id] }, { 'id': port2_id, 'device_id': server_id, 'security_groups': [sg2_id] }] port_list = {'ports': ports} sg1 = {'id': sg1_id, 'name': 'wol'} sg2 = {'id': sg2_id, 'name': 'eor'} security_groups_list = {'security_groups': [sg1, sg2]} sg_bindings = {server_id: [{'name': 'wol'}, {'name': 'eor'}]} self.mocked_client.list_ports.return_value = port_list self.mocked_client.list_security_groups.return_value = ( security_groups_list) sg_api = neutron_driver.SecurityGroupAPI() result = sg_api.get_instances_security_groups_bindings( self.context, servers) self.assertEqual(sg_bindings, result) self.mocked_client.list_ports.assert_called_once_with( device_id=[server_id]) self.mocked_client.list_security_groups.assert_called_once_with( id=mock.ANY) self.assertEqual( sorted([sg1_id, sg2_id]), sorted(self.mocked_client.list_security_groups.call_args[1]['id']))
def test_get_with_name_duplicated(self): sg_name = 'web_server' expected_sg_id = '85cc3048-abc3-43cc-89b3-377341426ac5' expected_sg = { 'security_group': { 'name': sg_name, 'id': expected_sg_id, 'tenant_id': self.context.project_id, 'description': 'server', 'rules': [] } } self.mocked_client.show_security_group.return_value = expected_sg sg_api = neutron_driver.SecurityGroupAPI() with mock.patch.object(neutronv20, 'find_resourceid_by_name_or_id', return_value=expected_sg_id): observed_sg = sg_api.get(self.context, name=sg_name) expected_sg['security_group']['project_id'] = self.context.tenant del expected_sg['security_group']['tenant_id'] self.assertEqual(expected_sg['security_group'], observed_sg) self.mocked_client.show_security_group.assert_called_once_with( expected_sg_id)
def _test_instances_security_group_bindings_scale(self, num_servers): max_query = 150 sg1_id = '2f7ce969-1a73-4ef9-bbd6-c9a91780ecd4' sg2_id = '20c89ce5-9388-4046-896e-64ffbd3eb584' sg1 = {'id': sg1_id, 'name': 'wol'} sg2 = {'id': sg2_id, 'name': 'eor'} security_groups_list = {'security_groups': [sg1, sg2]} servers = [] device_ids = [] ports = [] sg_bindings = {} for i in range(0, num_servers): server_id = "server-%d" % i port_id = "port-%d" % i servers.append({'id': server_id}) device_ids.append(server_id) ports.append({ 'id': port_id, 'device_id': server_id, 'security_groups': [sg1_id, sg2_id] }) sg_bindings[server_id] = [{'name': 'wol'}, {'name': 'eor'}] for x in range(0, num_servers, max_query): self.moxed_client.list_ports( device_id=device_ids[x:x + max_query]).\ AndReturn({'ports': ports[x:x + max_query]}) self.moxed_client.list_security_groups(id=mox.SameElementsAs( [sg2_id, sg1_id])).AndReturn(security_groups_list) self.mox.ReplayAll() sg_api = neutron_driver.SecurityGroupAPI() result = sg_api.get_instances_security_groups_bindings( self.context, servers) self.assertEqual(sg_bindings, result)
def test_populate_security_groups(self): sg_api = neutron_driver.SecurityGroupAPI() r = sg_api.populate_security_groups('ignore') self.assertIsInstance(r, objects.SecurityGroupList) self.assertEqual(0, len(r))