grr/test_lib/flow_test_lib.py
def TestFlowHelper(flow_urn_or_cls_name,
                   client_mock=None,
                   client_id=None,
                   check_flow_errors=True,
                   token=None,
                   sync=True,
                   **kwargs):
  """Build a full test harness: client - worker + start flow.

  Args:
    flow_urn_or_cls_name: RDFURN pointing to existing flow (in this case the
      given flow will be run) or flow class name (in this case flow of the given
      class will be created and run).
    client_mock: Client mock object.
    client_id: Client id of an emulated client.
    check_flow_errors: If True, TestFlowHelper will raise on errors during flow
      execution.
    token: Security token.
    sync: Whether StartAFF4Flow call should be synchronous or not.
    **kwargs: Arbitrary args that will be passed to flow.StartAFF4Flow().

  Returns:
    The session id of the flow that was run.
  """

  if data_store.RelationalDBFlowsEnabled():
    if isinstance(client_id, rdfvalue.RDFURN):
      client_id = client_id.Basename()

    flow_cls = registry.FlowRegistry.FlowClassByName(flow_urn_or_cls_name)
    return StartAndRunFlow(
        flow_cls,
        creator=token.username,
        client_mock=client_mock,
        client_id=client_id,
        check_flow_errors=check_flow_errors,
        flow_args=kwargs.pop("args", None),
        **kwargs)

  if client_id or client_mock:
    client_mock = MockClient(client_id, client_mock, token=token)

  worker_mock = worker_test_lib.MockWorker(
      check_flow_errors=check_flow_errors, token=token)

  if isinstance(flow_urn_or_cls_name, rdfvalue.RDFURN):
    session_id = flow_urn_or_cls_name
  else:
    # Instantiate the flow:
    session_id = flow.StartAFF4Flow(
        client_id=client_id,
        flow_name=flow_urn_or_cls_name,
        sync=sync,
        token=token,
        **kwargs)

  total_flows = set()
  total_flows.add(session_id)

  # Run the client and worker until nothing changes any more.
  while True:
    if client_mock:
      client_processed = client_mock.Next()
    else:
      client_processed = 0

    flows_run = []
    for flow_run in worker_mock.Next():
      total_flows.add(flow_run)
      flows_run.append(flow_run)

    if client_processed == 0 and not flows_run:
      break

  # We should check for flow errors:
  if check_flow_errors:
    CheckFlowErrors(total_flows, token=token)

  return session_id
Build a full test harness: client - worker + start flow.

  Args:
    flow_urn_or_cls_name: RDFURN pointing to existing flow (in this case the
      given flow will be run) or flow class name (in this case flow of the given
      class will be created and run).
    client_mock: Client mock object.
    client_id: Client id of an emulated client.
    check_flow_errors: If True, TestFlowHelper will raise on errors during flow
      execution.
    token: Security token.
    sync: Whether StartAFF4Flow call should be synchronous or not.
    **kwargs: Arbitrary args that will be passed to flow.StartAFF4Flow().

  Returns:
    The session id of the flow that was run.
  
Build a full test harness: client - worker + start flow.
Build a full test harness : client - worker + start flow .
def TestFlowHelper(flow_urn_or_cls_name,
                   client_mock=None,
                   client_id=None,
                   check_flow_errors=True,
                   token=None,
                   sync=True,
                   **kwargs):
  if data_store.RelationalDBFlowsEnabled():
    if isinstance(client_id, rdfvalue.RDFURN):
      client_id = client_id.Basename()
    flow_cls = registry.FlowRegistry.FlowClassByName(flow_urn_or_cls_name)
    return StartAndRunFlow(
        flow_cls,
        creator=token.username,
        client_mock=client_mock,
        client_id=client_id,
        check_flow_errors=check_flow_errors,
        flow_args=kwargs.pop("args", None),
        **kwargs)
  if client_id or client_mock:
    client_mock = MockClient(client_id, client_mock, token=token)
  worker_mock = worker_test_lib.MockWorker(
      check_flow_errors=check_flow_errors, token=token)
  if isinstance(flow_urn_or_cls_name, rdfvalue.RDFURN):
    session_id = flow_urn_or_cls_name
  else:
    session_id = flow.StartAFF4Flow(
        client_id=client_id,
        flow_name=flow_urn_or_cls_name,
        sync=sync,
        token=token,
        **kwargs)
  total_flows = set()
  total_flows.add(session_id)
  while True:
    if client_mock:
      client_processed = client_mock.Next()
    else:
      client_processed = 0
    flows_run = []
    for flow_run in worker_mock.Next():
      total_flows.add(flow_run)
      flows_run.append(flow_run)
    if client_processed == 0 and not flows_run:
      break
  if check_flow_errors:
    CheckFlowErrors(total_flows, token=token)
  return session_id
def TestFlowHelper ( flow_urn_or_cls_name , client_mock = None , client_id = None , check_flow_errors = True , token = None , sync = True , ** kwargs ) : if data_store . RelationalDBFlowsEnabled ( ) : if isinstance ( client_id , rdfvalue . RDFURN ) : client_id = client_id . Basename ( ) flow_cls = registry . FlowRegistry . FlowClassByName ( flow_urn_or_cls_name ) return StartAndRunFlow ( flow_cls , creator = token . username , client_mock = client_mock , client_id = client_id , check_flow_errors = check_flow_errors , flow_args = kwargs . pop ( "args" , None ) , ** kwargs ) if client_id or client_mock : client_mock = MockClient ( client_id , client_mock , token = token ) worker_mock = worker_test_lib . MockWorker ( check_flow_errors = check_flow_errors , token = token ) if isinstance ( flow_urn_or_cls_name , rdfvalue . RDFURN ) : session_id = flow_urn_or_cls_name else : session_id = flow . StartAFF4Flow ( client_id = client_id , flow_name = flow_urn_or_cls_name , sync = sync , token = token , ** kwargs ) total_flows = set ( ) total_flows . add ( session_id ) while True : if client_mock : client_processed = client_mock . Next ( ) else : client_processed = 0 flows_run = [ ] for flow_run in worker_mock . Next ( ) : total_flows . add ( flow_run ) flows_run . append ( flow_run ) if client_processed == 0 and not flows_run : break if check_flow_errors : CheckFlowErrors ( total_flows , token = token ) return session_id
Build a full test harness: client - worker + start flow.
Build a full test harness : client - worker + start flow .
"""Build a full test harness: client - worker + start flow.

  Args:
    flow_urn_or_cls_name: RDFURN pointing to existing flow (in this case the
      given flow will be run) or flow class name (in this case flow of the given
      class will be created and run).
    client_mock: Client mock object.
    client_id: Client id of an emulated client.
    check_flow_errors: If True, TestFlowHelper will raise on errors during flow
      execution.
    token: Security token.
    sync: Whether StartAFF4Flow call should be synchronous or not.
    **kwargs: Arbitrary args that will be passed to flow.StartAFF4Flow().

  Returns:
    The session id of the flow that was run.
  """ # Instantiate the flow: # Run the client and worker until nothing changes any more. # We should check for flow errors:
The session id of the flow that was run.
The session id of the flow that was run .
flow_urn_or_cls_name
RDFURN pointing to existing flow (in this case the
given flow will be run) or flow class name (in this case flow of the given
class will be created and run).
RDFURN pointing to existing flow ( in this case the given flow will be run ) or flow class name ( in this case flow of the given class will be created and run ) .
client_mock
Client mock object.
Client mock object .
client_id
Client id of an emulated client.
Client id of an emulated client .
check_flow_errors
If True, TestFlowHelper will raise on errors during flow
execution.
If True TestFlowHelper will raise on errors during flow execution .
token
None
sync
Whether StartAFF4Flow call should be synchronous or not.
Whether StartAFF4Flow call should be synchronous or not .
**kwargs
Arbitrary args that will be passed to flow.StartAFF4Flow().
Arbitrary args that will be passed to flow . StartAFF4Flow () .