def test_1_purge_users_no_guests(monkeypatch, auth_fc): from sirepo import auth_db from pykern.pkunit import pkeq, pkok from sirepo import srunit from sirepo.pkcli import admin from sirepo import auth from sirepo import srtime if not auth_fc.sr_uid: auth_fc.sr_login_as_guest() days = 1 adjusted_time = days + 10 res = admin.purge_guest_users(days=days, confirm=False) pkeq({}, res, '{}: no old users so no deletes', res) dirs_in_fs = _get_dirs() uids_in_db = auth_db.UserRegistration.search_all_for_column('uid') pkeq(1, len(dirs_in_fs), '{}: expecting exactly one user dir', dirs_in_fs) pkeq(1, len(uids_in_db), '{}: expecting exactly one uid in db', uids_in_db) srtime.adjust_time(adjusted_time) monkeypatch.setattr(auth, 'guest_uids', lambda: []) res = admin.purge_guest_users(days=days, confirm=False) pkeq({}, res, '{}: no guest users so no deletes', res) pkok(dirs_in_fs[0].check(dir=True), '{}: directory not deleted', dirs_in_fs) pkeq( auth_db.UserRegistration.search_by(uid=uids_in_db[0]).uid, uids_in_db[0], '{}: expecting uid to still be in db', uids_in_db)
def test_purge_users(monkeypatch): from pykern.pkunit import pkeq, pkok from pykern.pkdebug import pkdp from pykern import pkio from pykern import pkconfig from sirepo import srunit srunit.init_user_db() from sirepo.pkcli import admin from sirepo import simulation_db from sirepo import api_auth import datetime res = admin.purge_users(days=1, confirm=False) pkeq([], res, '{}: no old users so empty') pkdp(simulation_db.user_dir_name('*')) g = simulation_db.user_dir_name('*') dirs = list(pkio.sorted_glob(g)) pkeq(1, len(dirs), '{}: expecting exactly one user dir', g) uid = dirs[0].basename #TODO(robnagler) really want the db to be created, but need # a test oauth class. monkeypatch.setattr(api_auth, 'all_uids', lambda: [uid]) for f in pkio.walk_tree(dirs[0]): f.setmtime(f.mtime() - 86400 * 2) res = admin.purge_users(days=1, confirm=False) pkeq([], res, '{}: all users registered so no deletes') monkeypatch.setattr(api_auth, 'all_uids', lambda: []) res = admin.purge_users(days=1, confirm=False) pkeq(dirs, res, '{}: no users registered so one delete', res) pkok(dirs[0].check(dir=True), '{}: nothing deleted', res) res = admin.purge_users(days=1, confirm=True) pkeq(dirs, res, '{}: no users registered so one delete', res) pkok(not dirs[0].check(dir=True), '{}: directory deleted', res)
def test_myapp(fc): from pykern import pkunit from pykern.pkdebug import pkdc, pkdp, pkdlog import threading import time d = fc.sr_sim_data() d.models.dog.favoriteTreat = 'agent_start_delay=5' x = dict( forceRun=False, models=d.models, report=_REPORT, simulationId=d.models.simulation.simulationId, simulationType=d.simulationType, ) t1 = threading.Thread(target=lambda: fc.sr_post('runSimulation', x)) t1.start() time.sleep(1) t2 = threading.Thread(target=lambda: fc.sr_post('runCancel', x)) t2.start() time.sleep(1) r = fc.sr_run_sim(d, _REPORT) pkdp('abc') p = r.get('plots') pkunit.pkok(p, 'expecting truthy r.plots={}', p)
def test_myapp(auth_fc): from pykern import pkunit from pykern.pkdebug import pkdlog, pkdexc, pkdp import sirepo.pkcli.setup_dev sirepo.pkcli.setup_dev.default_command() fc = auth_fc # POSIT: Guests get all roles fc.sr_get('authGuestLogin', {'simulation_type': fc.sr_sim_type}) # no forbidden fc.sr_sim_data() fc.sr_logout() r = fc.sr_post('authEmailLogin', {'email': '[email protected]', 'simulationType': fc.sr_sim_type}) fc.sr_email_confirm(fc, r) fc.sr_post( 'authCompleteRegistration', { 'displayName': 'abc', 'simulationType': fc.sr_sim_type, }, ) r = fc.sr_post('listSimulations', {'simulationType': fc.sr_sim_type}, raw_response=True) pkunit.pkeq(403, r.status_code) import sirepo.auth_db sirepo.auth_db.UserRole.add_roles( fc.sr_auth_state().uid, [sirepo.auth.role_for_sim_type(fc.sr_sim_type)], ) r = fc.sr_run_sim(fc.sr_sim_data(), 'heightWeightReport') p = r.get('plots') pkunit.pkok(p, 'expecting truthy r.plots={}', p)
def _t2(): pkdlog('start 2') r2 = fc.sr_post( 'runSimulation', dict( forceRun=False, models=d.models, report=r, simulationId=d.models.simulation.simulationId, simulationType=d.simulationType, ), ) pkdlog(r2) for _ in range(20): pkunit.pkok(r2.state != 'error', 'unexpected error state: {}') if r2.state == 'running': break if r2.state == 'canceled': pkdlog('canceled') break time.sleep(.1) pkdlog('runStatus 2') r2 = fc.sr_post('runStatus', r2.nextRequest) else: pkunit.pkfail('runStatus: failed to start running: {}', r2)
def test_dict(): """Validate Dict()""" n = Dict() n.a = 1 pkok( 1 == n.a, 'new attribute should work with x.y format', ) pkok( 1 == n['a'], \ 'x["y"] should retrieve 1', ) n.a = 2 pkok( 2 == n.a, 'overwrite attr', ) with pkexcept(pkcollections.DictNameError): delattr(n, 'a') with pkexcept(pkcollections.DictNameError): setattr(n, 'keys', 3) expect = 'invalid key for Dict' with pkexcept(expect): setattr(n, '__getattr__', 3) with pkexcept(pkcollections.DictNameError): delattr(n, 'items') n = Dict(items=1) pkok(list(n.items()) == [('items', 1)], 'items() should be retrievable') n['items'] = '' del n['items'] pkok(n.items() == [], 'items() should be deletabley') with pkexcept(AttributeError): n.missing_attribute() with pkexcept(KeyError): n['missing key']
def test_login(): from pykern import pkunit, pkcompat from pykern.pkunit import pkeq, pkok, pkre, pkfail, pkexcept from sirepo import auth import flask import sirepo.auth.guest import sirepo.cookie import sirepo.http_request import sirepo.util r = auth.api_authState() pkre('LoggedIn": false.*Registration": false', pkcompat.from_bytes(r.data)) delattr(flask.g, 'sirepo_cookie') auth.process_request() with pkunit.pkexcept('SRException.*routeName=login'): auth.logged_in_user() with pkexcept('SRException.*routeName=login'): auth.require_user() sirepo.cookie.set_sentinel() # copying examples for new user takes time try: r = auth.login(sirepo.auth.guest, sim_type='myapp') pkfail('expecting sirepo.util.Response') except sirepo.util.Response as e: r = e.sr_args.response pkre(r'LoggedIn":\s*true.*Registration":\s*false', pkcompat.from_bytes(r.data)) u = auth.logged_in_user() pkok(u, 'user should exist') # guests do not require completeRegistration auth.require_user()
def test_myapp_cancel_error(fc): from pykern import pkunit import time d = fc.sr_sim_data() d.models.simulation.name = 'srunit_long_run' r = fc.sr_post( 'runSimulation', dict( forceRun=False, models=d.models, report=_REPORT, simulationId=d.models.simulation.simulationId, simulationType=d.simulationType, ), ) for _ in range(10): pkunit.pkok(r.state != 'error', 'unexpected error state: {}') if r.state == 'running': break time.sleep(r.nextRequestSeconds) r = fc.sr_post('runStatus', r.nextRequest) else: pkunit.pkfail('runStatus: failed to start running: {}', r) x = r.nextRequest x.simulationType = 'nosuchtype' r = fc.sr_post('runCancel', x) pkunit.pkeq('canceled', r.state) x.simulationType = d.simulationType r = fc.sr_post('runStatus', x) pkunit.pkeq('running', r.state) r = fc.sr_post('runCancel', x) pkunit.pkeq('canceled', r.state) r = fc.sr_post('runStatus', x) pkunit.pkeq('canceled', r.state)
def test_login(): from pykern import pkunit from pykern.pkdebug import pkdp from pykern.pkunit import pkeq, pkok, pkre, pkexcept from sirepo import auth import flask import sirepo.auth.guest import sirepo.cookie import sirepo.http_request r = auth.api_authState() pkre('LoggedIn": false.*Registration": false', r.data) delattr(flask.g, 'sirepo_cookie') auth.process_request() with pkunit.pkexcept('SRException.*routeName=login'): auth.logged_in_user() with pkexcept('SRException.*routeName=login'): auth.require_user() sirepo.cookie.set_sentinel() # copying examples for new user takes time with pkunit.pkexcept('SRException.*routeName=None'): r = auth.login(sirepo.auth.guest, sim_type='myapp') r = auth.api_authState() pkre('LoggedIn": true.*Registration": false', r.data) u = auth.logged_in_user() pkok(u, 'user should exist') # guests do not require completeRegistration auth.require_user()
def test_srw_discard_example(fc): """Emulates what the GUI does""" from pykern.pkcollections import PKDict from pykern.pkdebug import pkdp from pykern.pkunit import pkok # See https://github.com/radiasoft/sirepo/issues/1972 n = 'Undulator Radiation' d = fc.sr_sim_data(n) fc.sr_post( 'deleteSimulation', PKDict( simulationType=fc.sr_sim_type, simulationId=d.models.simulation.simulationId, ), ) r = fc.sr_get( 'findByNameWithAuth', PKDict(simulation_type=fc.sr_sim_type, application_mode='default', simulation_name=n), redirect=False, ) i = r.headers['Location'].split('/').pop() r = fc.sr_get( 'pythonSource', PKDict( simulation_type=fc.sr_sim_type, simulation_id=i, ), ) pkok('srwl_bl.SRWLBeamline' in r.data, 'incomplete python={}', r.data)
def _code(files=None): from pykern import pkunit, pkio, pkjson from pykern.pkdebug import pkdp import inspect import sirepo.lib for i, s in enumerate( pkio.sorted_glob(pkunit.data_dir().join( f'{inspect.stack()[1].function.split("_")[1]}_*', ))): t = s.basename.split('_')[0] d = sirepo.lib.Importer(t).parse_file( pkio.sorted_glob(s.join('first*'))[0]) d2 = d.copy() d2.pkdel('version') for k in [k for k in d2.keys() if '_SimData__' in k]: d2.pkdel(k) pkunit.file_eq(s.join('out.json'), d2) w = pkunit.work_dir().join(s.basename) r = d.write_files(w) for o in pkio.sorted_glob(pkunit.data_dir().join(s.basename, '*.out')): pkunit.file_eq(o, actual_path=w.join(o.basename).new(ext='')) if files: pkunit.pkok( set(files[i]).issubset(set(r.output_files)), 'expecting files={} to be subset of output_files={}', files, r.output_files, )
def test_myapp_sim(fc): from pykern import pkunit from pykern.pkdebug import pkdp, pkdlog import time d = fc.sr_sim_data() r = fc.sr_post( 'runSimulation', dict( forceRun=False, models=d.models, report=_REPORT, simulationId=d.models.simulation.simulationId, simulationType=d.simulationType, ), ) for _ in range(10): pkdlog(r) pkunit.pkok(r.state != 'error', 'expected error state: {}') if r.state == 'completed': break time.sleep(r.nextRequestSeconds) r = fc.sr_post('runStatus', r.nextRequest) else: pkunit.pkfail('runStatus: failed to complete: {}', r) # Just double-check it actually worked pkunit.pkok(u'plots' in r, '"plots" not in response={}', r)
def test_purge_users(monkeypatch): from pykern.pkunit import pkeq, pkok from pykern.pkdebug import pkdp from pykern import pkio from pykern import pkconfig from sirepo import srunit srunit.init_auth_db(sim_types='myapp') from sirepo.pkcli import admin from sirepo import simulation_db from sirepo import auth_db import datetime res = admin.purge_users(days=1, confirm=False) pkeq([], res, '{}: no old users so empty') g = simulation_db.user_dir_name('*') dirs = list(pkio.sorted_glob(g)) pkeq(1, len(dirs), '{}: expecting exactly one user dir', g) uid = dirs[0].basename #TODO(robnagler) really want the db to be created, but need # a test oauth class. monkeypatch.setattr(auth_db, 'all_uids', lambda: [uid]) for f in pkio.walk_tree(dirs[0]): f.setmtime(f.mtime() - 86400 * 2) res = admin.purge_users(days=1, confirm=False) pkeq([], res, '{}: all users registered so no deletes') monkeypatch.setattr(auth_db, 'all_uids', lambda: []) res = admin.purge_users(days=1, confirm=False) pkeq(dirs, res, '{}: no users registered so one delete', res) pkok(dirs[0].check(dir=True), '{}: nothing deleted', res) res = admin.purge_users(days=1, confirm=True) pkeq(dirs, res, '{}: no users registered so one delete', res) pkok(not dirs[0].check(dir=True), '{}: directory deleted', res)
def test_dict(): """Validate Dict()""" n = Dict() n.a = 1 pkok( 1 == n.a, 'new attribute should work with x.y format', ) pkok( 1 == n['a'], \ 'x["y"] should retrieve 1', ) n.a = 2 pkok( 2 == n.a, 'overwrite attr', ) with pkexcept(pkcollections.DictNameError): delattr(n, 'a') with pkexcept(pkcollections.DictNameError): setattr(n, 'keys', 3) expect = 'invalid key for Dict' with pkexcept(expect): setattr(n, '__getattr__', 3) with pkexcept(pkcollections.DictNameError): delattr(n, 'items') n = Dict(items=1) pkok(list(n.items()) == [('items', 1)], 'items() should be retrievable') n['items'] = '' del n['items'] pkok(n.items() == [], 'items() should be deletabley') with pkexcept(AttributeError): n.missing_attribute() with pkexcept(KeyError): n['missing key']
def test_2_purge_users_guests_present(auth_fc): from sirepo import auth_db from pykern.pkunit import pkeq, pkok from sirepo import srunit from sirepo.pkcli import admin from sirepo import srtime if not auth_fc.sr_uid: auth_fc.sr_login_as_guest() days = 1 adjusted_time = days + 10 dirs_in_fs = _get_dirs() uids_in_db = auth_db.UserRegistration.search_all_for_column('uid') dirs_and_uids = {dirs_in_fs[0]: uids_in_db[0]} srtime.adjust_time(adjusted_time) res = admin.purge_guest_users(days=days, confirm=False) pkeq(dirs_and_uids, res, '{}: one guest user so one dir and uid to delete', res) res = admin.purge_guest_users(days=days, confirm=True) pkeq(dirs_and_uids, res, '{}: one guest user so one dir and uid to delete', res) pkok(not res.keys()[0].check(dir=True), '{}: directory deleted', res) pkeq(auth_db.UserRegistration.search_by(uid=res.values()[0]), None, '{}: expecting uid to deleted from db', res)
def test_myapp_basic(fc): from pykern import pkunit, pkcompat from pykern.pkunit import pkok, pkeq r = fc.sr_get('/robots.txt') pkunit.pkre('elegant.*myapp.*srw', pkcompat.from_bytes(r.data)) r = fc.sr_get('/') pkok(not re.search(r'googletag', pkcompat.from_bytes(r.data)), 'Unexpected injection of googletag data={}', r.data)
def test_srw_upload(fc): from pykern import pkunit from pykern.pkcollections import PKDict from pykern.pkdebug import pkdp import sirepo.sim_data d = fc.sr_sim_data('NSLS-II CHX beamline') s = sirepo.sim_data.get_class(fc.sr_sim_type) f = s.lib_file_resource_dir().join('mirror_1d.dat') t = 'mirror' r = fc.sr_post_form( 'uploadFile', params=PKDict( simulation_type=fc.sr_sim_type, simulation_id=d.models.simulation.simulationId, file_type=t, ), data=PKDict(), file=f, ) pkunit.pkre('in use in other', r.get('error', '')) r = fc.sr_post_form( 'uploadFile', params=PKDict( simulation_type=fc.sr_sim_type, simulation_id=d.models.simulation.simulationId, file_type=t, ), data=PKDict(confirm='1'), file=f, ) e = r.get('error', '') pkunit.pkok(not e, 'unexpected error={}', e) r = fc.sr_post_form( 'uploadFile', params=PKDict( simulation_type=fc.sr_sim_type, simulation_id=d.models.simulation.simulationId, file_type='invalid file type', ), data=PKDict(), file=f, ) pkunit.pkre('invalid file type', r.get('error', '')) # the above used to delete the file r = fc.sr_get( 'downloadFile', params=PKDict( simulation_type=fc.sr_sim_type, simulation_id=d.models.simulation.simulationId, filename=f.basename, ), data=PKDict(), ) pkunit.pkre(r'^\s*-1.39500', r.data)
def test_pkok(): from pykern.pkunit import pkok import inspect assert 1 == pkok(1, 'should not see this'), \ 'Result of a successful ok is the condition value' lineno = inspect.currentframe().f_lineno + 2 try: pkok(0, 'xyzzy {} {k1}', '333', k1='abc') except AssertionError as e: # May not match exactly, because depends on start directory assert 'pkunit_test.py:{}:test_pkok xyzzy 333 abc'.format(lineno) in e.message
def sr_run_sim(self, data, model, expect_completed=True, timeout=10, **post_args): from pykern import pkunit from pykern.pkdebug import pkdlog, pkdexc import time if self.sr_job_run_mode: data.models[model].jobRunMode = self.sr_job_run_mode cancel = None try: r = self.sr_post( 'runSimulation', PKDict( models=data.models, report=model, simulationId=data.models.simulation.simulationId, simulationType=data.simulationType, ).pkupdate(**post_args), ) if r.state == 'completed': return r cancel = r.get('nextRequest') for _ in range(timeout): if r.state in ('completed', 'error'): pkdlog(r.state) cancel = None break r = self.sr_post('runStatus', r.nextRequest) time.sleep(1) else: pkunit.pkok(not expect_completed, 'did not complete: runStatus={}', r) if expect_completed: pkunit.pkeq('completed', r.state) return r finally: if cancel: pkdlog('runCancel') self.sr_post('runCancel', cancel) import subprocess o = pkcompat.from_bytes( subprocess.check_output(['ps', 'axww'], stderr=subprocess.STDOUT), ) o = list(filter(lambda x: 'mpiexec' in x, o.split('\n'))) if o: pkdlog('found "mpiexec" after cancel in ps={}', '\n'.join(o)) # this exception won't be seen because in finally raise AssertionError('cancel failed')
def test_pkok(): from pykern.pkunit import pkok import inspect assert 1 == pkok(1, 'should not see this'), \ 'Result of a successful ok is the condition value' lineno = inspect.currentframe().f_lineno + 2 try: pkok(0, 'xyzzy {} {k1}', '333', k1='abc') except AssertionError as e: # May not match exactly, because depends on start directory assert 'pkunit_test.py:{}:test_pkok xyzzy 333 abc'.format( lineno) in e.message
def test_migration(): """See if user gets migrated""" from pykern.pkunit import pkeq, pkok, pkexcept, work_dir from pykern.pkdebug import pkdp from sirepo import auth # deprecated methods raise Unauthorized, but still login with pkexcept('UNAUTHORIZED'): auth.login(auth.github, uid='jeTJR5G4') # verify logged in pkeq('jeTJR5G4', auth.user_if_logged_in('github')) pkok(work_dir().join('db/auth.db').exists(), 'auth.db does not exist')
def test_json_load_any(): """Validate json_load_any()""" import json j = json.dumps({'a': 33}) j2 = pkcollections.json_load_any(j) pkok( 33 == j2.a, '{}: j2.a is not 33', j2.a, ) j = json.dumps({'a': 33, 'b': {'values': 'will collide, but ok'}}) j2 = pkcollections.json_load_any(j) pkcollections.json_load_any(j, object_pairs_hook=pkcollections.Dict)
def test_elegant_concurrent_sim_frame(fc): """https://github.com/radiasoft/sirepo/issues/2474""" from pykern import pkunit from pykern.pkcollections import PKDict from pykern.pkdebug import pkdlog, pkdp import sirepo.sim_data import threading import time def _get_frames(): for i in range(3): f = fc.sr_get_json( 'simulationFrame', PKDict(frame_id=s.frame_id(d, r1, 'elementAnimation19-5', 0)), ) pkunit.pkeq('completed', f.state) def _t2(get_frames): get_frames() d = fc.sr_sim_data(sim_name='Backtracking', sim_type='elegant') s = sirepo.sim_data.get_class(fc.sr_sim_type) r = 'animation' r1 = PKDict() try: pkdlog('start 1') r1 = fc.sr_post( 'runSimulation', dict( forceRun=False, models=d.models, report=r, simulationId=d.models.simulation.simulationId, simulationType=d.simulationType, ), ) for _ in range(10): pkunit.pkok(r1.state != 'error', 'unexpected error state: {}') if r1.state == 'completed': break time.sleep(1) r1 = fc.sr_post('runStatus', r1.nextRequest) else: pkunit.pkfail('runStatus: failed to complete: {}', r1) t2 = threading.Thread(target=_t2, args=(_get_frames, )) t2.start() _get_frames() t2.join() finally: if r1.get('nextRequest'): fc.sr_post('runCancel', r1.nextRequest)
def test_1_serial_stomp(): from pykern.pkdebug import pkdp, pkdpretty from pykern.pkunit import pkfail, pkok from sirepo import sr_unit import copy fc = sr_unit.flask_client() sim_type = "srw" data = fc.sr_post("listSimulations", {"simulationType": sim_type}) for youngs in data: if youngs["name"] == "Young's Double Slit Experiment": break else: pkfail("{}: Young's not found", pkdpretty(data)) data = fc.sr_get( "simulationData", {"simulation_type": sim_type, "pretty": "0", "simulation_id": youngs["simulationId"]} ) prev_serial = data["models"]["simulation"]["simulationSerial"] prev_data = copy.deepcopy(data) pkok(prev_serial > _MIN_SERIAL, "{}: serial must be greater than {}", prev_serial, _MIN_SERIAL) data["models"]["beamline"][4]["position"] = "61" curr_data = fc.sr_post("saveSimulationData", data) curr_serial = curr_data["models"]["simulation"]["simulationSerial"] pkok(prev_serial < curr_serial, "{}: serial not incremented, still < {}", prev_serial, curr_serial) prev_data["models"]["beamline"][4]["position"] = "60.5" failure = fc.sr_post("saveSimulationData", prev_data) pkok(failure["error"] == "invalidSerial", "{}: unexpected status, expected serial failure", failure) curr_data["models"]["beamline"][4]["position"] = "60.5" curr_serial = curr_data["models"]["simulation"]["simulationSerial"] new_data = fc.sr_post("saveSimulationData", curr_data) new_serial = new_data["models"]["simulation"]["simulationSerial"] pkok(curr_serial < new_serial, "{}: serial not incremented, still < {}", new_serial, curr_serial)
def test_injection(fc): from pykern import pkcompat, pkunit from pykern.pkdebug import pkdc, pkdp, pkdlog from pykern.pkunit import pkeq, pkok, pkre import re # test non-static page r = fc.get('myapp') pkok(not re.search(r'googletag', pkcompat.from_bytes(r.data)), 'Unexpected injection of googletag data={}', r.data) # test successful injection r = fc.get('/en/landing.html') pkre(_TEST_ID, pkcompat.from_bytes(r.data))
def test_1_serial_stomp(): from pykern.pkdebug import pkdp, pkdpretty from pykern.pkunit import pkfail, pkok from sirepo import srunit import copy fc = srunit.flask_client() sim_type = 'srw' fc.get('/{}'.format(sim_type)) data = fc.sr_post('listSimulations', {'simulationType': sim_type}) for youngs in data: if youngs['name'] == "Young's Double Slit Experiment": break else: pkfail("{}: Young's not found", pkdpretty(data)) data = fc.sr_get( 'simulationData', { 'simulation_type': sim_type, 'pretty': '0', 'simulation_id': youngs['simulationId'], }, ) prev_serial = data['models']['simulation']['simulationSerial'] prev_data = copy.deepcopy(data) pkok( prev_serial > _MIN_SERIAL, '{}: serial must be greater than {}', prev_serial, _MIN_SERIAL, ) data['models']['beamline'][4]['position'] = '61' curr_data = fc.sr_post('saveSimulationData', data) curr_serial = curr_data['models']['simulation']['simulationSerial'] pkok( prev_serial < curr_serial, '{}: serial not incremented, still < {}', prev_serial, curr_serial, ) prev_data['models']['beamline'][4]['position'] = '60.5' failure = fc.sr_post('saveSimulationData', prev_data) pkok( failure['error'] == 'invalidSerial', '{}: unexpected status, expected serial failure', failure, ) curr_data['models']['beamline'][4]['position'] = '60.5' curr_serial = curr_data['models']['simulation']['simulationSerial'] new_data = fc.sr_post('saveSimulationData', curr_data) new_serial = new_data['models']['simulation']['simulationSerial'] pkok( curr_serial < new_serial, '{}: serial not incremented, still < {}', new_serial, curr_serial, )
def test_myapp_analysis(fc): from pykern import pkunit from pykern.pkdebug import pkdp d = fc.sr_sim_data() r = fc.sr_run_sim(d, 'heightWeightReport', expect_completed=True) r = fc.sr_get( 'downloadDataFile', PKDict( simulation_type=d.simulationType, simulation_id=d.models.simulation.simulationId, model='heightWeightReport', frame='-1', suffix='sr_long_analysis', ), ) pkunit.pkok(r.status_code == 404, 'r={}', r)
def test_different_email(): fc, sim_type = _fc() from pykern import pkconfig, pkunit, pkio from pykern.pkunit import pkok, pkre from pykern.pkdebug import pkdp import re r = fc.sr_post( 'authEmailLogin', { 'email': '[email protected]', 'simulationType': sim_type }, ) s = fc.sr_auth_state(isLoggedIn=False) fc.get(r.url) s = fc.sr_auth_state(isLoggedIn=True, needCompleteRegistration=True) fc.sr_post( 'authCompleteRegistration', { 'displayName': 'abc', 'simulationType': sim_type, }, ) t = fc.sr_auth_state(userName='******', displayName='abc') r = fc.sr_get('authLogout', {'simulation_type': sim_type}) pkre('/{}$'.format(sim_type), r.headers['Location']) uid = fc.sr_auth_state(userName=None, isLoggedIn=False).uid r = fc.sr_post('authEmailLogin', { 'email': '[email protected]', 'simulationType': sim_type }) fc.get(r.url) fc.sr_post( 'authCompleteRegistration', { 'displayName': 'xyz', 'simulationType': sim_type, }, ) uid2 = fc.sr_auth_state(displayName='xyz', isLoggedIn=True, userName='******').uid pkok(uid != uid2, 'did not get a new uid={}', uid)
def _expr(expr, expect, variables=None): from pykern.pkunit import pkok, pkfail res = _elegant().get_application_data(PKDict( method='rpn_value', value=expr, variables=variables or {}, )) if not 'result' in res: pkfail('{}: no result for {}', res, expr) delta = abs(float(expect) - float(res['result'])) pkok( # Only needs to be very approximate delta < 0.01, '(expected) {} != {} (actual) expr={}', expect, float(res['result']), expr, )
def _assert_py3(): """Check if the py3 environment is set up properly""" res = dict() for k, v in os.environ.items(): if ('PYENV' in k or 'PYTHON' in k): continue if k in ('PATH', 'LD_LIBRARY_PATH'): v2 = [] for x in v.split(':'): if x and 'py2' not in x: v2.append(x) v = ':'.join(v2) res[k] = v res['PYENV_VERSION'] = 'py3' try: out = subprocess.check_output( ['pyenv', 'which', 'sirepo'], env=res, stderr=subprocess.STDOUT, ) except subprocess.CalledProcessError as e: out = e.output from pykern import pkunit pkunit.pkok( '/py3/bin/sirepo' in out, 'expecting sirepo in a py3: {}', out, ) try: out = subprocess.check_output( ['pyenv', 'exec', 'sirepo', 'runner', '--help'], env=res, stderr=subprocess.STDOUT, ) except subprocess.CalledProcessError as e: out = e.output pkunit.pkok( 'runner daemon' in out, '"runner daemon" not in help: {}', out, ) return res
def test_oauth(): from pykern import pkconfig pkconfig.reset_state_for_testing({ 'SIREPO_SERVER_OAUTH_LOGIN': '******', 'SIREPO_OAUTH_GITHUB_KEY': 'n/a', 'SIREPO_OAUTH_GITHUB_SECRET': 'n/a', 'SIREPO_OAUTH_GITHUB_CALLBACK_URI': 'n/a', }) from pykern.pkunit import pkfail, pkok from sirepo import server from sirepo import sr_unit import re sim_type = 'srw' fc = sr_unit.flask_client() fc.sr_post('listSimulations', {'simulationType': sim_type}) text = fc.sr_get( 'oauthLogin', { 'simulation_type': sim_type, 'oauth_type': 'github', }, raw_response=True, ).data state = re.search(r'state=(.*?)"', text).group(1) #TODO(pjm): causes a forbidden error due to missing variables, need to mock-up an oauth test type text = fc.get('/oauth-authorized/github') text = fc.sr_get( 'oauthLogout', { 'simulation_type': sim_type, }, raw_response=True, ).data pkok( text.find('Redirecting') > 0, 'missing redirect', ) pkok( text.find('"/{}"'.format(sim_type)) > 0, 'missing redirect target', )
def test_create_zip(): from pykern import pkio from pykern import pkunit from pykern.pkdebug import pkdp, pkdpretty from pykern.pkunit import pkfail, pkok from sirepo import sr_unit import copy import zipfile fc = sr_unit.flask_client() imported = _import(fc) for sim_type, sim_name, expect in imported + [ ('elegant', 'bunchComp - fourDipoleCSR', ['WAKE-inputfile.knsl45.liwake.sdds', 'run.py', 'sirepo-data.json']), ('srw', 'Tabulated Undulator Example', ['magnetic_measurements.zip', 'run.py', 'sirepo-data.json']), ('warp', 'WARP example laser simulation', ['run.py', 'sirepo-data.json']), ]: sim_id = fc.sr_sim_data( sim_type, sim_name)['models']['simulation']['simulationId'] resp = fc.sr_get( 'exportArchive', { 'simulation_type': sim_type, 'simulation_id': sim_id, 'filename': 'anything.zip', }, raw_response=True, ) with pkio.save_chdir(pkunit.work_dir()): fn = sim_name + '.zip' with open(fn, 'wb') as f: f.write(resp.data) z = zipfile.ZipFile(fn) nl = sorted(z.namelist()) pkok( nl == expect, '{}: zip namelist incorrect, expect={}', nl, expect, )
def test_login_logout(): from pykern import pkconfig from pykern.pkunit import pkfail, pkok from sirepo import srunit import re fc = srunit.flask_client({ 'SIREPO_FEATURE_CONFIG_API_MODULES': 'oauth', 'SIREPO_OAUTH_GITHUB_KEY': 'n/a', 'SIREPO_OAUTH_GITHUB_SECRET': 'n/a', 'SIREPO_OAUTH_GITHUB_CALLBACK_URI': 'n/a', }) sim_type = 'srw' fc.get('/{}'.format(sim_type)) fc.sr_post('listSimulations', {'simulationType': sim_type}) text = fc.sr_get( 'oauthLogin', { 'simulation_type': sim_type, 'oauth_type': 'github', }, raw_response=True, ).data state = re.search(r'state=(.*?)"', text).group(1) #TODO(pjm): causes a forbidden error due to missing variables, need to mock-up an oauth test type text = fc.get('/oauth-authorized/github') text = fc.sr_get( 'oauthLogout', { 'simulation_type': sim_type, }, raw_response=True, ).data pkok( text.find('Redirecting') > 0, 'missing redirect', ) pkok( text.find('"/{}"'.format(sim_type)) > 0, 'missing redirect target', )
def test_add_code(): from pykern import pkio from pykern import pkjson from pykern import pkunit from pykern.pkunit import pkok, pkeq, pkre from pykern.pkdebug import pkdp from pykern.pkcli import rsmanifest import re with pkunit.save_chdir_work(is_pkunit_prefix=True) as d: rsmanifest.add_code('A', 'b', 'c', 'd', pyenv='v') j = pkjson.load_any(pkio.py_path(rsmanifest.USER_FILE).read()) pkok(20170101.0 < float(j.version), 'version must be after 2017') pkeq('A', j.codes.v.a.name) pkeq('b', j.codes.v.a.version) rsmanifest.add_code('a', 'bb', 'cc', 'dd') j = pkjson.load_any(pkio.expand_user_path(rsmanifest.USER_FILE).read()) pkeq('A', j.codes.v.a.name) pkeq('a', j.codes[''].a.name) pkeq('bb', j.codes[''].a.version) pkre('20.*T.*Z', j.codes[''].a.installed)
def test_add_code(): from pykern import pkio from pykern import pkjson from pykern import pkunit from pykern.pkunit import pkok, pkeq, pkre from pykern.pkdebug import pkdp from pykern.pkcli import rsmanifest import re with pkunit.save_chdir_work(is_pkunit_prefix=True) as d: rsmanifest.add_code('A', 'b', 'c', 'd', pyenv='v') j = pkjson.load_any(pkio.py_path(rsmanifest.USER_FILE).read()) pkok(20170101.0 < float(j.version), 'version must be after 2017') pkeq('A', j.codes.v.a.name) pkeq('b', j.codes.v.a.version) rsmanifest.add_code('a', 'bb', 'cc', 'dd') j = pkjson.load_any(pkio.expand_user_path(rsmanifest.USER_FILE).read()) pkeq('A', j.codes.v.a.name) pkeq('a', j.codes[''].a.name) pkeq('bb', j.codes[''].a.version) pkre('20.*T.*Z', j.codes[''].a.installed)
def test_login(): from pykern import pkunit from pykern.pkdebug import pkdp from pykern.pkunit import pkeq, pkok, pkre from sirepo import auth import flask import sirepo.auth.guest import sirepo.cookie import sirepo.http_request r = auth.api_authState() pkre('LoggedIn": false.*Registration": false', r.data) delattr(flask.g, 'sirepo_cookie') auth.process_request() with pkunit.pkexcept('Unauthorized'): auth.logged_in_user() r = auth.require_user() pkeq(400, r.status_code, 'status should be BAD REQUEST') pkre('"routeName": "login"', r.data) sirepo.cookie.set_sentinel() # copying examples for new user takes time r = auth.login(sirepo.auth.guest) pkeq(None, r, 'user created') r = auth.api_authState() pkre('LoggedIn": true.*Registration": true', r.data) u = auth.logged_in_user() pkok(u, 'user should exist') r = auth.require_user() pkeq(400, r.status_code, 'status should be BAD REQUEST') pkre('"routeName": "completeRegistration"', r.data) flask.request = 'abcdef' def parse_json(*args, **kwargs): return pkcollections.Dict(simulationType='myapp', displayName='Joe Bob') setattr(sirepo.http_request, 'parse_json', parse_json) auth.api_authCompleteRegistration() r = auth.api_authState() pkre('Name": "Joe Bob".*In": true.*.*Registration": false', r.data)
def test_init_and_run(monkeypatch): from pykern import pkio from pykern import pkunit from pykern.pkcli import sim from pykern.pkcli import rsmanifest import netrc import os import os.path import re import subprocess cfg = pkunit.cfg.aux.get('sim_test', None) if not cfg: # No testing if there's no auth config return u, p = cfg.split(' ') monkeypatch.setattr(netrc, 'netrc', _netrc) _netrc.result = (u, None, p) with pkunit.save_chdir_work(is_pkunit_prefix=True): f = 'out/log' expect_code = pkunit.random_alpha() pkio.write_text('run.sh', 'echo {}>{}'.format(expect_code, f)) rsmanifest.pkunit_setup() sim._cmd_init() sim._cmd_run() x = subprocess.check_output(['git', 'remote', '-v']), m = re.search(r'/(sim-sim_work-\d+-\d+)\.git', x[0]) repo = m.group(1) pkunit.pkok(m, 'git remote: failed: {}', x) pkunit.pkeq(expect_code, pkio.read_text('out/log').rstrip()) os.remove('run.sh') sim._cmd_pip('djson') pkio.write_text('run.py', 'import djson'.format(expect_code, f)) sim._cmd_run() sim._git_api_request( 'delete', 'repositories/{user}/{repo}', dict(repo=repo), )
def test_create_zip(): from pykern import pkio from pykern import pkunit from pykern.pkdebug import pkdp, pkdpretty from pykern.pkunit import pkfail, pkok from sirepo import srunit import copy import zipfile fc = srunit.flask_client() fc.get('/srw') imported = _import(fc) for sim_type, sim_name, expect in imported + [ ('elegant', 'bunchComp - fourDipoleCSR', ['WAKE-inputfile.knsl45.liwake.sdds', 'run.py', 'sirepo-data.json']), ('srw', 'Tabulated Undulator Example', ['magnetic_measurements.zip', 'run.py', 'sirepo-data.json']), ('warppba', 'Laser Pulse', ['run.py', 'sirepo-data.json']), ]: sim_id = fc.sr_sim_data(sim_type, sim_name)['models']['simulation']['simulationId'] resp = fc.sr_get( 'exportArchive', { 'simulation_type': sim_type, 'simulation_id': sim_id, 'filename': 'anything.zip', }, raw_response=True, ) with pkio.save_chdir(pkunit.work_dir()): fn = sim_name + '.zip' with open(fn, 'wb') as f: f.write(resp.data) z = zipfile.ZipFile(fn) nl = sorted(z.namelist()) pkok( nl == expect, '{}: zip namelist incorrect, expect={}', nl, expect, )
def _expr(expr, expect, variables=None): from pykern.pkunit import pkok, pkfail global elegant if not elegant: import sirepo.template elegant = sirepo.template.import_module('elegant') res = elegant.get_application_data(dict( method='rpn_value', value=expr, variables=(variables or {}), )) if not 'result' in res: pkfail('{}: no result for {}', res, expr) delta = abs(float(expect) - float(res['result'])) pkok( # Only needs to be very approximate delta < 0.01, '(expected) {} != {} (actual) expr={}', expect, float(res['result']), expr, )