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
示例#2
0
    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()
示例#3
0
    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 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 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 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 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
示例#9
0
    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 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])
示例#12
0
    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_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()
示例#15
0
    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()
示例#16
0
    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_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()
示例#19
0
    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()
示例#20
0
    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])
示例#21
0
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
示例#22
0
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
示例#23
0
    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)
示例#24
0
class IntegrationNoConcurrencyTestCase(object):
    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 tearDown(self):
        GLOBAL_REGISTRY.clear()

        for process_handler in self.process_handlers:
            process_handler.stop()

    def test_simple_single_uses(self):
        for _ in range(1):
            self._single_use()
        self._single_use()
        self._single_use()

    def _single_use(self, logout = True, plus_async_use = True):
        """
        Will use an experiment.
        @param logout If true, the user will be logged out after the use. Otherwise not.
        @param plus_async_use If true, after using the experiment synchronously, it will use it
        again using the asynchronous versions of the send_command and send_file requests.
        """
        self._single_sync_use(logout)
        if plus_async_use:
            self._single_async_use(logout)

    def _single_sync_use(self, logout = True):
        session_id, reservation_id = self._get_reserved()

        CONTENT = "content of the program FPGA"
        response = self.client.send_file(reservation_id, ExperimentUtil.serialize(CONTENT), 'program')
        self.assertEquals(response.commandstring, 'ack')

        response = self.client.send_command(reservation_id, Command.Command("STATE"))
        self.assertEquals(response.commandstring, 'STATE')

        response = self.client.send_command(reservation_id, Command.Command("ChangeSwitch on 0"))
        self.assertEquals(response.commandstring, "ChangeSwitch on 0")

        if logout:
            self.client.logout(session_id)

    def _get_reserved(self):
        session_id = self.client.login('intstudent1', 'password')

        user_information = self.client.get_user_information(session_id)
        self.assertEquals( 'intstudent1', user_information.login) 
        self.assertEquals( 'Name of integration test 1', user_information.full_name)
        self.assertEquals( '*****@*****.**', user_information.email)

        experiments = self.client.list_experiments(session_id)
        self.assertEquals( 2, len(experiments))

        dummy1_experiments = [ exp.experiment for exp in experiments if exp.experiment.name == 'dummy1' ]
        self.assertEquals( len(dummy1_experiments), 1)

        dummy1_experiment = dummy1_experiments[0]

        status = self.client.reserve_experiment(session_id, dummy1_experiment.to_experiment_id(), "{}", "{}")

        reservation_id = status.reservation_id

        # wait until it is reserved
        short_time = 0.1

        # Time extended from 9.0 to 15.0 because at times the test failed, possibly for that reason.
        times      = 15.0 / short_time

        while times > 0:
            new_status = self.client.get_reservation_status(reservation_id)
            if not isinstance(new_status, Reservation.WaitingConfirmationReservation) and not isinstance(new_status, Reservation.WaitingReservation):
                break
            times -= 1
            time.sleep(short_time)
        reservation = self.client.get_reservation_status(reservation_id)
        self.assertTrue(
                isinstance(reservation, Reservation.ConfirmedReservation),
                "Reservation %s is not Confirmed, as expected by this time" % reservation
            )
        return session_id, reservation_id

    def _single_async_use(self, logout = True):
        session_id, reservation_id = self._get_reserved()

        # send the program again, but asynchronously. Though this should work, it is not really very customary
        # to send_file more than once in the same session. In fact, it is a feature which might get removed in
        # the future. When/if that happens, this will need to be modified.
        CONTENT = "content of the program FPGA"
        reqid = self.client.send_async_file(reservation_id, ExperimentUtil.serialize(CONTENT), 'program')

        # Wait until send_async_file query is actually finished.
        #self._get_async_response(session_id, reqid)
        self._wait_async_done(reservation_id, (reqid,))

        # We need to wait for the programming to finish, while at the same
        # time making sure that the tests don't dead-lock.
        reqid = self.client.send_async_command(reservation_id, Command.Command("STATE"))
        respcmd = self._get_async_response(reservation_id, reqid)
        response = respcmd.get_command_string()

        # Check that the current state is "Ready"
        self.assertEquals("STATE", response)


        reqid = self.client.send_async_command(reservation_id, Command.Command("ChangeSwitch on 0"))
        self._wait_async_done(reservation_id, (reqid,))

        reqid = self.client.send_async_command(reservation_id, Command.Command("ClockActivation on 250"))
        self._wait_async_done(reservation_id, (reqid,))

        if logout:
            self.client.logout(session_id)

    def _wait_async_done(self, reservation_id, reqids):
        """
        _wait_async_done(session_id, reqids)
        Helper methods that waits for the specified asynchronous requests to be finished,
        and which asserts that they were successful. Note that it doesn't actually return
        their responses.
        @param reqids Tuple containing the request ids for the commands to check.
        @return Nothing
        """
        # Wait until send_async_file query is actually finished.
        reqsl = list(reqids)
        max_count = 15
        while len(reqsl) > 0:
            time.sleep(0.1)
            max_count -= 1
            if max_count == 0:
                raise Exception("Maximum time spent waiting async done")
            requests = self.client.check_async_command_status(reservation_id, tuple(reqsl))
            self.assertEquals(len(reqsl), len(requests))
            for rid, req in six.iteritems(requests):
                status = req[0]
                self.assertTrue(status in ("running", "ok", "error"))
                if status != "running":
                    self.assertEquals("ok", status, "Contents: " + req[1])
                    reqsl.remove(rid)

    def _get_async_response(self, reservation_id, reqid):
        """
        _get_async_response(reqids)
        Helper method that synchronously gets the response for the specified async request, asserting that
        it was successful.
        @param reqid The request identifier for the async request whose response we want
        @return Response to the request, if successful. None, otherwise.
        """
        # Wait until send_async_file query is actually finished.
        max_counter = 15
        while True:
            max_counter -= 1
            if max_counter == 0:
                raise Exception("Maximum times running get_async_response")
            time.sleep(0.1)
            requests = self.client.check_async_command_status(reservation_id, (reqid,))
            self.assertEquals(1, len(requests))
            self.assertTrue(reqid in requests)
            req = requests[reqid]
            status = req[0]
            self.assertTrue(status in ("running", "ok", "error"))
            if status != "running":
                self.assertEquals("ok", status, "Contents: " + req[1])
                return Command.Command(req[1])

    def test_single_uses_timeout(self):
        core_experiment_poll_time = 1.5
        core_time_between_checks = 1.5
        self.core_config._set_value('core_experiment_poll_time', core_experiment_poll_time)
        self.core_config._set_value('core_time_between_checks', core_time_between_checks)
        self._single_use(logout = False, plus_async_use = False)
        time.sleep(core_experiment_poll_time + 0.3 + core_time_between_checks)
        self._single_use(logout = False, plus_async_use = False)

    def test_two_multiple_uses_of_different_devices(self):
        user1_session_id = self.client.login('intstudent1','password')

        user1_experiments = self.client.list_experiments(user1_session_id)
        self.assertEquals( 2, len(user1_experiments))

        dummy1_experiments = [ exp.experiment for exp in user1_experiments if exp.experiment.name == 'dummy1' ]
        self.assertEquals( len(dummy1_experiments), 1)

        # reserve it
        status = self.client.reserve_experiment( user1_session_id, dummy1_experiments[0].to_experiment_id(), "{}", "{}")
        user1_reservation_id = status.reservation_id


        user2_session_id = self.client.login('intstudent2','password')

        user2_experiments = self.client.list_experiments(user2_session_id)
        self.assertEquals( 2, len(user2_experiments))

        dummy2_experiments = [ exp.experiment for exp in user2_experiments if exp.experiment.name == 'dummy2' ]
        self.assertEquals( len(dummy2_experiments), 1)

        # reserve it
        status = self.client.reserve_experiment(user2_session_id, dummy2_experiments[0].to_experiment_id(), "{}", "{}")
        user2_reservation_id = status.reservation_id



        short_time = 0.1
        times      = 9.0 / short_time

        while times > 0:
            time.sleep(short_time)

            new_status1 = self.client.get_reservation_status(user1_reservation_id)

            new_status2 = self.client.get_reservation_status(user2_reservation_id)

            if not isinstance(new_status1, Reservation.WaitingConfirmationReservation):
                if not isinstance(new_status2, Reservation.WaitingConfirmationReservation):
                    break
            times -= 1

        self.assertTrue(isinstance(self.client.get_reservation_status(user1_reservation_id), Reservation.ConfirmedReservation))
        self.assertTrue(isinstance(self.client.get_reservation_status(user2_reservation_id), Reservation.ConfirmedReservation))

        # send a program
        CONTENT1 = "content of the program DUMMY1"
        response = self.client.send_file(user1_reservation_id, ExperimentUtil.serialize(CONTENT1), 'program')
        self.assertEquals('ack', response.commandstring)

        # We need to wait for the programming to finish.
        respcmd = self.client.send_command(user1_reservation_id, Command.Command("STATE DUMMY1"))
        response = respcmd.get_command_string()

        # Check that the current state is "Ready"
        self.assertEquals("STATE DUMMY1", response)

        CONTENT2 = "content of the program PLD"
        response = self.client.send_file(user2_reservation_id, ExperimentUtil.serialize(CONTENT2), 'program')
        self.assertEquals('ack', response.commandstring)

        # We need to wait for the programming to finish.
        respcmd = self.client.send_command(user2_reservation_id, Command.Command("STATE DUMMY2"))
        # Check that the current state is "Ready"
        self.assertEquals("STATE DUMMY2", respcmd.commandstring)

        # end session
        self.client.logout(user1_session_id)
        self.client.logout(user2_session_id)
示例#25
0
    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)
示例#26
0
 def create_client(self):
     return WebLabDeustoClient(self.address)
class IntegrationMultipleUsersTestCase(object):
    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 tearDown(self):
        GLOBAL_REGISTRY.clear()

        for process_handler in self.process_handlers:
            process_handler.stop()

    def test_single_uses_timeout(self):
        # 6 users get into the system
        session_id1 = self.client.login('intstudent1', 'password')
        session_id2 = self.client.login('intstudent2', 'password')
        session_id3 = self.client.login('intstudent3', 'password')
        session_id4 = self.client.login('intstudent4', 'password')
        session_id5 = self.client.login('intstudent5', 'password')
        session_id6 = self.client.login('intstudent6', 'password')

        # they all have access to the ud-fpga experiment
        experiments1 = self.client.list_experiments(session_id1)
        dummy_experiments1 = [
            exp.experiment for exp in experiments1
            if exp.experiment.name == 'dummy1'
        ]
        self.assertEquals(len(dummy_experiments1), 1)

        experiments2 = self.client.list_experiments(session_id2)
        dummy_experiments2 = [
            exp.experiment for exp in experiments2
            if exp.experiment.name == 'dummy1'
        ]
        self.assertEquals(len(dummy_experiments2), 1)

        experiments3 = self.client.list_experiments(session_id3)
        dummy_experiments3 = [
            exp.experiment for exp in experiments3
            if exp.experiment.name == 'dummy1'
        ]
        self.assertEquals(len(dummy_experiments3), 1)

        experiments4 = self.client.list_experiments(session_id4)
        dummy_experiments4 = [
            exp.experiment for exp in experiments4
            if exp.experiment.name == 'dummy1'
        ]
        self.assertEquals(len(dummy_experiments4), 1)

        experiments5 = self.client.list_experiments(session_id5)
        dummy_experiments5 = [
            exp.experiment for exp in experiments5
            if exp.experiment.name == 'dummy1'
        ]
        self.assertEquals(len(dummy_experiments5), 1)

        experiments6 = self.client.list_experiments(session_id6)
        dummy_experiments6 = [
            exp.experiment for exp in experiments6
            if exp.experiment.name == 'dummy1'
        ]
        self.assertEquals(len(dummy_experiments6), 1)

        # 3 users try to reserve the experiment
        status1 = self.client.reserve_experiment(
            session_id1, dummy_experiments1[0].to_experiment_id(), "{}", "{}")
        reservation_id1 = status1.reservation_id

        status2 = self.client.reserve_experiment(
            session_id2, dummy_experiments2[0].to_experiment_id(), "{}", "{}")
        reservation_id2 = status2.reservation_id

        status3 = self.client.reserve_experiment(
            session_id3, dummy_experiments3[0].to_experiment_id(), "{}", "{}")
        reservation_id3 = status3.reservation_id

        # wait until it is reserved
        short_time = 0.1
        times = 10.0 / short_time

        while times > 0:
            time.sleep(short_time)
            new_status = self.client.get_reservation_status(reservation_id1)
            if not isinstance(new_status,
                              Reservation.WaitingConfirmationReservation):
                break
            times -= 1

        # first user got the device. The other two are in WaitingReservation
        reservation1 = self.client.get_reservation_status(reservation_id1)
        self.assertTrue(
            isinstance(reservation1, Reservation.ConfirmedReservation))

        reservation2 = self.client.get_reservation_status(reservation_id2)
        self.assertTrue(
            isinstance(reservation2, Reservation.WaitingReservation))
        self.assertEquals(0, reservation2.position)

        reservation3 = self.client.get_reservation_status(reservation_id3)
        self.assertTrue(
            isinstance(reservation3, Reservation.WaitingReservation))
        self.assertEquals(1, reservation3.position)

        # Another user tries to reserve the experiment. He goes to the WaitingReservation, position 2
        status4 = self.client.reserve_experiment(
            session_id4, dummy_experiments4[0].to_experiment_id(), "{}", "{}")

        reservation_id4 = status4.reservation_id

        reservation4 = self.client.get_reservation_status(reservation_id4)
        self.assertTrue(
            isinstance(reservation4, Reservation.WaitingReservation))
        self.assertEquals(2, reservation4.position)

        # The state of other users does not change
        reservation1 = self.client.get_reservation_status(reservation_id1)
        self.assertTrue(
            isinstance(reservation1, Reservation.ConfirmedReservation))

        reservation2 = self.client.get_reservation_status(reservation_id2)
        self.assertTrue(
            isinstance(reservation2, Reservation.WaitingReservation))
        self.assertEquals(0, reservation2.position)

        reservation3 = self.client.get_reservation_status(reservation_id3)
        self.assertTrue(
            isinstance(reservation3, Reservation.WaitingReservation))
        self.assertEquals(1, reservation3.position)

        # The user number 2 frees the experiment
        self.client.finished_experiment(reservation_id2)

        # Whenever he tries to do poll or send_command, he receives an exception
        try:
            time.sleep(1)
            self.client.poll(reservation_id2)
            self.client.poll(reservation_id2)
            self.client.poll(reservation_id2)
        except Exception as e:
            pass  # All right :-)
            self.assertTrue("does not have any experiment" in repr(e))
        else:
            self.fail("Expected exception when polling")

        # send a program
        CONTENT = "content of the program DUMMY1"
        self.client.send_file(reservation_id1,
                              ExperimentUtil.serialize(CONTENT), 'program')

        # We need to wait for the programming to finish.
        response = self.client.send_command(reservation_id1,
                                            Command.Command("STATE"))
        self.assertEquals("STATE", response.commandstring)

        self.client.logout(session_id1)
示例#28
0
    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)
class IntegrationNoConcurrencyTestCase(object):
    def setUp(self):
        GLOBAL_REGISTRY.clear()
        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 tearDown(self):
        GLOBAL_REGISTRY.clear()

        for process_handler in self.process_handlers:
            process_handler.stop()

    def test_simple_single_uses(self):
        for _ in range(1):
            self._single_use()
        self._single_use()
        self._single_use()

    def _single_use(self, logout=True, plus_async_use=True):
        """
        Will use an experiment.
        @param logout If true, the user will be logged out after the use. Otherwise not.
        @param plus_async_use If true, after using the experiment synchronously, it will use it
        again using the asynchronous versions of the send_command and send_file requests.
        """
        self._single_sync_use(logout)
        if plus_async_use:
            self._single_async_use(logout)

    def _single_sync_use(self, logout=True):
        session_id, reservation_id = self._get_reserved()

        CONTENT = "content of the program FPGA"
        response = self.client.send_file(reservation_id,
                                         ExperimentUtil.serialize(CONTENT),
                                         'program')
        self.assertEquals(response.commandstring, 'ack')

        response = self.client.send_command(reservation_id,
                                            Command.Command("STATE"))
        self.assertEquals(response.commandstring, 'STATE')

        response = self.client.send_command(
            reservation_id, Command.Command("ChangeSwitch on 0"))
        self.assertEquals(response.commandstring, "ChangeSwitch on 0")

        if logout:
            self.client.logout(session_id)

    def _get_reserved(self):
        session_id = self.client.login('intstudent1', 'password')

        user_information = self.client.get_user_information(session_id)
        self.assertEquals('intstudent1', user_information.login)
        self.assertEquals('Name of integration test 1',
                          user_information.full_name)
        self.assertEquals('*****@*****.**', user_information.email)

        experiments = self.client.list_experiments(session_id)
        self.assertEquals(2, len(experiments))

        dummy1_experiments = [
            exp.experiment for exp in experiments
            if exp.experiment.name == 'dummy1'
        ]
        self.assertEquals(len(dummy1_experiments), 1)

        dummy1_experiment = dummy1_experiments[0]

        status = self.client.reserve_experiment(
            session_id, dummy1_experiment.to_experiment_id(), "{}", "{}")

        reservation_id = status.reservation_id

        # wait until it is reserved
        short_time = 0.1

        # Time extended from 9.0 to 15.0 because at times the test failed, possibly for that reason.
        times = 15.0 / short_time

        while times > 0:
            new_status = self.client.get_reservation_status(reservation_id)
            if not isinstance(
                    new_status, Reservation.WaitingConfirmationReservation
            ) and not isinstance(new_status, Reservation.WaitingReservation):
                break
            times -= 1
            time.sleep(short_time)
        reservation = self.client.get_reservation_status(reservation_id)
        self.assertTrue(
            isinstance(reservation, Reservation.ConfirmedReservation),
            "Reservation %s is not Confirmed, as expected by this time" %
            reservation)
        return session_id, reservation_id

    def _single_async_use(self, logout=True):
        session_id, reservation_id = self._get_reserved()

        # send the program again, but asynchronously. Though this should work, it is not really very customary
        # to send_file more than once in the same session. In fact, it is a feature which might get removed in
        # the future. When/if that happens, this will need to be modified.
        CONTENT = "content of the program FPGA"
        reqid = self.client.send_async_file(reservation_id,
                                            ExperimentUtil.serialize(CONTENT),
                                            'program')

        # Wait until send_async_file query is actually finished.
        #self._get_async_response(session_id, reqid)
        self._wait_async_done(reservation_id, (reqid, ))

        # We need to wait for the programming to finish, while at the same
        # time making sure that the tests don't dead-lock.
        reqid = self.client.send_async_command(reservation_id,
                                               Command.Command("STATE"))
        respcmd = self._get_async_response(reservation_id, reqid)
        response = respcmd.get_command_string()

        # Check that the current state is "Ready"
        self.assertEquals("STATE", response)

        reqid = self.client.send_async_command(
            reservation_id, Command.Command("ChangeSwitch on 0"))
        self._wait_async_done(reservation_id, (reqid, ))

        reqid = self.client.send_async_command(
            reservation_id, Command.Command("ClockActivation on 250"))
        self._wait_async_done(reservation_id, (reqid, ))

        if logout:
            self.client.logout(session_id)

    def _wait_async_done(self, reservation_id, reqids):
        """
        _wait_async_done(session_id, reqids)
        Helper methods that waits for the specified asynchronous requests to be finished,
        and which asserts that they were successful. Note that it doesn't actually return
        their responses.
        @param reqids Tuple containing the request ids for the commands to check.
        @return Nothing
        """
        # Wait until send_async_file query is actually finished.
        reqsl = list(reqids)
        max_count = 15
        while len(reqsl) > 0:
            time.sleep(0.1)
            max_count -= 1
            if max_count == 0:
                raise Exception("Maximum time spent waiting async done")
            requests = self.client.check_async_command_status(
                reservation_id, tuple(reqsl))
            self.assertEquals(len(reqsl), len(requests))
            for rid, req in six.iteritems(requests):
                status = req[0]
                self.assertTrue(status in ("running", "ok", "error"))
                if status != "running":
                    self.assertEquals("ok", status, "Contents: " + req[1])
                    reqsl.remove(rid)

    def _get_async_response(self, reservation_id, reqid):
        """
        _get_async_response(reqids)
        Helper method that synchronously gets the response for the specified async request, asserting that
        it was successful.
        @param reqid The request identifier for the async request whose response we want
        @return Response to the request, if successful. None, otherwise.
        """
        # Wait until send_async_file query is actually finished.
        max_counter = 15
        while True:
            max_counter -= 1
            if max_counter == 0:
                raise Exception("Maximum times running get_async_response")
            time.sleep(0.1)
            requests = self.client.check_async_command_status(
                reservation_id, (reqid, ))
            self.assertEquals(1, len(requests))
            self.assertTrue(reqid in requests)
            req = requests[reqid]
            status = req[0]
            self.assertTrue(status in ("running", "ok", "error"))
            if status != "running":
                self.assertEquals("ok", status, "Contents: " + req[1])
                return Command.Command(req[1])

    def test_single_uses_timeout(self):
        core_experiment_poll_time = 1.5
        core_time_between_checks = 1.5
        self.core_config._set_value('core_experiment_poll_time',
                                    core_experiment_poll_time)
        self.core_config._set_value('core_time_between_checks',
                                    core_time_between_checks)
        self._single_use(logout=False, plus_async_use=False)
        time.sleep(core_experiment_poll_time + 0.3 + core_time_between_checks)
        self._single_use(logout=False, plus_async_use=False)

    def test_two_multiple_uses_of_different_devices(self):
        user1_session_id = self.client.login('intstudent1', 'password')

        user1_experiments = self.client.list_experiments(user1_session_id)
        self.assertEquals(2, len(user1_experiments))

        dummy1_experiments = [
            exp.experiment for exp in user1_experiments
            if exp.experiment.name == 'dummy1'
        ]
        self.assertEquals(len(dummy1_experiments), 1)

        # reserve it
        status = self.client.reserve_experiment(
            user1_session_id, dummy1_experiments[0].to_experiment_id(), "{}",
            "{}")
        user1_reservation_id = status.reservation_id

        user2_session_id = self.client.login('intstudent2', 'password')

        user2_experiments = self.client.list_experiments(user2_session_id)
        self.assertEquals(2, len(user2_experiments))

        dummy2_experiments = [
            exp.experiment for exp in user2_experiments
            if exp.experiment.name == 'dummy2'
        ]
        self.assertEquals(len(dummy2_experiments), 1)

        # reserve it
        status = self.client.reserve_experiment(
            user2_session_id, dummy2_experiments[0].to_experiment_id(), "{}",
            "{}")
        user2_reservation_id = status.reservation_id

        short_time = 0.1
        times = 9.0 / short_time

        while times > 0:
            time.sleep(short_time)

            new_status1 = self.client.get_reservation_status(
                user1_reservation_id)

            new_status2 = self.client.get_reservation_status(
                user2_reservation_id)

            if not isinstance(new_status1,
                              Reservation.WaitingConfirmationReservation):
                if not isinstance(new_status2,
                                  Reservation.WaitingConfirmationReservation):
                    break
            times -= 1

        self.assertTrue(
            isinstance(
                self.client.get_reservation_status(user1_reservation_id),
                Reservation.ConfirmedReservation))
        self.assertTrue(
            isinstance(
                self.client.get_reservation_status(user2_reservation_id),
                Reservation.ConfirmedReservation))

        # send a program
        CONTENT1 = "content of the program DUMMY1"
        response = self.client.send_file(user1_reservation_id,
                                         ExperimentUtil.serialize(CONTENT1),
                                         'program')
        self.assertEquals('ack', response.commandstring)

        # We need to wait for the programming to finish.
        respcmd = self.client.send_command(user1_reservation_id,
                                           Command.Command("STATE DUMMY1"))
        response = respcmd.get_command_string()

        # Check that the current state is "Ready"
        self.assertEquals("STATE DUMMY1", response)

        CONTENT2 = "content of the program PLD"
        response = self.client.send_file(user2_reservation_id,
                                         ExperimentUtil.serialize(CONTENT2),
                                         'program')
        self.assertEquals('ack', response.commandstring)

        # We need to wait for the programming to finish.
        respcmd = self.client.send_command(user2_reservation_id,
                                           Command.Command("STATE DUMMY2"))
        # Check that the current state is "Ready"
        self.assertEquals("STATE DUMMY2", respcmd.commandstring)

        # end session
        self.client.logout(user1_session_id)
        self.client.logout(user2_session_id)
示例#30
0
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))
class AbstractFederatedWebLabDeustoTestCase(object):
    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 tearDown(self):
        self.consumer_handler.stop()
        self.provider1_handler.stop()
        self.provider2_handler.stop()
        time.sleep(1)

    #
    # This test may take even 20-30 seconds; therefore it is not splitted
    # into subtests (the setup and teardown are long)
    #
    def test_federated_experiment(self):
        debug("Test test_federated_experiment starts")

        #######################################################
        #
        #   Local testing  (to check that everything is right)
        #
        #   We enter as a student of Consumer, and we ask for an
        #   experiment that only the Consumer university has
        #   (dummy2).
        #
        session_id = self.consumer_client.login('fedstudent1', 'password')

        reservation_id = self._test_reservation(session_id, self.dummy2, 'Consumer', True, True)
        self._wait_multiple_reservations(20, session_id, [ reservation_id ], [0])
        reservation_result = self.consumer_client.get_experiment_use_by_id(session_id, reservation_id)
        self.assertTrue(reservation_result.is_finished())
        self._find_command(reservation_result, 'Consumer')


        #######################################################
        #
        #   Simple federation
        #
        #   Now we ask for an experiment that only Provider 1
        #   has. There is no load balance, neither
        #   subcontracting
        #
        reservation_id = self._test_reservation(session_id, self.dummy3, 'Provider 1', True, True)
        self._wait_multiple_reservations(20, session_id, [ reservation_id ], [0])
        reservation_result = self.consumer_client.get_experiment_use_by_id(session_id, reservation_id)
        self.assertTrue(reservation_result.is_finished())
        self._find_command(reservation_result, 'Provider 1')

        #######################################################
        #
        #   Subcontracted federation
        #
        #   Now we ask for an experiment that only Provider 2
        #   has. There is no load balance, but Consumer will
        #   contact Provider 1, which will contact Provider 2
        #
        reservation_id = self._test_reservation(session_id, self.dummy4, 'Provider 2', True, True)
        self._wait_multiple_reservations(20, session_id, [ reservation_id ], [0])
        reservation_result = self.consumer_client.get_experiment_use_by_id(session_id, reservation_id)
        self.assertTrue(reservation_result.is_finished())
        self._find_command(reservation_result, 'Provider 2')

        #######################################################
        #
        #   Cross-domain load balancing
        #
        #   Now we ask for an experiment that Consumer has,
        #   but also Provider 1 and Provider 2.
        #

        reservation_id1 = self._test_reservation(session_id, self.dummy1, 'Consumer',  True, False,  user_agent = 'Chrome')
        reservation_id2 = self._test_reservation(session_id, self.dummy1, 'Provider 1', True, False, user_agent = 'Firefox')
        reservation_id3 = self._test_reservation(session_id, self.dummy1, 'Provider 2', True, False, user_agent = 'Safari')

        reservation_ids = (reservation_id1, reservation_id2, reservation_id3)
        reservation_results = self.consumer_client.get_experiment_uses_by_id(session_id, reservation_ids)

        self.assertEquals(RunningReservationResult(), reservation_results[0])
        self.assertEquals(RunningReservationResult(), reservation_results[1])
        self.assertEquals(RunningReservationResult(), reservation_results[2])


        #
        # What if one of them goes out and another comes? Is the load of experiments balanced correctly?
        #
        self.consumer_client.finished_experiment(reservation_id2)

        # Wait a couple of seconds to check that it has been propagated
        self._wait_multiple_reservations(20, session_id, reservation_ids, [1])

        reservation_results = self.consumer_client.get_experiment_uses_by_id(session_id, reservation_ids)

        # The other two are still running
        self.assertEquals(RunningReservationResult(), reservation_results[0])
        self.assertEquals(RunningReservationResult(), reservation_results[2])
        
        # But the one finished is actually finished
        self.assertTrue( reservation_results[1].is_finished() ) 
        self.assertEquals('Firefox', reservation_results[1].experiment_use.request_info['user_agent'])
        self.assertEquals(4, len(reservation_results[1].experiment_use.commands))
        self.assertEquals('Provider 1', reservation_results[1].experiment_use.commands[2].response.commandstring)

        reservation_id2b = self._test_reservation(session_id, self.dummy1, 'Provider 1', True, False)

        self.consumer_client.finished_experiment(reservation_id1)
        self._test_reservation(session_id, self.dummy1, 'Consumer', True, False)
        reservation_status = self.consumer_client.get_reservation_status(reservation_id3)
        provider2_reservation_id = reservation_status.remote_reservation_id

        self.consumer_client.finished_experiment(reservation_id3)
        self._test_reservation(session_id, self.dummy1, 'Provider 2', True, False)

        # Check for the other uses
        self._wait_multiple_reservations(70, session_id, reservation_ids, [0,2])

        reservation_results = self.consumer_client.get_experiment_uses_by_id(session_id, reservation_ids)
        self.assertTrue( reservation_results[0].is_finished() )
        self.assertEquals('Chrome', reservation_results[0].experiment_use.request_info['user_agent'])
        self.assertEquals('Consumer', reservation_results[0].experiment_use.commands[2].response.commandstring)

        self.assertTrue( reservation_results[2].is_finished() )
        self.assertEquals('Safari', reservation_results[2].experiment_use.request_info['user_agent'])
        self.assertEquals('Provider 2', reservation_results[2].experiment_use.commands[2].response.commandstring)

        provider2_session_id = self.provider2_client.login('provider1', 'password')
        provider2_result = self.provider2_client.get_experiment_use_by_id(provider2_session_id, provider2_reservation_id)
        self.assertTrue(provider2_result.is_finished())
        self.assertEquals('Safari', provider2_result.experiment_use.request_info['user_agent'])

        #
        # What if another 2 come in? What is the position of their queues?
        #

        reservation_4 = self._test_reservation(session_id, self.dummy1, '', False, False)
        reservation_status = self.consumer_client.get_reservation_status(reservation_4)
        self.assertEquals(Reservation.WAITING, reservation_status.status)
        self.assertEquals(0, reservation_status.position)

        reservation_5 = self._test_reservation(session_id, self.dummy1, '', False, False)
        reservation_status = self.consumer_client.get_reservation_status(reservation_5)
        self.assertEquals(Reservation.WAITING, reservation_status.status)
        self.assertEquals(1, reservation_status.position)

        #
        # Once again, freeing a session affects them?
        #
        self.consumer_client.finished_experiment(reservation_id2b)
        self._wait_reservation(reservation_4, 'Provider 1', True)

        self.consumer_client.finished_experiment(reservation_4)
        self._wait_reservation(reservation_5, 'Provider 1', True)

        # Check for the other uses
        for _ in range(50):
            time.sleep(0.5)
            # Checking every half second
            results = self.consumer_client.get_experiment_uses_by_id(session_id, (reservation_id2b, reservation_4))
            if results[0].is_finished() and results[1].is_finished():
                break

        final_reservation_results = self.consumer_client.get_experiment_uses_by_id(session_id, (reservation_id2b, reservation_4))
        self.assertTrue(final_reservation_results[0].is_finished())
        self.assertTrue(final_reservation_results[1].is_finished())
        self.assertEquals('Provider 1', final_reservation_results[0].experiment_use.commands[2].response.commandstring)
        self.assertEquals('Provider 1', final_reservation_results[1].experiment_use.commands[2].response.commandstring)

        debug("Test test_federated_experiment finishes successfully")

    def _wait_multiple_reservations(self, times, session_id, reservation_ids, reservations_to_wait):
        for _ in range(times):
            time.sleep(0.5)
            # Checking every half second
            results = self.consumer_client.get_experiment_uses_by_id(session_id, reservation_ids)
            all_finished = True

            for reservation_to_wait in reservations_to_wait:
                all_finished = all_finished and results[reservation_to_wait].is_finished()

            if all_finished:
                break

    def _find_command(self, reservation_result, expected_response):
        found = False
        commands = reservation_result.experiment_use.commands
        for command in commands:
            if command.command.commandstring == 'server_info':
                found = True
                response = command.response.commandstring
                self.assertEquals(expected_response, response, "Message %s not found in commands %s; instead found %s" % (expected_response, commands, response))
        self.assertTrue(found, "server_info not found in commands")

    def _test_reservation(self, session_id, experiment_id, expected_server_info, wait, finish, user_agent = None):
        debug("Reserving with session_id %r a experiment %r; will I wait? %s; will I finish? %s" % (session_id, experiment_id, wait, finish))
        reservation_status = self.consumer_client.reserve_experiment(session_id, experiment_id, "{}", "{}", user_agent = user_agent)

        reservation_id = reservation_status.reservation_id

        if not wait:
            if finish:
                debug("Finishing... %r" % reservation_id)
                self.consumer_client.finished_experiment(reservation_id)
            debug("Not waiting... %r" % reservation_id)
            return reservation_id

        reservation_id = self._wait_reservation(reservation_id, expected_server_info, finish)
        debug("Finished waiting... %r" % reservation_id)
        return reservation_id

    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 _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
示例#33
0
class IntegrationMultipleUsersTestCase(object):
    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 tearDown(self):
        GLOBAL_REGISTRY.clear()

        for process_handler in self.process_handlers:
            process_handler.stop()

    def test_single_uses_timeout(self):
        # 6 users get into the system
        session_id1 = self.client.login('intstudent1','password')
        session_id2 = self.client.login('intstudent2','password')
        session_id3 = self.client.login('intstudent3','password')
        session_id4 = self.client.login('intstudent4','password')
        session_id5 = self.client.login('intstudent5','password')
        session_id6 = self.client.login('intstudent6','password')

        # they all have access to the ud-fpga experiment
        experiments1 = self.client.list_experiments(session_id1)
        dummy_experiments1 = [ exp.experiment for exp in experiments1 if exp.experiment.name == 'dummy1' ]
        self.assertEquals( len(dummy_experiments1), 1 )

        experiments2 = self.client.list_experiments(session_id2)
        dummy_experiments2 = [ exp.experiment for exp in experiments2 if exp.experiment.name == 'dummy1' ]
        self.assertEquals( len(dummy_experiments2), 1 )

        experiments3 = self.client.list_experiments(session_id3)
        dummy_experiments3 = [ exp.experiment for exp in experiments3 if exp.experiment.name == 'dummy1' ]
        self.assertEquals( len(dummy_experiments3), 1 )

        experiments4 = self.client.list_experiments(session_id4)
        dummy_experiments4 = [ exp.experiment for exp in experiments4 if exp.experiment.name == 'dummy1' ]
        self.assertEquals( len(dummy_experiments4), 1 )

        experiments5 = self.client.list_experiments(session_id5)
        dummy_experiments5 = [ exp.experiment for exp in experiments5 if exp.experiment.name == 'dummy1' ]
        self.assertEquals( len(dummy_experiments5), 1 )

        experiments6 = self.client.list_experiments(session_id6)
        dummy_experiments6 = [ exp.experiment for exp in experiments6 if exp.experiment.name == 'dummy1' ]
        self.assertEquals( len(dummy_experiments6), 1 )

        # 3 users try to reserve the experiment
        status1 = self.client.reserve_experiment(session_id1, dummy_experiments1[0].to_experiment_id(), "{}", "{}")
        reservation_id1 = status1.reservation_id

        status2 = self.client.reserve_experiment(session_id2, dummy_experiments2[0].to_experiment_id(), "{}", "{}")
        reservation_id2 = status2.reservation_id

        status3 = self.client.reserve_experiment(session_id3, dummy_experiments3[0].to_experiment_id(), "{}", "{}")
        reservation_id3 = status3.reservation_id

        # wait until it is reserved
        short_time = 0.1
        times      = 10.0 / short_time

        while times > 0:
            time.sleep(short_time)
            new_status = self.client.get_reservation_status(reservation_id1)
            if not isinstance(new_status, Reservation.WaitingConfirmationReservation):
                break
            times -= 1

        # first user got the device. The other two are in WaitingReservation
        reservation1 = self.client.get_reservation_status(reservation_id1)
        self.assertTrue(isinstance(reservation1, Reservation.ConfirmedReservation))

        reservation2 = self.client.get_reservation_status(reservation_id2)
        self.assertTrue(isinstance(reservation2, Reservation.WaitingReservation))
        self.assertEquals( 0, reservation2.position)

        reservation3 = self.client.get_reservation_status(reservation_id3)
        self.assertTrue(isinstance(reservation3, Reservation.WaitingReservation))
        self.assertEquals( 1, reservation3.position)

        # Another user tries to reserve the experiment. He goes to the WaitingReservation, position 2
        status4 = self.client.reserve_experiment(session_id4, dummy_experiments4[0].to_experiment_id(), "{}", "{}")

        reservation_id4 = status4.reservation_id

        reservation4 = self.client.get_reservation_status(reservation_id4)
        self.assertTrue(isinstance( reservation4, Reservation.WaitingReservation))
        self.assertEquals( 2, reservation4.position)

        # The state of other users does not change
        reservation1 = self.client.get_reservation_status(reservation_id1)
        self.assertTrue(isinstance( reservation1, Reservation.ConfirmedReservation))

        reservation2 = self.client.get_reservation_status(reservation_id2)
        self.assertTrue(isinstance( reservation2, Reservation.WaitingReservation))
        self.assertEquals( 0, reservation2.position)

        reservation3 = self.client.get_reservation_status(reservation_id3)
        self.assertTrue(isinstance(reservation3, Reservation.WaitingReservation))
        self.assertEquals( 1, reservation3.position )

        # The user number 2 frees the experiment
        self.client.finished_experiment(reservation_id2)

        # Whenever he tries to do poll or send_command, he receives an exception
        try:
            time.sleep(1)
            self.client.poll(reservation_id2)
            self.client.poll(reservation_id2)
            self.client.poll(reservation_id2)
        except Exception as e:
            pass # All right :-)
            self.assertTrue("does not have any experiment" in repr(e))
        else:
            self.fail("Expected exception when polling")

        # send a program
        CONTENT = "content of the program DUMMY1"
        self.client.send_file(reservation_id1, ExperimentUtil.serialize(CONTENT), 'program')


        # We need to wait for the programming to finish.
        response = self.client.send_command(reservation_id1, Command.Command("STATE"))
        self.assertEquals("STATE", response.commandstring)

        self.client.logout(session_id1)
示例#34
0
 def _create_client(self, cookies=None):
     client = WebLabDeustoClient(self.baseurl)
     if cookies is not None:
         client.set_cookies(cookies)
     return client
示例#35
0
class AbstractFederatedWebLabDeustoTestCase(object):
    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 tearDown(self):
        self.consumer_handler.stop()
        self.provider1_handler.stop()
        self.provider2_handler.stop()
        time.sleep(1)

    #
    # This test may take even 20-30 seconds; therefore it is not splitted
    # into subtests (the setup and teardown are long)
    #
    def test_federated_experiment(self):
        debug("Test test_federated_experiment starts")

        #######################################################
        #
        #   Local testing  (to check that everything is right)
        #
        #   We enter as a student of Consumer, and we ask for an
        #   experiment that only the Consumer university has
        #   (dummy2).
        #
        session_id = self.consumer_client.login('fedstudent1', 'password')

        reservation_id = self._test_reservation(session_id, self.dummy2,
                                                'Consumer', True, True)
        self._wait_multiple_reservations(20, session_id, [reservation_id], [0])
        reservation_result = self.consumer_client.get_experiment_use_by_id(
            session_id, reservation_id)
        self.assertTrue(reservation_result.is_finished())
        self._find_command(reservation_result, 'Consumer')

        #######################################################
        #
        #   Simple federation
        #
        #   Now we ask for an experiment that only Provider 1
        #   has. There is no load balance, neither
        #   subcontracting
        #
        reservation_id = self._test_reservation(session_id, self.dummy3,
                                                'Provider 1', True, True)
        self._wait_multiple_reservations(20, session_id, [reservation_id], [0])
        reservation_result = self.consumer_client.get_experiment_use_by_id(
            session_id, reservation_id)
        self.assertTrue(reservation_result.is_finished())
        self._find_command(reservation_result, 'Provider 1')

        #######################################################
        #
        #   Subcontracted federation
        #
        #   Now we ask for an experiment that only Provider 2
        #   has. There is no load balance, but Consumer will
        #   contact Provider 1, which will contact Provider 2
        #
        reservation_id = self._test_reservation(session_id, self.dummy4,
                                                'Provider 2', True, True)
        self._wait_multiple_reservations(20, session_id, [reservation_id], [0])
        reservation_result = self.consumer_client.get_experiment_use_by_id(
            session_id, reservation_id)
        self.assertTrue(reservation_result.is_finished())
        self._find_command(reservation_result, 'Provider 2')

        #######################################################
        #
        #   Cross-domain load balancing
        #
        #   Now we ask for an experiment that Consumer has,
        #   but also Provider 1 and Provider 2.
        #

        reservation_id1 = self._test_reservation(session_id,
                                                 self.dummy1,
                                                 'Consumer',
                                                 True,
                                                 False,
                                                 user_agent='Chrome')
        reservation_id2 = self._test_reservation(session_id,
                                                 self.dummy1,
                                                 'Provider 1',
                                                 True,
                                                 False,
                                                 user_agent='Firefox')
        reservation_id3 = self._test_reservation(session_id,
                                                 self.dummy1,
                                                 'Provider 2',
                                                 True,
                                                 False,
                                                 user_agent='Safari')

        reservation_ids = (reservation_id1, reservation_id2, reservation_id3)
        reservation_results = self.consumer_client.get_experiment_uses_by_id(
            session_id, reservation_ids)

        self.assertEquals(RunningReservationResult(), reservation_results[0])
        self.assertEquals(RunningReservationResult(), reservation_results[1])
        self.assertEquals(RunningReservationResult(), reservation_results[2])

        #
        # What if one of them goes out and another comes? Is the load of experiments balanced correctly?
        #
        self.consumer_client.finished_experiment(reservation_id2)

        # Wait a couple of seconds to check that it has been propagated
        self._wait_multiple_reservations(20, session_id, reservation_ids, [1])

        reservation_results = self.consumer_client.get_experiment_uses_by_id(
            session_id, reservation_ids)

        # The other two are still running
        self.assertEquals(RunningReservationResult(), reservation_results[0])
        self.assertEquals(RunningReservationResult(), reservation_results[2])

        # But the one finished is actually finished
        self.assertTrue(reservation_results[1].is_finished())
        self.assertEquals(
            'Firefox',
            reservation_results[1].experiment_use.request_info['user_agent'])
        self.assertEquals(4,
                          len(reservation_results[1].experiment_use.commands))
        self.assertEquals(
            'Provider 1', reservation_results[1].experiment_use.commands[2].
            response.commandstring)

        reservation_id2b = self._test_reservation(session_id, self.dummy1,
                                                  'Provider 1', True, False)

        self.consumer_client.finished_experiment(reservation_id1)
        self._test_reservation(session_id, self.dummy1, 'Consumer', True,
                               False)
        reservation_status = self.consumer_client.get_reservation_status(
            reservation_id3)
        provider2_reservation_id = reservation_status.remote_reservation_id

        self.consumer_client.finished_experiment(reservation_id3)
        self._test_reservation(session_id, self.dummy1, 'Provider 2', True,
                               False)

        # Check for the other uses
        self._wait_multiple_reservations(70, session_id, reservation_ids,
                                         [0, 2])

        reservation_results = self.consumer_client.get_experiment_uses_by_id(
            session_id, reservation_ids)
        self.assertTrue(reservation_results[0].is_finished())
        self.assertEquals(
            'Chrome',
            reservation_results[0].experiment_use.request_info['user_agent'])
        self.assertEquals(
            'Consumer', reservation_results[0].experiment_use.commands[2].
            response.commandstring)

        self.assertTrue(reservation_results[2].is_finished())
        self.assertEquals(
            'Safari',
            reservation_results[2].experiment_use.request_info['user_agent'])
        self.assertEquals(
            'Provider 2', reservation_results[2].experiment_use.commands[2].
            response.commandstring)

        provider2_session_id = self.provider2_client.login(
            'provider1', 'password')
        provider2_result = self.provider2_client.get_experiment_use_by_id(
            provider2_session_id, provider2_reservation_id)
        self.assertTrue(provider2_result.is_finished())
        self.assertEquals(
            'Safari',
            provider2_result.experiment_use.request_info['user_agent'])

        #
        # What if another 2 come in? What is the position of their queues?
        #

        reservation_4 = self._test_reservation(session_id, self.dummy1, '',
                                               False, False)
        reservation_status = self.consumer_client.get_reservation_status(
            reservation_4)
        self.assertEquals(Reservation.WAITING, reservation_status.status)
        self.assertEquals(0, reservation_status.position)

        reservation_5 = self._test_reservation(session_id, self.dummy1, '',
                                               False, False)
        reservation_status = self.consumer_client.get_reservation_status(
            reservation_5)
        self.assertEquals(Reservation.WAITING, reservation_status.status)
        self.assertEquals(1, reservation_status.position)

        #
        # Once again, freeing a session affects them?
        #
        self.consumer_client.finished_experiment(reservation_id2b)
        self._wait_reservation(reservation_4, 'Provider 1', True)

        self.consumer_client.finished_experiment(reservation_4)
        self._wait_reservation(reservation_5, 'Provider 1', True)

        # Check for the other uses
        for _ in range(50):
            time.sleep(0.5)
            # Checking every half second
            results = self.consumer_client.get_experiment_uses_by_id(
                session_id, (reservation_id2b, reservation_4))
            if results[0].is_finished() and results[1].is_finished():
                break

        final_reservation_results = self.consumer_client.get_experiment_uses_by_id(
            session_id, (reservation_id2b, reservation_4))
        self.assertTrue(final_reservation_results[0].is_finished())
        self.assertTrue(final_reservation_results[1].is_finished())
        self.assertEquals(
            'Provider 1', final_reservation_results[0].experiment_use.
            commands[2].response.commandstring)
        self.assertEquals(
            'Provider 1', final_reservation_results[1].experiment_use.
            commands[2].response.commandstring)

        debug("Test test_federated_experiment finishes successfully")

    def _wait_multiple_reservations(self, times, session_id, reservation_ids,
                                    reservations_to_wait):
        for _ in range(times):
            time.sleep(0.5)
            # Checking every half second
            results = self.consumer_client.get_experiment_uses_by_id(
                session_id, reservation_ids)
            all_finished = True

            for reservation_to_wait in reservations_to_wait:
                all_finished = all_finished and results[
                    reservation_to_wait].is_finished()

            if all_finished:
                break

    def _find_command(self, reservation_result, expected_response):
        found = False
        commands = reservation_result.experiment_use.commands
        for command in commands:
            if command.command.commandstring == 'server_info':
                found = True
                response = command.response.commandstring
                self.assertEquals(
                    expected_response, response,
                    "Message %s not found in commands %s; instead found %s" %
                    (expected_response, commands, response))
        self.assertTrue(found, "server_info not found in commands")

    def _test_reservation(self,
                          session_id,
                          experiment_id,
                          expected_server_info,
                          wait,
                          finish,
                          user_agent=None):
        debug(
            "Reserving with session_id %r a experiment %r; will I wait? %s; will I finish? %s"
            % (session_id, experiment_id, wait, finish))
        reservation_status = self.consumer_client.reserve_experiment(
            session_id, experiment_id, "{}", "{}", user_agent=user_agent)

        reservation_id = reservation_status.reservation_id

        if not wait:
            if finish:
                debug("Finishing... %r" % reservation_id)
                self.consumer_client.finished_experiment(reservation_id)
            debug("Not waiting... %r" % reservation_id)
            return reservation_id

        reservation_id = self._wait_reservation(reservation_id,
                                                expected_server_info, finish)
        debug("Finished waiting... %r" % reservation_id)
        return reservation_id

    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