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 1eb3d52dfaadb8120f5602b06b665bf5192a022e..88382a4d1e0544f0606e0c277e136c36ebfd8586 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 @@ -430,6 +430,26 @@ mock-server-5 self._vc.clone_experiment_to_storage('foo/bar1.xml') mock_logger.assert_called_once() + def test_clone_cloned_experiment(self): + self.assertRaises(AssertionError, self._vc.clone_cloned_experiment, 123) + + @patch('hbp_nrp_virtual_coach.virtual_coach.VirtualCoach._VirtualCoach__get_experiment_list') + def test_clone_missing_experiment(self, mock_list): + mock_list.return_value = [] + self.assertRaises(ValueError, self._vc.clone_cloned_experiment, 'missing_id') + + @patch('hbp_nrp_virtual_coach.virtual_coach.VirtualCoach._VirtualCoach__get_experiment_list') + @patch('requests.post') + def test_clone_experiment_to_storage_fail(self, request, mock_list): + mock_list.return_value = ['missing_id'] + + class Request(object): + status_code = 477 + + request.return_value = Request() + self._vc._VirtualCoach__storage_username = 'token' + self.assertRaises(Exception, self._vc.clone_cloned_experiment, 'missing_id') + @patch('hbp_nrp_virtual_coach.virtual_coach.VirtualCoach._VirtualCoach__get_experiment_list') def test_print_cloned_experiments_fail(self, mock_list): mock_list.return_value = self._mock_exp_list 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 b6b0eb96cf517db5acb5082d6fd8e2b0e08fb0c9..51211a2b2fa92cdf63ee67b493c4a3cf047a6372 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 @@ -320,6 +320,35 @@ class VirtualCoach(object): else: logger.info('Experiment "%s" cloned successfully', exp_name) + def clone_cloned_experiment(self, experiment_id): + """ + 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 + """ + assert isinstance(experiment_id, str) + + exp_list = self.__get_experiment_list(cloned=True) + if experiment_id not in exp_list: + raise ValueError('Experiment id : %s is invalid, please check the list ' + 'of all Experiments ids:\n%s' % (experiment_id, '\n'.join(exp_list))) + + # Raise Error in case no storage server token available. To get the token, the VC has to be + # instantiated with the storage_username parameter + if self.__storage_username is None: + raise ValueError('No Storage Server credentials found.' + 'To be able to clone experiments, you have to instantiate the' + 'Virtual Coach either with the storage_username parameter or' + 'the oidc_username parameter and login successfully') + + res = requests.post('%s/%s' % (self.__config['proxy-services']['experiment-clone'], + experiment_id), + headers=self.__storage_headers) + if res.status_code != 200: + raise Exception('Cloning Experiment failed, Status Code: %s' % res.status_code) + else: + logger.info('Experiment "%s" cloned successfully', experiment_id) + def print_cloned_experiments(self): """ Prints the list of the cloned experiments' names. Only works if the Virtual Coach was