grr/server/grr_response_server/flows/general/administrative_test.py
def testAlertEmailIsSentWhenClientKilled(self):
    """Test that client killed messages are handled correctly."""
    client_id = self.SetupClient(0)
    self.SetupTestClientObject(0)

    self.email_messages = []

    def SendEmail(address, sender, title, message, **_):
      self.email_messages.append(
          dict(address=address, sender=sender, title=title, message=message))

    with utils.Stubber(email_alerts.EMAIL_ALERTER, "SendEmail", SendEmail):
      client = flow_test_lib.CrashClientMock(client_id, self.token)
      flow_id = flow_test_lib.TestFlowHelper(
          flow_test_lib.FlowWithOneClientRequest.__name__,
          client,
          client_id=client_id,
          token=self.token,
          check_flow_errors=False)

    self.assertLen(self.email_messages, 1)
    email_message = self.email_messages[0]

    # We expect the email to be sent.
    self.assertEqual(
        email_message.get("address", ""),
        config.CONFIG["Monitoring.alert_email"])

    # Make sure the flow state is included in the email message.
    self.assertIn("Host-0.example.com", email_message["message"])
    self.assertIn("http://localhost:8000/#/clients/C.1000000000000000",
                  email_message["message"])

    if data_store.RelationalDBFlowsEnabled():
      self.assertIn(client_id.Basename(), email_message["title"])
      rel_flow_obj = data_store.REL_DB.ReadFlowObject(client_id.Basename(),
                                                      flow_id)
      self.assertEqual(rel_flow_obj.flow_state, rel_flow_obj.FlowState.ERROR)

      # Make sure client object is updated with the last crash.
      crash = data_store.REL_DB.ReadClientCrashInfo(client_id.Basename())
      self.CheckCrash(crash, client.flow_id, client_id.Basename())
    else:
      self.assertIn(client_id.Basename(), email_message["title"])
      flow_obj = aff4.FACTORY.Open(
          flow_id, age=aff4.ALL_TIMES, token=self.token)
      self.assertEqual(flow_obj.context.state,
                       rdf_flow_runner.FlowContext.State.ERROR)

      # Make sure client object is updated with the last crash.
      client_obj = aff4.FACTORY.Open(client_id, token=self.token)
      crash = client_obj.Get(client_obj.Schema.LAST_CRASH)
      self.CheckCrash(crash, client.flow_id, client_id)

      # Make sure crashes collections are created and written
      # into proper locations. First check the per-client crashes collection.
      client_crashes = sorted(
          list(aff4_grr.VFSGRRClient.CrashCollectionForCID(client_id)),
          key=lambda x: x.timestamp)

      self.assertLen(client_crashes, 1)
      crash = list(client_crashes)[0]
      self.CheckCrash(crash, client.flow_id, client_id)

      # Check per-flow crash collection. Check that crash written there is
      # equal to per-client crash.
      flow_crashes = sorted(
          list(flow_obj.GetValuesForAttribute(flow_obj.Schema.CLIENT_CRASH)),
          key=lambda x: x.timestamp)
      self.assertLen(flow_crashes, len(client_crashes))
      for a, b in zip(flow_crashes, client_crashes):
        self.assertEqual(a, b)
Test that client killed messages are handled correctly.
Test that client killed messages are handled correctly.
Test that client killed messages are handled correctly .
def testAlertEmailIsSentWhenClientKilled(self):
    client_id = self.SetupClient(0)
    self.SetupTestClientObject(0)
    self.email_messages = []
    def SendEmail(address, sender, title, message, **_):
      self.email_messages.append(
          dict(address=address, sender=sender, title=title, message=message))
    with utils.Stubber(email_alerts.EMAIL_ALERTER, "SendEmail", SendEmail):
      client = flow_test_lib.CrashClientMock(client_id, self.token)
      flow_id = flow_test_lib.TestFlowHelper(
          flow_test_lib.FlowWithOneClientRequest.__name__,
          client,
          client_id=client_id,
          token=self.token,
          check_flow_errors=False)
    self.assertLen(self.email_messages, 1)
    email_message = self.email_messages[0]
    self.assertEqual(
        email_message.get("address", ""),
        config.CONFIG["Monitoring.alert_email"])
    self.assertIn("Host-0.example.com", email_message["message"])
    self.assertIn("http://localhost:8000/#/clients/C.1000000000000000",
                  email_message["message"])
    if data_store.RelationalDBFlowsEnabled():
      self.assertIn(client_id.Basename(), email_message["title"])
      rel_flow_obj = data_store.REL_DB.ReadFlowObject(client_id.Basename(),
                                                      flow_id)
      self.assertEqual(rel_flow_obj.flow_state, rel_flow_obj.FlowState.ERROR)
      crash = data_store.REL_DB.ReadClientCrashInfo(client_id.Basename())
      self.CheckCrash(crash, client.flow_id, client_id.Basename())
    else:
      self.assertIn(client_id.Basename(), email_message["title"])
      flow_obj = aff4.FACTORY.Open(
          flow_id, age=aff4.ALL_TIMES, token=self.token)
      self.assertEqual(flow_obj.context.state,
                       rdf_flow_runner.FlowContext.State.ERROR)
      client_obj = aff4.FACTORY.Open(client_id, token=self.token)
      crash = client_obj.Get(client_obj.Schema.LAST_CRASH)
      self.CheckCrash(crash, client.flow_id, client_id)
      client_crashes = sorted(
          list(aff4_grr.VFSGRRClient.CrashCollectionForCID(client_id)),
          key=lambda x: x.timestamp)
      self.assertLen(client_crashes, 1)
      crash = list(client_crashes)[0]
      self.CheckCrash(crash, client.flow_id, client_id)
      flow_crashes = sorted(
          list(flow_obj.GetValuesForAttribute(flow_obj.Schema.CLIENT_CRASH)),
          key=lambda x: x.timestamp)
      self.assertLen(flow_crashes, len(client_crashes))
      for a, b in zip(flow_crashes, client_crashes):
        self.assertEqual(a, b)
def testAlertEmailIsSentWhenClientKilled ( self ) : client_id = self . SetupClient ( 0 ) self . SetupTestClientObject ( 0 ) self . email_messages = [ ] def SendEmail ( address , sender , title , message , ** _ ) : self . email_messages . append ( dict ( address = address , sender = sender , title = title , message = message ) ) with utils . Stubber ( email_alerts . EMAIL_ALERTER , "SendEmail" , SendEmail ) : client = flow_test_lib . CrashClientMock ( client_id , self . token ) flow_id = flow_test_lib . TestFlowHelper ( flow_test_lib . FlowWithOneClientRequest . __name__ , client , client_id = client_id , token = self . token , check_flow_errors = False ) self . assertLen ( self . email_messages , 1 ) email_message = self . email_messages [ 0 ] self . assertEqual ( email_message . get ( "address" , "" ) , config . CONFIG [ "Monitoring.alert_email" ] ) self . assertIn ( "Host-0.example.com" , email_message [ "message" ] ) self . assertIn ( "http://localhost:8000/#/clients/C.1000000000000000" , email_message [ "message" ] ) if data_store . RelationalDBFlowsEnabled ( ) : self . assertIn ( client_id . Basename ( ) , email_message [ "title" ] ) rel_flow_obj = data_store . REL_DB . ReadFlowObject ( client_id . Basename ( ) , flow_id ) self . assertEqual ( rel_flow_obj . flow_state , rel_flow_obj . FlowState . ERROR ) crash = data_store . REL_DB . ReadClientCrashInfo ( client_id . Basename ( ) ) self . CheckCrash ( crash , client . flow_id , client_id . Basename ( ) ) else : self . assertIn ( client_id . Basename ( ) , email_message [ "title" ] ) flow_obj = aff4 . FACTORY . Open ( flow_id , age = aff4 . ALL_TIMES , token = self . token ) self . assertEqual ( flow_obj . context . state , rdf_flow_runner . FlowContext . State . ERROR ) client_obj = aff4 . FACTORY . Open ( client_id , token = self . token ) crash = client_obj . Get ( client_obj . Schema . LAST_CRASH ) self . CheckCrash ( crash , client . flow_id , client_id ) client_crashes = sorted ( list ( aff4_grr . VFSGRRClient . CrashCollectionForCID ( client_id ) ) , key = lambda x : x . timestamp ) self . assertLen ( client_crashes , 1 ) crash = list ( client_crashes ) [ 0 ] self . CheckCrash ( crash , client . flow_id , client_id ) flow_crashes = sorted ( list ( flow_obj . GetValuesForAttribute ( flow_obj . Schema . CLIENT_CRASH ) ) , key = lambda x : x . timestamp ) self . assertLen ( flow_crashes , len ( client_crashes ) ) for a , b in zip ( flow_crashes , client_crashes ) : self . assertEqual ( a , b )
Test that client killed messages are handled correctly.
Test that client killed messages are handled correctly .
"""Test that client killed messages are handled correctly.""" # We expect the email to be sent. # Make sure the flow state is included in the email message. # Make sure client object is updated with the last crash. # Make sure client object is updated with the last crash. # Make sure crashes collections are created and written # into proper locations. First check the per-client crashes collection. # Check per-flow crash collection. Check that crash written there is # equal to per-client crash.
self