Beispiel #1
0
 def _CheckLog(self, command, log_name):
   # This is a wrapper around cli_helpers.CheckLog that adds timestamp to the
   # log filename and substitutes placeholders such as {src}, {story},
   # {device_id} in the command.
   story_regex = '^%s$' % re.escape(self.story)
   command = [
       c.format(src=SRC_ROOT, story=story_regex, device_id=self.device_id)
       for c in command]
   timestamp = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S')
   log_path = os.path.join(self.output_dir, '%s_%s' % (log_name, timestamp))
   cli_helpers.CheckLog(command, log_path=log_path, env=_PrepareEnv())
   return log_path
  def testCheckLogError(
      self, open_mock, call_mock, check_call_mock, error_mock, print_mock):
    del print_mock, open_mock  # Unused.
    check_call_mock.side_effect = [subprocess.CalledProcessError(87, ['cmd'])]

    with self.assertRaises(subprocess.CalledProcessError):
      cli_helpers.CheckLog(['cmd'], '/tmp/tmpXYZ.tmp')

    call_mock.assert_called_once_with(['cat', '/tmp/tmpXYZ.tmp'])
    self.assertListEqual(error_mock.mock_calls, [
      mock.call('=' * 80),
      mock.call('Received non-zero return code. Log content:'),
      mock.call('=' * 80),
      mock.call('=' * 80),
    ])
    def testCheckLog(self, dt_mock, open_mock, check_call_mock, print_mock):
        file_mock = mock.Mock()
        open_mock.return_value.__enter__.return_value = file_mock
        dt_mock.now.return_value.strftime.return_value = '_2018_12_10_16_22_11'

        cli_helpers.CheckLog(['command', 'arg with space'],
                             '/tmp/tmpXYZ.tmp',
                             env={'foo': 'bar'})

        check_call_mock.assert_called_once_with(['command', 'arg with space'],
                                                stdout=file_mock,
                                                stderr=subprocess.STDOUT,
                                                shell=False,
                                                env={'foo': 'bar'})
        open_mock.assert_called_once_with('/tmp/tmpXYZ.tmp', 'w')
        self.assertListEqual(print_mock.mock_calls, [
            mock.call("\033[94mcommand 'arg with space'\033[0m"),
            mock.call(
                '\033[94mLogging stdout & stderr to /tmp/tmpXYZ.tmp\033[0m'),
        ])