Пример #1
0
    def testRunSuccess(self):
        args = endtoend.EndToEndTestFlowArgs(test_names=[
            "TestListDirectoryOSLinuxDarwin", "MockEndToEndTest",
            "TestListDirectoryOSLinuxDarwin"
        ])

        with test_lib.Instrument(flow.GRRFlow, "SendReply") as send_reply:
            for _ in flow_test_lib.TestFlowHelper(
                    endtoend.EndToEndTestFlow.__name__,
                    self.client_mock,
                    client_id=self.client_id,
                    token=self.token,
                    args=args):
                pass

            results = []
            for _, reply in send_reply.args:
                if isinstance(reply, endtoend.EndToEndTestResult):
                    results.append(reply)
                    self.assertTrue(reply.success)
                    self.assertTrue(
                        reply.test_class_name in
                        ["TestListDirectoryOSLinuxDarwin", "MockEndToEndTest"])
                    self.assertFalse(reply.log)

            # We only expect 2 results because we dedup test names
            self.assertEqual(len(results), 2)
Пример #2
0
    def Start(self):
        self.state.hunt_id = None
        self.state.client_ids = set()
        self.state.client_ids_failures = set()
        self.state.client_ids_result_reported = set()

        self.state.client_ids = list(
            base.GetClientTestTargets(token=self.token))

        if not self.state.client_ids:
            self.Log("No clients to test on, define them in "
                     "Test.end_to_end_client_ids")
            return

        # SetUID is required to run a hunt on the configured end-to-end client
        # targets without an approval.
        token = access_control.ACLToken(
            username="******", reason="Running endtoend tests.").SetUID()
        runner_args = rdf_flows.FlowRunnerArgs(flow_name="EndToEndTestFlow")

        flow_request = hunts_standard.FlowRequest(
            client_ids=self.state.client_ids,
            args=flows_endtoend.EndToEndTestFlowArgs(),
            runner_args=runner_args)

        bogus_rule = rdf_foreman.ForemanRegexClientRule(
            attribute_name="System", attribute_regex="Does not match anything")

        client_rule_set = rdf_foreman.ForemanClientRuleSet(rules=[
            rdf_foreman.ForemanClientRule(
                rule_type=rdf_foreman.ForemanClientRule.Type.REGEX,
                regex=bogus_rule)
        ])

        hunt_args = hunts_standard.VariableGenericHuntArgs(
            flows=[flow_request])

        hunt_args.output_plugins = self.GetOutputPlugins()

        with hunts.GRRHunt.StartHunt(hunt_name="VariableGenericHunt",
                                     args=hunt_args,
                                     client_rule_set=client_rule_set,
                                     client_rate=0,
                                     expiry_time="1d",
                                     token=token) as hunt:

            self.state.hunt_id = hunt.session_id
            hunt.SetDescription("EndToEnd tests run by cron")
            hunt.Run()
            hunt.ManuallyScheduleClients(token=token)

        # Set a callback to check the results after 50 minutes.  This should be
        # plenty of time for the clients to receive the hunt and run the tests, but
        # not so long that the flow lease will expire.

        wait_duration = rdfvalue.Duration(
            config_lib.CONFIG.Get("Test.end_to_end_result_check_wait"))
        completed_time = rdfvalue.RDFDatetime.Now() + wait_duration

        self.CallState(next_state="CheckResults", start_time=completed_time)
Пример #3
0
    def testRunSuccessAndFail(self):
        args = endtoend.EndToEndTestFlowArgs()

        with utils.Stubber(
                base.AutomatedTest, "classes", {
                    "MockEndToEndTest": endtoend_mocks.MockEndToEndTest,
                    "TestFailure": endtoend_mocks.TestFailure
                }):
            with test_lib.Instrument(flow.GRRFlow, "SendReply") as send_reply:
                for _ in flow_test_lib.TestFlowHelper(
                        endtoend.EndToEndTestFlow.__name__,
                        self.client_mock,
                        client_id=self.client_id,
                        token=self.token,
                        args=args):
                    pass

                results = []
                for _, reply in send_reply.args:
                    if isinstance(reply, endtoend.EndToEndTestResult):
                        results.append(reply)
                        if reply.test_class_name == "MockEndToEndTest":
                            self.assertTrue(reply.success)
                            self.assertFalse(reply.log)
                        elif reply.test_class_name == "TestFailure":
                            self.assertFalse(reply.success)
                            self.assertTrue(
                                "This should be logged" in reply.log)

                self.assertItemsEqual([x.test_class_name for x in results],
                                      ["MockEndToEndTest", "TestFailure"])
                self.assertEqual(len(results), 2)
Пример #4
0
  def testRunBadTearDown(self):
    args = endtoend.EndToEndTestFlowArgs(
        test_names=["TestBadTearDown"])

    self.assertRaises(RuntimeError, list, test_lib.TestFlowHelper(
        "EndToEndTestFlow", self.client_mock, client_id=self.client_id,
        token=self.token, args=args))
Пример #5
0
  def testNoApplicableTests(self):
    """Try to run linux tests on windows."""
    install_time = rdfvalue.RDFDatetime().Now()
    user = "******"
    userobj = rdf_client.User(username=user)
    interface = rdf_client.Interface(ifname="eth0")
    self.client = aff4.FACTORY.Create(self.client_id, "VFSGRRClient", mode="rw",
                                      token=self.token, age=aff4.ALL_TIMES)

    self.client.Set(self.client.Schema.HOSTNAME("hostname"))
    self.client.Set(self.client.Schema.SYSTEM("Windows"))
    self.client.Set(self.client.Schema.OS_RELEASE("7"))
    self.client.Set(self.client.Schema.OS_VERSION("6.1.7601SP1"))
    self.client.Set(self.client.Schema.KERNEL("6.1.7601"))
    self.client.Set(self.client.Schema.FQDN("hostname.example.com"))
    self.client.Set(self.client.Schema.ARCH("AMD64"))
    self.client.Set(self.client.Schema.INSTALL_DATE(install_time))
    self.client.Set(self.client.Schema.USER([userobj]))
    self.client.Set(self.client.Schema.USERNAMES([user]))
    self.client.Set(self.client.Schema.LAST_INTERFACES([interface]))
    self.client.Flush()

    args = endtoend.EndToEndTestFlowArgs(
        test_names=["TestListDirectoryOSLinuxDarwin",
                    "MockEndToEndTest",
                    "TestListDirectoryOSLinuxDarwin"])

    self.assertRaises(flow.FlowError, list, test_lib.TestFlowHelper(
        "EndToEndTestFlow", self.client_mock, client_id=self.client_id,
        token=self.token, args=args))
Пример #6
0
    def testRunBadSetUp(self):
        args = endtoend.EndToEndTestFlowArgs(test_names=["TestBadSetUp"])

        self.assertRaises(
            RuntimeError, list,
            flow_test_lib.TestFlowHelper(endtoend.EndToEndTestFlow.__name__,
                                         self.client_mock,
                                         client_id=self.client_id,
                                         token=self.token,
                                         args=args))
Пример #7
0
  def testRunBadFlow(self):
    """Test behaviour when test flow raises in Start.

    A flow that raises in its Start method will kill the EndToEndTest run.
    Protecting and reporting on this significantly complicates this code, and a
    flow raising in Start is really broken, so we allow this behaviour.
    """
    args = endtoend.EndToEndTestFlowArgs(
        test_names=["MockEndToEndTestBadFlow", "MockEndToEndTest"])

    self.assertRaises(RuntimeError, list, test_lib.TestFlowHelper(
        "EndToEndTestFlow", self.client_mock, client_id=self.client_id,
        token=self.token, args=args))
Пример #8
0
  def testNoApplicableTests(self):
    """Try to run linux tests on windows."""
    self.SetupClients(
        1, system="Windows", os_version="6.1.7601SP1", arch="AMD64")
    install_time = rdfvalue.RDFDatetime.Now()
    user = "******"
    userobj = rdf_client.User(username=user)
    interface = rdf_client.Interface(ifname="eth0")
    self.client = aff4.FACTORY.Create(
        self.client_id,
        aff4_grr.VFSGRRClient,
        mode="rw",
        token=self.token,
        age=aff4.ALL_TIMES)

    kb = self.client.Get(self.client.Schema.KNOWLEDGE_BASE)
    kb.users.Append(userobj)
    self.client.Set(self.client.Schema.HOSTNAME("hostname"))
    self.client.Set(self.client.Schema.OS_RELEASE("7"))
    self.client.Set(self.client.Schema.KERNEL("6.1.7601"))
    self.client.Set(self.client.Schema.FQDN("hostname.example.com"))
    self.client.Set(self.client.Schema.INSTALL_DATE(install_time))
    self.client.Set(self.client.Schema.KNOWLEDGE_BASE(kb))
    self.client.Set(self.client.Schema.USERNAMES([user]))
    self.client.Set(self.client.Schema.INTERFACES([interface]))
    self.client.Flush()

    args = endtoend.EndToEndTestFlowArgs(test_names=[
        "TestListDirectoryOSLinuxDarwin", "MockEndToEndTest",
        "TestListDirectoryOSLinuxDarwin"
    ])

    self.assertRaises(
        flow.FlowError,
        list,
        test_lib.TestFlowHelper(
            "EndToEndTestFlow",
            self.client_mock,
            client_id=self.client_id,
            token=self.token,
            args=args))
Пример #9
0
  def testEndToEndTestFailure(self):
    args = endtoend.EndToEndTestFlowArgs(
        test_names=["TestFailure"])

    with test_lib.Instrument(flow.GRRFlow, "SendReply") as send_reply:

      for _ in test_lib.TestFlowHelper(
          "EndToEndTestFlow", self.client_mock, client_id=self.client_id,
          token=self.token, args=args):
        pass

      results = []
      for _, reply in send_reply.args:
        if isinstance(reply, endtoend.EndToEndTestResult):
          results.append(reply)
          self.assertFalse(reply.success)
          self.assertEqual(reply.test_class_name,
                           "TestFailure")
          self.assertTrue("This should be logged" in reply.log)

      self.assertEqual(len(results), 1)