def test_get_security_group(self, m_t, m_rq): tenant_id = fakes_nova.tenants["baz"]["id"] m_t.return_value = tenant_id sc_group = fakes_nova.security_groups[tenant_id][0] id = sc_group['id'] m_t.return_value = tenant_id resp = fakes_network.create_fake_json_resp( {"security_group": sc_group}, 200) req_mock = mock.MagicMock() req_mock.get_response.return_value = resp m_rq.return_value = req_mock ret = self.helper.get_security_group_details(None, id) self.assertEqual(sc_group['id'], ret["id"]) self.assertEqual(sc_group['description'], ret["summary"]) occi_os_group = os_helpers.build_security_group_from_nova( fakes_nova.security_groups[tenant_id])[0] cont = 0 for r in ret["rules"]: self.assertEqual(occi_os_group['rules'][cont]['protocol'], r["protocol"]) self.assertEqual(occi_os_group['rules'][cont]['range'], r["range"]) self.assertEqual(occi_os_group['rules'][cont]['port'], r["port"]) self.assertEqual(occi_os_group['rules'][cont]['type'], r["type"]) cont += 1 m_rq.assert_called_with( None, method="GET", path="/%s/os-security-groups/%s" % (tenant_id, id), )
def test_create_security_group(self, m_t, m_rq): tenant_id = fakes_nova.tenants["baz"]["id"] m_t.return_value = tenant_id sc_group = fakes_nova.security_groups[tenant_id][0] occi_os_group = os_helpers.build_security_group_from_nova( fakes_nova.security_groups[tenant_id])[0] resp = fakes_network.create_fake_json_resp( {"security_group": sc_group}, 200) req_mock = mock.MagicMock() req_mock.get_response.return_value = resp resp_rule1 = fakes_network.create_fake_json_resp( {"security_group_rule": sc_group['rules'][0]}, 200) req_mock_rule1 = mock.MagicMock() req_mock_rule1.get_response.return_value = resp_rule1 resp_rule2 = fakes_network.create_fake_json_resp( {"security_group_rule": sc_group['rules'][1]}, 200) req_mock_rule2 = mock.MagicMock() req_mock_rule2.get_response.return_value = resp_rule2 m_rq.side_effect = [req_mock, req_mock_rule1, req_mock_rule2] ret = self.helper.create_security_group( None, name=occi_os_group['title'], description=occi_os_group['summary'], rules=occi_os_group['rules']) cont = 0 for r in ret["rules"]: self.assertEqual(occi_os_group['rules'][cont]['protocol'], r["protocol"]) self.assertEqual(occi_os_group['rules'][cont]['range'], r["range"]) self.assertEqual(occi_os_group['rules'][cont]['port'], r["port"]) self.assertEqual(occi_os_group['rules'][cont]['type'], r["type"]) cont += 1
def test_get_security_group(self, m_t, m_rq): tenant_id = fakes_nova.tenants["baz"]["id"] m_t.return_value = tenant_id sc_group = fakes_nova.security_groups[tenant_id][0] id = sc_group['id'] m_t.return_value = tenant_id resp = fakes_network.create_fake_json_resp( {"security_group": sc_group}, 200 ) req_mock = mock.MagicMock() req_mock.get_response.return_value = resp m_rq.return_value = req_mock ret = self.helper.get_security_group_details(None, id) self.assertEqual(sc_group['id'], ret["id"]) self.assertEqual(sc_group['description'], ret["summary"]) occi_os_group = os_helpers.build_security_group_from_nova( fakes_nova.security_groups[tenant_id] )[0] cont = 0 for r in ret["rules"]: self.assertEqual( occi_os_group['rules'][cont]['protocol'], r["protocol"]) self.assertEqual( occi_os_group['rules'][cont]['range'], r["range"]) self.assertEqual( occi_os_group['rules'][cont]['port'], r["port"]) self.assertEqual( occi_os_group['rules'][cont]['type'], r["type"]) cont += 1 m_rq.assert_called_with( None, method="GET", path="/%s/os-security-groups/%s" % (tenant_id, id), )
def get_security_group_details(self, req, sec_id): """Get details about a security group. :param req: the incoming request :param sec_id: security group id to show """ net = self._get_security_group(req, sec_id) ooi_sec = os_helpers.build_security_group_from_nova([net]) return ooi_sec[0]
def get_security_group_details(self, req, sec_id): """Get details about a security group. :param req: the incoming request :param sec_id: security group id to show """ net = self._get_security_group(req, sec_id) ooi_sec = os_helpers.build_security_group_from_nova([net]) return ooi_sec[0]
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) path = "os-security-groups" path = "/%s/%s" % (tenant_id, path) param_group = { "description": description, "name": name, } body = utils.make_body('security_group', param_group) os_req = self._get_req(req, path=path, content_type="application/json", body=json.dumps(body), method="POST") response_group = os_req.get_response(self.app) secgroup = self.get_from_response( response_group, "security_group", {}) sec_id = secgroup["id"] secgroup["rules"] = [] for rule in rules: port_min, port_max = os_helpers.security_group_rule_port( rule["port"] ) param_rules = { "parent_group_id": sec_id, "ip_protocol": rule["protocol"], "from_port": port_min, "to_port": port_max, "cidr": rule.get("range", "0.0.0.0/0") } body_rules = utils.make_body('security_group_rule', param_rules) path = "/%s/os-security-group-rules" % (tenant_id) os_req_rules = self._get_req(req, path=path, content_type="application/json", body=json.dumps(body_rules), method="POST") response_rules = os_req_rules.get_response(self.app) secrules = self.get_from_response( response_rules, "security_group_rule", {}) secgroup["rules"].append(secrules) ooi_sec = os_helpers.build_security_group_from_nova( [secgroup] ) return ooi_sec[0] except Exception as ex: raise ex
def test_get_server_security_link(self, mock_get): tenant_id = fakes_nova.tenants["baz"]["id"] server_id = uuid.uuid4().hex sg = fakes_nova.security_groups[tenant_id] segroup = os_helpers.build_security_group_from_nova(sg)[0] mock_get.return_value = [segroup] ret = self.helper.get_server_security_link(None, server_id, segroup["id"]) self.assertEqual(server_id, ret[0]['compute_id']) self.assertEqual(segroup["title"], ret[0]['securitygroup']["title"])
def test_index(self, mock_list): tenant_id = fakes_nova.tenants['bar']["id"] servers = fakes_nova.servers[tenant_id] sg = fakes_nova.security_groups[tenant_id] segroup = os_helpers.build_security_group_from_nova(sg)[0] links = [] for server in servers: link = {"compute_id": server["id"], "securitygroup": segroup} links.append(link) mock_list.return_value = links ret = self.controller.index(None) self.assertIsInstance(ret, collection.Collection)
def test_get_server_security_link(self, mock_get): tenant_id = fakes_nova.tenants["baz"]["id"] server_id = uuid.uuid4().hex sg = fakes_nova.security_groups[tenant_id] segroup = os_helpers.build_security_group_from_nova(sg)[0] mock_get.return_value = [segroup] ret = self.helper.get_server_security_link(None, server_id, segroup["id"]) self.assertEqual(server_id, ret[0]['compute_id']) self.assertEqual(segroup["title"], ret[0]['securitygroup']["title"])
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) path = "os-security-groups" path = "/%s/%s" % (tenant_id, path) param_group = { "description": description, "name": name, } body = utils.make_body('security_group', param_group) os_req = self._get_req(req, path=path, content_type="application/json", body=json.dumps(body), method="POST") response_group = os_req.get_response(self.app) secgroup = self.get_from_response(response_group, "security_group", {}) sec_id = secgroup["id"] secgroup["rules"] = [] for rule in rules: port_min, port_max = os_helpers.security_group_rule_port( rule["port"]) param_rules = { "parent_group_id": sec_id, "ip_protocol": rule["protocol"], "from_port": port_min, "to_port": port_max, "cidr": rule.get("range", "0.0.0.0/0") } body_rules = utils.make_body('security_group_rule', param_rules) path = "/%s/os-security-group-rules" % (tenant_id) os_req_rules = self._get_req(req, path=path, content_type="application/json", body=json.dumps(body_rules), method="POST") response_rules = os_req_rules.get_response(self.app) secrules = self.get_from_response(response_rules, "security_group_rule", {}) secgroup["rules"].append(secrules) ooi_sec = os_helpers.build_security_group_from_nova([secgroup]) return ooi_sec[0] except Exception as ex: raise ex
def list_security_groups(self, req): """List security groups :param req: the incoming request """ path = "os-security-groups" tenant_id = self.tenant_from_req(req) path = "/%s/%s" % (tenant_id, path) os_req = self._get_req(req, path=path, method="GET") response = os_req.get_response(self.app) sec = self.get_from_response(response, "security_groups", []) ooi_sec = os_helpers.build_security_group_from_nova(sec) return ooi_sec
def test_show(self, mock_list, mock_get): tenant_id = fakes_nova.tenants['baz']["id"] server = fakes_nova.servers[tenant_id][0] server_id = server['id'] secgroup_name = server['security_groups'][0]["name"] link_id = '%s_%s' % (server_id, secgroup_name) sec_group = os_helpers.build_security_group_from_nova( fakes_nova.security_groups[tenant_id]) link = {"compute_id": server_id, "securitygroup": sec_group[0]} mock_get.return_value = [link] mock_list.return_value = sec_group ret = self.controller.show(None, link_id) self.assertIsInstance(ret, securitygroup_link.SecurityGroupLink)
def list_security_groups(self, req): """List security groups :param req: the incoming request """ path = "os-security-groups" tenant_id = self.tenant_from_req(req) path = "/%s/%s" % (tenant_id, path) os_req = self._get_req(req, path=path, method="GET") response = os_req.get_response(self.app) sec = self.get_from_response(response, "security_groups", []) ooi_sec = os_helpers.build_security_group_from_nova(sec) return ooi_sec
def test_list_server_security_links(self, mock_get, mock_list): tenant_id = fakes_nova.tenants["baz"]["id"] servers = fakes_nova.servers[tenant_id] mock_list.return_value = servers sg = fakes_nova.security_groups[tenant_id] segroup = os_helpers.build_security_group_from_nova(sg)[0] mock_get.return_value = [segroup] ret = self.helper.list_server_security_links(None) cont = 0 for server in servers: self.assertEqual(server["id"], ret[cont]['compute_id']) self.assertEqual(segroup["title"], ret[cont]['securitygroup']["title"]) cont += 1
def _get_server_security_group(self, req, server_id): """Get security group from a server :param req: incoming request :param server_id: server id :return: information about the security group """ path = "os-security-groups" tenant_id = self.tenant_from_req(req) path = "/%s/servers/%s/%s" % (tenant_id, server_id, path) os_req = self._get_req(req, path=path, method="GET") response = os_req.get_response(self.app) sec = self.get_from_response(response, "security_groups", []) ooi_sec = os_helpers.build_security_group_from_nova(sec) return ooi_sec
def test_index(self, mock_list): tenant_id = fakes_nova.tenants['bar']["id"] servers = fakes_nova.servers[tenant_id] sg = fakes_nova.security_groups[tenant_id] segroup = os_helpers.build_security_group_from_nova(sg)[0] links = [] for server in servers: link = { "compute_id": server["id"], "securitygroup": segroup } links.append(link) mock_list.return_value = links ret = self.controller.index(None) self.assertIsInstance(ret, collection.Collection)
def test_list_server_security_links(self, mock_get, mock_list): tenant_id = fakes_nova.tenants["baz"]["id"] servers = fakes_nova.servers[tenant_id] mock_list.return_value = servers sg = fakes_nova.security_groups[tenant_id] segroup = os_helpers.build_security_group_from_nova(sg)[0] mock_get.return_value = [segroup] ret = self.helper.list_server_security_links(None) cont = 0 for server in servers: self.assertEqual(server["id"], ret[cont]['compute_id']) self.assertEqual(segroup["title"], ret[cont]['securitygroup']["title"]) cont += 1
def test_show(self, mock_list, mock_get): tenant_id = fakes_nova.tenants['baz']["id"] server = fakes_nova.servers[tenant_id][0] server_id = server['id'] secgroup_name = server['security_groups'][0]["name"] link_id = '%s_%s' % (server_id, secgroup_name) sec_group = os_helpers.build_security_group_from_nova( fakes_nova.security_groups[tenant_id] ) link = { "compute_id": server_id, "securitygroup": sec_group[0] } mock_get.return_value = [link] mock_list.return_value = sec_group ret = self.controller.show(None, link_id) self.assertIsInstance(ret, securitygroup_link.SecurityGroupLink)
def test_create_security_group(self, m_t, m_rq): tenant_id = fakes_nova.tenants["baz"]["id"] m_t.return_value = tenant_id sc_group = fakes_nova.security_groups[tenant_id][0] occi_os_group = os_helpers.build_security_group_from_nova( fakes_nova.security_groups[tenant_id] )[0] resp = fakes_network.create_fake_json_resp( {"security_group": sc_group}, 200 ) req_mock = mock.MagicMock() req_mock.get_response.return_value = resp resp_rule1 = fakes_network.create_fake_json_resp( {"security_group_rule": sc_group['rules'][0]}, 200 ) req_mock_rule1 = mock.MagicMock() req_mock_rule1.get_response.return_value = resp_rule1 resp_rule2 = fakes_network.create_fake_json_resp( {"security_group_rule": sc_group['rules'][1]}, 200 ) req_mock_rule2 = mock.MagicMock() req_mock_rule2.get_response.return_value = resp_rule2 m_rq.side_effect = [req_mock, req_mock_rule1, req_mock_rule2] ret = self.helper.create_security_group( None, name=occi_os_group['title'], description=occi_os_group['summary'], rules=occi_os_group['rules'] ) cont = 0 for r in ret["rules"]: self.assertEqual( occi_os_group['rules'][cont]['protocol'], r["protocol"] ) self.assertEqual( occi_os_group['rules'][cont]['range'], r["range"] ) self.assertEqual( occi_os_group['rules'][cont]['port'], r["port"]) self.assertEqual( occi_os_group['rules'][cont]['type'], r["type"]) cont += 1
def _get_server_security_group(self, req, server_id): """Get security group from a server :param req: incoming request :param server_id: server id :return: information about the security group """ path = "os-security-groups" tenant_id = self.tenant_from_req(req) path = "/%s/servers/%s/%s" % (tenant_id, server_id, path ) os_req = self._get_req(req, path=path, method="GET") response = os_req.get_response(self.app) sec = self.get_from_response(response, "security_groups", []) ooi_sec = os_helpers.build_security_group_from_nova(sec) return ooi_sec
def test_get_server_security_group(self, mock_tenant, mock_get): tenant_id = fakes_nova.tenants["baz"]["id"] server_id = uuid.uuid4().hex sc_group = fakes_nova.security_groups[tenant_id] mock_tenant.return_value = tenant_id resp = fakes_network.create_fake_json_resp( {"security_groups": sc_group}, 200) req_mock = mock.MagicMock() req_mock.get_response.return_value = resp mock_get.return_value = req_mock ret = self.helper._get_server_security_group(None, server_id) segroup = os_helpers.build_security_group_from_nova(sc_group) cont = 0 for s in segroup: self.assertEqual(s, ret[cont]) cont += 1 mock_get.assert_called_with( None, method="GET", path="/%s/servers/%s/os-security-groups" % (tenant_id, server_id), )
def test_get_server_security_group(self, mock_tenant, mock_get): tenant_id = fakes_nova.tenants["baz"]["id"] server_id = uuid.uuid4().hex sc_group = fakes_nova.security_groups[tenant_id] mock_tenant.return_value = tenant_id resp = fakes_network.create_fake_json_resp( {"security_groups": sc_group}, 200 ) req_mock = mock.MagicMock() req_mock.get_response.return_value = resp mock_get.return_value = req_mock ret = self.helper._get_server_security_group(None, server_id) segroup = os_helpers.build_security_group_from_nova( sc_group ) cont = 0 for s in segroup: self.assertEqual(s, ret[cont]) cont += 1 mock_get.assert_called_with( None, method="GET", path="/%s/servers/%s/os-security-groups" % (tenant_id, server_id), )