def test_get_ups_session_ids_from_username(self): db_sess_id = ValidDatabaseSessionId('student2', "student") sess_id1, _ = self.ups._reserve_session(db_sess_id) db_sess_id = ValidDatabaseSessionId('student2', "student") sess_id2, _ = self.ups._reserve_session(db_sess_id) sessions = methods.get_ups_session_ids_from_username.call("student2") self.assertEquals(set([sess_id1, sess_id2]), set(sessions))
def test_get_experiment_uses_by_id_notfound(self): reservations, usages = self._store_two_reservations() reservation1 = self._reserve_experiment() reservation2 = self._reserve_experiment() db_sess_id = ValidDatabaseSessionId('student1', "student") sess_id, _ = self.ups._reserve_session(db_sess_id) with wlcontext(self.ups, session_id=sess_id): experiment_results = core_api.get_experiment_uses_by_id( (reservations[0], reservation1, reservation2)) self.assertEquals(3, len(experiment_results)) # reservation_id1 is for student1, so it returns a real object (with a real experiment_use_id) self.assertTrue(experiment_results[0].is_finished()) experiment_results[0].experiment_use.experiment_use_id = None self.assertEquals( FinishedReservationResult(usages[0].load_files('.')), experiment_results[0]) # reservation_id2 is for student2, and the session is for student1, so it returns None self.assertTrue(experiment_results[1].is_alive()) self.assertTrue(experiment_results[2].is_alive())
def test_kickout_from_ups(self): db_sess_id = ValidDatabaseSessionId('student2', "student") sess_id, _ = self.ups._reserve_session(db_sess_id) methods.kickout_from_ups.call(sess_id.id) with wlcontext(self.ups, session_id = sess_id): self.assertRaises( core_exc.SessionNotFoundError, core_api.get_reservation_status)
def test_reserve_experiment(self): db_sess_id = ValidDatabaseSessionId('student2', "student") sess_id, _ = self.ups._reserve_session(db_sess_id) exp_id = ExperimentId('this does not experiment', 'this neither') with wlcontext(self.ups, session_id=sess_id): self.assertRaises(coreExc.UnknownExperimentIdError, core_api.reserve_experiment, exp_id, "{}", "{}") exp_id = ExperimentId('ud-dummy', 'Dummy experiments') lab_sess_id = SessionId.SessionId("lab_session_id") self.lab_mock.reserve_experiment(exp_id, "{}") self.mocker.result(lab_sess_id) self.mocker.count(0, 1) self.lab_mock.resolve_experiment_address(lab_sess_id) self.mocker.result(CoordAddress.translate('foo:bar@machine')) self.mocker.count(0, 1) self.mocker.replay() reservation = core_api.reserve_experiment(exp_id, "{}", "{}") self.assertTrue(isinstance(reservation, Reservation.Reservation)) core_api.logout()
def setUp(self): self.mocker = mocker.Mocker() self.lab_mock = self.mocker.mock() self.locator = FakeLocator(lab=self.lab_mock) self.db = FakeDatabase() self.cfg_manager = ConfigurationManager.ConfigurationManager() self.cfg_manager.append_module(configuration_module) self.cfg_manager._set_value( COORDINATOR_LABORATORY_SERVERS, { 'server:laboratoryserver@labmachine': { 'inst1|ud-dummy|Dummy experiments': 'res_inst@res_type' } }) self.commands_store = TemporalInformationStore.CommandsTemporalInformationStore( ) self.coordinator = coordinator_create(SQLALCHEMY, self.locator, self.cfg_manager, ConfirmerClass=FakeConfirmer) self.coordinator._clean() self.coordinator.add_experiment_instance_id( "server:laboratoryserver@labmachine", ExperimentInstanceId('inst', 'ud-dummy', 'Dummy experiments'), Resource("res_type", "res_inst")) self.processor = UserProcessor.UserProcessor( self.locator, {'db_session_id': ValidDatabaseSessionId('my_db_session_id')}, self.cfg_manager, self.coordinator, self.db, self.commands_store)
def _validate_simple_authn(self, username, credentials): """ When the login() method is called, this method is used with the username and credentials (e.g., password, IP address, etc.). This method will only check the SimpleAuthn instances. """ try: login, role_name, user_auths = self._db.retrieve_role_and_user_auths( username) except DbUserNotFoundError: return self._process_invalid() # login could be different to username. # For example, in MySQL, where login = '******' is equivalent to where login = '******' # For this reason, we don't trust "username", and retrieve login from the database errors = False for user_auth in user_auths: # Take only those auth types that use a simple interface if user_auth.is_simple_authn(): # With each user auth, try to authenticate the user. try: authenticated = user_auth.authenticate(login, credentials) except: # If there is an error, the user could not be authenticated. log.log( LoginManager, log.level.Warning, "Username: %s with user_auth %s: ERROR" % (login, user_auth)) log.log_exc(LoginManager, log.level.Warning) errors = True traceback.print_exc() continue if authenticated: # If authenticated, return that it was correctly authenticated. log.log( LoginManager, log.level.Debug, "Username: %s with user_auth %s: SUCCESS" % (login, user_auth)) return ValidDatabaseSessionId(login, role_name) else: # If not authenticated, log it and continue with the next user_auth. log.log( LoginManager, log.level.Warning, "Username: %s with user_auth %s: FAIL" % (login, user_auth)) if errors: # Raise error: there was a server problem and this might be the reason for not # authenticating the user. Examples: LDAP server is down, there is an error in the # local database or so. raise LoginErrors.LoginError( "Error checking credentials. Contact administrators!") return self._process_invalid()
def test_reserve_session(self): db_sess_id = ValidDatabaseSessionId('student2', "student") sess_id, _ = self.ups._reserve_session(db_sess_id) session_manager = self.ups._session_manager sess = session_manager.get_session(sess_id) self.assertEquals(sess['db_session_id'].username, db_sess_id.username) with wlcontext(self.ups, session_id=sess_id): core_api.logout()
def test_get_reservation_id_one_user(self): category = "Dummy experiments" experiment = "ud-dummy" db_sess_id = ValidDatabaseSessionId('student2', "student") sess_id, _ = self.ups._reserve_session(db_sess_id) with wlcontext(self.ups, session_id = sess_id): core_api.reserve_experiment(ExperimentId( experiment, category ), "{}", "{}") reservation_id = methods.get_reservation_id.call(sess_id.id) self.assertNotEquals(None, reservation_id)
def _reserve_fake_session(self): fake_names = ('student1', 'porduna', 'user7', 'admin') exc = None for fake_name in fake_names: try: session_id, route = self.core_server._reserve_session(ValidDatabaseSessionId(fake_name, 'administrator')) except Exception as exc: pass else: return session_id, route raise exc
def test_get_user_information(self): db_sess_id = ValidDatabaseSessionId('student2', "student") sess_id, _ = self.ups._reserve_session(db_sess_id) with wlcontext(self.ups, session_id=sess_id): user = core_api.get_user_information() self.assertEquals("student2", user.login) self.assertEquals("Name of student 2", user.full_name) self.assertEquals("*****@*****.**", user.email) core_api.logout()
def test_list_experiments(self): # student1 db_sess_id1 = ValidDatabaseSessionId('student1', "student") sess_id1, _ = self.ups._reserve_session(db_sess_id1) with wlcontext(self.ups, session_id=sess_id1): experiments = core_api.list_experiments() self.assertLessEqual(5, len(experiments)) experiment_names = list( (experiment.experiment.name for experiment in experiments)) self.assertTrue('ud-dummy' in experiment_names) self.assertTrue('ud-logic' in experiment_names) self.assertTrue('ud-fpga' in experiment_names) self.assertTrue('flashdummy' in experiment_names) self.assertTrue('javadummy' in experiment_names) core_api.logout() # student2 db_sess_id2 = ValidDatabaseSessionId('student2', "student") sess_id2, _ = self.ups._reserve_session(db_sess_id2) with wlcontext(self.ups, session_id=sess_id2): experiments = core_api.list_experiments() self.assertEquals(7, len(experiments)) experiment_names = list( (experiment.experiment.name for experiment in experiments)) self.assertTrue('ud-dummy' in experiment_names) self.assertTrue('ud-fpga' in experiment_names) self.assertTrue('ud-pld' in experiment_names) self.assertTrue('ud-gpib' in experiment_names) self.assertTrue('ud-logic' in experiment_names) self.assertTrue('javadummy' in experiment_names) self.assertTrue('flashdummy' in experiment_names) core_api.logout()
def test_list_all_users(self): first_time = time.time() db_sess_id = ValidDatabaseSessionId('student2', "student") sess_id, _ = self.ups._reserve_session(db_sess_id) result = methods.list_all_users.call() self.assertEquals(1, len(result)) session_id, user_info, latest = result[0] current_time = time.time() + 1 self.assertTrue( first_time <= latest <= current_time ) self.assertEquals( 'student2', user_info.login )
def _reserve_experiment(self): db_sess_id = ValidDatabaseSessionId('student1', "student") sess_id, _ = self.ups._reserve_session(db_sess_id) with wlcontext(self.ups, session_id=sess_id): exp_id = ExperimentId('ud-dummy', 'Dummy experiments') lab_sess_id = SessionId.SessionId("lab_session_id") self.lab_mock.reserve_experiment(exp_id, "{}") self.mocker.result(lab_sess_id) self.mocker.count(0, 1) self.lab_mock.resolve_experiment_address(lab_sess_id) self.mocker.result(CoordAddress.translate('foo:bar@machine')) self.mocker.count(0, 1) self.mocker.replay() reservation = core_api.reserve_experiment(exp_id, "{}", "{}") return reservation.reservation_id
def test_get_experiment_use_by_id_found(self): reservations, usages = self._store_two_reservations() db_sess_id = ValidDatabaseSessionId('student1', "student") sess_id, _ = self.ups._reserve_session(db_sess_id) with wlcontext(self.ups, session_id=sess_id): finished_result = core_api.get_experiment_use_by_id( reservations[0]) self.assertTrue(finished_result.is_finished()) # reservation_id1 is for student1, so it returns a real object (with a real experiment_use_id) finished_result.experiment_use.experiment_use_id = None self.assertEquals( FinishedReservationResult(usages[0].load_files('.')), finished_result)
def test_list_all_users_invalid_user(self): first_time = time.time() db_sess_id = ValidDatabaseSessionId('student2', "student") sess_id, _ = self.ups._reserve_session(db_sess_id) sess_mgr = self.ups._session_manager sess_obj = sess_mgr.get_session(sess_id) sess_obj.pop('user_information') result = methods.list_all_users.call() self.assertEquals(1, len(result)) session_id, user_info, latest = result[0] current_time = time.time() + 1 self.assertTrue( first_time <= latest <= current_time ) self.assertEquals( '<unknown>', user_info.login )
def test_kickout_from_coordinator(self): category = "Dummy experiments" experiment = "ud-dummy" db_sess_id = ValidDatabaseSessionId('student2', "student") sess_id, _ = self.ups._reserve_session(db_sess_id) with wlcontext(self.ups, session_id = sess_id): status = core_api.reserve_experiment(ExperimentId( experiment, category ), "{}", "{}") reservation_session_id = status.reservation_id with wlcontext(self.ups, reservation_id = reservation_session_id, session_id = sess_id): status = core_api.get_reservation_status() self.assertNotEquals( None, status ) reservation_id = methods.get_reservation_id.call(sess_id.id) methods.kickout_from_coordinator.call(reservation_id) self.assertRaises( core_exc.NoCurrentReservationError, core_api.get_reservation_status)
def test_get_experiment_ups_session_ids(self): category = "Dummy experiments" experiment = "ud-dummy" db_sess_id = ValidDatabaseSessionId('student2', "student") sess_id, _ = self.ups._reserve_session(db_sess_id) # # It returns only the sessions_ids of the experiments # result = methods.get_experiment_ups_session_ids.call(category, experiment) self.assertEquals( [], result ) with wlcontext(self.ups, session_id = sess_id): status = core_api.reserve_experiment(ExperimentId( experiment, category ), "{}", "{}") result = methods.get_experiment_ups_session_ids.call(category, experiment) self.assertEquals( 1, len(result) ) session_id, login, reservation_id = result[0] self.assertEquals( status.reservation_id.id.split(';')[0], session_id ) self.assertEquals( "student2", login )
def test_get_reservation_info(self): db_sess_id = ValidDatabaseSessionId('student2', "student") sess_id, _ = self.ups._reserve_session(db_sess_id) exp_id = ExperimentId('ud-dummy', 'Dummy experiments') lab_sess_id = SessionId.SessionId("lab_session_id") self.lab_mock.reserve_experiment(exp_id, "{}") self.mocker.result(lab_sess_id) self.mocker.count(0, 1) self.lab_mock.resolve_experiment_address(lab_sess_id) self.mocker.result(CoordAddress.translate('foo:bar@machine')) self.mocker.count(0, 1) self.mocker.replay() with wlcontext(self.ups, session_id=sess_id): reservation = core_api.reserve_experiment(exp_id, "{}", "{}") with wlcontext(self.ups, reservation_id=reservation.reservation_id): reservation_info = core_api.get_reservation_info() self.assertEquals('ud-dummy', reservation_info.exp_name) self.assertEquals('Dummy experiments', reservation_info.cat_name) with wlcontext(self.ups, session_id=sess_id): core_api.logout()
def test_get_reservation_id_no_one_using_it(self): db_sess_id = ValidDatabaseSessionId('student2', "student") sess_id1, _ = self.ups._reserve_session(db_sess_id) reservation_id = methods.get_reservation_id.call(sess_id1.id) self.assertEquals(None, reservation_id)