def test_show_security_group_not_found(self, m_list): tenant_id = fakes.tenants["baz"]["id"] sec_group = openstack_helper.build_security_group_from_neutron( fakes.security_groups[tenant_id]) m_list.return_value = sec_group req = fakes.create_req_test(None, None) self.assertRaises(exception.NotFound, self.controller.show, req, None)
def test_list_security_group_empty(self, m_list): tenant_id = fakes.tenants["foo"]["id"] sec_group = openstack_helper.build_security_group_from_neutron( fakes.security_groups[tenant_id]) req = fakes.create_req_test(None, None) m_list.return_value = sec_group result = self.controller.index(req) self.assertEqual(result.resources.__len__(), 0)
def test_list_security_group_empty(self, m_list): tenant_id = fakes.tenants["foo"]["id"] sec_group = openstack_helper.build_security_group_from_neutron( fakes.security_groups[tenant_id] ) req = fakes.create_req_test(None, None) m_list.return_value = sec_group result = self.controller.index(req) self.assertEqual(result.resources.__len__(), 0)
def test_list_security_group(self, m_list): tenant_id = fakes.tenants["baz"]["id"] sec_group = fakes.security_groups[tenant_id] expected = openstack_helper.build_security_group_from_neutron( sec_group) m_list.return_value = sec_group ret = self.helper.list_security_groups(None) self.assertEqual(2, ret.__len__()) self.assertEqual(expected, ret) m_list.assert_called_with(None, 'security-groups', response_resource="security_groups")
def test_show_security_group_not_found(self, m_list): tenant_id = fakes.tenants["baz"]["id"] sec_group = openstack_helper.build_security_group_from_neutron( fakes.security_groups[tenant_id] ) m_list.return_value = sec_group req = fakes.create_req_test(None, None) self.assertRaises(exception.NotFound, self.controller.show, req, None)
def test_show_security_group(self, m_list): tenant_id = fakes.tenants["baz"]["id"] sec_group = fakes.security_groups[tenant_id][0] expected = openstack_helper.build_security_group_from_neutron( [sec_group])[0] list_sec = sec_group m_list.return_value = list_sec ret = self.helper.get_security_group_details(None, None) self.assertEqual(expected, ret) m_list.assert_called_with(None, 'security-groups', None, response_resource="security_group")
def test_list_security_group(self, m_list): tenant_id = fakes.tenants["baz"]["id"] sec_group = openstack_helper.build_security_group_from_neutron( fakes.security_groups[tenant_id]) req = fakes.create_req_test(None, None) m_list.return_value = sec_group result = self.controller.index(req) expected = self.controller._get_security_group_resources(sec_group) self.assertEqual(result.resources.__len__(), expected.__len__()) for r in result.resources: self.assertIsInstance(r, occi_security_group.SecurityGroupResource) m_list.assert_called_with(req)
def test_show_security_group(self, m_list): tenant_id = fakes.tenants["baz"]["id"] sec_group = openstack_helper.build_security_group_from_neutron( [fakes.security_groups[tenant_id][0]]) req = fakes.create_req_test(None, None) m_list.return_value = sec_group[0] result = self.controller.show(req, None) expected = self.controller._get_security_group_resources(sec_group)[0] self.assertIsInstance(result, occi_security_group.SecurityGroupResource) self.assertEqual(result, expected) m_list.assert_called_with(req, None)
def test_show_security_group(self, m_list): tenant_id = fakes.tenants["baz"]["id"] sec_group = openstack_helper.build_security_group_from_neutron( [fakes.security_groups[tenant_id][0]] ) req = fakes.create_req_test(None, None) m_list.return_value = sec_group[0] result = self.controller.show(req, None) expected = self.controller._get_security_group_resources(sec_group)[0] self.assertIsInstance( result, occi_security_group.SecurityGroupResource) self.assertEqual(result, expected) m_list.assert_called_with(req, None)
def test_list_security_group(self, m_list): tenant_id = fakes.tenants["baz"]["id"] sec_group = openstack_helper.build_security_group_from_neutron( fakes.security_groups[tenant_id] ) req = fakes.create_req_test(None, None) m_list.return_value = sec_group result = self.controller.index(req) expected = self.controller._get_security_group_resources(sec_group) self.assertEqual(result.resources.__len__(), expected.__len__()) for r in result.resources: self.assertIsInstance(r, occi_security_group.SecurityGroupResource) m_list.assert_called_with(req)
def list_security_groups(self, req): """List security groups :param req: the incoming request """ try: secgroup = self.list_resources(req, 'security-groups', response_resource="security_groups") ooi_sec = os_helpers.build_security_group_from_neutron( secgroup ) return ooi_sec except Exception: raise exception.NotFound()
def get_security_group_details(self, req, sec_id): """Get info about a security group. :param req: the incoming request :param sec_id: security group id to show """ try: secgroup = self.get_resource(req, 'security-groups', sec_id, response_resource="security_group") ooi_sec = os_helpers.build_security_group_from_neutron( [secgroup] ) return ooi_sec[0] except Exception: raise exception.NotFound()
def test_create_security_groups(self, m_tenant, m_create): tenant_id = fakes.tenants["baz"]["id"] sec_group = fakes.security_groups[tenant_id][0] expected = openstack_helper.build_security_group_from_neutron( [sec_group])[0] m_tenant.return_value = uuid.uuid4().hex group_info = {"name": sec_group["name"], "id": sec_group["id"], "description": sec_group["description"]} rules_out_1 = sec_group["security_group_rules"][0] rules_out_2 = sec_group["security_group_rules"][1] m_create.side_effect = [group_info, rules_out_1, rules_out_2] ret = self.helper.create_security_group(None, expected["title"], expected["summary"], expected["rules"]) self.assertEqual(expected, ret) self.assertEqual(3, m_create.call_count)
def create_security_group(self, req, name, description, rules): """Create security group :param req: the incoming request :param name: security group name :param description: security group description :param rules: security group rules """ try: tenant_id = self.tenant_from_req(req) param_group = {"tenant_id": tenant_id, "description": description, "name": name, } secgroup = self.create_resource( req, 'security-groups', param_group, response_resource="security_group") sec_id = secgroup["id"] secgroup["security_group_rules"] = [] for rule in rules: port_min, port_max = os_helpers.security_group_rule_port( rule["port"] ) param_rule = { "ethertype": rule.get("ipversion", "IPv4"), "port_range_max": port_max, "port_range_min": port_min, "direction": os_helpers.security_group_rule_type( rule["type"]), "remote_ip_prefix": rule.get("range", "0.0.0.0/0"), "protocol": rule["protocol"], "security_group_id": sec_id, } secrule = self.create_resource( req, 'security-group-rules', param_rule, response_resource="security_group_rule") secgroup["security_group_rules"].append(secrule) ooi_sec = os_helpers.build_security_group_from_neutron( [secgroup] ) return ooi_sec[0] except Exception as ex: raise ex
def test_create_security_groups(self, m_tenant, m_create): tenant_id = fakes.tenants["baz"]["id"] sec_group = fakes.security_groups[tenant_id][0] expected = openstack_helper.build_security_group_from_neutron( [sec_group])[0] m_tenant.return_value = uuid.uuid4().hex group_info = { "name": sec_group["name"], "id": sec_group["id"], "description": sec_group["description"] } rules_out_1 = sec_group["security_group_rules"][0] rules_out_2 = sec_group["security_group_rules"][1] m_create.side_effect = [group_info, rules_out_1, rules_out_2] ret = self.helper.create_security_group(None, expected["title"], expected["summary"], expected["rules"]) self.assertEqual(expected, ret) self.assertEqual(3, m_create.call_count)
def test_create_security_groups(self, m_create): tenant_id = fakes.tenants["baz"]["id"] sec_group = openstack_helper.build_security_group_from_neutron( fakes.security_groups[tenant_id])[0] params = { "occi.core.title": sec_group["title"], "occi.securitygroup.rules": sec_group["rules"], "occi.core.summary": sec_group["summary"] } categories = {occi_security_group.SecurityGroupResource.kind} req = fakes.create_req_json_occi(params, categories) m_create.return_value = sec_group ret = self.controller.create(req, params) expected = self.controller._get_security_group_resources([sec_group]) self.assertIsInstance(ret.resources[0], occi_security_group.SecurityGroupResource) self.assertEqual(expected[0], ret.resources[0]) m_create.assert_called_with(req, sec_group["title"], sec_group["summary"], sec_group["rules"])
def test_create_security_groups(self, m_create): tenant_id = fakes.tenants["baz"]["id"] sec_group = openstack_helper.build_security_group_from_neutron( fakes.security_groups[tenant_id] )[0] params = {"occi.core.title": sec_group["title"], "occi.securitygroup.rules": sec_group["rules"], "occi.core.summary": sec_group["summary"] } categories = {occi_security_group.SecurityGroupResource.kind} req = fakes.create_req_json_occi(params, categories) m_create.return_value = sec_group ret = self.controller.create(req, params) expected = self.controller._get_security_group_resources( [sec_group]) self.assertIsInstance(ret.resources[0], occi_security_group.SecurityGroupResource) self.assertEqual(expected[0], ret.resources[0]) m_create.assert_called_with(req, sec_group["title"], sec_group["summary"], sec_group["rules"])