示例#1
0
文件: srunit.py 项目: JiayangY/sirepo
def test_in_request(op, cfg=None, before_request=None, headers=None, want_cookie=True, want_user=True, **kwargs):
    fc = flask_client(cfg, **kwargs)
    try:
        from pykern import pkunit
        from pykern import pkcollections

        if before_request:
            before_request(fc)
        setattr(
            server._app,
            server.SRUNIT_TEST_IN_REQUEST,
            PKDict(op=op, want_cookie=want_cookie, want_user=want_user),
        )
        from sirepo import uri_router
        r = fc.get(
            uri_router.srunit_uri,
            headers=headers,
        )
        pkunit.pkeq(200, r.status_code, 'FAIL: status={}', r.status)
        if r.mimetype == 'text/html':
            m = _JAVASCRIPT_REDIRECT_RE.search(r.data)
            if m:
                pkunit.pkfail('redirect={}', m.group(1))
            pkunit.pkfail('other html response={}', r.data)
        d = pkcollections.json_load_any(r.data)
        pkunit.pkeq('ok', d.get('state'), 'FAIL: data={}', d)
    finally:
        try:
            delattr(server._app, server.SRUNIT_TEST_IN_REQUEST)
        except AttributeError:
            pass
    return r
示例#2
0
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()
示例#3
0
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)
示例#4
0
 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)
示例#5
0
def test_runError(fc):
    from pykern import pkunit
    from pykern.pkdebug import pkdc, pkdp, pkdlog
    import time

    d = fc.sr_sim_data()
    d.models.simulation.name = 'srunit_error_run'
    d = fc.sr_post(
        'runSimulation',
        dict(
            forceRun=False,
            models=d.models,
            report='heightWeightReport',
            simulationId=d.models.simulation.simulationId,
            simulationType=d.simulationType,
        ),
    )
    for _ in range(10):
        if d.state == 'error':
            pkunit.pkeq("a big ugly error", d.error)
            return
        time.sleep(d.nextRequestSeconds)
        d = fc.sr_post('runStatus', d.nextRequest)
    else:
        pkunit.pkfail('Error never returned d={}', d)
示例#6
0
def _job_supervisor_start(request, cfg=None):
    import os
    if os.environ.get('SIREPO_FEATURE_CONFIG_JOB') != '1':
        return None, None

    from pykern import pkunit
    from pykern.pkcollections import PKDict
    import subprocess
    import time

    env, fc = _job_supervisor_setup(request, cfg)
    p = subprocess.Popen(
        ['pyenv', 'exec', 'sirepo', 'job_supervisor'],
        env=env,
    )
    for i in range(30):
        r = fc.sr_post('jobSupervisorPing',
                       PKDict(simulationType=fc.SR_SIM_TYPE_DEFAULT))
        if r.state == 'ok':
            break
        time.sleep(.1)
    else:
        import sirepo.job_api
        pkunit.pkfail('could not connect to {}', sirepo.job_api.SUPERVISOR_URI)
    return p, fc
示例#7
0
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)
示例#8
0
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)
示例#9
0
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)
示例#10
0
文件: job_test.py 项目: ahebnl/Sirepo
def test_runSimulation(fc):
    from pykern import pkunit
    from pykern.pkdebug import pkdp, pkdlog
    from sirepo import job
    import time

    d = fc.sr_sim_data()
    d = 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(d)
        assert d.state != 'error'
        if d.state == 'completed':
            break
        time.sleep(d.nextRequestSeconds)
        d = fc.sr_post('runStatus', d.nextRequest)
    else:
        pkunit.pkfail('runStatus: failed to complete: {}', d)
    # Just double-check it actually worked
    assert u'plots' in d
示例#11
0
def _run_sim(fc, op):
    from pykern import pkunit

    n = "Young's Double Slit Experiment"
    m = 'multiElectronAnimation'
    t = 'srw'
    c = None

    fc.sr_login_as_guest(sim_type=t)
    d = fc.sr_sim_data(n)
    try:
        r = fc.sr_post(
            'runSimulation',
            PKDict(
                models=d.models,
                report=m,
                simulationId=d.models.simulation.simulationId,
                simulationType=d.simulationType,
            ))
        c = r.nextRequest
        for _ in range(10):
            if r.state == 'running':
                op(fc, t)
                return
            r = fc.sr_post('runStatus', r.nextRequest)
            time.sleep(1)
        else:
            pkunit.pkfail('Never entered running state')
    finally:
        fc.sr_post('runCancel', c)
示例#12
0
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,
    )
示例#13
0
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)
示例#14
0
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,
    )
示例#15
0
def test_srw(fc):
    from pykern import pkunit, pkcompat
    from pykern.pkdebug import pkdlog, pkdexc
    import time
    m = 'multiElectronAnimation'
    data = fc.sr_sim_data("Young's Double Slit Experiment")
    try:
        r = fc.sr_post(
            'runSimulation',
            PKDict(
                models=data.models,
                report=m,
                simulationId=data.models.simulation.simulationId,
                simulationType=data.simulationType,
            ),
        )
        if r.state == 'completed':
            return r
        cancel = r.get('nextRequest')
        for _ in range(10):
            if r.state == 'canceled':
                pkunit.pkeq(
                    int(_MAX_SECS_PARALLEL_PREMIUM),
                    r.canceledAfterSecs,
                )
                cancel = None
                break
            r = fc.sr_post('runStatus', r.nextRequest)
            time.sleep(1)
        else:
            pkunit.pkfail('did not cancel in time')
    finally:
        if cancel:
            fc.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))
            raise AssertionError('cancel failed')
示例#16
0
 def _get_simulation_running():
     d = auth_fc.sr_sim_data(sim_name=n, sim_type='srw')
     r = fc.sr_post(
         'runSimulation',
         PKDict(
             models=d.models,
             report=m,
             simulationId=d.models.simulation.simulationId,
             simulationType=d.simulationType,
         ))
     try:
         for _ in range(10):
             if r.state == 'running':
                 return r.nextRequest
             r = fc.sr_post('runStatus', r.nextRequest)
             time.sleep(1)
         else:
             pkunit.pkfail('Never entered running state')
     except Exception:
         fc.sr_post('runCancel', r.nextRequest)
         raise
示例#17
0
def test_synergia(fc):
    from pykern import pkunit
    from pykern.pkdebug import pkdc, pkdp, pkdlog
    import time

    d = fc.sr_sim_data(sim_name='IOTA 6-6 with NLINSERT',
                       sim_type=fc.sr_sim_type)
    r = fc.sr_post(
        'runSimulation',
        dict(
            forceRun=False,
            models=d.models,
            report='bunchReport1',
            simulationId=d.models.simulation.simulationId,
            simulationType=d.simulationType,
        ),
    )
    for _ in range(50):
        assert r.state != 'error'
        if r.state == 'running':
            break
        time.sleep(.1)
        r = fc.sr_post('runStatus', r.nextRequest)
    else:
        pkunit.pkfail('runStatus: failed to start running: {}', r)
    x = r.nextRequest
    r = fc.sr_post('runCancel', x)
    assert r.state == 'canceled'
    r = fc.sr_post('runStatus', x)
    assert r.state == 'canceled'
    r = fc.sr_post(
        'runSimulation',
        dict(
            forceRun=False,
            models=d.models,
            report='bunchReport1',
            simulationId=d.models.simulation.simulationId,
            simulationType=d.simulationType,
        ),
    )
示例#18
0
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()
示例#19
0
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,
    )
示例#20
0
def test_srw_cancel(fc):
    from pykern import pkunit, pkcompat
    import subprocess
    import time

    d = fc.sr_sim_data("Young's Double Slit Experiment", sim_type='srw')
    r = fc.sr_post(
        'runSimulation',
        dict(
            forceRun=False,
            models=d.models,
            report='multiElectronAnimation',
            simulationId=d.models.simulation.simulationId,
            simulationType=d.simulationType,
        ),
    )
    for _ in range(10):
        pkunit.pkok(r.state != 'error', 'expected 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
    r = fc.sr_post('runCancel', x)
    pkunit.pkeq('canceled', r.state)
    r = fc.sr_post('runStatus', x)
    pkunit.pkeq('canceled', r.state)
    o = pkcompat.from_bytes(
        subprocess.check_output(['ps', 'axww'], stderr=subprocess.STDOUT), )
    o = list(filter(lambda x: 'mpiexec' in x, o.split('\n')))
    pkunit.pkok(
        not o,
        'found "mpiexec" after cancel in ps={}',
        '\n'.join(o),
    )
示例#21
0
文件: job_test.py 项目: ahebnl/Sirepo
def test_runCancel(fc):
    from pykern import pkunit
    from pykern.pkdebug import pkdc, pkdp, pkdlog
    import time

    d = fc.sr_sim_data()
    d.models.simulation.name = 'srunit_long_run'
    d = fc.sr_post(
        'runSimulation',
        dict(
            forceRun=False,
            models=d.models,
            report=_REPORT,
            simulationId=d.models.simulation.simulationId,
            simulationType=d.simulationType,
        ),
    )
    for _ in range(10):
        assert d.state != 'error'
        if d.state == 'running':
            break
        time.sleep(d.nextRequestSeconds)
        d = fc.sr_post('runStatus', d.nextRequest)
    else:
        pkunit.pkfail('runStatus: failed to start running: {}', d)

    x = d.nextRequest
    d = fc.sr_post(
        'runCancel',
        x,
    )
    assert d.state == 'canceled'
    d = fc.sr_post(
        'runStatus',
        x,
    )
    assert d.state == 'canceled'
示例#22
0
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,
    )
示例#23
0
文件: srunit.py 项目: njsmith/sirepo
    def sr_sim_data(self, sim_type, sim_name):
        """Return simulation data by name

        Args:
            sim_type (str): app
            sim_name (str): case sensitive name

        Returns:
            dict: data
        """
        data = self.sr_post('listSimulations', {'simulationType': sim_type})
        for d in data:
            if d['name'] == sim_name:
                break
        else:
            pkunit.pkfail('{}: not found in ', sim_name, pkdpretty(data))
        return self.sr_get(
            'simulationData',
            {
                'simulation_type': sim_type,
                'pretty': '0',
                'simulation_id': d['simulationId'],
            },
        )
示例#24
0
文件: conftest.py 项目: cchall/sirepo
 def _timeout(*args, **kwargs):
     signal.signal(signal.SIGALRM, _timeout_failed)
     signal.alarm(1)
     pkunit.pkfail('MAX_CASE_RUN_SECS={} exceeded', MAX_CASE_RUN_SECS)
示例#25
0
def test_pkexcept():
    import re, inspect
    from pykern.pkunit import pkexcept, pkfail
    with pkexcept(KeyError, 'should see a KeyError'):
        {}['not found']
    with pkexcept('KeyError.*xyzzy'):
        {}['xyzzy']
    try:
        lineno = inspect.currentframe().f_lineno + 2
        with pkexcept(KeyError, 'xyzzy'):
            pass
    except AssertionError as e:
        assert 'xyzzy' in e.message
        assert 'pkunit_test.py:{}:test_pkexcept'.format(lineno) in e.message
    except Exception as e:
        pkfail('{}: got exception, but not AssertionError', e)
    else:
        pkfail('did not raise AssertionError')
    try:
        with pkexcept(KeyError):
            raise NameError('whatever')
    except AssertionError as e:
        assert re.search(r'exception was raised.*but expected.*KeyError',
                         e.message)
    except Exception as e:
        pkfail('{}: got exception, but not AssertionError', e)
    else:
        pkfail('did not raise AssertionError')
    try:
        lineno = inspect.currentframe().f_lineno + 2
        with pkexcept('any pattern'):
            pass
    except AssertionError as e:
        assert 'pkunit_test.py:{}:test_pkexcept'.format(lineno) in e.message
        assert 'was not raised' in e.message
    except Exception as e:
        pkfail('{}: got exception, but not AssertionError', e)
    else:
        pkfail('did not raise AssertionError')
示例#26
0
def test_get_data_file():
    from sirepo import srunit
    from pykern import pkunit
    from pykern import pkio
    import sdds

    fc = srunit.flask_client()
    fc.get('/elegant')
    data = fc.sr_post(
        'listSimulations',
        {'simulationType': 'elegant', 'search': {'simulationName': 'fourDipoleCSR'}},
    )
    data = data[0].simulation
    data = fc.sr_get(
        'simulationData',
        params=dict(
            pretty='1',
            simulation_id=data.simulationId,
            simulation_type='elegant',
        ),
    )
    run = fc.sr_post(
        'runSimulation',
        dict(
            forceRun=False,
            models=data.models,
            report='bunchReport1',
            simulationId=data.models.simulation.simulationId,
            simulationType=data.simulationType,
        ),
    )
    for _ in range(10):
        run = fc.sr_post(
            'runStatus',
            run.nextRequest
        )
        if run.state == 'completed':
            break
        time.sleep(1)
    else:
        pkunit.pkfail('runStatus: failed to complete: {}', run)
    resp = fc.sr_get(
        'downloadDataFile',
        dict(
            simulation_type=data.simulationType,
            simulation_id=data.models.simulation.simulationId,
            model='bunchReport1',
            frame='-1',
            suffix='csv',
        ),
        raw_response=True,
    )
    rows = csv.reader(StringIO.StringIO(resp.get_data()))
    assert len(list(rows)) == 5001
    resp = fc.sr_get(
        'downloadDataFile',
        dict(
            simulation_type=data.simulationType,
            simulation_id=data.models.simulation.simulationId,
            model='bunchReport1',
            frame='-1',
        ),
        raw_response=True,
    )
    m = re.search(r'attachment; filename="([^"]+)"', resp.headers['Content-Disposition'])
    with pkunit.save_chdir_work():
        path = pkio.py_path(m.group(1))
        with open(str(path), 'w') as f:
            f.write(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)
示例#27
0
 def _timeout(*args, **kwargs):
     pkunit.pkfail('MAX_CASE_RUN_SECS={} exceeded', MAX_CASE_RUN_SECS)
示例#28
0
def test_get_data_file():
    from sirepo import sr_unit
    from pykern import pkunit
    from pykern import pkio
    import sdds

    fc = sr_unit.flask_client()
    fc.get('/elegant')
    data = fc.sr_post(
        'listSimulations',
        {'simulationType': 'elegant', 'search': {'simulationName': 'fourDipoleCSR'}},
    )
    data = data[0].simulation
    data = fc.sr_get(
        'simulationData',
        params=dict(
            pretty='1',
            simulation_id=data.simulationId,
            simulation_type='elegant',
        ),
    )
    run = fc.sr_post(
        'runSimulation',
        dict(
            forceRun=False,
            models=data.models,
            report='bunchReport1',
            simulationId=data.models.simulation.simulationId,
            simulationType=data.simulationType,
        ),
    )
    for _ in range(10):
        run = fc.sr_post(
            'runStatus',
            run.nextRequest
        )
        if run.state == 'completed':
            break
        time.sleep(1)
    else:
        pkunit.pkfail('runStatus: failed to complete: {}', run)
    resp = fc.sr_get(
        'downloadDataFile',
        dict(
            simulation_type=data.simulationType,
            simulation_id=data.models.simulation.simulationId,
            model='bunchReport1',
            frame='-1',
            suffix='csv',
        ),
        raw_response=True,
    )
    rows = csv.reader(StringIO.StringIO(resp.get_data()))
    assert len(list(rows)) == 5001
    resp = fc.sr_get(
        'downloadDataFile',
        dict(
            simulation_type=data.simulationType,
            simulation_id=data.models.simulation.simulationId,
            model='bunchReport1',
            frame='-1',
        ),
        raw_response=True,
    )
    m = re.search(r'attachment; filename="([^"]+)"', resp.headers['Content-Disposition'])
    with pkunit.save_chdir_work():
        path = pkio.py_path(m.group(1))
        with open(str(path), 'w') as f:
            f.write(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)
示例#29
0
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)
示例#30
0
def test_runner_myapp():
    os.environ['SIREPO_FEATURE_CONFIG_RUNNER_DAEMON'] = '1'
    os.environ['PYTHONUNBUFFERED'] = '1'
    py3_env = _assert_py3()

    from pykern import pkio
    from pykern import pkunit
    from pykern.pkdebug import pkdc, pkdp
    from sirepo import srunit

    fc = srunit.flask_client(sim_types='myapp')
    fc.sr_login_as_guest()

    from sirepo import srdb
    pkdc(srdb.runner_socket_path())

    pkio.unchecked_remove(srdb.runner_socket_path())

    runner_env = dict(py3_env)
    runner_env['SIREPO_SRDB_ROOT'] = str(srdb.root())
    runner = subprocess.Popen(
        ['pyenv', 'exec', 'sirepo', 'runner', 'start'],
        env=runner_env,
    )
    try:
        for _ in range(30):
            if srdb.runner_socket_path().exists():
                break
            time.sleep(0.1)
        else:
            pkunit.pkfail('runner daemon did not start up')

        fc.get('/myapp')
        data = fc.sr_post(
            'listSimulations',
            {'simulationType': 'myapp',
             'search': {'simulationName': 'heightWeightReport'}},
        )
        pkdc(data)
        data = data[0].simulation
        pkdc(data)
        data = fc.sr_get_json(
            'simulationData',
            params=dict(
                pretty='1',
                simulation_id=data.simulationId,
                simulation_type='myapp',
            ),
        )
        pkdc(data)
        run = fc.sr_post(
            'runSimulation',
            dict(
                forceRun=False,
                models=data.models,
                report='heightWeightReport',
                simulationId=data.models.simulation.simulationId,
                simulationType=data.simulationType,
            ),
        )
        pkdc(run)
        for _ in range(10):
            run = fc.sr_post(
                'runStatus',
                run.nextRequest
            )
            pkdc(run)
            if run.state == 'completed':
                break
            time.sleep(1)
        else:
            pkunit.pkfail('runStatus: failed to complete: {}', run)
        # Just double-check it actually worked
        assert u'plots' in run
    finally:
        runner.terminate()
        runner.wait()
示例#31
0
def test_myapp_cancel(fc):
    """https://github.com/radiasoft/sirepo/issues/2346"""
    from pykern import pkunit
    import time
    import threading
    from pykern.pkdebug import pkdlog

    d = fc.sr_sim_data()
    d.models.simulation.name = 'srunit_long_run'
    r = 'heightWeightReport'

    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)

    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):
        pkdlog(r1)
        pkunit.pkok(r1.state != 'error', 'unexpected error state: {}')
        if r1.state == 'running':
            break
        time.sleep(.1)
        r1 = fc.sr_post('runStatus', r1.nextRequest)
    else:
        pkunit.pkfail('runStatus: failed to start running: {}', r1)

    t2 = threading.Thread(target=_t2)
    t2.start()
    time.sleep(.1)
    pkdlog('runCancel')
    c = fc.sr_post('runCancel', r1.nextRequest)
    pkunit.pkeq('canceled', c.state)

    pkdlog('start 3')
    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 == 'running':
            break
        time.sleep(.1)
        r1 = fc.sr_post('runStatus', r1.nextRequest)
    else:
        pkunit.pkfail('runStatus: failed to start running: {}', r1)
    c = fc.sr_post('runCancel', r1.nextRequest)
    pkunit.pkeq('canceled', c.state)
示例#32
0
def test_pkexcept():
    import re, inspect
    from pykern.pkunit import pkexcept, pkfail
    with pkexcept(KeyError, 'should see a KeyError'):
        {}['not found']
    with pkexcept('KeyError.*xyzzy'):
        {}['xyzzy']
    try:
        lineno = inspect.currentframe().f_lineno + 2
        with pkexcept(KeyError, 'xyzzy'):
            pass
    except AssertionError as e:
        assert 'xyzzy' in e.message
        assert 'pkunit_test.py:{}:test_pkexcept'.format(lineno) in e.message
    except Exception as e:
        pkfail('{}: got exception, but not AssertionError', e)
    else:
        pkfail('did not raise AssertionError')
    try:
        with pkexcept(KeyError):
            raise NameError('whatever')
    except AssertionError as e:
        assert re.search(r'exception was raised.*but expected.*KeyError', e.message)
    except Exception as e:
        pkfail('{}: got exception, but not AssertionError', e)
    else:
        pkfail('did not raise AssertionError')
    try:
        lineno = inspect.currentframe().f_lineno + 2
        with pkexcept('any pattern'):
            pass
    except AssertionError as e:
        assert 'pkunit_test.py:{}:test_pkexcept'.format(lineno) in e.message
        assert 'was not raised' in e.message
    except Exception as e:
        pkfail('{}: got exception, but not AssertionError', e)
    else:
        pkfail('did not raise AssertionError')