Esempio n. 1
0
    def set_up_mocks(self, su=None):
        self.mox.StubOutWithMock(dirutil, 'safe_mkdtemp')
        dirutil.safe_mkdtemp().AndReturn('/tmp/test')
        self.mox.StubOutWithMock(log, 'init')
        log.init('/tmp/test/current_run').AndReturn(0)

        self.mox.StubOutWithMock(CommandUtil, 'execute_and_get_output')
        stub = CommandUtil.execute_and_get_output(['git', 'remote', '-v'])
        stub.AndReturn(
            (0,
             dedent("""origin  https://git.twitter.biz/science (fetch)
    origin  https://git.twitter.biz/science (push)""")))
        stub2 = CommandUtil.execute_and_get_output(
            ['git', 'rev-parse', '--abbrev-ref', 'HEAD'])
        stub2.AndReturn((0, "test_br"))

        self.mox.StubOutWithMock(psutil, 'cpu_percent')
        psutil.cpu_percent(interval=1).AndReturn(1.0)
        self.mox.StubOutWithMock(psutil, 'network_io_counters')
        psutil.network_io_counters().AndReturn("1000,10000,1000")
        self.mox.StubOutWithMock(psutil, 'NUM_CPUS')
        psutil.NUM_CPUS = 5

        self.mox.StubOutWithMock(socket, 'gethostname')
        socket.gethostname().AndReturn("localhost")
        self.mox.StubOutWithMock(socket, 'gethostbyname')
        socket.gethostbyname("localhost").AndReturn("localhost")

        self.mox.StubOutWithMock(sys, 'exit')
        sys.exit(0).AndReturn(0)
        self.mox.ReplayAll()
Esempio n. 2
0
  def set_up_mocks(self, su=None):
    self.mox.StubOutWithMock(dirutil, 'safe_mkdtemp')
    dirutil.safe_mkdtemp().AndReturn('/tmp/test')
    self.mox.StubOutWithMock(log, 'init')
    log.init('/tmp/test/current_run').AndReturn(0)

    self.mox.StubOutWithMock(CommandUtil, 'execute_and_get_output')
    stub = CommandUtil.execute_and_get_output(['git','remote', '-v'])
    stub.AndReturn((0, dedent("""origin  https://git.twitter.biz/science (fetch)
    origin  https://git.twitter.biz/science (push)""")))
    stub2 = CommandUtil.execute_and_get_output(['git','rev-parse', '--abbrev-ref', 'HEAD'])
    stub2.AndReturn((0,"test_br"))

    self.mox.StubOutWithMock(psutil, 'cpu_percent')
    psutil.cpu_percent(interval=1).AndReturn(1.0)
    self.mox.StubOutWithMock(psutil, 'network_io_counters')
    psutil.network_io_counters().AndReturn("1000,10000,1000")
    self.mox.StubOutWithMock(psutil, 'NUM_CPUS')
    psutil.NUM_CPUS = 5

    self.mox.StubOutWithMock(socket, 'gethostname')
    socket.gethostname().AndReturn("localhost")
    self.mox.StubOutWithMock(socket, 'gethostbyname')
    socket.gethostbyname("localhost").AndReturn("localhost")

    self.mox.StubOutWithMock(sys, 'exit')
    sys.exit(0).AndReturn(0)
    self.mox.ReplayAll()
    def test_execute_suppress_out_err(self):
        temp_filename = tempfile.mktemp()
        handler = logging.FileHandler(temp_filename)
        logging.getLogger().addHandler(handler)
        logging.getLogger().setLevel(logging.INFO)
        ret = CommandUtil.execute_suppress_stdout_stderr(['echo', 'test'],
                                                         True)
        self.assertEqual(ret, 0)
        with open(temp_filename, "r") as file1:
            str1 = file1.read()
        self.assertTrue(bool(re.search("Executing: echo test",
                                       str1)))  #command logged
        self.assertFalse(bool(re.search("\ntest", str1)))  #Output not logged

        #Test case 2
        ret = CommandUtil.execute_suppress_stdout_stderr(['ls', '-z'], True)
        self.assertNotEqual(ret, 0)
        with open(temp_filename, "r") as file1:
            str1 = file1.read()

        self.assertTrue(bool(re.search("Executing: ls -z",
                                       str1)))  #command logged
        self.assertFalse(bool(re.search("illegal option",
                                        str1)))  #error not logged
        logging.getLogger().removeHandler(handler)
Esempio n. 4
0
  def test_execute_and_get_output(self):
    (ret, output) = CommandUtil.execute_and_get_output(['echo', 'test'])
    self.assertEqual(ret, 0)
    self.assertEqual(output, 'test\n')

    (ret, output) = CommandUtil.execute_and_get_output(['echo1', 'test'])
    self.assertEqual(ret, 1)
    self.assertEqual(output, None)
    def test_execute_and_get_output(self):
        (ret, output) = CommandUtil.execute_and_get_output(['echo', 'test'])
        self.assertEqual(ret, 0)
        self.assertEqual(output, 'test\n')

        (ret, output) = CommandUtil.execute_and_get_output(['echo1', 'test'])
        self.assertEqual(ret, 1)
        self.assertEqual(output, None)
Esempio n. 6
0
  def collect_host_env_info(self,stats):
    #Get Environment Variable
    stats["env"] = os.environ.data
    stats["timestamp"] = int(time.time())
    try:
      #Get the System info
      import psutil
      stats["cpu_time"] = psutil.cpu_percent(interval=1)
      stats["network_counter"] = psutil.network_io_counters()
      stats["no_of_cpus"] = psutil.NUM_CPUS
    except Exception as e:
      log.debug("Exception %s. Cannot collect psutil stats" % e)

    #Get Git info
    stats["git"] = {}
    (ret, git_origin) = CommandUtil().execute_and_get_output(["git", "remote", "-v"])
    if ret == 0:
      for url in git_origin.splitlines():
        origin = url.split()
        str1 = origin[2].strip("(").strip(")")
        if origin:
          stats["git"][str1] = origin[1]

    #Get git branch
    (ret, git_branch) = CommandUtil().execute_and_get_output(["git", "rev-parse", "--abbrev-ref",
                                                              "HEAD"])
    if ret == 0:
      stats["git"]["branch"] = git_branch.strip()
    #Network IP
    try:
      stats["ip"] = socket.gethostbyname(socket.gethostname())
    except Exception as e:
      log.debug("Exception %s. Cannot get ip stats" % e)
    log.debug("Done collecting stats")

    #get the last modified time for the File so that we can upload the stats if they havent being
    #Uploaded for last 6 hours.
    last_modified = None
    if os.path.exists(self._pants_stat_file):
      last_modified = int(os.path.getmtime(self._pants_stat_file))
    try:
      with open(self._pants_stat_file , 'a') as stats_file:
        json_response = json.dumps(stats, cls=PythonObjectEncoder)
        stats_file.write(json_response + "\n")
      return last_modified
    except IOError as e:
      log.debug("Could not write the pants stats %s" % e)
Esempio n. 7
0
 def test_execute(self):
   temp_filename = tempfile.mktemp()
   ret = CommandUtil.execute(['echo' , 'test'], True, temp_filename)
   self.assertEqual(ret, 0)
   with open(temp_filename, "r") as file1:
     str1 = file1.read()
   self.assertEqual("test\n", str1) #output stored in the input file
   os.remove(temp_filename)
 def test_execute(self):
     temp_filename = tempfile.mktemp()
     ret = CommandUtil.execute(['echo', 'test'], True, temp_filename)
     self.assertEqual(ret, 0)
     with open(temp_filename, "r") as file1:
         str1 = file1.read()
     self.assertEqual("test\n", str1)  #output stored in the input file
     os.remove(temp_filename)
Esempio n. 9
0
 def test_execute_suppress_stdout(self):
   temp_filename = tempfile.mktemp()
   handler = logging.FileHandler(temp_filename)
   logging.getLogger().addHandler(handler)
   logging.getLogger().setLevel(logging.INFO)
   ret = CommandUtil.execute_suppress_stdout(['echo' , 'test'], True)
   self.assertEqual(ret, 0)
   with open(temp_filename, "r") as file1:
     str1 = file1.read()
   self.assertTrue(bool(re.search("Executing: echo", str1))) #command Logged
   self.assertFalse(bool(re.search("\ntest", str1)))   #Output not logged
Esempio n. 10
0
  def test_execute_suppress_out_err(self):
    temp_filename = tempfile.mktemp()
    handler = logging.FileHandler(temp_filename)
    logging.getLogger().addHandler(handler)
    logging.getLogger().setLevel(logging.INFO)
    ret = CommandUtil.execute_suppress_stdout_stderr(['echo' , 'test'], True)
    self.assertEqual(ret, 0)
    with open(temp_filename, "r") as file1:
      str1 = file1.read()
    self.assertTrue(bool(re.search("Executing: echo test", str1)))  #command logged
    self.assertFalse(bool(re.search("\ntest", str1)))   #Output not logged

    #Test case 2
    ret = CommandUtil.execute_suppress_stdout_stderr(['ls' , '-z'], True)
    self.assertNotEqual(ret, 0)
    with open(temp_filename, "r") as file1:
      str1 = file1.read()

    self.assertTrue(bool(re.search("Executing: ls -z", str1)))  #command logged
    self.assertFalse(bool(re.search("illegal option", str1)))  #error not logged
    logging.getLogger().removeHandler(handler)
Esempio n. 11
0
 def test_execute_internal(self):
   temp_filename = tempfile.mktemp()
   handler = logging.FileHandler(temp_filename)
   logging.getLogger().addHandler(handler)
   logging.getLogger().setLevel(logging.INFO)
   ret = CommandUtil._execute_internal(['ls' , '-z' ], True, True, True)
   self.assertNotEqual(ret, 0)
   logging.getLogger().removeHandler(handler)
   with open(temp_filename, "r") as file1:
     str1 = file1.read()
   self.assertTrue(bool(re.search(".*Executing: ls -z.*", str1))) #command logged
   self.assertTrue(bool(re.search(".*illegal option.*", str1)) or bool(re.search(".*invalid option.*", str1))) #Error logged
 def test_execute_suppress_stdout(self):
     temp_filename = tempfile.mktemp()
     handler = logging.FileHandler(temp_filename)
     logging.getLogger().addHandler(handler)
     logging.getLogger().setLevel(logging.INFO)
     ret = CommandUtil.execute_suppress_stdout(['echo', 'test'], True)
     self.assertEqual(ret, 0)
     with open(temp_filename, "r") as file1:
         str1 = file1.read()
     self.assertTrue(bool(re.search("Executing: echo",
                                    str1)))  #command Logged
     self.assertFalse(bool(re.search("\ntest", str1)))  #Output not logged
 def test_execute_internal(self):
     temp_filename = tempfile.mktemp()
     handler = logging.FileHandler(temp_filename)
     logging.getLogger().addHandler(handler)
     logging.getLogger().setLevel(logging.INFO)
     ret = CommandUtil._execute_internal(['ls', '-z'], True, True, True)
     self.assertNotEqual(ret, 0)
     logging.getLogger().removeHandler(handler)
     with open(temp_filename, "r") as file1:
         str1 = file1.read()
     self.assertTrue(bool(re.search(".*Executing: ls -z.*",
                                    str1)))  #command logged
     self.assertTrue(
         bool(re.search(".*illegal option.*", str1))
         or bool(re.search(".*invalid option.*", str1)))  #Error logged
Esempio n. 14
0
 def test_cmd_within_path(self):
   self.assertEqual(CommandUtil.cmd_within_path('ls'), True)
   self.assertEqual(CommandUtil.cmd_within_path('xxx'), False)
Esempio n. 15
0
 def test_check_call(self):
   self.assertRaises(subprocess.CalledProcessError, CommandUtil.check_call, ['ls' , '-z'])
   ret = CommandUtil.check_call(['ls' , '-v'])
   self.assertEqual(ret, 0)
 def test_cmd_within_path(self):
     self.assertEqual(CommandUtil.cmd_within_path('ls'), True)
     self.assertEqual(CommandUtil.cmd_within_path('xxx'), False)
 def test_check_call(self):
     self.assertRaises(subprocess.CalledProcessError,
                       CommandUtil.check_call, ['ls', '-z'])
     ret = CommandUtil.check_call(['ls', '-v'])
     self.assertEqual(ret, 0)