Esempio n. 1
0
def _do(file_ext, parse):
    from pykern import pkio
    from pykern import pkunit
    from pykern import pkcollections
    from pykern.pkdebug import pkdp
    from pykern.pkunit import pkeq, pkfail, pkok
    from sirepo import sr_unit
    import re

    fc = sr_unit.flask_client()
    for suffix in '', ' (2)', ' (3)':
        for f in pkio.sorted_glob(pkunit.data_dir().join('*.' + file_ext)):
            json, stream = parse(f)
            sim_type = re.search(r'^([a-z]+)_', f.basename).group(1)
            is_dev = 'deviance' in f.basename
            if not is_dev:
                sim_name = pkcollections.json_load_any(
                    json).models.simulation.name
            res = fc.sr_post_form(
                'importFile',
                {
                    'file': (stream, f.basename),
                    'folder': '/importer_test',
                },
                {'simulation_type': sim_type},
            )
            if is_dev:
                m = re.search(r'Error: (.+)', json)
                if m:
                    expect = m.group(1)
                    pkeq(expect, res.error)
                continue
            pkeq(sim_name + suffix, res.models.simulation.name)
Esempio n. 2
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)
Esempio n. 3
0
def test_auth_login():
    from pykern import pkcollections
    from pykern.pkdebug import pkdp
    from pykern.pkunit import pkeq
    from sirepo import sr_unit

    fc = sr_unit.flask_client(cfg=dict(
        SIREPO_BLUESKY_AUTH_SECRET=
        '3SExmbOzn1WeoCWeJxekaE6bMDUj034Pu5az1hLNnvENyvL1FAJ1q3eowwODoa3f', ),
                              )
    from sirepo import simulation_db
    from sirepo import bluesky

    fc.get('/srw')
    data = fc.sr_post(
        'listSimulations',
        {
            'simulationType': 'srw',
            'search': {
                'simulationName': 'Bending Magnet Radiation'
            }
        },
    )
    data = data[0].simulation
    req = pkcollections.Dict(
        simulationType='srw',
        simulationId=data.simulationId,
    )
    bluesky.auth_hash(req)
    resp = fc.sr_post('blueskyAuth', req)
    pkeq('ok', resp['state'])
    pkeq(req.simulationId, simulation_db.parse_sid(resp['data']))
    pkeq('srw', resp['schema']['simulationType'])
Esempio n. 4
0
def test_not_found():
    from pykern.pkdebug import pkdp
    from pykern.pkunit import pkeq
    from sirepo import sr_unit

    fc = sr_unit.flask_client()
    for uri in ('/some random uri', '/srw/wrong-param', '/export-archive'):
        resp = fc.get(uri)
        pkeq(404, resp.status_code)
Esempio n. 5
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,
    )
Esempio n. 6
0
def test_srw():
    from pykern import pkio
    from pykern.pkdebug import pkdpretty
    from sirepo import sr_unit
    import json

    fc = sr_unit.flask_client()
    resp = fc.get('/srw')
    assert '<!DOCTYPE html' in resp.data, \
        'Top level document is html'
    data = fc.sr_post(
        'listSimulations',
        {'simulationType': 'srw', 'search': ''},
    )
Esempio n. 7
0
def test_srw():
    from pykern import pkio
    from pykern.pkdebug import pkdpretty
    from sirepo import sr_unit
    import json

    fc = sr_unit.flask_client()
    resp = fc.get('/srw')
    assert '<!DOCTYPE html' in resp.get_data(), \
        'Top level document is html'
    data = fc.sr_post(
        'listSimulations',
        {'simulationType': 'srw', 'search': ''},
    )
Esempio n. 8
0
    def t():
        from pykern.pkdebug import pkdp
        from pykern.pkunit import pkeq, pkexcept, pkre
        from sirepo import uri_router
        import re

        fc = sr_unit.flask_client()
        uri = uri_router.uri_for_api('homePage')
        pkre('http://[^/]+/light$', uri)
        uri = uri_router.uri_for_api('homePage', external=False)
        pkre('^/light$', uri)
        with pkexcept(KeyError):
            uri_router.uri_for_api('notAnApi')
        with pkexcept('missing parameter'):
            uri_router.uri_for_api('exportArchive', {'simulation_type': 'srw'})
Esempio n. 9
0
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',
    )
Esempio n. 10
0
def test_importer_in_flask():
    from pykern import pkio
    from pykern import pkunit
    from pykern.pkdebug import pkdp
    from pykern.pkunit import pkeq
    from sirepo import sr_unit
    import re

    fc = sr_unit.flask_client()
    for f in [pkunit.data_dir().join('lcls_sxr.py')]:
        res = fc.sr_post_form(
            'importFile',
            {
                'file': (open(str(f), 'r'), f.basename),
                'folder': '/srw_import_test',
            },
            {'simulation_type': 'srw'},
        )
        pkeq(f.purebasename, res.models.simulation.name)
Esempio n. 11
0
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,
            )
Esempio n. 12
0
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',
            '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)
Esempio n. 13
0
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)
Esempio n. 14
0
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)
Esempio n. 15
0
def test_basic():
    from sirepo import sr_unit
    fc = sr_unit.flask_client()
    resp = fc.get('/')
    assert 'LandingPageController' in resp.data, \
        'Top level document is the landing page'
Esempio n. 16
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)
Esempio n. 17
0
def test_basic():
    from sirepo import sr_unit
    fc = sr_unit.flask_client()
    resp = fc.get('/')
    assert 'LandingPageController' in resp.get_data(), \
        'Top level document is the landing page'