Example #1
0
    def setUp(self):
        super(GRRBaseTest, self).setUp()

        tmpdir = os.environ.get("TEST_TMPDIR") or config.CONFIG["Test.tmpdir"]

        if platform.system() != "Windows":
            # Make a temporary directory for test files.
            self.temp_dir = tempfile.mkdtemp(dir=tmpdir)
        else:
            self.temp_dir = tempfile.mkdtemp()

        config.CONFIG.SetWriteBack(
            os.path.join(self.temp_dir, "writeback.yaml"))

        logging.info("Starting test: %s.%s", self.__class__.__name__,
                     self._testMethodName)
        self.last_start_time = time.time()

        data_store.DB.ClearTestDB()

        aff4.FACTORY.Flush()

        # Create a Foreman and Filestores, they are used in many tests.
        aff4_grr.GRRAFF4Init().Run()
        filestore.FileStoreInit().Run()
        hunts_results.ResultQueueInitHook().Run()
        email_alerts.EmailAlerterInit().RunOnce()

        # Stub out the email function
        self.emails_sent = []

        def SendEmailStub(to_user, from_user, subject, message,
                          **unused_kwargs):
            self.emails_sent.append((to_user, from_user, subject, message))

        self.mail_stubber = utils.MultiStubber(
            (email_alerts.EMAIL_ALERTER, "SendEmail", SendEmailStub),
            (email.utils, "make_msgid", lambda: "<message id stub>"))
        self.mail_stubber.Start()

        self.nanny_stubber = utils.Stubber(
            client_utils_linux.NannyController,
            "StartNanny",
            lambda unresponsive_kill_period=None, nanny_logfile=None: True)
        self.nanny_stubber.Start()

        # We don't want to send actual email in our tests
        self.smtp_patcher = mock.patch("smtplib.SMTP")
        self.mock_smtp = self.smtp_patcher.start()

        def DisabledSet(*unused_args, **unused_kw):
            raise NotImplementedError(
                "Usage of Set() is disabled, please use a configoverrider in tests."
            )

        self.config_set_disable = utils.Stubber(config.CONFIG, "Set",
                                                DisabledSet)
        self.config_set_disable.Start()
Example #2
0
    def setUp(self):
        super(GRRBaseTest, self).setUp()

        self.temp_dir = TempDirPath()
        config.CONFIG.SetWriteBack(
            os.path.join(self.temp_dir, "writeback.yaml"))

        logging.info("Starting test: %s.%s", self.__class__.__name__,
                     self._testMethodName)
        self.last_start_time = time.time()

        data_store.DB.ClearTestDB()
        data_store.REL_DB.ClearTestDB()

        aff4.FACTORY.Flush()

        # Create a Foreman and Filestores, they are used in many tests.
        aff4_grr.GRRAFF4Init().Run()
        filestore.FileStoreInit().Run()
        hunts_results.ResultQueueInitHook().Run()
        email_alerts.EmailAlerterInit().RunOnce()
        audit.AuditEventListener.created_logs.clear()

        # Stub out the email function
        self.emails_sent = []

        def SendEmailStub(to_user, from_user, subject, message,
                          **unused_kwargs):
            self.emails_sent.append((to_user, from_user, subject, message))

        self.mail_stubber = utils.MultiStubber(
            (email_alerts.EMAIL_ALERTER, "SendEmail", SendEmailStub),
            (email.utils, "make_msgid", lambda: "<message id stub>"))
        self.mail_stubber.Start()

        # We don't want to send actual email in our tests
        self.smtp_patcher = mock.patch("smtplib.SMTP")
        self.mock_smtp = self.smtp_patcher.start()

        def DisabledSet(*unused_args, **unused_kw):
            raise NotImplementedError(
                "Usage of Set() is disabled, please use a configoverrider in tests."
            )

        self.config_set_disable = utils.Stubber(config.CONFIG, "Set",
                                                DisabledSet)
        self.config_set_disable.Start()

        if self.use_relational_reads:
            self.relational_read_stubber = utils.Stubber(
                data_store, "RelationalDBReadEnabled", lambda: True)
            self.relational_read_stubber.Start()