Exemple #1
0
    def setUp(self):
        super().setUp()
        self.pool_obj = mock.MagicMock()
        pool_patcher = mock.patch.object(multiprocessing,
                                         "Pool",
                                         return_value=self.pool_obj)
        self.mock_pool = pool_patcher.start()
        self.addCleanup(pool_patcher.stop)

        config_dir = temp.TempDirPath()
        self.label1_config = os.path.join(config_dir, "label1.yaml")
        self.label2_config = os.path.join(config_dir, "label2.yaml")
        with io.open(self.label1_config, mode="w") as filedesc:
            filedesc.write("Client.labels: [label1]")
        with io.open(self.label2_config, mode="w") as filedesc:
            filedesc.write("Client.labels: [label2]")
        self.template_dir = temp.TempDirPath()
        self.deb_template = os.path.join(self.template_dir,
                                         "grr_3.1.0.2_amd64.deb.zip")
        self.exe_template = os.path.join(self.template_dir,
                                         "GRR_3.1.0.2_i386.exe.zip")
        self.xar_template = os.path.join(self.template_dir,
                                         "grr_3.1.0.2_amd64.xar.zip")
        with io.open(self.deb_template, mode="w") as filedesc:
            filedesc.write("linux")
        with io.open(self.exe_template, mode="w") as filedesc:
            filedesc.write("windows")
        with io.open(self.xar_template, mode="w") as filedesc:
            filedesc.write("darwin")

        self.output_dir = temp.TempDirPath()
Exemple #2
0
  def testSuffix(self):
    dirpath = temp.TempDirPath(suffix="foo")

    self.assertTrue(os.path.exists(dirpath))
    self.assertEndsWith(os.path.basename(dirpath), "foo")

    os.rmdir(dirpath)
Exemple #3
0
  def testPrefix(self):
    dirpath = temp.TempDirPath(prefix="foo")

    self.assertTrue(os.path.exists(dirpath))
    self.assertStartsWith(os.path.basename(dirpath), "foo")

    os.rmdir(dirpath)
Exemple #4
0
  def testEmptyDirCreation(self):
    dirpath = temp.TempDirPath()

    self.assertTrue(os.path.exists(dirpath))
    self.assertEmpty(os.listdir(dirpath))

    os.rmdir(dirpath)
Exemple #5
0
  def setUp(self):
    super(GRRBaseTest, self).setUp()

    test_user = u"test"

    system_users_patcher = mock.patch.object(
        access_control, "SYSTEM_USERS",
        frozenset(itertools.chain(access_control.SYSTEM_USERS, [test_user])))
    system_users_patcher.start()
    self.addCleanup(system_users_patcher.stop)

    self.token = access_control.ACLToken(
        username=test_user, reason="Running tests")

    self.temp_dir = temp.TempDirPath()
    config.CONFIG.SetWriteBack(os.path.join(self.temp_dir, "writeback.yaml"))
    self.addCleanup(lambda: shutil.rmtree(self.temp_dir, ignore_errors=True))

    # Each datastore is wrapped with DatabaseValidationWrapper, so we have
    # to access the delegate directly (assuming it's an InMemoryDB
    # implementation).
    data_store.REL_DB.delegate.ClearTestDB()

    email_alerts.InitializeEmailAlerterOnce()

    # 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.addCleanup(self.mail_stubber.Stop)

    # 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()
    self.addCleanup(self.smtp_patcher.stop)

    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()
    self.addCleanup(self.config_set_disable.Stop)

    self._SetupFakeStatsContext()

    # Turn off WithLimitedCallFrequency-based caching in tests. Tests that need
    # to test caching behavior explicitly, should turn it on explicitly.
    with_limited_call_frequency_stubber = utils.Stubber(
        cache, "WITH_LIMITED_CALL_FREQUENCY_PASS_THROUGH", True)
    with_limited_call_frequency_stubber.Start()
    self.addCleanup(with_limited_call_frequency_stubber.Stop)
Exemple #6
0
    def setUp(self):
        super(GRRBaseTest, self).setUp()

        self.temp_dir = temp.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()
        # Each datastore is wrapped with DatabaseValidationWrapper, so we have
        # to access the delegate directly (assuming it's an InMemoryDB
        # implementation).
        data_store.REL_DB.delegate.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()

        self._SetupFakeStatsContext()
Exemple #7
0
def InitGRRWithTestSources(self, artifacts_data):
  artifact_registry.REGISTRY.ClearSources()
  artifact_registry.REGISTRY.ClearRegistry()

  artifacts_temp_dir = temp.TempDirPath()
  with open(os.path.join(artifacts_temp_dir, "test_artifacts.yaml"), "w") as fd:
    fd.write(artifacts_data)

  artifact_registry.REGISTRY.AddDirSources([artifacts_temp_dir])
  self.addCleanup(lambda: shutil.rmtree(artifacts_temp_dir))
  self.addCleanup(artifact_registry.REGISTRY.AddDefaultSources)
  self.addCleanup(artifact_registry.REGISTRY.ClearRegistry)
  self.addCleanup(artifact_registry.REGISTRY.ClearSources)
 def setUp(self):
   super().setUp()
   self.temp_dir = temp.TempDirPath()
   self.addCleanup(lambda: shutil.rmtree(self.temp_dir, ignore_errors=True))
Exemple #9
0
 def setUp(self):
   super(StatCacheTest, self).setUp()
   self.temp_dir = temp.TempDirPath()
   self.addCleanup(shutil.rmtree, self.temp_dir)
Exemple #10
0
  def setUp(self):
    super(GRRBaseTest, self).setUp()

    self.temp_dir = temp.TempDirPath()
    config.CONFIG.SetWriteBack(os.path.join(self.temp_dir, "writeback.yaml"))
    self.addCleanup(lambda: shutil.rmtree(self.temp_dir, ignore_errors=True))

    data_store.DB.ClearTestDB()
    # Each datastore is wrapped with DatabaseValidationWrapper, so we have
    # to access the delegate directly (assuming it's an InMemoryDB
    # implementation).
    data_store.REL_DB.delegate.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()
    self.addCleanup(self.mail_stubber.Stop)

    # 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()
    self.addCleanup(self.smtp_patcher.stop)

    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()
    self.addCleanup(self.config_set_disable.Stop)

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

    self._SetupFakeStatsContext()

    # Turn off WithLimitedCallFrequency-based caching in tests. Tests that need
    # to test caching behavior explicitly, should turn it on explicitly.
    with_limited_call_frequency_stubber = utils.Stubber(
        cache, "WITH_LIMITED_CALL_FREQUENCY_PASS_THROUGH", True)
    with_limited_call_frequency_stubber.Start()
    self.addCleanup(with_limited_call_frequency_stubber.Stop)
Exemple #11
0
 def setUp(self):
     super(ListMigrationsToProcessTest, self).setUp()
     self.temp_dir = temp.TempDirPath()
     self.addCleanup(
         lambda: shutil.rmtree(self.temp_dir, ignore_errors=True))
Exemple #12
0
 def setUp(self):
     super(DirHierarchyTestMixin, self).setUp()
     self.tempdir = temp.TempDirPath()
     self.addCleanup(lambda: shutil.rmtree(self.tempdir))
Exemple #13
0
 def setUp(self):
     self.temp_dir = temp.TempDirPath()
Exemple #14
0
 def setUp(self):
     super(DirHierarchyTestMixin, self).setUp()
     self.tempdir = temp.TempDirPath()