def test_create_delete_webhook(real_actor_id, tenant_id): '''Ensure various properties are present and the right class''' r = Reactor() webhook_uri = r.create_webhook(actorId=real_actor_id) assert tenant_id in webhook_uri assert real_actor_id in webhook_uri r.delete_webhook(webhook_uri, actorId=real_actor_id)
def test_read_override(monkeypatch): '''Ensure various properties are present and the right class''' r = Reactor() assert 'logs' in r.settings.keys() assert r.settings.logs.level == 'DEBUG' monkeypatch.setenv('_REACTOR_LOGS_LEVEL', 'INFO') p = Reactor() assert p.settings.logs.level == 'INFO'
def test_session_env(monkeypatch): '''can we set up environment for messaging''' monkeypatch.setenv('SESSION', 'slimy-eel') r = Reactor() e = r._get_environment() assert isinstance(e, dict) assert 'x_session' in e assert e['x_session'] == 'slimy-eel'
def test_validate_schema(): '''Ensure at least one schema can validate''' pytest.skip() r = Reactor() messagedict = json.loads('{"key": "value"}') valid = r.validate_message(messagedict, messageschema='/message.jsonschema', permissive=False) assert valid is True
def test_resolve_valid_actor_id(): '''If passed an actorID, return it''' valid_actor_id = 'OZYYyRpreyYBz' invalid_actor_id = 'rRpyeOZYYyYBz' r = Reactor() id = r.resolve_actor_alias(valid_actor_id) assert id == valid_actor_id id = r.resolve_actor_alias(invalid_actor_id) assert id == invalid_actor_id
def test_add_delete_nonce(real_actor_id, tenant_id): '''Ensure various properties are present and the right class''' r = Reactor() nonce = r.add_nonce(permission='READ', maxuses=1, actorId=real_actor_id) assert 'id' in nonce nonce_id = nonce.get('id') assert nonce_id != '' # Nonces include the tenant ID to allow for routing upstream of APIM assert nonce_id.upper().startswith(tenant_id.upper()) deleted = r.delete_nonce(nonce_id, actorId=real_actor_id) assert deleted is None
def test_resolve_app_id(): '''If passed an Agave appID, return it''' private_app_id = 'chimichangas-2.0.0' public_app_id = 'chimichangas-2.0.0u1' invalid_app_id = 'chimichangas' r = Reactor() id = r.resolve_actor_alias(private_app_id) assert id == private_app_id id = r.resolve_actor_alias(public_app_id) assert id == public_app_id id = r.resolve_actor_alias(invalid_app_id) assert id == invalid_app_id
def test_reactors_send_message(actor_id, message, success): r = Reactor() if success is True: exec_id = r.send_message(actor_id, message, retryMaxAttempts=1, ignoreErrors=False) assert exec_id is not None else: with pytest.raises(AgaveError): r.send_message(actor_id, message, retryMaxAttempts=1, ignoreErrors=False)
def test_pemagent_listdir(system, path, fail): r = Reactor() if fail is False: assert isinstance(r.pemagent.listdir(system, path), tuple) else: with pytest.raises(Exception): r.pemagent.listdir(system, path)
def main(): """Main function""" # create the reactor object r = Reactor() r.logger.info("Hello this is actor {}".format(r.uid)) # pull in reactor context context = r.context print(context) # get the message that was sent to the actor message = context.message_dict # ex UI site = message['site'] subject_id = message['subject_id'] session = message['session'] zipfile = message['zipfile'] outdir = message['outdir'] # check datasets table for subject and baseline visit entries # and write entries if they don't exist (subject_dataset_key, baseline_visit_protocol_key, baseline_visit_dataset_key, image_protocol_key) = \ check_and_write_datasets(subject_id) # upload event site_key = get_key_for_table('organization_id', 'organization', 'name', site) write_data_event(image_protocol_key, 1, subject_id, site_key, baseline_visit_dataset_key)
def test_username_from_client(): '''Can we pick up username from Agave.client''' from agavepy.agave import Agave ag = Agave.restore() local_uname = ag.profiles.get()['username'] r = Reactor() assert local_uname == r.username
def test_username_from_env(monkeypatch): '''Can we pick up username from ENV''' monkeypatch.setenv('_abaco_username', 'taco') assert '_abaco_username' in os.environ r = Reactor() assert 'username' in r.context r_uname = r.username assert r_uname == 'taco'
def test_log_redact_inited(caplog, capsys, monkeypatch): '''Verify that content of 'redactions' list passed to init() are honored''' monkeypatch.setenv('_REACTOR_LOGS_LEVEL', 'DEBUG') message = 'Sekwit' r = Reactor(redactions=[message]) r.logger.debug('I have a Very{}Message for you!'.format(message)) out, err = capsys.readouterr() assert message not in err assert message in caplog.text
def test_log_stderr(caplog, capsys): '''Verify logging to stderr works''' message = 'Hello' r = Reactor() r.logger.info(message) out, err = capsys.readouterr() assert [message] == [rec.message for rec in caplog.records] assert message in err assert message not in out
def test_env_from_mock(): '''Does the environment get populated in mock context''' r = Reactor() tok = r.client._token srv = r.client.api_server assert os.environ.get('_abaco_api_server', None) == srv assert os.environ.get('_abaco_access_token', None) == tok for k, v in ABACO_VARS_MAP.items(): assert v in os.environ.keys()
def test_log_redact_apitoken(caplog, capsys, monkeypatch): '''Verify that x-nonce is redacted since it is an impersonation token''' monkeypatch.setenv('_REACTOR_LOGS_LEVEL', 'DEBUG') r = Reactor() message = r._token r.logger.debug('redact this token: {}'.format(message)) out, err = capsys.readouterr() assert message not in err assert message in caplog.text
def test_log_redact_nonce(caplog, capsys, monkeypatch): '''Verify that x-nonce is redacted since it is an impersonation token''' message = 'VewyVewySekwit' monkeypatch.setenv('x-nonce', message) monkeypatch.setenv('_REACTOR_LOGS_LEVEL', 'DEBUG') r = Reactor() r.logger.debug('context: {}'.format(r.context)) out, err = capsys.readouterr() assert message not in err assert message in caplog.text
def test_log_redact_env(caplog, capsys, monkeypatch): '''Verify that the text of an override value cannot be logged''' monkeypatch.setenv('_REACTOR_REDACT', 'VewyVewySekwit') monkeypatch.setenv('_REACTOR_LOGS_LEVEL', 'DEBUG') r = Reactor() r.logger.debug(r.settings) out, err = capsys.readouterr() assert 'VewyVewySekwit' in caplog.text assert 'VewyVewySekwit' not in err assert 'VewyVewySekwit' not in out
def test_abacoschemas(test_data): '''Test all known Abaco schemas''' pytest.skip() r = Reactor() exceptions = [] for comp in test_data: mdict = comp.get('object', {}) schem = os.path.join('/abacoschemas', comp.get('schema')) # perm = True validates = comp.get('validates') try: r.validate_message(mdict, messageschema=schem, permissive=False) except Exception as e: # The message did not validate tho we expected it to if validates is True: exceptions.append(e) if len(exceptions) > 0: raise Exception("Exceptions: {}".format(exceptions))
def test_get_aliases(fake_alias, fake_actor_id): '''Ensure various properties are present and the right class''' r = Reactor() r.aliases.set_alias(fake_alias, fake_actor_id) aliases = r.aliases.get_aliases() assert isinstance(aliases, list) assert isinstance(fake_alias, basestring) for test_alias in aliases: assert isinstance(test_alias, basestring) assert set([fake_alias]).issubset(set(aliases))
def test_init(): '''Ensure various properties are present and the right class''' r = Reactor() assert r.uid is not None assert r.execid is not None assert r.state is not None assert isinstance(r.local, bool) assert isinstance(r.context, AttrDict) # assert isinstance(r.state, dict) assert isinstance(r.client, Agave) assert isinstance(r.logger, Logger)
def test_get_me(monkeypatch, fake_alias, fake_actor_id): monkeypatch.setenv('_abaco_actor_id', fake_actor_id) r = Reactor() actor_id = r.uid assert actor_id in fake_actor_id response = r.aliases.get_name(alias='me') assert response in fake_actor_id assert isinstance(response, basestring) response = r.aliases.get_name(alias=u'me') assert response in fake_actor_id assert isinstance(response, basestring)
def test_delete_all_nonces(real_actor_id): '''Ensure various properties are present and the right class''' r = Reactor() for a in range(0, 5): r.add_nonce(permission='READ', maxuses=1, actorId=real_actor_id) nonces = r.list_nonces(actorId=real_actor_id) assert isinstance(nonces, list) count_nonces = len(nonces) assert count_nonces >= 5 r.delete_all_nonces(actorId=real_actor_id) nonces = r.list_nonces(actorId=real_actor_id) assert isinstance(nonces, list) count_nonces = len(nonces) assert count_nonces == 0
def main(): """Main function""" r = Reactor() r.logger.info("Hello this is actor {}".format(r.uid)) r = Reactor() #xnonce = r.add_nonce(permission='EXECUTE', maxuses=-1) #print("NONCE:") #print(xnonce) ag = r.client # Agave client context = r.context # Actor context pprint.pprint(context) m = context.message_dict status = None try: status = context.status #or m['status'] except Exception as e: try: status = m.get('status') except Exception as e: status = None r.logger.info("Triggered action for last action of {}".format(status)) if status == "FINISHED": resub(r, ag) r.on_success("successfully ran") # m = context.message_dict # lastAction = None # try: # lastAction = context.lastAction # except Exception as e: # try: # lastAction = m.get('lastAction') # except Exception as e: # lastAction = None # # r.logger.info("Triggered action for last action of {}".format(lastAction)) # for action in defaultTaskPath[lastAction]: # if action is not None: # action(r) r.on_success("successfully ran")
def test_list_nonces(real_actor_id): '''Ensure various properties are present and the right class''' r = Reactor() nonce = r.add_nonce(permission='READ', maxuses=1, actorId=real_actor_id) nonce_id = nonce.get('id') nonces = r.list_nonces(actorId=real_actor_id) assert isinstance(nonces, list) count_nonces = len(nonces) assert count_nonces >= 1 r.delete_nonce(nonce_id, actorId=real_actor_id)
def main(): """Main function""" # create the reactor object r = Reactor() r.logger.info("Hello this is actor {}".format(r.uid)) # pull in reactor context context = r.context #print(context) # get the message that was sent to the actor message = context.message_dict site = message['site'] subject = message['subject'] session = message['session'] zipfile = message['zipfile'] outdir = message['outdir'] submit_heudiconv(r, site, subject, session, zipfile, outdir)
def main(): """Main function""" r = Reactor() r.logger.info("Hello this is actor {}".format(r.uid)) context = r.context print(context) file = context.message_dict.file print(file) system = file.get('systemId') path = file.get('path') print(system) print(path) if path.split('/')[-1] == 'analyzed': print("not reacting to the analyzed directory upload") sys.exit else: submit(r,system,path)
def main(): r = Reactor() m = r.context.get('message_dict') # shortcut to message_dict p = r.pemagent r.logger.debug("Message: {}".format(m)) # Use JSONschema-based message validator # - In theory, this obviates some get() boilerplate if r.validate_message(m) is False: r.on_failure("Invalid message: {}".format(m)) (syst, abspath, fname) = agaveutils.from_agave_uri(m.get('uri')) try: fullpath = os.path.join(abspath, fname) r.logger.debug('fullpath: {}'.format(fullpath)) p.grant(syst, fullpath, m.get('username'), m.get('permission')) except Exception as e: r.on_failure(e)
def test_session_autoname(monkeypatch): '''can we bootstrap a session name''' monkeypatch.setenv('nickname', 'slimy-eel') r = Reactor() assert r.nickname == r.session
def test_session(monkeypatch): '''can we get SESSION from environment''' monkeypatch.setenv('SESSION', 'slimy-eel') r = Reactor() assert r.session == 'slimy-eel'