def test_ClientRootKeeper_Deletions(self): """Test whether directories emerge and go away appropriately.""" # Test with a one-level value of DISTCC_CLIENT_TMP. os.environ['DISTCC_CLIENT_TMP'] = '/tmp' client_root_keeper = basics.ClientRootKeeper() client_root_keeper.ClientRootMakedir(117) self.assertTrue( os.path.isdir(client_root_keeper._client_root_before_padding)) self.assertTrue(os.path.isdir(client_root_keeper.client_root)) self.assertTrue(client_root_keeper.client_root.endswith('/padding')) client_root_keeper.ClientRootMakedir(118) client_root_keeper.CleanOutClientRoots() # Directories must be gone now! self.assertTrue( not os.path.isdir(client_root_keeper._client_root_before_padding)) # Test with a two-level value of DISTCC_CLIENT_TMP. try: os.environ['DISTCC_CLIENT_TMP'] = tempfile.mkdtemp('basics_test', dir='/tmp') client_root_keeper = basics.ClientRootKeeper() client_root_keeper.ClientRootMakedir(117) self.assertTrue( os.path.isdir(client_root_keeper._client_root_before_padding)) self.assertTrue(os.path.isdir(client_root_keeper.client_root)) client_root_keeper.ClientRootMakedir(118) client_root_keeper.CleanOutClientRoots() self.assertTrue(os.path.isdir, client_root_keeper._client_root_before_padding) finally: os.rmdir(os.environ['DISTCC_CLIENT_TMP'])
def test_ClientRootKeeper(self): os.environ['DISTCC_CLIENT_TMP'] = 'to/be' self.assertRaises(SystemExit, basics.ClientRootKeeper) os.environ['DISTCC_CLIENT_TMP'] = '/to/be/or' self.assertRaises(SystemExit, basics.ClientRootKeeper) try: tempfile_mkdtemp = tempfile.mkdtemp os_makedirs = os.makedirs def Mock_tempfile_mkdtemp(pat, dir): self.assertTrue( (pat, dir) in [('.%s-%s-%d' % (basics.ClientRootKeeper.INCLUDE_SERVER_NAME, os.getpid(), generation), prefix) for generation, prefix in [(1, '/to/be'), (2, '/to')]]) return (dir == '/to/be' and '/to/be/xxxxxx' or dir == '/to' and '/to/xxxxxxx') def Mock_os_makedirs(f, *unused_args): if not f.startswith('/to/'): raise Exception(f) tempfile.mkdtemp = Mock_tempfile_mkdtemp os.makedirs = Mock_os_makedirs os.environ['DISTCC_CLIENT_TMP'] = '/to/be' client_root_keeper = basics.ClientRootKeeper() client_root_keeper.ClientRootMakedir(1) self.assertEqual(os.path.dirname(client_root_keeper.client_root), "/to/be") os.environ['DISTCC_CLIENT_TMP'] = '/to' client_root_keeper = basics.ClientRootKeeper() client_root_keeper.ClientRootMakedir(2) self.assertEqual( os.path.dirname(os.path.dirname( client_root_keeper.client_root)), "/to") self.assertEqual(os.path.basename(client_root_keeper.client_root), "padding") self.assertEqual( len([ None for ch in client_root_keeper.client_root if ch == '/' ]), 3) finally: tempfile.mkdtemp = tempfile_mkdtemp os.makedirs = os_makedirs
def _SetUp(include_server_port): """Setup include_analyzer and socket server. Returns: (include_analyzer, server)""" try: os.unlink(include_server_port) except (IOError, OSError): pass # this would be expected, the port provided should not exist if os.sep != '/': sys.exit("Expected '/' as separator in filepaths.") client_root_keeper = basics.ClientRootKeeper() # Clean out any junk left over from prior runs. client_root_keeper.CleanOutOthers() Debug(DEBUG_TRACE, "Starting socketserver %s" % include_server_port) # Create the analyser. include_analyzer = ( include_analyzer_memoizing_node.IncludeAnalyzerMemoizingNode( client_root_keeper, basics.opt_stat_reset_triggers)) include_analyzer.email_sender = _EmailSender() # Wrap it inside a handler that is a part of a UnixStreamServer. server = Queuingsocketserver( include_server_port, # Now, produce a StreamRequestHandler subclass whose new objects has # a handler which calls the include_analyzer just made. DistccIncludeHandlerGenerator(include_analyzer)) return (include_analyzer, server)
def setUp(self): basics.opt_debug_pattern = 1 client_root_keeper = basics.ClientRootKeeper() self.include_analyzer = ( include_analyzer_memoizing_node.IncludeAnalyzerMemoizingNode( client_root_keeper)) self.includepath_map = self.include_analyzer.includepath_map self.canonical_path = self.include_analyzer.canonical_path self.directory_map = self.include_analyzer.directory_map self.realpath_map = self.include_analyzer.realpath_map
def setUp(self): statistics.StartTiming() self.global_dirs = [] basics.opt_print_statistics = False basics.opt_debug_pattern = 1 client_root_keeper = basics.ClientRootKeeper() if algorithm == basics.MEMOIZING: self.include_analyzer = ( include_analyzer_memoizing_node.IncludeAnalyzerMemoizingNode( client_root_keeper)) else: self.fail("Algorithm not known.") statistics.StartTiming() self.directory_map = self.include_analyzer.directory_map self.compiler_defaults = self.include_analyzer.compiler_defaults self.canonical_path = self.include_analyzer.canonical_path
def test_IncludeHandler_handle(self): self_test = self client_root_keeper = basics.ClientRootKeeper() old_RWcd = distcc_pump_c_extensions.RCwd distcc_pump_c_extensions.RCwd = None # to be set below old_RArgv = distcc_pump_c_extensions.RArgv distcc_pump_c_extensions.RArgv = None # to be set below old_XArgv = distcc_pump_c_extensions.XArgv distcc_pump_c_extensions.XArgv = lambda _, __: None old_StreamRequestHandler = ( include_server.socketserver.StreamRequestHandler) class Mock_StreamRequestHandler(object): def __init__(self): self.rfile = lambda: None self.rfile.fileno = lambda: 27 self.wfile = lambda: None self.wfile.fileno = lambda: 27 include_server.socketserver.StreamRequestHandler = ( Mock_StreamRequestHandler) include_analyzer = (include_analyzer_memoizing_node. IncludeAnalyzerMemoizingNode(client_root_keeper)) class Mock_EmailSender(object): def __init(self): self.expect = lambda: None def MaybeSendEmail(self, fd, force=False, never=False): fd.seek(0) text = fd.read() self.expect(text, force, never) fd.close() raise mock_email_sender = include_analyzer.email_sender = Mock_EmailSender() include_handler = ( include_server.DistccIncludeHandlerGenerator(include_analyzer)()) # Wow, that was a lot of set-up. Now exercise the include server and # analyzer with an emphasis on triggering exceptions. # Exercise 1: non-existent translation unit. distcc_pump_c_extensions.RArgv = lambda self: ["gcc", "parse.c"] distcc_pump_c_extensions.RCwd = lambda self: os.getcwd() def Expect1(txt, force, never): self_test.assertTrue( "Include server not covering: " + "Could not find translation unit 'parse.c'" in txt, txt) self_test.assertEqual(never, True) mock_email_sender.expect = Expect1 try: include_handler.handle() except NotCoveredError: pass else: raise AssertionError # Exercise 2: provoke assertion error in cache_basics by providing an # entirely false value of current directory as provided in RCwd. distcc_pump_c_extensions.RArgv = lambda self: ["gcc", "parse.c"] distcc_pump_c_extensions.RCwd = lambda self: "/" # The cwd will be changed because of false value. oldcwd = os.getcwd() # We must distinguish between provoked and erroneous exceptions. So, we # mock out, in a sense, the provoked assertion exception that we # expect. The variable got_here allows us to filter the provoked exception # away from unexpected ones. got_here = [] def Expect2(txt, force, never): got_here.append(True) self_test.assertTrue("Include server internal error" in txt, txt) self_test.assertTrue("exceptions.AssertionError" in txt, txt) self_test.assertTrue("for translation unit 'parse.c'" in txt, txt) # This email should be sent. self_test.assertEqual(never, False) mock_email_sender.expect = Expect2 try: include_handler.handle() except AssertionError: os.chdir(oldcwd) # Make sure that we're catching the induced AssertionError, not one # produced in Except2. self.assertTrue(got_here) else: raise AssertionError # Exercise 3: provoke a NotCoveredError due to an absolute #include. distcc_pump_c_extensions.RArgv = lambda self: [ "gcc", "test_data/contains_abs_include.c" ] distcc_pump_c_extensions.RCwd = lambda self: os.getcwd() def Expect3(txt, force, never): self_test.assertTrue( "Filepath must be relative but isn't: '/love/of/my/life'." in txt, txt) # Now check that this email is scheduled to not be sent. self_test.assertEqual(never, True) mock_email_sender.expect = Expect3 try: include_handler.handle() except NotCoveredError: pass distcc_pump_c_extensions.RWcd = old_RWcd distcc_pump_c_extensions.RArgv = old_RArgv distcc_pump_c_extensions.XArgv = old_XArgv include_server.socketserver.StreamRequestHandler = ( old_StreamRequestHandler)
def setUp(self): include_server.print_statistics = False client_root_keeper = basics.ClientRootKeeper() include_server.write_include_closure_file = True self.include_analyzer = include_analyzer.IncludeAnalyzer( client_root_keeper)