diff --git a/examples/integration_test/it.py b/examples/integration_test/it.py index e7d166387900ff1c0d9ed8ae4c0ad0c0e560a575..0ff25fdec37920be744a4527f07e423152897f82 100644 --- a/examples/integration_test/it.py +++ b/examples/integration_test/it.py @@ -134,8 +134,8 @@ def run(oidc_username): # ensure there is a running server that is not currently running an experiment results.start('Checking For Available Backend') - servers = server_info.itervalues().next()['availableServers'] - if len(servers) == 0: + available_servers = vc._VirtualCoach__get_available_server_list() + if len(available_servers) == 0: raise TestCaseError('No available backends to run test on.') results.done(True) diff --git a/hbp_nrp_virtual_coach/hbp_nrp_virtual_coach/config.py b/hbp_nrp_virtual_coach/hbp_nrp_virtual_coach/config.py index 6cc08485ac44d5fe39501e7e0aa7eac34b138494..b4cc360b60859d52affcc1610d92a8a160eafdda 100644 --- a/hbp_nrp_virtual_coach/hbp_nrp_virtual_coach/config.py +++ b/hbp_nrp_virtual_coach/hbp_nrp_virtual_coach/config.py @@ -83,7 +83,7 @@ class Config(dict): # validate required sections of the config, except if any values are missing self.__validate('oidc', ['user']) self.__validate('proxy', ['staging', 'dev', 'local', environment]) - self.__validate('proxy-services', ['experiment-list', 'server-info']) + self.__validate('proxy-services', ['experiment-list', 'available-servers', 'server-info']) self.__validate('simulation-services', ['create', 'state', 'reset', 'csv-recorders']) self.__validate('simulation-scripts', ['state-machine', 'transfer-function', 'brain']) self.__validate('reset-services', ['robot_pose', 'full', 'world', 'brain']) 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 6895787a0c1c9bb707ef8f13bbd34ba0ef8c2755..19ef25429674f7d787679123df5ec9c7212793c2 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 @@ -54,6 +54,12 @@ class TestVirtualCoach(unittest.TestCase): self._vc = VirtualCoach() + self._mock_available_servers_list = [ + {"id": 'mock-server-1'}, + {"id":'mock-server-4'}, + {"id":'mock-server-5'} + ] + self._mock_exp_list = {'MockExperiment1': {'configuration': {'name': 'C', 'maturity': 'production', 'description': 'Mock C', @@ -66,17 +72,15 @@ class TestVirtualCoach(unittest.TestCase): {'server': 'mock-server-6', 'runningSimulation': {'owner': '3', 'state': 'started', - 'creationDate': 'Feb 03 12:15:24 UTC 2017'}}], - 'availableServers': [{"id": 'mock-server-1'}, {"id":'mock-server-4'}, - {"id":'mock-server-5'}]}, + 'creationDate': 'Feb 03 12:15:24 UTC 2017'}}] + }, 'MockExperiment2': {'configuration': {'name': 'A', 'maturity': 'production', 'description': 'Mock A', 'timeout': 900, 'experimentConfiguration': 'foo/bar3.xml'}, - 'joinableServers': {}, - 'availableServers': [{"id": 'mock-server-1'}, {"id":'mock-server-4'}, - {"id":'mock-server-5'}]}, + 'joinableServers': [] + }, 'MockDevExperiment': {'configuration': {'name': 'B', 'maturity': 'development', 'description': 'Mock B', @@ -85,9 +89,8 @@ class TestVirtualCoach(unittest.TestCase): 'joinableServers': [{'server': 'mock-server-2', 'runningSimulation': {'owner': '2', 'state': 'created', - 'creationDate': 'Feb 03 12:07:58 UTC 2017'}}], - 'availableServers': [{"id": 'mock-server-1'}, {"id":'mock-server-4'}, - {"id":'mock-server-5'}]}} + 'creationDate': 'Feb 03 12:07:58 UTC 2017'}}] + }} self._mock_exp_list_local = {'MockExperiment1': {'configuration': {'name': 'C', 'maturity': 'production', @@ -97,15 +100,15 @@ class TestVirtualCoach(unittest.TestCase): 'joinableServers': [{'server': 'localhost', 'runningSimulation': {'owner': '1', 'state': 'paused', - 'creationDate': 'Feb 03 12:11:10 UTC 2017'}}], - 'availableServers': []}, + 'creationDate': 'Feb 03 12:11:10 UTC 2017'}}] + }, 'MockExperiment2': {'configuration': {'name': 'A', 'maturity': 'production', 'description': 'Mock A', 'timeout': 900, 'experimentConfiguration': 'foo/bar3.xml'}, - 'joinableServers': [], - 'availableServers': []}} + 'joinableServers': [] + }} def test_init_asserts(self): # invalid username @@ -241,12 +244,12 @@ class TestVirtualCoach(unittest.TestCase): """ self.assertEqual(mock_stdout.getvalue().strip(), running_table.strip()) - @patch('hbp_nrp_virtual_coach.virtual_coach.VirtualCoach._VirtualCoach__get_experiment_list') + @patch('hbp_nrp_virtual_coach.virtual_coach.VirtualCoach._VirtualCoach__get_available_server_list') @patch('sys.stdout', new_callable=StringIO) - def test_print_available_servers(self, mock_stdout, mock_list): + def test_print_available_servers(self, mock_stdout, available_servers): # mock the OIDC server call - mock_list.return_value = self._mock_exp_list + available_servers.return_value = self._mock_available_servers_list self._vc.print_available_servers() available_servers = """ @@ -256,17 +259,12 @@ mock-server-5 """ self.assertEqual(mock_stdout.getvalue().strip(), available_servers.strip()) - @patch('hbp_nrp_virtual_coach.virtual_coach.VirtualCoach._VirtualCoach__get_experiment_list') + @patch('hbp_nrp_virtual_coach.virtual_coach.VirtualCoach._VirtualCoach__get_available_server_list') @patch('sys.stdout', new_callable=StringIO) - def test_print_no_available_servers(self, mock_stdout, mock_list): - - # remove all available servers (deep copy to avoid an issues with test order) - mock_exp_list_copy = copy.deepcopy(self._mock_exp_list) - for k in mock_exp_list_copy: - mock_exp_list_copy[k]['availableServers'] = [] + def test_print_no_available_servers(self, mock_stdout, available_servers): # mock the OIDC server call - mock_list.return_value = mock_exp_list_copy + available_servers.return_value = [] self._vc.print_available_servers() available_servers = 'No available servers.' @@ -299,27 +297,26 @@ mock-server-5 mock_list.return_value = self._mock_exp_list self.assertRaises(ValueError, self._vc.launch_experiment, 'MockExperiment1', 'invalid-server-1') + @patch('hbp_nrp_virtual_coach.virtual_coach.VirtualCoach._VirtualCoach__get_available_server_list') @patch('hbp_nrp_virtual_coach.virtual_coach.VirtualCoach._VirtualCoach__get_experiment_list') - def test_launch_no_available_servers(self, mock_list): - - # remove all available servers (deep copy to avoid an issues with test order) - mock_exp_list_copy = copy.deepcopy(self._mock_exp_list) - for k in mock_exp_list_copy: - mock_exp_list_copy[k]['availableServers'] = [] + def test_launch_no_available_servers(self, mock_list, servers_list): # mock the OIDC server call - mock_list.return_value = mock_exp_list_copy + mock_list.return_value = self._mock_exp_list + servers_list.return_value = [] self.assertRaises(ValueError, self._vc.launch_experiment, 'MockExperiment1') + @patch('hbp_nrp_virtual_coach.virtual_coach.VirtualCoach._VirtualCoach__get_available_server_list') @patch('hbp_nrp_virtual_coach.virtual_coach.VirtualCoach._VirtualCoach__get_experiment_list') @patch('hbp_nrp_virtual_coach.simulation.Simulation.__init__') @patch('hbp_nrp_virtual_coach.simulation.Simulation.launch') - def test_launch_one_server(self, mock_sim_launch, mock_sim, mock_list): + def test_launch_one_server(self, mock_sim_launch, mock_sim, mock_list, servers_list): # mock sim launch to succeed mock_sim.return_value = None mock_sim_launch.return_value = True + servers_list.return_value = self._mock_available_servers_list # mock the OIDC server call mock_list.return_value = self._mock_exp_list self._vc.launch_experiment('MockExperiment1', 'mock-server-4') 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 a12a5e6f845c730676c42e5e512a0cd112ef131e..d312c7a02a3808c92220148e90a96e8fe1671a1a 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 @@ -184,8 +184,8 @@ class VirtualCoach(object): # retrieve the experiment list, and take the first list of available servers, this is the # same across all experiment listings - exp_list = self.__get_experiment_list() - servers = [server['id'] for server in exp_list.itervalues().next()['availableServers']] + available_servers = self.__get_available_server_list() + servers = [server['id'] for server in available_servers] # add a display value if there are no available servers if len(servers) == 0: @@ -219,8 +219,11 @@ class VirtualCoach(object): experiment_id) # get the experiment configuration details and available servers that can be used + available_servers = self.__get_available_server_list() + from pprint import pprint + pprint(available_servers) + servers = [available_server['id'] for available_server in available_servers] experiment = exp_list[experiment_id] - servers = [available_server['id'] for available_server in experiment['availableServers']] experiment_conf = experiment['configuration']['experimentConfiguration'] # if the user provided a specific server, ensure it is available before trying to launch @@ -258,3 +261,12 @@ class VirtualCoach(object): logger.info('Retrieving list of experiments.') _, l_json = self.__oidc_client.request(self.__config['proxy-services']['experiment-list']) return json.loads(l_json) + + def __get_available_server_list(self): + """ + Internal helper to retrieve the available server list from the backend proxy. + """ + + logger.info('Retrieving list of available servers.') + _, l_json = self.__oidc_client.request(self.__config['proxy-services']['available-servers']) + return json.loads(l_json)