Beispiel #1
0
    def test_stop(self):
        """
    Runs a DescriptorReader over the root directory, then checks that calling
    stop() makes it terminate in a timely fashion.
    """

        # Skip on windows since SIGALRM is unavailable

        if system.is_windows():
            test.runner.skip(self, '(SIGALRM unavailable)')

        is_test_running = True
        reader = stem.descriptor.reader.DescriptorReader('/usr')

        # Fails the test after a couple seconds if we don't finish successfully.
        # Depending on what we're blocked on this might not work when the test
        # fails, requiring that we give a manual kill to the test.

        def timeout_handler(signum, frame):
            if is_test_running:
                self.fail()

        signal.signal(signal.SIGALRM, timeout_handler)
        signal.alarm(2)

        reader.start()
        time.sleep(0.1)
        reader.stop()
        is_test_running = False
Beispiel #2
0
    def test_skip_listener_read_failure(self):
        """
    Listens for a file that's skipped because we lack read permissions.
    """

        if system.is_windows():
            test.runner.skip(self, "(chmod not functional)")

        test_path = test.runner.get_runner().get_test_dir("secret_file")

        try:
            test_file = open(test_path, "w")
            test_file.write(
                "test data for test_skip_listener_unrecognized_type()")
            test_file.close()

            os.chmod(test_path, 0077)  # remove read permissions

            skip_listener = SkipListener()
            reader = stem.descriptor.reader.DescriptorReader(test_path)
            reader.register_skip_listener(skip_listener.listener)

            with reader:
                list(reader)  # iterates over all of the descriptors

            self.assertEqual(1, len(skip_listener.results))

            skipped_path, skip_exception = skip_listener.results[0]
            self.assertEqual(test_path, skipped_path)
            self.assertTrue(
                isinstance(skip_exception, stem.descriptor.reader.ReadFailed))
            self.assertTrue(isinstance(skip_exception.exception, IOError))
        finally:
            if os.path.exists(test_path):
                os.remove(test_path)
Beispiel #3
0
 def test_skip_listener_read_failure(self):
   """
   Listens for a file that's skipped because we lack read permissions.
   """
   
   if system.is_windows():
     test.runner.skip(self, "(chmod not functional)")
   
   test_path = test.runner.get_runner().get_test_dir("secret_file")
   
   try:
     test_file = open(test_path, "w")
     test_file.write("test data for test_skip_listener_unrecognized_type()")
     test_file.close()
     
     os.chmod(test_path, 0077) # remove read permissions
     
     skip_listener = SkipListener()
     reader = stem.descriptor.reader.DescriptorReader(test_path)
     reader.register_skip_listener(skip_listener.listener)
     with reader: list(reader) # iterates over all of the descriptors
     
     self.assertEqual(1, len(skip_listener.results))
     
     skipped_path, skip_exception = skip_listener.results[0]
     self.assertEqual(test_path, skipped_path)
     self.assertTrue(isinstance(skip_exception, stem.descriptor.reader.ReadFailed))
     self.assertTrue(isinstance(skip_exception.exception, IOError))
   finally:
     if os.path.exists(test_path):
       os.remove(test_path)
Beispiel #4
0
 def test_stop(self):
   """
   Runs a DescriptorReader over the root directory, then checks that calling
   stop() makes it terminate in a timely fashion.
   """
   
   # Skip on windows since SIGALRM is unavailable
   if system.is_windows(): test.runner.skip(self, "(SIGALRM unavailable)")
   
   is_test_running = True
   reader = stem.descriptor.reader.DescriptorReader("/usr")
   
   # Fails the test after a couple seconds if we don't finish successfully.
   # Depending on what we're blocked on this might not work when the test
   # fails, requiring that we give a manual kill to the test.
   
   def timeout_handler(signum, frame):
     if is_test_running:
       self.fail()
   
   signal.signal(signal.SIGALRM, timeout_handler)
   signal.alarm(2)
   
   reader.start()
   time.sleep(0.1)
   reader.stop()
   is_test_running = False
Beispiel #5
0
 def test_load_processed_files_permissions(self):
   """
   Tests the load_processed_files() function with a file that can't be read
   due to permissions.
   """
   
   # Skip the test on windows, since you can only set the file's
   # read-only flag with os.chmod(). For more information see...
   # http://docs.python.org/library/os.html#os.chmod
   
   if system.is_windows():
     test.runner.skip(self, "(chmod not functional)")
   
   test_listing_path = _make_processed_files_listing(BASIC_LISTING)
   os.chmod(test_listing_path, 0077) # remove read permissions
   self.assertRaises(IOError, stem.descriptor.reader.load_processed_files, test_listing_path)
Beispiel #6
0
    def test_load_processed_files_permissions(self):
        """
    Tests the load_processed_files() function with a file that can't be read
    due to permissions.
    """

        # Skip the test on windows, since you can only set the file's
        # read-only flag with os.chmod(). For more information see...
        # http://docs.python.org/library/os.html#os.chmod

        if system.is_windows():
            test.runner.skip(self, "(chmod not functional)")

        test_listing_path = _make_processed_files_listing(BASIC_LISTING)
        os.chmod(test_listing_path, 0077)  # remove read permissions
        self.assertRaises(IOError, stem.descriptor.reader.load_processed_files,
                          test_listing_path)
Beispiel #7
0
    def test_skip_listener_read_failure(self):
        """
    Listens for a file that's skipped because we lack read permissions.
    """

        # test relies on being unable to read a file

        if getpass.getuser() == 'root':
            test.runner.skip(self, '(running as root)')
            return
        elif system.is_windows():
            test.runner.skip(self, '(chmod not functional)')
            return

        test_path = os.path.join(self.temp_directory, 'secret_file')

        try:
            test_file = open(test_path, 'w')
            test_file.write(
                'test data for test_skip_listener_unrecognized_type()')
            test_file.close()

            os.chmod(test_path, 0o077)  # remove read permissions

            skip_listener = SkipListener()
            reader = stem.descriptor.reader.DescriptorReader(test_path)
            reader.register_skip_listener(skip_listener.listener)

            with reader:
                list(reader)  # iterates over all of the descriptors

            self.assertEqual(1, len(skip_listener.results))

            skipped_path, skip_exception = skip_listener.results[0]
            self.assertEqual(test_path, skipped_path)
            self.assertTrue(
                isinstance(skip_exception, stem.descriptor.reader.ReadFailed))
            self.assertTrue(isinstance(skip_exception.exception, IOError))
        finally:
            if os.path.exists(test_path):
                os.remove(test_path)
Beispiel #8
0
  def test_skip_listener_read_failure(self):
    """
    Listens for a file that's skipped because we lack read permissions.
    """

    # test relies on being unable to read a file

    if getpass.getuser() == 'root':
      self.skipTest('(running as root)')
      return
    elif system.is_windows():
      self.skipTest('(chmod not functional)')
      return

    test_path = os.path.join(self.temp_directory, 'secret_file')

    try:
      test_file = open(test_path, 'w')
      test_file.write('test data for test_skip_listener_unrecognized_type()')
      test_file.close()

      os.chmod(test_path, 0o077)  # remove read permissions

      skip_listener = SkipListener()
      reader = stem.descriptor.reader.DescriptorReader(test_path)
      reader.register_skip_listener(skip_listener.listener)

      with reader:
        list(reader)  # iterates over all of the descriptors

      self.assertEqual(1, len(skip_listener.results))

      skipped_path, skip_exception = skip_listener.results[0]
      self.assertEqual(test_path, skipped_path)
      self.assertTrue(isinstance(skip_exception, stem.descriptor.reader.ReadFailed))
      self.assertTrue(isinstance(skip_exception.exception, IOError))
    finally:
      if os.path.exists(test_path):
        os.remove(test_path)
Beispiel #9
0
  def test_load_processed_files_permissions(self):
    """
    Tests the load_processed_files() function with a file that can't be read
    due to permissions.
    """

    # test relies on being unable to read a file

    if getpass.getuser() == 'root':
      self.skipTest('(running as root)')
      return

    # Skip the test on windows, since you can only set the file's
    # read-only flag with os.chmod(). For more information see...
    # http://docs.python.org/library/os.html#os.chmod

    if system.is_windows():
      self.skipTest('(chmod not functional)')

    test_listing_path = self._make_processed_files_listing(BASIC_LISTING)
    os.chmod(test_listing_path, 0o077)  # remove read permissions
    self.assertRaises(IOError, stem.descriptor.reader.load_processed_files, test_listing_path)
Beispiel #10
0
# Copyright 2011-2016, Damian Johnson and The Tor Project
# See LICENSE for licensing information

"""
Variety of filters for the python unit testing output, which can be chained
together for improved readability.
"""

import re
import sys

import stem.util.enum

from stem.util import system, term

COLOR_SUPPORT = sys.stdout.isatty() and not system.is_windows()

DIVIDER = '=' * 70
HEADER_ATTR = (term.Color.CYAN, term.Attr.BOLD)
CATEGORY_ATTR = (term.Color.GREEN, term.Attr.BOLD)

NO_NL = 'no newline'
STDERR = 'stderr'

# formatting for various categories of messages

STATUS = (term.Color.BLUE, term.Attr.BOLD)
SUBSTATUS = (term.Color.BLUE, )

SUCCESS = (term.Color.GREEN, term.Attr.BOLD)
ERROR = (term.Color.RED, term.Attr.BOLD)
Beispiel #11
0
  Parses our commandline arguments, loading our custom test configuration if
  '--config' was provided and then appending arguments to that. This does some
  sanity checking on the input, printing an error and quitting if validation
  fails.
  """

  arg_overrides, config_path = {}, None

  try:
    opts = getopt.getopt(sys.argv[1:], OPT, OPT_EXPANDED)[0]
  except getopt.GetoptError, exc:
    print "%s (for usage provide --help)" % exc
    sys.exit(1)

  # suppress color output if our output is being piped
  if (not sys.stdout.isatty()) or system.is_windows():
    arg_overrides["argument.no_color"] = "true"

  for opt, arg in opts:
    if opt in ("-a", "--all"):
      arg_overrides["argument.unit"] = "true"
      arg_overrides["argument.integ"] = "true"
      arg_overrides["argument.style"] = "true"
    elif opt in ("-u", "--unit"):
      arg_overrides["argument.unit"] = "true"
    elif opt in ("-i", "--integ"):
      arg_overrides["argument.integ"] = "true"
    elif opt in ("-s", "--style"):
      arg_overrides["argument.style"] = "true"
    elif opt == "--python3":
      arg_overrides["argument.python3"] = "true"