diff options
author | ReemMahmoud <rfawzy.ext@orange.com> | 2018-03-22 12:06:48 +0200 |
---|---|---|
committer | sgdt6900 <rhanafy.ext@orange.com> | 2018-03-26 16:28:53 +0200 |
commit | 7cf97412a3e8059c4ddd0a471bc3095a7b5a266b (patch) | |
tree | 389c4b9c5587d09699e7e92d1e1d89b0d7b3fe5e /moon_orchestrator/tests | |
parent | 6d2dbd0af7da445b39bbb7e16a36260268c685cd (diff) |
Add more tests and fix bug in moon_orchestrator
adding more validation
adding more test cases covering pods, slaves
applying comments mentioned
Change-Id: I7e58f184a3f216fc3740097244a5538c184bd940
Signed-off-by: ReemMahmoud <rfawzy.ext@orange.com>
Diffstat (limited to 'moon_orchestrator/tests')
-rw-r--r-- | moon_orchestrator/tests/unit_python/mock_pods.py | 13 | ||||
-rw-r--r-- | moon_orchestrator/tests/unit_python/test_pods.py | 154 |
2 files changed, 161 insertions, 6 deletions
diff --git a/moon_orchestrator/tests/unit_python/mock_pods.py b/moon_orchestrator/tests/unit_python/mock_pods.py index 84e6c7ea..59e1b3c0 100644 --- a/moon_orchestrator/tests/unit_python/mock_pods.py +++ b/moon_orchestrator/tests/unit_python/mock_pods.py @@ -208,12 +208,25 @@ def patch_k8s(monkeypatch): 'create_namespaced_deployment', create_namespaced_deployment_mockreturn) + def delete_namespaced_deployment_mockreturn(*args, **kwargs): + return None + + monkeypatch.setattr(client.ExtensionsV1beta1Api, + 'delete_namespaced_deployment', + delete_namespaced_deployment_mockreturn) + def create_namespaced_service_mockreturn(*args, **kwargs): return {} monkeypatch.setattr(client.CoreV1Api, 'create_namespaced_service', create_namespaced_service_mockreturn) + def delete_namespaced_service_mockreturn(*args, **kwargs): + return {} + monkeypatch.setattr(client.CoreV1Api, + 'delete_namespaced_service', + delete_namespaced_service_mockreturn) + def register_pods(m): """ Modify the response from Requests module diff --git a/moon_orchestrator/tests/unit_python/test_pods.py b/moon_orchestrator/tests/unit_python/test_pods.py index 678645be..5e1b3767 100644 --- a/moon_orchestrator/tests/unit_python/test_pods.py +++ b/moon_orchestrator/tests/unit_python/test_pods.py @@ -15,6 +15,7 @@ def test_get_pods(context, monkeypatch): data = get_json(req.data) assert isinstance(data, dict) assert "pods" in data + assert data["pods"] def test_get_pods_failure(context, monkeypatch): @@ -30,6 +31,7 @@ def test_get_pods_failure(context, monkeypatch): assert isinstance(data, dict) assert not data["pods"] +############################ /post ############################ def test_add_pods_with_pipeline(context, monkeypatch): patch_k8s(monkeypatch) @@ -52,7 +54,7 @@ def test_add_pods_with_pipeline(context, monkeypatch): assert data["pods"] -def test_add_pods_without_pipeline_with_bad_slave_name(context, monkeypatch): +def test_add_pods_without_pipeline_with_bad_slave_name_failure(context, monkeypatch): patch_k8s(monkeypatch) import moon_orchestrator.server @@ -89,7 +91,7 @@ def test_add_pods_without_pipeline_with_good_slave_name(context, monkeypatch): assert data["pods"] -def test_add_pods_without_pipeline_without_slave_name(context, monkeypatch): +def test_add_pods_without_pipeline_without_slave_name_failure(context, monkeypatch): patch_k8s(monkeypatch) import moon_orchestrator.server @@ -106,7 +108,7 @@ def test_add_pods_without_pipeline_without_slave_name(context, monkeypatch): assert 'The slave is unknown.' in data['message'] -def test_add_pods_with_no_data(context, monkeypatch): +def test_add_pods_with_no_data_failure(context, monkeypatch): patch_k8s(monkeypatch) import moon_orchestrator.server server = moon_orchestrator.server.create_server() @@ -140,6 +142,146 @@ def test_add_pods_with_no_policies_no_models(context, monkeypatch, no_requests): assert req.status_code == 200 -def test_delete_pods(context, monkeypatch): - # TODO - pass +def test_add_pods_with_empty_pdp_id_and_keystone_project_id_failure(context, monkeypatch): + patch_k8s(monkeypatch) + + import moon_orchestrator.server + server = moon_orchestrator.server.create_server() + _client = server.app.test_client() + data = { + "keystone_project_id": "", + "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 == 400 + assert req.data + data = get_json(req.data) + assert "The pdp is unknown." in data['message'] + + +def test_add_pods_with_empty_security_pipeline_failure(context, monkeypatch): + patch_k8s(monkeypatch) + + import moon_orchestrator.server + server = moon_orchestrator.server.create_server() + _client = server.app.test_client() + data = { + "keystone_project_id": context.get('project_id'), + "pdp_id": context.get('pdp_id'), + "security_pipeline": "", + } + req = _client.post("/pods", data=json.dumps(data), + headers={'Content-Type': 'application/json'}) + assert req.status_code == 400 + assert req.data + data = get_json(req.data) + assert 'The policy is unknown.' in data['message'] + + +def test_add_different_pods_with_same_pdp_id(context, monkeypatch): + patch_k8s(monkeypatch) + + import moon_orchestrator.server + server = moon_orchestrator.server.create_server() + _client = server.app.test_client() + 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'}) + data["keystone_project_id"] = data["keystone_project_id"] + "x" + req = _client.post("/pods", data=json.dumps(data), + headers={'Content-Type': 'application/json'}) + assert req.status_code == 200 + + +def test_add_different_pods_with_same_keystone_project_id_failure(context, monkeypatch): + patch_k8s(monkeypatch) + + import moon_orchestrator.server + server = moon_orchestrator.server.create_server() + _client = server.app.test_client() + 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'}) + data["pdp_id"] = data["pdp_id"] + "xyz" + req = _client.post("/pods", data=json.dumps(data), + headers={'Content-Type': 'application/json'}) + assert req.status_code == 409 + data = get_json(req.data) + assert isinstance(data, dict) + assert 'A Pipeline already exist for the specified slave.' in data['message'] + + +def test_add_pod_with_slave_more_than_once_failure(context, monkeypatch): + patch_k8s(monkeypatch) + + import moon_orchestrator.server + server = moon_orchestrator.server.create_server() + _client = server.app.test_client() + data = { + "slave_name": "active_context", + } + req = _client.post("/pods", data=json.dumps(data), + headers={'Content-Type': 'application/json'}) + req = _client.post("/pods", data=json.dumps(data), + headers={'Content-Type': 'application/json'}) + assert req.status_code == 409 + assert req.data + data = get_json(req.data) + assert isinstance(data, dict) + assert 'A Wrapper already exist for the specified slave.' in data['message'] + +############################ /delete ############################ + +def test_delete_pod_valid_uuid(context, monkeypatch): + patch_k8s(monkeypatch) + + import moon_orchestrator.server + server = moon_orchestrator.server.create_server() + _client = server.app.test_client() + 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 + assert req.data + data = get_json(req.data) + for key in data["pods"]: + req = _client.delete("/pods/{}".format(key)) + assert req.status_code == 200 + +def test_delete_pod_Invalid_uuid_failure(context, monkeypatch): + patch_k8s(monkeypatch) + + import moon_orchestrator.server + server = moon_orchestrator.server.create_server() + _client = server.app.test_client() + + req = _client.delete("/pods/invalid") + assert req.status_code == 400 + data = get_json(req.data) + assert 'The slave is unknown.' in data['message'] + +def test_delete_pod_without_uuid_failure(context, monkeypatch): + patch_k8s(monkeypatch) + + import moon_orchestrator.server + server = moon_orchestrator.server.create_server() + _client = server.app.test_client() + + req = _client.delete("/pods/") + assert req.status_code == 400 + data = get_json(req.data) + assert 'The slave is unknown.' in data['message']
\ No newline at end of file |