def testRunSuccess(self): args = rdfvalue.EndToEndTestFlowArgs(test_names=[ "TestListDirectoryOSLinuxDarwin", "MockEndToEndTest", "TestListDirectoryOSLinuxDarwin" ]) 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, rdfvalue.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)
def testRunSuccessAndFail(self): args = rdfvalue.EndToEndTestFlowArgs() with utils.Stubber(base.AutomatedTest, "classes", { "MockEndToEndTest": MockEndToEndTest, "TestFailure": 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, rdfvalue.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)
def testNoApplicableTests(self): """Try to run linux tests on windows.""" summary = rdfvalue.ClientSummary( system_info=rdfvalue.Uname(system="Windows", node="hostname", release="7", version="6.1.7601SP1", machine="AMD64", kernel="6.1.7601", fqdn="hostname.example.com")) self.client = aff4.FACTORY.Open(self.client_id, mode="rw", token=self.token) self.client.Set(self.client.SchemaCls.SUMMARY(summary)) self.client.Flush() args = rdfvalue.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))
def testRunBadTearDown(self): args = rdfvalue.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))
def Start(self): self.state.Register("hunt_id", None) self.state.Register("client_ids", set()) self.state.Register("client_ids_failures", set()) self.state.Register("client_ids_result_reported", set()) self.state.client_ids = 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 token = access_control.ACLToken(username="******", reason="Running endtoend tests.").SetUID() runner_args = rdfvalue.FlowRunnerArgs(flow_name="EndToEndTestFlow") flow_request = rdfvalue.FlowRequest( client_ids=self.state.client_ids, args=rdfvalue.EndToEndTestFlowArgs(), runner_args=runner_args) bogus_rule = rdfvalue.ForemanAttributeRegex( attribute_name="System", attribute_regex="Does not match anything") hunt_args = rdfvalue.VariableGenericHuntArgs(flows=[flow_request]) hunt_args.output_plugins = self.GetOutputPlugins() with hunts.GRRHunt.StartHunt( hunt_name="VariableGenericHunt", args=hunt_args, regex_rules=[bogus_rule], 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)
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 = rdfvalue.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))
def testEndToEndTestFailure(self): args = rdfvalue.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, rdfvalue.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)
def testNoApplicableTests(self): """Try to run linux tests on windows.""" install_time = rdfvalue.RDFDatetime().Now() user = "******" userobj = rdfvalue.User(username=user) interface = rdfvalue.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 = rdfvalue.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))