From 154000e62681f47dfa413bc41f04c7d2e4f71273 Mon Sep 17 00:00:00 2001
From: Nikolay Stanchev <ns17@it-innovation.soton.ac.uk>
Date: Thu, 21 Feb 2019 10:18:38 +0000
Subject: [PATCH] Adds tests to check if the execute_pipeline endpoint opens a
 separate process in the correct way

---
 src/service/clmcservice/graphapi/tests.py | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/src/service/clmcservice/graphapi/tests.py b/src/service/clmcservice/graphapi/tests.py
index 0aa1122..f96a8d0 100644
--- a/src/service/clmcservice/graphapi/tests.py
+++ b/src/service/clmcservice/graphapi/tests.py
@@ -24,7 +24,7 @@
 
 from json import dumps
 import pytest
-from unittest.mock import patch
+from unittest.mock import patch, Mock
 from pyramid import testing
 from clmcservice.graphapi.views import GraphAPI
 from pyramid.httpexceptions import HTTPBadRequest, HTTPNotFound
@@ -495,13 +495,21 @@ class TestGraphAPI(object):
             error_raised = True
         assert error_raised, error_msg
 
+    @patch('clmcservice.graphapi.views.Popen')
     @patch('clmcservice.graphapi.views.uuid4')
-    def test_execute_pipeline_graph(self, uuid_mock):
+    def test_execute_pipeline_graph(self, uuid_mock, popen_mock):
         """
         Tests the functionality to start a pipeline script executing the graph API workflow - build, query, delete
         """
 
+        # mock the behaviour of the uuid function
         uuid_mock.return_value = "monitor_test_uuid1"
+
+        # mock the behaviour of the Popen class
+        popen_intance_mock = Mock()
+        popen_intance_mock.pid = Mock(return_value=111)
+        popen_mock.return_value = popen_intance_mock
+
         service_functions = dict(nginx={"measurement_name": "nginx", "response_time_field": "mean(avg_processing_time)",
                                         "request_size_field": "mean(avg_request_size)", "response_size_field": "mean(avg_response_size)"},
                                  minio={"measurement_name": "minio_http", "response_time_field": "mean(total_processing_time)/mean(total_requests_count)",
@@ -515,7 +523,8 @@ class TestGraphAPI(object):
         response = GraphAPI(request).execute_graph_pipeline()
         assert response == {"uuid": uuid_mock.return_value, "database": "test_sfc"}
 
-        assert False, "Implement tests for checking that an actual process is started."
+        popen_mock.assert_called_with("graph_pipeline.sh", body)  # assert that the graph pipeline script is ran with the JSON config that was received in the request
+        popen_intance_mock.pid.assert_called()  # assert that the process ID attribute was called and saved
 
     @staticmethod
     def check_exist_relationship(relationships_tuple, graph, uuid):
-- 
GitLab