def setUp(self): # Clean the global registry of servers GLOBAL_REGISTRY.clear() CONSUMER_CONFIG_PATH = self.FEDERATED_DEPLOYMENTS + '/consumer/' PROVIDER1_CONFIG_PATH = self.FEDERATED_DEPLOYMENTS + '/provider1/' PROVIDER2_CONFIG_PATH = self.FEDERATED_DEPLOYMENTS + '/provider2/' self.consumer_handler = load_dir(CONSUMER_CONFIG_PATH).load_process( 'consumer_machine', 'main_instance') self.provider1_handler = load_dir(PROVIDER1_CONFIG_PATH).load_process( 'provider1_machine', 'main_instance') self.provider2_handler = load_dir(PROVIDER2_CONFIG_PATH).load_process( 'provider2_machine', 'main_instance') self.consumer_client = WebLabDeustoClient( "http://127.0.0.1:%s/weblab/" % 18345) self.provider1_client = WebLabDeustoClient( "http://127.0.0.1:%s/weblab/" % 28345) self.provider2_client = WebLabDeustoClient( "http://127.0.0.1:%s/weblab/" % 38345) # dummy1: deployed in consumer, provider1, provider2 self.dummy1 = ExperimentId("dummy1", "Dummy experiments") # dummy2: deployed in consumer self.dummy2 = ExperimentId("dummy2", "Dummy experiments") # dummy3: deployed in provider1 as "dummy3_with_other_name" self.dummy3 = ExperimentId("dummy3", "Dummy experiments") # dummy4: deployed in provider2 self.dummy4 = ExperimentId("dummy4", "Dummy experiments")
def _wait_reservation(self, reservation_id, expected_server_info, finish): max_timeout = 10 initial_time = time.time() reservation_status = self.consumer_client.get_reservation_status( reservation_id) while reservation_status.status in (Reservation.WAITING, Reservation.WAITING_CONFIRMATION): if time.time() - initial_time > max_timeout: self.fail("Waiting too long in the queue for %s" % expected_server_info) time.sleep(0.1) reservation_status = self.consumer_client.get_reservation_status( reservation_id) self.assertEquals(Reservation.CONFIRMED, reservation_status.status) experiment_reservation_id = reservation_status.remote_reservation_id if experiment_reservation_id.id == '': experiment_reservation_id = reservation_id client = WebLabDeustoClient(reservation_status.url) response = client.send_command(experiment_reservation_id, Command("server_info")) self.assertEquals(expected_server_info, response.get_command_string()) if finish: self.consumer_client.finished_experiment(reservation_id) return reservation_id
def test_get_reservation_status(self): port = 15128 self.configurationManager._set_value(self.rfs.FACADE_JSON_PORT, port) self.rfs.start() try: client = WebLabDeustoClient("http://localhost:%s/weblab/" % port) expected_sess_id = SessionId.SessionId("whatever") NUMBER = 5 expected_confirmed_reservation = Reservation.ConfirmedReservation("reservation_id", NUMBER, "{}", 'http://www.weblab.deusto.es/...', '') self.mock_server.return_values['get_reservation_status'] = expected_confirmed_reservation confirmed_reservation = client.get_reservation_status(expected_sess_id) self.assertEquals( expected_sess_id.id, self.mock_server.arguments['get_reservation_status'][0] ) self.assertEquals( expected_confirmed_reservation.time, confirmed_reservation.time ) self.assertEquals( expected_confirmed_reservation.status, confirmed_reservation.status ) finally: self.rfs.stop()
def test_send_command(self): port = 15130 self.configurationManager._set_value(self.rfs.FACADE_JSON_PORT, port) self.rfs.start() try: client = WebLabDeustoClient("http://localhost:%s/weblab/" % port) expected_sess_id = SessionId.SessionId("whatever") expected_request_command = Command.Command('my request command') expected_response_command = Command.Command('my response command') self.mock_server.return_values['send_command'] = expected_response_command obtained_response_command = client.send_command(expected_sess_id, expected_request_command) self.assertEquals( expected_sess_id.id, self.mock_server.arguments['send_command'][0] ) self.assertEquals( expected_request_command.get_command_string(), self.mock_server.arguments['send_command'][1].get_command_string() ) self.assertEquals( expected_response_command.get_command_string(), obtained_response_command.get_command_string() ) finally: self.rfs.stop()
def do_full_experiment_use(): """ Uses the configured experiment trying to resemble the way a human would do it. This method will block for a while. :return: """ wc = WebLabDeustoClient(config.WEBLAB_BASE_URL) sessionid = wc.login(config.LOGIN, config.PASSWORD) if not sessionid: raise Exception("Wrong login") # Reserve the flash dummy experiment. experiment_id = ExperimentId(config.EXP_NAME, config.EXP_CATEGORY) waiting = wc.reserve_experiment(sessionid, experiment_id, "{}", "{}", None) # print "Reserve response: %r" % waiting reservation_id = waiting.reservation_id while True: status = wc.get_reservation_status(reservation_id) # print "Reservation status: %r" % status if type(status) is WaitingReservation: time.sleep(0.5) elif type(status) is ConfirmedReservation: break elif type(status) is WaitingConfirmationReservation: time.sleep(0.5) else: print "Unknown reservation status." print "Experiment reserved." global users_in users_in += 1 # Send some commands. for i in range(config.COMMANDS_PER_USER): # What's commandstring actually for?? cmd = Command(config.COMMAND) result = wc.send_command(reservation_id, cmd) if not result.commandstring.startswith("Received command"): raise Exception("Unrecognized command response") # print "Command result: %r" % result time.sleep(config.TIME_BETWEEN_COMMANDS) users_in -= 1 result = wc.logout(sessionid) print "Logout result: %r" % result
def test_get_experiment_use_by_id(self): port = 15129 self.configurationManager._set_value(self.rfs.FACADE_JSON_PORT, port) self.rfs.start() try: client = WebLabDeustoClient("http://localhost:%s/weblab/" % port) expected_sess_id = SessionId.SessionId("whatever") expected_reservation_id = SessionId.SessionId("foobar") expected_alive_result = RunningReservationResult() self.mock_server.return_values['get_experiment_use_by_id'] = expected_alive_result obtained_result = client.get_experiment_use_by_id(expected_sess_id, expected_reservation_id) self.assertEquals(RunningReservationResult(), obtained_result) finally: self.rfs.stop()
def setUp(self): self.global_config = load_dir(self.DEPLOYMENT_DIR) self.process_handlers = [] for process in self.PROCESSES: process_handler = self.global_config.load_process( 'myhost', process) self.process_handlers.append(process_handler) self.core_server = GLOBAL_REGISTRY[self.CORE_ADDRESS] self.experiment_dummy1 = GLOBAL_REGISTRY[self.EXPERIMENT_DUMMY1] self.experiment_dummy2 = GLOBAL_REGISTRY[self.EXPERIMENT_DUMMY2] self.core_config = self.core_server.config self.client = WebLabDeustoClient( 'http://localhost:%s/weblab/' % self.core_config[configuration_doc.CORE_FACADE_PORT])
def test_finished_experiment(self): port = 15127 self.configurationManager._set_value(self.rfs.FACADE_JSON_PORT, port) self.rfs.start() try: client = WebLabDeustoClient("http://localhost:%s/weblab/" % port) expected_sess_id = SessionId.SessionId("whatever") self.mock_server.return_values['finished_experiment'] = None client.finished_experiment(expected_sess_id) self.assertEquals( expected_sess_id.id, self.mock_server.arguments['finished_experiment'][0] ) finally: self.rfs.stop()
def test_login(self): port = 15123 self.configurationManager._set_value(self.rfs.FACADE_JSON_PORT, port) self.rfs.start() try: client = WebLabDeustoClient('http://127.0.0.1:%s/weblab/' % port) expected_sess_id = SessionId.SessionId("whatever") USERNAME = '******' PASSWORD = '******' self.mock_server.return_values['login'] = expected_sess_id session = client.login(USERNAME, PASSWORD) self.assertEquals(expected_sess_id.id, session.id) self.assertEquals(USERNAME, self.mock_server.arguments['login'][0]) self.assertEquals(PASSWORD, self.mock_server.arguments['login'][1]) finally: self.rfs.stop()
def test_reserve_experiment(self): port = 15126 self.configurationManager._set_value(self.rfs.FACADE_JSON_PORT, port) self.rfs.start() try: client = WebLabDeustoClient("http://127.0.0.1:%s/weblab/" % port) expected_sess_id = SessionId.SessionId("whatever") NUMBER = 5 expected_confirmed_reservation = Reservation.ConfirmedReservation("reservation_id", NUMBER, "{}", 'http://www.weblab.deusto.es/...', '') expected_experiment_id = self._generate_two_experiments()[0].to_experiment_id() self._generate_experiments_allowed() self.mock_server.return_values['reserve_experiment'] = expected_confirmed_reservation confirmed_reservation = client.reserve_experiment(expected_sess_id, expected_experiment_id, "{}", "{}") self.assertEquals( expected_sess_id.id, self.mock_server.arguments['reserve_experiment'][0] ) self.assertEquals( expected_experiment_id.exp_name, self.mock_server.arguments['reserve_experiment'][1].exp_name ) self.assertEquals( expected_experiment_id.cat_name, self.mock_server.arguments['reserve_experiment'][1].cat_name ) self.assertEquals( expected_confirmed_reservation.time, confirmed_reservation.time ) self.assertEquals( expected_confirmed_reservation.status, confirmed_reservation.status ) finally: self.rfs.stop()
def test_get_experiment_uses_by_id(self): port = 15131 self.configurationManager._set_value(self.rfs.FACADE_JSON_PORT, port) self.rfs.start() try: client = WebLabDeustoClient("http://localhost:%s/weblab/" % port) expected_sess_id = SessionId.SessionId("whatever") expected_usage = ExperimentUsage(10, time.time(), time.time(), '127.0.0.1', ExperimentId("exp","cat"), 'reser1', CoordAddress('machine','instance','server')) command_sent = CommandSent(Command.Command("request"), time.time(), Command.Command("response"), time.time()) expected_usage.append_command(command_sent) loaded_file_sent = LoadedFileSent('content-of-the-file', time.time(), Command.Command("response"), time.time(), 'program') expected_usage.append_file(loaded_file_sent) expected_finished_result = FinishedReservationResult(expected_usage) expected_alive_result = RunningReservationResult() expected_cancelled_result = CancelledReservationResult() self.mock_server.return_values['get_experiment_uses_by_id'] = (expected_finished_result, expected_alive_result, expected_cancelled_result) expected_reservations = (SessionId.SessionId('reservation'), SessionId.SessionId('reservation2'), SessionId.SessionId('reservation3') ) results = client.get_experiment_uses_by_id(expected_sess_id, expected_reservations) self.assertEquals( expected_sess_id.id, self.mock_server.arguments['get_experiment_uses_by_id'][0]) self.assertEquals( expected_reservations, tuple(self.mock_server.arguments['get_experiment_uses_by_id'][1])) self.assertEquals(3, len(results)) self.assertEquals(expected_finished_result.status, results[0].status) self.assertEquals(expected_alive_result.status, results[1].status) self.assertEquals(expected_cancelled_result.status, results[2].status) self.assertEquals(expected_usage, results[0].experiment_use) finally: self.rfs.stop()
def create_client(self): return WebLabDeustoClient(self.address)
def _create_login_client(self, cookies=None): client = WebLabDeustoClient(self.login_baseurl or self.baseurl) if cookies is not None: client.set_cookies(cookies) return client
def run(self): assertions = [] times = [] print "Starting process" reservation_id = None try: weblab = WebLabDeustoClient(self.url) session_id = weblab.login(self.username, self.password) reservation = weblab.reserve_experiment(session_id, ExperimentId("visir", "Visir experiments"), "{}", "{}") while reservation.status in (Reservation.WAITING_CONFIRMATION or Reservation.WAITING): time.sleep(1) reservation = weblab.get_reservation_status( reservation.reservation_id ) if reservation.status != Reservation.CONFIRMED: raise Exception("Confirmed reservation expected for reservation_id (%r). Found status: %r" % (reservation.reservation_id, reservation.status)) print "Confirmed reservation, starting..." reservation_id = reservation.reservation_id response = weblab.send_command(reservation_id, Command("GIVE_ME_SETUP_DATA")) cookie = json.loads(response.commandstring)['cookie'] login_response = weblab.send_command(reservation_id, Command(visir_commands.visir_login_request % cookie)) visir_sessionid = visir_commands.parse_login_response(login_response) for _ in xrange(self.executions): before = time.time() response = weblab.send_command(reservation_id, Command(visir_commands.visir_request_11k % visir_sessionid)) after = time.time() result = visir_commands.parse_command_response(response) assertions.append(AssertionResult(11000.0, 200, result)) times.append(after - before) before = time.time() response = weblab.send_command(reservation_id, Command(visir_commands.visir_request_rectifier % visir_sessionid)) after = time.time() # Don't know how to measure the response, but at least check that the response is a valid VISIR response result = visir_commands.parse_command_response(response, 'dmm_resolution') assertions.append(AssertionResult(3.5, 200, result)) times.append(after - before) before = time.time() response = weblab.send_command(reservation_id, Command(visir_commands.visir_request_900 % visir_sessionid)) after = time.time() result = visir_commands.parse_command_response(response) assertions.append(AssertionResult(900.0, 200, result)) times.append(after - before) before = time.time() response = weblab.send_command(reservation_id, Command(visir_commands.visir_request_1k % visir_sessionid)) after = time.time() result = visir_commands.parse_command_response(response) assertions.append(AssertionResult(1000.0, 200, result)) times.append(after - before) before = time.time() response = weblab.send_command(reservation_id, Command(visir_commands.visir_request_10k % visir_sessionid)) after = time.time() result = visir_commands.parse_command_response(response) assertions.append(AssertionResult(10000.0, 200, result)) times.append(after - before) weblab.finished_experiment(reservation_id) except Exception as exception: if reservation_id is not None: try: weblab.finished_experiment(reservation_id) loggedout = True except: loggedout = False else: loggedout = "no id provided" print "Finished with exception and logged out: %s" % loggedout traceback.print_exc() return TesterResult(True, assertions, exception, times) else: print "Finished without exception" return TesterResult(any(map(lambda assertion : assertion.failed, assertions)), assertions, None, times)
import sys import time from weblab.core.reservations import Reservation from weblab.core.coordinator.clients.weblabdeusto import WebLabDeustoClient from weblab.data.command import Command from weblab.data.experiments import ExperimentId URL = "http://www.weblab.deusto.es/weblab/" USERNAME = "******" PASSWORD = "" EXP_ID = ExperimentId("robot-movement", "Robot experiments") N = 50 weblab = WebLabDeustoClient( URL ) session_id = weblab.login(USERNAME, PASSWORD) for n in xrange(N): print "Reserving (%s)..." % n, sys.stdout.flush() reservation = weblab.reserve_experiment(session_id, EXP_ID, "{}", "{}") while reservation.status in (Reservation.WAITING_CONFIRMATION or Reservation.WAITING): time.sleep(1) reservation = weblab.get_reservation_status( reservation.reservation_id ) print ".", sys.stdout.flush() if reservation.status != Reservation.CONFIRMED: raise Exception("Confirmed reservation expected for reservation_id (%r). Found status: %r" % (reservation.reservation_id, reservation.status))
def run(self): assertions = [] times = [] print "Starting process" reservation_id = None try: weblab = WebLabDeustoClient(self.url) session_id = weblab.login(self.username, self.password) reservation = weblab.reserve_experiment( session_id, ExperimentId(VISIR_EXPERIMENT, "Visir experiments"), "{}", "{}") while reservation.status in (Reservation.WAITING_CONFIRMATION or Reservation.WAITING): time.sleep(1) reservation = weblab.get_reservation_status( reservation.reservation_id) if reservation.status != Reservation.CONFIRMED: raise Exception( "Confirmed reservation expected for reservation_id (%r). Found status: %r" % (reservation.reservation_id, reservation.status)) print "Confirmed reservation, starting..." reservation_id = reservation.reservation_id initial_config = reservation.initial_configuration cookie = json.loads(initial_config)['cookie'] login_response = weblab.send_command( reservation_id, Command(visir_commands.visir_login_request % cookie)) visir_sessionid = visir_commands.parse_login_response( login_response) iteration = 0 for _ in xrange(self.executions): before = time.time() response = weblab.send_command( reservation_id, Command(visir_commands.visir_request_11k % visir_sessionid)) after = time.time() result = visir_commands.parse_command_response(response) ar1 = AssertionResult(11000.0, 11000.0 * 0.2, result) if DEBUG and ar1.failed: print "[Failed at 1st]" + str(ar1) if not IGNORE_ASSERTIONS: assertions.append(ar1) times.append(after - before) # This command is currently commented out because it does not seem to be compatible with lxi_visir. # before = time.time() # response = weblab.send_command(reservation_id, Command(visir_commands.visir_request_rectifier % visir_sessionid)) # after = time.time() # # Don't know how to measure the response, but at least check that the response is a valid VISIR response # result = visir_commands.parse_command_response(response, 'dmm_resolution') # assertions.append(AssertionResult(3.5, 200, result)) # times.append(after - before) time.sleep(1) before = time.time() response = weblab.send_command( reservation_id, Command(visir_commands.visir_request_900 % visir_sessionid)) after = time.time() result = visir_commands.parse_command_response(response) ar3 = AssertionResult(900.0, 900.0 * 0.2, result) if DEBUG and ar3.failed: print "[Failed at 3rd]" + str(ar3) if not IGNORE_ASSERTIONS: assertions.append(ar3) times.append(after - before) time.sleep(1) before = time.time() response = weblab.send_command( reservation_id, Command(visir_commands.visir_request_1k % visir_sessionid)) after = time.time() result = visir_commands.parse_command_response(response) ar4 = AssertionResult(1000.0, 1000 * 0.2, result) if DEBUG and ar4.failed: print "[Failed at 4th]" + str(ar4) if not IGNORE_ASSERTIONS: assertions.append(ar4) times.append(after - before) time.sleep(1) before = time.time() response = weblab.send_command( reservation_id, Command(visir_commands.visir_request_10k % visir_sessionid)) after = time.time() result = visir_commands.parse_command_response(response) ar5 = AssertionResult(10000.0, 10000 * 0.2, result) if DEBUG and ar5.failed: print "[Failed at 5th]" + str(ar5) if not IGNORE_ASSERTIONS: assertions.append(ar5) times.append(after - before) iteration += 1 time.sleep(1) weblab.finished_experiment(reservation_id) except Exception as exception: if reservation_id is not None: try: weblab.finished_experiment(reservation_id) loggedout = True except: loggedout = False else: loggedout = "no id provided" print "Finished with exception and logged out: %s" % loggedout traceback.print_exc() return TesterResult(True, assertions, exception, times) else: print "Finished without exception" return TesterResult( any(map(lambda assertion: assertion.failed, assertions)), assertions, None, times)