Exemplo n.º 1
0
 def _plugin_manual(self, job, config):
     # Get the outcome from the callback, if available,
     # or put the special OUTCOME_UNDECIDED value.
     if self._interaction_callback is not None:
         return self._interaction_callback(self, job, config)
     else:
         return DiskJobResult({'outcome': IJobResult.OUTCOME_UNDECIDED})
Exemplo n.º 2
0
 def _interaction_callback(self,
                           runner,
                           job,
                           config,
                           prompt=None,
                           allowed_outcome=None):
     result = {}
     if prompt is None:
         prompt = "Select an outcome or an action: "
     if allowed_outcome is None:
         allowed_outcome = [
             IJobResult.OUTCOME_PASS, IJobResult.OUTCOME_FAIL,
             IJobResult.OUTCOME_SKIP
         ]
     allowed_actions = ['comments']
     if job.command:
         allowed_actions.append('test')
     result['outcome'] = IJobResult.OUTCOME_UNDECIDED
     while result['outcome'] not in allowed_outcome:
         print("Allowed answers are: {}".format(", ".join(allowed_outcome +
                                                          allowed_actions)))
         choice = input(prompt)
         if choice in allowed_outcome:
             result['outcome'] = choice
             break
         elif choice == 'test':
             (result['return_code'],
              result['io_log_filename']) = runner._run_command(job, config)
         elif choice == 'comments':
             result['comments'] = input('Please enter your comments:\n')
     return DiskJobResult(result)
 def test_everything(self, mock_logger):
     result = DiskJobResult({
         'outcome':
         IJobResult.OUTCOME_PASS,
         'comments':
         "it said blah",
         'io_log_filename':
         make_io_log([(0, 'stdout', b'blah\n')], self.scratch_dir.name),
         'return_code':
         0
     })
     self.assertEqual(str(result), "pass")
     # This result contains a random vale of io_log_filename so direct repr
     # comparison is not feasable. All we want to check here is that it
     # looks right and that it has the outcome value
     self.assertTrue(repr(result).startswith("<DiskJobResult"))
     self.assertTrue(repr(result).endswith(">"))
     self.assertIn("outcome:'pass'", repr(result))
     self.assertEqual(result.outcome, IJobResult.OUTCOME_PASS)
     self.assertEqual(result.comments, "it said blah")
     self.assertEqual(result.io_log, ((0, 'stdout', b'blah\n'), ))
     assert mock_logger.warning.call_count == 1
     self.assertEqual(result.io_log_as_flat_text, 'blah\n')
     self.assertEqual(result.return_code, 0)
     self.assertFalse(result.is_hollow)
Exemplo n.º 4
0
 def test_smoke(self):
     result = DiskJobResult({})
     self.assertEqual(str(result), "None")
     self.assertEqual(repr(result), "<DiskJobResult outcome:None>")
     self.assertIsNone(result.outcome)
     self.assertIsNone(result.comments)
     self.assertEqual(result.io_log, ())
     self.assertIsNone(result.return_code)
Exemplo n.º 5
0
 def test_repr_JobResult_with_DiskJobResult(self, mocked_helper):
     """
     verify that _repr_JobResult() called with DiskJobResult
     calls _repr_DiskJobResult
     """
     result = DiskJobResult({})
     self.helper._repr_JobResult(result)
     mocked_helper._repr_DiskJobResult.assertCalledOnceWith(result)
Exemplo n.º 6
0
 def test_smoke(self, mock_logger):
     result = DiskJobResult({})
     self.assertEqual(str(result), "None")
     self.assertEqual(repr(result), "<DiskJobResult>")
     self.assertIsNone(result.outcome)
     self.assertIsNone(result.comments)
     self.assertEqual(result.io_log, ())
     self.assertIsNone(result.return_code)
     self.assertTrue(result.is_hollow)
    def _build_JobResult(cls, result_repr, flags, location):
        """
        Reconstruct a single job result.

        Convert the representation of MemoryJobResult or DiskJobResult
        back into an actual instance.
        """
        # Load all common attributes...
        outcome = _validate(
            result_repr, key='outcome', value_type=str,
            value_choice=sorted(
                OUTCOME_METADATA_MAP.keys(),
                key=lambda outcome: outcome or "none"
            ), value_none=True)
        comments = _validate(
            result_repr, key='comments', value_type=str, value_none=True)
        return_code = _validate(
            result_repr, key='return_code', value_type=int, value_none=True)
        execution_duration = _validate(
            result_repr, key='execution_duration', value_type=float,
            value_none=True)
        # Construct either DiskJobResult or MemoryJobResult
        if 'io_log_filename' in result_repr:
            io_log_filename = cls._load_io_log_filename(
                result_repr, flags, location)
            if (flags & cls.FLAG_FILE_REFERENCE_CHECKS_F
                    and not os.path.isfile(io_log_filename)
                    and flags & cls.FLAG_REWRITE_LOG_PATHNAMES_F):
                io_log_filename2 = cls._rewrite_pathname(io_log_filename,
                                                         location)
                logger.warning(_("Rewrote file name from %r to %r"),
                               io_log_filename, io_log_filename2)
                io_log_filename = io_log_filename2
            if (flags & cls.FLAG_FILE_REFERENCE_CHECKS_F
                    and not os.path.isfile(io_log_filename)):
                raise BrokenReferenceToExternalFile(
                    _("cannot access file: {!r}").format(io_log_filename))
            return DiskJobResult({
                'outcome': outcome,
                'comments': comments,
                'execution_duration': execution_duration,
                'io_log_filename': io_log_filename,
                'return_code': return_code
            })
        else:
            io_log = [
                cls._build_IOLogRecord(record_repr)
                for record_repr in _validate(
                    result_repr, key='io_log', value_type=list)]
            return MemoryJobResult({
                'outcome': outcome,
                'comments': comments,
                'execution_duration': execution_duration,
                'io_log': io_log,
                'return_code': return_code
            })
Exemplo n.º 8
0
 def test_repr_JobResult_with_DiskJobResult(self):
     """
     verify that _repr_JobResult() called with DiskJobResult
     calls _repr_DiskJobResult
     """
     mpo = mock.patch.object
     with mpo(self.helper, '_repr_DiskJobResult'):
         result = DiskJobResult({})
         self.helper._repr_JobResult(result, self.session_dir)
         self.helper._repr_DiskJobResult.assert_called_once_with(
             result, None)
Exemplo n.º 9
0
 def get(self, job_checksum, compute_fn):
     """
     Get a result from cache run compute_fn to acquire it
     """
     if job_checksum not in self._cache.keys():
         logger.debug(_("%s not found in cache"), job_checksum)
         result = compute_fn().get_builder().as_dict()
         self._store(job_checksum, result.copy())
     else:
         logger.info(_("%s found in cache"), job_checksum)
         result = self._cache[job_checksum]
     return DiskJobResult(result)
Exemplo n.º 10
0
 def get(self, job_checksum, compute_fn):
     """
     Get a result from cache or run compute_fn to acquire it.
     Return a pair containing:
         - a bool signifying whether the result was found in cache
         - a DiskJobResult object with the result
     """
     in_cache = job_checksum in self._cache.keys()
     if not in_cache:
         logger.debug(_("%s not found in cache"), job_checksum)
         result = compute_fn().get_builder().as_dict()
         self._store(job_checksum, result.copy())
     else:
         logger.info(_("%s found in cache"), job_checksum)
         result = self._cache[job_checksum]
     return in_cache, DiskJobResult(result)
Exemplo n.º 11
0
 def _build_JobResult(cls, result_repr):
     """
     Convert the representation of MemoryJobResult or DiskJobResult
     back into an actual instance.
     """
     # Load all common attributes...
     outcome = _validate(result_repr,
                         key='outcome',
                         value_type=str,
                         value_choice=IJobResult.ALL_OUTCOME_LIST,
                         value_none=True)
     comments = _validate(result_repr,
                          key='comments',
                          value_type=str,
                          value_none=True)
     return_code = _validate(result_repr,
                             key='return_code',
                             value_type=int,
                             value_none=True)
     execution_duration = _validate(result_repr,
                                    key='execution_duration',
                                    value_type=float,
                                    value_none=True)
     # Construct either DiskJobResult or MemoryJobResult
     if 'io_log_filename' in result_repr:
         io_log_filename = _validate(result_repr,
                                     key='io_log_filename',
                                     value_type=str)
         return DiskJobResult({
             'outcome': outcome,
             'comments': comments,
             'execution_duration': execution_duration,
             'io_log_filename': io_log_filename,
             'return_code': return_code
         })
     else:
         io_log = [
             cls._build_IOLogRecord(record_repr) for record_repr in
             _validate(result_repr, key='io_log', value_type=list)
         ]
         return MemoryJobResult({
             'outcome': outcome,
             'comments': comments,
             'execution_duration': execution_duration,
             'io_log': io_log,
             'return_code': return_code
         })
Exemplo n.º 12
0
 def test_everything(self):
     result = DiskJobResult({
         'outcome':
         IJobResult.OUTCOME_PASS,
         'comments':
         "it said blah",
         'io_log_filename':
         make_io_log([(0, 'stdout', b'blah\n')], self.scratch_dir.name),
         'return_code':
         0
     })
     self.assertEqual(str(result), "pass")
     self.assertEqual(repr(result), "<DiskJobResult outcome:'pass'>")
     self.assertEqual(result.outcome, IJobResult.OUTCOME_PASS)
     self.assertEqual(result.comments, "it said blah")
     self.assertEqual(result.io_log, ((0, 'stdout', b'blah\n'), ))
     self.assertEqual(result.return_code, 0)
Exemplo n.º 13
0
 def _just_run_command(self, job, config):
     # Run the embedded command
     start_time = time.time()
     return_code, record_path = self._run_command(job, config)
     execution_duration = time.time() - start_time
     # Convert the return of the command to the outcome of the job
     if return_code == 0:
         outcome = IJobResult.OUTCOME_PASS
     else:
         outcome = IJobResult.OUTCOME_FAIL
     # Create a result object and return it
     return DiskJobResult({
         'outcome': outcome,
         'return_code': return_code,
         'io_log_filename': record_path,
         'execution_duration': execution_duration
     })