def pytest_runtest_protocol(item, *args, **kwargs): import signal from pykern import pkunit def _timeout(*args, **kwargs): signal.signal(signal.SIGALRM, _timeout_failed) signal.alarm(1) pkunit.pkfail('MAX_CASE_RUN_SECS={} exceeded', MAX_CASE_RUN_SECS) def _timeout_failed(*args, **kwargs): import os import sys from pykern.pkdebug import pkdlog pkdlog('failed to die after timeout (pkfail)') os.killpg(os.getpgrp(), signal.SIGKILL) # Seems to be the only way to get the module under test m = item._request.module is_new = m != pkunit.module_under_test if is_new: signal.signal(signal.SIGALRM, _timeout) pkunit.module_under_test = m signal.alarm(MAX_CASE_RUN_SECS) if is_new: from pykern import pkio pkio.unchecked_remove(pkunit.work_dir())
def test_elegant_data_file(fc): from pykern import pkio from pykern import pkunit from pykern.pkcollections import PKDict from pykern.pkdebug import pkdp import sdds data = fc.sr_sim_data('bunchComp - fourDipoleCSR') run = fc.sr_post( 'runSimulation', PKDict( forceRun=False, models=data.models, report='bunchReport1', simulationId=data.models.simulation.simulationId, simulationType=data.simulationType, ), ) pkunit.pkeq('pending', run.state, 'not pending, run={}', run) for _ in range(10): if run.state == 'completed': break run = fc.sr_post( 'runStatus', run.nextRequest ) time.sleep(1) else: pkunit.pkfail('runStatus: failed to complete: {}', run) resp = fc.sr_get( 'downloadDataFile', PKDict( simulation_type=data.simulationType, simulation_id=data.models.simulation.simulationId, model='bunchReport1', frame='-1', suffix='csv', ), ) pkunit.pkre('no-cache', resp.headers['Cache-Control']) rows = csv.reader(StringIO.StringIO(resp.get_data())) pkunit.pkeq(50001, len(list(rows)), '50,000 particles plus header row') resp = fc.sr_get( 'downloadDataFile', PKDict( simulation_type=data.simulationType, simulation_id=data.models.simulation.simulationId, model='bunchReport1', frame='-1', ), ) m = re.search(r'attachment; filename="([^"]+)"', resp.headers['Content-Disposition']) d = pkunit.work_dir() path = d.join(m.group(1)) path.write_binary(resp.get_data()) assert sdds.sddsdata.InitializeInput(0, str(path)) == 1, \ '{}: sdds failed to open'.format(path) # Verify we can read something assert 0 <= len(sdds.sddsdata.GetColumnNames(0)) sdds.sddsdata.Terminate(0)
def test_prepare_aux_files(): from sirepo.template import template_common from pykern.pkdebug import pkdp from pykern import pkcollections import sirepo.auth import sirepo.auth.guest sirepo.auth.login(sirepo.auth.guest) # Needed to initialize simulation_db data = pkcollections.json_load_any('''{ "simulationType": "srw", "models": { "simulation": { "sourceType": "t" }, "tabulatedUndulator": { "undulatorType": "u_t", "magneticFile": "magnetic_measurements.zip" }, "beamline": { } }, "report": "intensityReport" }''') d = pkunit.work_dir() template_common.copy_lib_files(data, None, d)
def test_importer(import_req): from pykern.pkcollections import PKDict from sirepo.template import elegant import sirepo.lib for fn in pkio.sorted_glob(pkunit.data_dir().join('*')): if not pkio.has_file_extension(fn, ('ele', 'lte')) \ or fn.basename.endswith('.ele.lte'): continue k = PKDict() pkdlog('file={}', fn) if fn.basename.startswith('deviance-'): try: data = elegant.import_file(import_req(fn)) except Exception as e: k.actual = f'{e}\n' else: k.actual = 'did not raise exception' elif fn.ext == '.lte': data = elegant.import_file(import_req(fn)) data['models']['commands'] = [] g = elegant._Generate(data) g.sim() j = g.jinja_env k.actual = j.rpn_variables + j.lattice else: f = sirepo.lib.Importer('elegant').parse_file(fn).write_files(pkunit.work_dir()) k.actual_path = f.commands pkunit.file_eq(fn.basename + '.txt', **k)
def flask_client(cfg=None, sim_types=None): """Return FlaskClient with easy access methods. Creates a new run directory every test file so can assume sharing of state on the server within a file (module). Two methods of interest: `sr_post` and `sr_get`. Args: cfg (dict): extra configuration for reset_state_for_testing sim_types (str): value for SIREPO_FEATURE_CONFIG_SIM_TYPES Returns: FlaskClient: for local requests to Flask server """ global server, app a = 'srunit_flask_client' if not cfg: cfg = {} if sim_types: cfg['SIREPO_FEATURE_CONFIG_SIM_TYPES'] = sim_types wd = pkunit.work_dir() cfg['SIREPO_SRDB_ROOT'] = str(pkio.mkdir_parent(wd.join('db'))) if not (server and hasattr(app, a)): with pkio.save_chdir(wd): pkconfig.reset_state_for_testing(cfg) from sirepo import server as s server = s app = server.init() app.config['TESTING'] = True app.test_client_class = _TestClient setattr(app, a, app.test_client()) return getattr(app, a)
def sr_user_dir(self, uid=None): """User's db dir""" from pykern import pkunit if not uid: uid = self.sr_auth_state().uid return pkunit.work_dir().join(_DB_DIR, 'user', uid)
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 flask_client(cfg=None): """Return FlaskClient with easy access methods. Creates a new run directory every test file so can assume sharing of state on the server within a file (module). Two methods of interest: `sr_post` and `sr_get`. Args: cfg (dict): extra configuration for reset_state_for_testing Returns: FlaskClient: for local requests to Flask server """ global server a = 'srunit_flask_client' if not cfg: cfg = {} wd = pkunit.work_dir() cfg['SIREPO_SERVER_DB_DIR'] = str(pkio.mkdir_parent(wd.join('db'))) if not (server and hasattr(server.app, a)): with pkio.save_chdir(wd): pkconfig.reset_state_for_testing(cfg) from sirepo import server as s server = s server.app.config['TESTING'] = True server.app.test_client_class = _TestClient server.init() setattr(server.app, a, server.app.test_client()) return getattr(server.app, a)
def setup_srdb_root(cfg=None): from pykern import pkunit, pkio import os e = cfg if not e: e = os.environ e.update(SIREPO_SRDB_ROOT=str( pkio.mkdir_parent(pkunit.work_dir().join(_DB_DIR))), )
def _generate_source(fc, sim, name): from pykern import pkio, pkunit, pkdebug import re d = fc.sr_get( 'pythonSource', PKDict( simulation_id=sim.models.simulation.simulationId, simulation_type=sim.simulationType, ), ).data n = re.sub( r'[^\w\-\.]', '', re.sub(r'\s', '-', '{}.py'.format(name.lower())), ) pkunit.work_dir().join(n).write(d, 'wb') pkunit.pkeq(pkunit.data_dir().join(n).read(), d)
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_run_beam_solver(): """Ensure pyhellweg.run_beam_solver produces output and does not crash""" from pykern import pkio from pykern.pkunit import pkeq from rslinac.pkcli import beam_solver f = _files() with pkunit.save_chdir_work(): pkio.write_text('Solenoid.txt', pkio.read_text(pkunit.data_dir().join('Solenoid.txt'))) beam_solver.run(f['ini'], f['input'], f['output']) assert f['output'].exists() for outfile in ('PARSED.TXT', 'test1.pid'): expect = pkio.read_text(pkunit.data_dir().join(outfile)) actual = pkio.read_text(pkunit.work_dir().join(outfile)) pkeq(expect, actual)
def _generate_source(fc, sim, name): from pykern.pkunit import pkeq resp = fc.sr_get( 'pythonSource', { 'simulation_id': sim['models']['simulation']['simulationId'], 'simulation_type': sim['simulationType'], }, ) filename = '{}.py'.format(name.lower()) filename = re.sub(r'\s', '-', filename) filename = re.sub(r'[^a-z0-9\-\.]', '', filename) with open(str(pkunit.work_dir().join(filename)), 'wb') as f: f.write(resp.data) expect = pkio.read_text(pkunit.data_dir().join(filename)) pkeq(expect, resp.data)
def test_create_zip(fc): from pykern import pkio from pykern import pkunit from pykern import pkcompat from pykern.pkcollections import PKDict from pykern.pkdebug import pkdp, pkdpretty from pykern.pkunit import pkeq from sirepo import srunit import base64 import re import zipfile 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_name, sim_type)['models']['simulation']['simulationId'] with pkio.save_chdir(pkunit.work_dir()) as d: for t in 'zip', 'html': r = fc.sr_get( 'exportArchive', PKDict( simulation_type=sim_type, simulation_id=sim_id, filename='anything.' + t, )) p = d.join(sim_name + '.' + t) x = r.data if t == 'html': x = pkcompat.from_bytes(x) m = re.search(r'name="zip" \S+ value="([^"]+)"', x, flags=re.DOTALL) x = base64.b64decode(pkcompat.to_bytes(m.group(1))) p.write_binary(x) e = expect if t == 'html': e.remove('run.py') pkeq( e, sorted(zipfile.ZipFile(str(p)).namelist()), )
def pytest_runtest_protocol(item, *args, **kwargs): import signal from pykern import pkunit def _timeout(*args, **kwargs): pkunit.pkfail('MAX_CASE_RUN_SECS={} exceeded', MAX_CASE_RUN_SECS) # Seems to be the only way to get the module under test m = item._request.module is_new = m != pkunit.module_under_test if is_new: signal.signal(signal.SIGALRM, _timeout) pkunit.module_under_test = m signal.alarm(MAX_CASE_RUN_SECS) if is_new: from pykern import pkio pkio.unchecked_remove(pkunit.work_dir())
def _subprocess_setup(request, cfg=None, uwsgi=False): """setup the supervisor""" import os from pykern.pkcollections import PKDict sbatch_module = 'sbatch' in request.module.__name__ env = PKDict(os.environ) if not cfg: cfg = PKDict() i = '127.0.0.1' from pykern import pkunit from pykern import pkio # different port than default so can run tests when supervisor running p = '8101' cfg.pkupdate( PYKERN_PKDEBUG_WANT_PID_TIME='1', SIREPO_PKCLI_JOB_SUPERVISOR_IP=i, SIREPO_PKCLI_JOB_SUPERVISOR_PORT=p, SIREPO_SRDB_ROOT=str(pkio.mkdir_parent(pkunit.work_dir().join('db'))), ) if uwsgi: cfg.SIREPO_PKCLI_SERVICE_PORT = '8102' cfg.SIREPO_PKCLI_SERVICE_NGINX_PROXY_PORT = '8180' for x in 'DRIVER_LOCAL', 'DRIVER_DOCKER', 'API', 'DRIVER_SBATCH': cfg['SIREPO_JOB_{}_SUPERVISOR_URI'.format(x)] = 'http://{}:{}'.format(i, p) if sbatch_module: cfg.pkupdate(SIREPO_SIMULATION_DB_SBATCH_DISPLAY='testing@123') env.pkupdate(**cfg) import sirepo.srunit c = None if uwsgi: c = sirepo.srunit.UwsgiClient(env) else: c = sirepo.srunit.flask_client( cfg=cfg, job_run_mode='sbatch' if sbatch_module else None, ) if sbatch_module: # must be performed after fc initialized so work_dir is configured _config_sbatch_supervisor_env(env) _job_supervisor_check(env) return (env, c)
def test_processed_image(): from pykern.pkdebug import pkdp from pykern.pkunit import pkeq, pkfail from pykern import pkunit from sirepo import srunit fc = srunit.flask_client() sim_type = 'srw' fc.get('/{}'.format(sim_type)) sim_id = fc.sr_sim_data(sim_type, 'Sample from Image') resp = fc.sr_post( 'getApplicationData', { 'simulationId': sim_id, 'simulationType': sim_type, 'method': 'processedImage', 'baseImage': 'sample.tif', 'model': { 'cutoffBackgroundNoise': 0.5, 'backgroundColor': 0, 'rotateAngle': 0, 'rotateReshape': '0', 'cropArea': '1', 'areaXStart': 0, 'areaXEnd': 1280, 'areaYStart': 0, 'areaYEnd': 834, 'shiftX': 0, 'shiftY': 0, 'invert': '0', 'tileImage': '0', 'tileRows': 1, 'tileColumns': 1, 'outputImageFormat': 'tif', } }, { 'filename': 'foo.tif', }, raw_response=True, ) with open(str(pkunit.work_dir().join('x.tif')), 'wb') as f: f.write(resp.data)
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 flask_client(): """Return FlaskClient with easy access methods. Creates a new run directory every test file so can assume sharing of state on the server within a file (module). Two methods of interest: `sr_post` and `sr_get`. Returns: FlaskClient: for local requests to Flask server """ a = 'sr_unit_flask_client' if not hasattr(server.app, a): with pkio.save_chdir(pkunit.work_dir()): db = pkio.mkdir_parent('db') server.app.config['TESTING'] = True server.app.test_client_class = _TestClient server.init(db) setattr(server.app, a, server.app.test_client()) return getattr(server.app, a)
def _generate_source(fc, sim, name): from pykern import pkio, pkunit, pkdebug, pkcompat import re d = pkcompat.from_bytes(fc.sr_get( 'pythonSource', PKDict( simulation_id=sim.models.simulation.simulationId, simulation_type=sim.simulationType, ), ).data) n = re.sub( r'[^\w\-\.]', '', re.sub(r'\s', '-', '{}.py'.format(name.lower())), ) e = pkunit.data_dir().join(n) a = pkunit.work_dir().join(n) pkio.write_text(a, d) pkunit.pkeq(pkio.read_text(e), d, 'diff {} {}', e, a)
def _t(tests): from sirepo.template.srw_importer import import_python from pykern import pkio from pykern import pkresource from pykern import pkunit from pykern.pkdebug import pkdc, pkdp import glob import py with pkio.save_chdir(pkunit.work_dir()): for b in sorted(tests.keys()): base_py = '{}.py'.format(tests[b][0]) code = pkio.read_text(pkunit.data_dir().join(base_py)) actual = import_python( code, tmp_dir='.', user_filename=r'c:\anything\{}.anysuffix'.format(tests[b][0]), arguments=tests[b][1], ) actual['version'] = 'IGNORE-VALUE' pkunit.assert_object_with_json(b, actual)
def test_elegant(): from pykern.pkdebug import pkdp from pykern import pkunit, pkio, pkjson import sirepo.lib import shutil for s in pkio.sorted_glob(pkunit.data_dir().join('*')): 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) #TODO(robnagler) may not exist in all cases pkunit.pkeq('run_setup.acceptance.sdds', r.output_files[0]) 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=''))
def _subprocess_start(request, cfg=None, uwsgi=False): from pykern import pkunit from pykern.pkcollections import PKDict import sirepo.srunit import time def _subprocess(cmd): p.append(subprocess.Popen(cmd, env=env, cwd=wd)) env, c = _subprocess_setup(request, cfg, uwsgi) wd = pkunit.work_dir() p = [] try: if uwsgi: for s in ('nginx-proxy', 'uwsgi'): _subprocess(('sirepo', 'service', s)) _subprocess(('sirepo', 'job_supervisor')) for i in range(30): try: r = c.sr_post( 'jobSupervisorPing', PKDict(simulationType=sirepo.srunit.SR_SIM_TYPE_DEFAULT)) if r.state == 'ok': break except requests.exceptions.ConnectionError: pass time.sleep(.1) else: import sirepo.job_api from pykern.pkdebug import pkdlog pkdlog(sirepo.job_api.cfg.supervisor_uri) pkunit.pkfail('could not connect to {}', sirepo.job_api.cfg.supervisor_uri) yield c finally: for x in p: x.terminate() x.wait()
def test_generate_python(): from pykern import pkio from pykern.pkunit import pkeq from sirepo.template import srw from sirepo import sr_unit fc = sr_unit.flask_client() for name in ('NSLS-II CHX beamline', 'Sample from Image', 'Boron Fiber (CRL with 3 lenses)', 'Tabulated Undulator Example', 'Gaussian X-ray beam through a Beamline containing Imperfect Mirrors', 'NSLS-II SRX beamline', 'NSLS-II ESM beamline', 'Mask example', 'NSLS-II SMI beamline'): sim = fc.sr_sim_data(srw.SIM_TYPE, name) resp = fc.sr_get( 'pythonSource', { 'simulation_id': sim['models']['simulation']['simulationId'], 'simulation_type': srw.SIM_TYPE, }, raw_response=True, ) filename = '{}.py'.format(name) with open(str(pkunit.work_dir().join(filename)), 'wb') as f: f.write(resp.data) expect = pkio.read_text(pkunit.data_dir().join(filename)) pkeq(expect, resp.data)
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 pytest_runtest_protocol(item, *args, **kwargs): """Make sure work directory is empty for a module. If `item` is in a module not seen before, it removes the `pkunit.work_dir`. Args: item (Item): pytest test item (case) Returns: None: always so that the next hook runs the item. """ if not _uses_pykern: return from pykern import pkunit # Seems to be the only way to get the module under test m = item._request.module is_new = m != pkunit.module_under_test pkunit.module_under_test = m if is_new: from pykern import pkio pkio.unchecked_remove(pkunit.work_dir())
def test_beam_solver(): """Ensure BeamSolver interface solves and produces output""" from pykern import pkio from pykern.pkunit import pkeq from rslinac.solver import BeamSolver f = _files() with pkunit.save_chdir_work(): pkio.write_text('Solenoid.txt', pkio.read_text(pkunit.data_dir().join('Solenoid.txt'))) solver = BeamSolver(f['ini'], f['input']) solver.solve() solver.save_output(f['output']) assert f['output'].exists() v = solver.get_structure_parameters(1) assert v[2] == 0.0006 solver.dump_bin('all-data.bin') for outfile in ('PARSED.TXT', 'test1.pid'): expect = pkio.read_text(pkunit.data_dir().join(outfile)) actual = pkio.read_text(pkunit.work_dir().join(outfile)) pkeq(expect, actual) solver.load_bin('all-data.bin') v = solver.get_structure_parameters(1) assert v[2] == 0.0006
def test_generate_python(): from pykern import pkio from pykern.pkunit import pkeq from sirepo.template import srw from sirepo import srunit fc = srunit.flask_client() fc.get('/{}'.format(srw.SIM_TYPE)) for name in ('NSLS-II CHX beamline', 'Sample from Image', 'Boron Fiber (CRL with 3 lenses)', 'Tabulated Undulator Example', 'Gaussian X-ray beam through a Beamline containing Imperfect Mirrors', 'NSLS-II SRX beamline', 'NSLS-II ESM beamline', 'Mask example', 'NSLS-II SMI beamline'): sim = fc.sr_sim_data(srw.SIM_TYPE, name) resp = fc.sr_get( 'pythonSource', { 'simulation_id': sim['models']['simulation']['simulationId'], 'simulation_type': srw.SIM_TYPE, }, raw_response=True, ) filename = '{}.py'.format(name) with open(str(pkunit.work_dir().join(filename)), 'wb') as f: f.write(resp.data) expect = pkio.read_text(pkunit.data_dir().join(filename)) pkeq(expect, resp.data)
def auth_fc_module(request): import sirepo.srunit from pykern.pkcollections import PKDict cfg = PKDict( SIREPO_AUTH_BASIC_PASSWORD='******', SIREPO_AUTH_BASIC_UID='dev-no-validate', SIREPO_AUTH_EMAIL_FROM_EMAIL='x', SIREPO_AUTH_EMAIL_FROM_NAME='x', SIREPO_AUTH_EMAIL_SMTP_PASSWORD='******', SIREPO_AUTH_EMAIL_SMTP_SERVER='dev', SIREPO_AUTH_EMAIL_SMTP_USER='******', SIREPO_AUTH_GITHUB_CALLBACK_URI='/uri', SIREPO_AUTH_GITHUB_KEY='key', SIREPO_AUTH_GITHUB_SECRET='secret', SIREPO_AUTH_GUEST_EXPIRY_DAYS='1', SIREPO_AUTH_METHODS='basic:email:guest', SIREPO_FEATURE_CONFIG_API_MODULES='status', ) if 'email3_test' in str(request.fspath): cfg.SIREPO_AUTH_METHODS += ':github' else: cfg.SIREPO_AUTH_DEPRECATED_METHODS = 'github' from pykern import pkconfig pkconfig.reset_state_for_testing(cfg) from pykern import pkunit from pykern import pkio cfg['SIREPO_SRDB_ROOT'] = str( pkio.mkdir_parent(pkunit.work_dir().join('db'))) p, fc = _job_supervisor_start(request, cfg=cfg) if p: yield fc p.terminate() p.wait() else: yield sirepo.srunit.flask_client(cfg=cfg)
def test_processed_image(): from pykern.pkdebug import pkdp from pykern.pkunit import pkeq, pkfail from pykern import pkunit from sirepo import sr_unit fc = sr_unit.flask_client() sim_type = 'srw' sim_id = fc.sr_sim_data(sim_type, 'Sample from Image') resp = fc.sr_post( 'getApplicationData', { 'simulationId': sim_id, 'simulationType': sim_type, 'method': 'processedImage', 'baseImage': 'sample.tif', }, { 'filename': 'foo.tif', }, raw_response=True, ) with open(str(pkunit.work_dir().join('x.tif')), 'wb') as f: f.write(resp.data)
def test_synergia_data_file(fc): from pykern import pkunit, pkcompat, pkio from pykern.pkcollections import PKDict from pykern.pkdebug import pkdp import h5py data = fc.sr_sim_data('IOTA 6-6 Bare') run = fc.sr_post( 'runSimulation', PKDict( forceRun=True, models=data.models, report='animation', simulationId=data.models.simulation.simulationId, simulationType=data.simulationType, ), ) pkunit.pkeq('pending', run.state, 'not pending, run={}', run) for _ in range(20): if run.state == 'completed': break run = fc.sr_post('runStatus', run.nextRequest) time.sleep(1) else: pkunit.pkfail('runStatus: failed to complete: {}', run) r = fc.sr_get( 'downloadDataFile', PKDict( simulation_type=data.simulationType, simulation_id=data.models.simulation.simulationId, model='bunchAnimation', frame='1', ), ) pkunit.pkeq(200, r.status_code) m = re.search( r'attachment; filename="([^"]+)"', r.headers.get('Content-Disposition'), ) d = pkunit.work_dir() path = d.join(m.group(1)) path.write_binary(r.data) with h5py.File(str(path), 'r') as f: pkunit.pkok(f['charge'], 'missing charge from {}', path) r = fc.sr_get( 'downloadDataFile', PKDict( simulation_type=data.simulationType, simulation_id=data.models.simulation.simulationId, model='beamEvolutionAnimation', frame='1', ), ) pkunit.pkeq(200, r.status_code) m = re.search( r'attachment; filename="([^"]+)"', r.headers.get('Content-Disposition'), ) d = pkunit.work_dir() path = d.join(m.group(1)) path.write_binary(r.data) with h5py.File(str(path), 'r') as f: pkunit.pkok(f['num_particles'], 'missing num_particles from {}', path)
def test_srw_delete(fc): from pykern import pkunit from pykern.pkcollections import PKDict from pykern.pkdebug import pkdp import sirepo.sim_data d = fc.sr_sim_data('Tabulated Undulator Example') s = sirepo.sim_data.get_class(fc.sr_sim_type) u = pkunit.work_dir().join('not_used_name.zip') s.lib_file_resource_dir().join('magnetic_measurements.zip').copy(u) t = 'undulatorTable' d.models.tabulatedUndulator.magneticFile = u.basename r = fc.sr_post( 'saveSimulationData', data=d, file=u, ) pkunit.pkeq(u.basename, r.models.tabulatedUndulator.magneticFile) 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=u, ) pkunit.pkeq(u.basename, r.get('filename'), 'unexpected response={}', r) r = fc.sr_post( 'deleteFile', PKDict( fileType=t, filename=u.basename, simulationType=fc.sr_sim_type, ), ) pkunit.pkre('in use', r.get('error', '')) fc.sr_post( 'deleteSimulation', PKDict( simulationType=fc.sr_sim_type, simulationId=d.models.simulation.simulationId, ), ) r = fc.sr_post( 'deleteFile', PKDict( fileType=t, filename=u.basename, simulationType=fc.sr_sim_type, ), ) pkunit.pkeq('ok', r.get('state'), 'unexpected response={}', r) r = fc.sr_get_json( 'downloadFile', params=PKDict( simulation_type=fc.sr_sim_type, simulation_id=d.models.simulation.simulationId, filename=u.basename, ), data=PKDict(), redirect=False, ) pkunit.pkre('does not exist', r.error)