diff options
Diffstat (limited to 'moon_orchestrator')
-rw-r--r-- | moon_orchestrator/moon_orchestrator/api/pods.py | 55 | ||||
-rw-r--r-- | moon_orchestrator/moon_orchestrator/drivers.py | 4 | ||||
-rw-r--r-- | moon_orchestrator/tests/unit_python/test_pods.py | 48 |
3 files changed, 82 insertions, 25 deletions
diff --git a/moon_orchestrator/moon_orchestrator/api/pods.py b/moon_orchestrator/moon_orchestrator/api/pods.py index a83ca9ae..31ae31de 100644 --- a/moon_orchestrator/moon_orchestrator/api/pods.py +++ b/moon_orchestrator/moon_orchestrator/api/pods.py @@ -44,15 +44,18 @@ class Pods(Resource): :internal_api: get_pdp """ pods = {} - if uuid: - return {"pods": self.driver.get_pods(uuid)} - for _pod_key, _pod_values in self.driver.get_pods().items(): - pods[_pod_key] = [] - for _pod_value in _pod_values: - if _pod_value['namespace'] != "moon": - continue - pods[_pod_key].append(_pod_value) - return {"pods": pods} + try: + if uuid: + return {"pods": self.driver.get_pods(uuid)} + for _pod_key, _pod_values in self.driver.get_pods().items(): + pods[_pod_key] = [] + for _pod_value in _pod_values: + if _pod_value['namespace'] != "moon": + continue + pods[_pod_key].append(_pod_value) + return {"pods": pods} + except Exception as e: + return {"result": False, "message": str(e)}, 500 @check_auth def post(self, uuid=None, user_id=None): @@ -74,21 +77,24 @@ class Pods(Resource): } """ logger.debug("POST param={}".format(request.json)) - self.driver.create_pipeline( - request.json.get("keystone_project_id"), - request.json.get("pdp_id"), - request.json.get("security_pipeline"), - manager_data=request.json, - active_context=None, - active_context_name=None) - pods = {} - for _pod_key, _pod_values in self.driver.get_pods().items(): - pods[_pod_key] = [] - for _pod_value in _pod_values: - if _pod_value['namespace'] != "moon": - continue - pods[_pod_key].append(_pod_value) - return {"pods": pods} + try: + self.driver.create_pipeline( + request.json.get("keystone_project_id"), + request.json.get("pdp_id"), + request.json.get("security_pipeline"), + manager_data=request.json, + active_context=None, + active_context_name=None) + pods = {} + for _pod_key, _pod_values in self.driver.get_pods().items(): + pods[_pod_key] = [] + for _pod_value in _pod_values: + if _pod_value['namespace'] != "moon": + continue + pods[_pod_key].append(_pod_value) + return {"pods": pods} + except Exception as e: + return {"result": False, "message": str(e)}, 500 @check_auth def delete(self, uuid=None, user_id=None): @@ -128,4 +134,3 @@ class Pods(Resource): :internal_api: update_pdp """ return {"pods": None} - diff --git a/moon_orchestrator/moon_orchestrator/drivers.py b/moon_orchestrator/moon_orchestrator/drivers.py index b21f2639..9c230cce 100644 --- a/moon_orchestrator/moon_orchestrator/drivers.py +++ b/moon_orchestrator/moon_orchestrator/drivers.py @@ -259,6 +259,8 @@ class K8S(Driver): }, ] logger.debug("data={}".format(data)) + # When policies and models are empty, is it right that it returns 200 ? + # Should it return no found policies or models ? policies = manager_data.get('policies') if not policies: logger.info("No policy data from Manager, trying to get them") @@ -274,6 +276,8 @@ class K8S(Driver): "models", dict()) logger.debug("models={}".format(models)) + if not policy_ids: + raise exceptions.PolicyUnknown for policy_id in policy_ids: if policy_id in policies: genre = policies[policy_id].get("genre", "authz") diff --git a/moon_orchestrator/tests/unit_python/test_pods.py b/moon_orchestrator/tests/unit_python/test_pods.py index 0a5a5ba5..f760aa62 100644 --- a/moon_orchestrator/tests/unit_python/test_pods.py +++ b/moon_orchestrator/tests/unit_python/test_pods.py @@ -17,6 +17,20 @@ def test_get_pods(context, monkeypatch): assert "pods" in data +def test_get_pods_failure(context, monkeypatch): + patch_k8s(monkeypatch) + + import moon_orchestrator.server + server = moon_orchestrator.server.create_server() + _client = server.app.test_client() + req = _client.get("/pods/invalid") + assert req.status_code == 200 + assert req.data + data = get_json(req.data) + assert isinstance(data, dict) + assert not data["pods"] + + def test_add_pods(context, monkeypatch): patch_k8s(monkeypatch) @@ -38,6 +52,40 @@ def test_add_pods(context, monkeypatch): assert data["pods"] +def test_add_pods_with_no_data(context, monkeypatch): + patch_k8s(monkeypatch) + import moon_orchestrator.server + server = moon_orchestrator.server.create_server() + _client = server.app.test_client() + req = _client.post("/pods", data=json.dumps({}), + headers={'Content-Type': 'application/json'}) + assert req.status_code == 500 + assert req.data + data = get_json(req.data) + assert '400: Policy Unknown' in data['message'] + + +def test_add_pods_with_no_policies_no_models(context, monkeypatch, no_requests): + patch_k8s(monkeypatch) + + import moon_orchestrator.server + server = moon_orchestrator.server.create_server() + _client = server.app.test_client() + no_requests.get("http://manager:8082/policies", + json={'policies': {}}) + + no_requests.get("http://manager:8082/models", + json={'models': {}}) + data = { + "keystone_project_id": context.get('project_id'), + "pdp_id": context.get('pdp_id'), + "security_pipeline": context.get('security_pipeline'), + } + req = _client.post("/pods", data=json.dumps(data), + headers={'Content-Type': 'application/json'}) + assert req.status_code == 200 + + def test_delete_pods(context, monkeypatch): # TODO pass |