From 00a98a74d50c9854944a965a4f4fd83b8949c11c Mon Sep 17 00:00:00 2001
From: Mahmoud Akl <mahmoud.akl@epfl.ch>
Date: Thu, 29 Mar 2018 16:06:48 +0200
Subject: [PATCH] [NRRPLT-5856] update it.py

updated the integration test script to test cloned experiments. The script
now has to be called with either the storage username or the oidc username
to be able to clone experiments.

The cloned experiments used in the tests are deleted after the tests are
finished.

Change-Id: I247875c2984ab68e70739a309c985a6973e7db40
---
 examples/integration_test/it.py               | 44 ++++++++++++++-----
 .../tests/test_virtual_coach.py               |  2 +
 .../hbp_nrp_virtual_coach/virtual_coach.py    |  5 +++
 3 files changed, 41 insertions(+), 10 deletions(-)

diff --git a/examples/integration_test/it.py b/examples/integration_test/it.py
index ca7e63f..6e93890 100644
--- a/examples/integration_test/it.py
+++ b/examples/integration_test/it.py
@@ -27,6 +27,7 @@ interfaces/APIs that are supported.
 
 import argparse
 import logging
+import json
 import sys
 import time
 import traceback
@@ -109,7 +110,7 @@ def run(oidc_username, storage_username):
 
     # running array of test case results, unfortunately we have to hardcode the number of
     # test cases because the indeterminate progress bar is not helpful for the tester
-    NUM_TEST_CASES = 29
+    NUM_TEST_CASES = 31
     results = TestCases(NUM_TEST_CASES)
 
     try:
@@ -142,13 +143,35 @@ def run(oidc_username, storage_username):
 
         # ensure the desired IT experiment is cloned and is available in the storage server. Clone
         # the experiment first if not.
-        results.start('Checking for Empty Template Husky Experiment in the Storage Server')
-        if 'template_husky_0' not in vc._VirtualCoach__get_experiment_list(cloned=True):
-            if 'ExDTemplateHusky' not in [s[0] for s in server_info.iteritems()]:
-                raise TestCaseError('Husky Template Experiment is not available on the server to be'
-                                    ' cloned.')
-            else:
-                vc.clone_experiment_to_storage('ExDTemplateHusky')
+        results.start('Cloning a new Empty Template Husky Experiment from the Storage Server')
+
+        if 'ExDTemplateHusky' not in [s[0] for s in server_info.iteritems()]:
+            raise TestCaseError('Husky Template Experiment is not available on the server to be'
+                                ' cloned.')
+        experiment_id = vc.clone_experiment_to_storage('ExDTemplateHusky')
+        stored_experiments = vc._VirtualCoach__get_experiment_list(cloned=True)
+        if experiment_id not in stored_experiments:
+            raise TestCaseError('Template Husky Experiment was not cloned to the storage.')
+        results.done(True)
+
+        ##
+        ## Clone a Cloned Experiment
+        ##
+
+        results.start('Cloning a cloned experiment')
+        new_experiment_id = json.loads(vc.clone_cloned_experiment(experiment_id))['clonedExp']
+        if new_experiment_id not in vc._VirtualCoach__get_experiment_list(cloned=True):
+            raise TestCaseError('Cloning the cloned Template Husky Experiment failed')
+        results.done(True)
+
+        ##
+        ## Delete a Cloned Experiment
+        ##
+
+        results.start('Deleting a cloned experiment')
+        vc.delete_cloned_experiment(new_experiment_id)
+        if new_experiment_id in vc._VirtualCoach__get_experiment_list(cloned=True):
+            raise TestCaseError('Deleting a cloned Experiment failed')
         results.done(True)
 
         ##
@@ -157,7 +180,7 @@ def run(oidc_username, storage_username):
 
         # launch an experiment
         results.start('Launching Empty Template Husky Experiment')
-        sim = vc.launch_experiment('template_husky_0')
+        sim = vc.launch_experiment(experiment_id)
         results.done(True)
 
         # status handlers for simulation status events, simply write to our global status message
@@ -421,13 +444,14 @@ def tf(t):
         results.done(True)
 
         ##
-        ## Shutdown and Cleanup
+        ## Shutdown, Cleanup and Delete the cloned experiment used in the tests
         ##
 
         # shutdown the experiment cleanly
         results.start('Stopping Simulation')
         sim.stop()
         wait_condition(5, 'Waiting for simulation to stop.', lambda x: x['state'] == 'stopped')
+        vc.delete_cloned_experiment(experiment_id)
         results.done(True)
 
     # handle test case or unexpected failures, attempt to cleanup and terminate
diff --git a/hbp_nrp_virtual_coach/hbp_nrp_virtual_coach/tests/test_virtual_coach.py b/hbp_nrp_virtual_coach/hbp_nrp_virtual_coach/tests/test_virtual_coach.py
index 364f3cf..5da507d 100644
--- a/hbp_nrp_virtual_coach/hbp_nrp_virtual_coach/tests/test_virtual_coach.py
+++ b/hbp_nrp_virtual_coach/hbp_nrp_virtual_coach/tests/test_virtual_coach.py
@@ -446,6 +446,7 @@ mock-server-5
 
         class Request(object):
             status_code = 200
+            content = 'experiment_id'
 
         request.return_value = Request()
         self._vc._VirtualCoach__storage_username = 'username'
@@ -496,6 +497,7 @@ mock-server-5
 
         class Request(object):
             status_code = 200
+            content = None
 
         request.return_value = Request()
         self._vc._VirtualCoach__storage_username = 'token'
diff --git a/hbp_nrp_virtual_coach/hbp_nrp_virtual_coach/virtual_coach.py b/hbp_nrp_virtual_coach/hbp_nrp_virtual_coach/virtual_coach.py
index 230191e..bedf6a3 100644
--- a/hbp_nrp_virtual_coach/hbp_nrp_virtual_coach/virtual_coach.py
+++ b/hbp_nrp_virtual_coach/hbp_nrp_virtual_coach/virtual_coach.py
@@ -281,6 +281,7 @@ class VirtualCoach(object):
         instantiated with Storage Server support, i.e. Storage Server credentials
 
         :param exp_id: The id of the experiment to be cloned
+        :returns: The ID of the cloned experiment
         """
         assert isinstance(exp_id, str)
 
@@ -297,6 +298,7 @@ class VirtualCoach(object):
             raise Exception('Cloning Experiment failed, Status Code: %s' % res.status_code)
         else:
             logger.info('Experiment "%s" cloned successfully', exp_id)
+            return res.content
 
     def delete_cloned_experiment(self, exp_id):
         """
@@ -318,6 +320,8 @@ class VirtualCoach(object):
         Attempts to clone a cloned experiment to the Storage Server.
 
         :param experiment_id: The id of the cloned experiment to be cloned. E.g. benchmark_p3dx_1
+        :returns: A dict containing the ID of the cloned experiment and the ID of the original
+                  experiment. Dict Keys are: 'clonedExp' and 'originalExp'
         """
         assert isinstance(experiment_id, str)
 
@@ -341,6 +345,7 @@ class VirtualCoach(object):
             raise Exception('Cloning Experiment failed, Status Code: %s' % res.status_code)
         else:
             logger.info('Experiment "%s" cloned successfully', experiment_id)
+            return res.content
 
     def print_cloned_experiments(self):
         """
-- 
GitLab