Home | History | Annotate | Download | only in cros_utils
      1 """Test to ensure we're not touching /dev/ptmx when running commands."""
      2 
      3 from __future__ import print_function
      4 
      5 import os
      6 import subprocess
      7 import tempfile
      8 import time
      9 import unittest
     10 from cros_utils import command_executer
     11 
     12 
     13 class NoPsuedoTerminalTest(unittest.TestCase):
     14   """Test to ensure we're not touching /dev/ptmx when running commands."""
     15 
     16   _strace_process = None
     17   STRACE_TIMEOUT = 10
     18 
     19   def _AttachStraceToSelf(self, output_file):
     20     """Attaches strace to the current process."""
     21     args = ['strace', '-o', output_file, '-p', str(os.getpid())]
     22     print(args)
     23     self._strace_process = subprocess.Popen(args)
     24     # Wait until we see some activity.
     25     start_time = time.time()
     26     while time.time() - start_time < self.STRACE_TIMEOUT:
     27       if os.path.isfile(output_file) and open(output_file).read(1):
     28         return True
     29       time.sleep(1)
     30     return False
     31 
     32   def _KillStraceProcess(self):
     33     """Kills strace that was started by _AttachStraceToSelf()."""
     34     self._strace_process.terminate()
     35     self._strace_process.wait()
     36     return True
     37 
     38   def testNoPseudoTerminalWhenRunningCommand(self):
     39     """Test to make sure we're not touching /dev/ptmx when running commands."""
     40     temp_file = tempfile.mktemp()
     41     self.assertTrue(self._AttachStraceToSelf(temp_file))
     42 
     43     ce = command_executer.GetCommandExecuter()
     44     ce.RunCommand('echo')
     45 
     46     self.assertTrue(self._KillStraceProcess())
     47 
     48     strace_contents = open(temp_file).read()
     49     self.assertFalse('/dev/ptmx' in strace_contents)
     50 
     51 
     52 if __name__ == '__main__':
     53   unittest.main()
     54