Example #1
0
 def test_overwriting_artifact_raises_value_error(self):
     host = FakeHost()
     output_dir = '%stmp' % host.sep
     ar = artifacts.Artifacts(output_dir,
                              host,
                              iteration=0,
                              test_name='retry_1')
     file_rel_path = host.join('stdout', 'test.jpg')
     ar.CreateArtifact('artifact_name', file_rel_path, b'contents')
     ar1 = artifacts.Artifacts(output_dir, host, iteration=1)
     with self.assertRaises(ValueError) as ve:
         ar1.CreateArtifact('artifact_name', file_rel_path,
                            b'overwritten contents')
     self.assertIn('already exists', str(ve.exception))
Example #2
0
 def test_force_overwriting_artifact_does_not_raise_error(self):
     host = FakeHost()
     output_dir = '%stmp' % host.sep
     ar = artifacts.Artifacts(output_dir,
                              host,
                              iteration=0,
                              test_name='a.b.c',
                              intial_results_base_dir=True)
     file_rel_path = host.join('stdout', 'test.txt')
     ar.CreateArtifact('artifact_name',
                       file_rel_path,
                       'contents',
                       write_as_text=True)
     self.assertEqual(
         host.read_text_file(
             host.join(output_dir, 'a.b.c', 'initial', 'stdout',
                       'test.txt')), 'contents')
     ar.CreateArtifact('artifact_name',
                       file_rel_path,
                       'overwritten contents',
                       force_overwrite=True,
                       write_as_text=True)
     self.assertEqual(
         host.read_text_file(
             host.join(output_dir, 'a.b.c', 'initial', 'stdout',
                       'test.txt')), 'overwritten contents')
Example #3
0
 def test_duplicate_artifact_raises_error_when_added_to_list(self):
     host = FakeHost()
     output_dir = '%stmp' % host.sep
     ar = artifacts.Artifacts(output_dir, host, iteration=0)
     ar.AddArtifact('artifact_name', 'foo.txt')
     with self.assertRaises(ValueError) as ve:
         ar.AddArtifact('artifact_name', 'foo.txt')
     self.assertIn('already exists', str(ve.exception))
Example #4
0
  def test_duplicates_allowed_across_iterations(self):
    """Tests that using Artifacts with different iterations works."""
    tempdir = tempfile.mkdtemp()
    try:
      ar = artifacts.Artifacts(tempdir, 'test_name', 'test_basename', 0)
      with ar.CreateArtifact('artifact_name') as f:
        f.write('contents')

      another_ar = artifacts.Artifacts(tempdir, 'test_name', 'test_basename', 1)
      with another_ar.CreateArtifact('artifact_name') as f:
        f.write('other contents')

      self._VerifyPathAndContents(tempdir, 'test_name', 'test_basename', '0',
          'artifact_name', 'contents')
      self._VerifyPathAndContents(tempdir, 'test_name', 'test_basename', '1',
          'artifact_name', 'other contents')
    finally:
      shutil.rmtree(tempdir)
Example #5
0
 def test_dont_raise_value_error_for_dupl_in_add_artifacts(self):
     host = FakeHost()
     output_dir = '%stmp' % host.sep
     ar = artifacts.Artifacts(output_dir, host, iteration=0)
     ar.AddArtifact('artifact_name', 'foo.txt')
     ar.AddArtifact('artifact_name',
                    'foo.txt',
                    raise_exception_for_duplicates=False)
     self.assertEqual(ar.artifacts['artifact_name'], ['foo.txt'])
Example #6
0
 def test_create_artifact_writes_to_disk_iteration_0_no_test_dir(self):
     host = FakeHost()
     output_dir = '%stmp' % host.sep
     ar = artifacts.Artifacts(output_dir, host)
     file_rel_path = host.join('stdout', 'test.jpg')
     ar.CreateArtifact('artifact_name', file_rel_path, b'contents')
     self.assertEqual(
         host.read_binary_file(host.join(output_dir, 'stdout', 'test.jpg')),
         b'contents')
Example #7
0
 def test_file_manager_writes_file(self):
     host = FakeHost()
     output_dir = '%stmp' % host.sep
     ar = artifacts.Artifacts(output_dir, host, iteration=0)
     file_path = host.join('failures', 'stderr.txt')
     ar.CreateArtifact('artifact_name',
                       file_path,
                       'exception raised',
                       write_as_text=True)
     self.assertEqual(host.read_text_file(file_path), 'exception raised')
Example #8
0
    def test_create_artifact_writes_to_disk(self):
        """Tests CreateArtifact will write to disk at the correct location."""
        tempdir = tempfile.mkdtemp()
        try:
            ar = artifacts.Artifacts(tempdir, 'test_name', 'test_basename', 0)
            with ar.CreateArtifact('artifact_name') as f:
                f.write(b'contents')

            self._VerifyPathAndContents(tempdir, 'test_name', 'test_basename',
                                        '0', 'artifact_name', 'contents')
        finally:
            shutil.rmtree(tempdir)
Example #9
0
 def test_create_artifact_duplicate(self):
     """Tests that CreateArtifact with duplicate names fails."""
     tempdir = tempfile.mkdtemp()
     try:
         ar = artifacts.Artifacts(tempdir, 'test_name', 'test_basename', 0)
         with ar.CreateArtifact('artifact_name') as f:
             f.write(b'contents')
         with self.assertRaises(ValueError):
             with ar.CreateArtifact('artifact_name') as f:
                 pass
     finally:
         shutil.rmtree(tempdir)
Example #10
0
 def test_create_artifact_writes_to_disk_initial_results_dir(self):
     host = FakeHost()
     output_dir = '%stmp' % host.sep
     ar = artifacts.Artifacts(output_dir,
                              host,
                              iteration=0,
                              test_name='a.b.c',
                              intial_results_base_dir=True)
     file_rel_path = host.join('stdout', 'test.jpg')
     ar.CreateArtifact('artifact_name', file_rel_path, b'contents')
     self.assertEqual(
         host.read_binary_file(
             host.join(output_dir, 'a.b.c', 'initial', 'stdout',
                       'test.jpg')), b'contents')
Example #11
0
def _run_one_test(child, test_input):
    h = child.host
    pid = h.getpid()
    test_name = test_input.name

    started = h.time()

    # It is important to capture the output before loading the test
    # to ensure that
    # 1) the loader doesn't logs something we don't captured
    # 2) neither the loader nor the test case grab a reference to the
    #    uncaptured stdout or stderr that later is used when the test is run.
    # This comes up when using the FakeTestLoader and testing typ itself,
    # but could come up when testing non-typ code as well.
    h.capture_output(divert=not child.passthrough)
    if child.has_expectations:
      expectation = child.expectations.expectations_for(test_name)
      expected_results, should_retry_on_failure = (
          expectation.results, expectation.should_retry_on_failure)
    else:
      expected_results, should_retry_on_failure = {ResultType.Pass}, False
    ex_str = ''
    try:
        orig_skip = unittest.skip
        orig_skip_if = unittest.skipIf
        if child.all:
            unittest.skip = lambda reason: lambda x: x
            unittest.skipIf = lambda condition, reason: lambda x: x
        elif ResultType.Skip in expected_results:
            h.restore_output()
            return (Result(test_name, ResultType.Skip, started, 0,
                           child.worker_num, expected=expected_results,
                           unexpected=False, pid=pid), False)

        test_name_to_load = child.test_name_prefix + test_name
        try:
            suite = child.loader.loadTestsFromName(test_name_to_load)
        except Exception as e:
            ex_str = ('loadTestsFromName("%s") failed: %s\n%s\n' %
                      (test_name_to_load, e, traceback.format_exc()))
            try:
                suite = _load_via_load_tests(child, test_name_to_load)
                ex_str += ('\nload_via_load_tests(\"%s\") returned %d tests\n' %
                           (test_name_to_load, len(list(suite))))
            except Exception as e:  # pragma: untested
                suite = []
                ex_str += ('\nload_via_load_tests("%s") failed: %s\n%s\n' %
                           (test_name_to_load, e, traceback.format_exc()))
    finally:
        unittest.skip = orig_skip
        unittest.skipIf = orig_skip_if

    tests = list(suite)
    if len(tests) != 1:
        err = 'Failed to load "%s" in run_one_test' % test_name
        if ex_str:  # pragma: untested
            err += '\n  ' + '\n  '.join(ex_str.splitlines())

        h.restore_output()
        return (Result(test_name, ResultType.Failure, started, took=0,
                       worker=child.worker_num, unexpected=True, code=1,
                       err=err, pid=pid), False)

    art = artifacts.Artifacts(
        child.artifact_output_dir, h, test_input.iteration, test_name)

    test_case = tests[0]
    if isinstance(test_case, TypTestCase):
        test_case.child = child
        test_case.context = child.context_after_setup
        test_case.set_artifacts(art)

    test_result = unittest.TestResult()
    out = ''
    err = ''
    try:
        if child.dry_run:
            pass
        elif child.debugger:  # pragma: no cover
            _run_under_debugger(h, test_case, suite, test_result)
        else:
            suite.run(test_result)
    finally:
        out, err = h.restore_output()
        # Clear the artifact implementation so that later tests don't try to
        # use a stale instance.
        if isinstance(test_case, TypTestCase):
          test_case.set_artifacts(None)

    took = h.time() - started
    result = _result_from_test_result(test_result, test_name, started, took, out,
                                    err, child.worker_num, pid,
                                    expected_results, child.has_expectations,
                                    art.artifacts)
    result.result_sink_retcode =\
            child.result_sink_reporter.report_individual_test_result(
                child.test_name_prefix, result, child.artifact_output_dir,
                child.expectations)
    return (result, should_retry_on_failure)
Example #12
0
 def test_create_artifact_no_output_dir(self):
     """Tests that CreateArtifact will fail if used without an output dir."""
     art = artifacts.Artifacts(None, 'test_name', 'test_basename', 0)
     with self.assertRaises(ValueError):
         with art.CreateArtifact('artifact_name') as f:
             pass
Example #13
0
 def test_create_link_newlines(self):
     ar = artifacts.Artifacts('', FakeHost())
     with self.assertRaises(ValueError):
         ar.CreateLink('link', 'https://some\nbadurl.com')
Example #14
0
 def test_create_link_non_https(self):
     ar = artifacts.Artifacts('', FakeHost())
     with self.assertRaises(ValueError):
         ar.CreateLink('link', 'http://testsite.com')
Example #15
0
 def test_create_link_invalid_url(self):
     ar = artifacts.Artifacts('', FakeHost())
     with self.assertRaises(ValueError):
         ar.CreateLink('link', 'https:/malformedurl.com')
Example #16
0
 def test_create_link(self):
     ar = artifacts.Artifacts('', FakeHost())
     ar.CreateLink('link', 'https://testsite.com')
     self.assertEqual(ar.artifacts, {'link': ['https://testsite.com']})
Example #17
0
 def test_create_link_newlines(self):
     ar = artifacts.Artifacts(None, 'test_name', 'test_basename', 0)
     with self.assertRaises(ValueError):
         ar.CreateLink('link', 'https://some\nbadurl.com')
Example #18
0
 def test_create_link_invalid_url(self):
     ar = artifacts.Artifacts(None, 'test_name', 'test_basename', 0)
     with self.assertRaises(ValueError):
         ar.CreateLink('link', 'https:/malformedurl.com')
Example #19
0
 def test_create_link_duplicate(self):
     ar = artifacts.Artifacts(None, 'test_name', 'test_basename', 0)
     ar.CreateLink('link', 'https://testsite.com')
     with self.assertRaises(ValueError):
         ar.CreateLink('link', 'https://testsite.com')
Example #20
0
 def test_create_link(self):
     ar = artifacts.Artifacts(None, 'test_name', 'test_basename', 0)
     ar.CreateLink('link', 'https://testsite.com')
     self.assertEqual(ar.files, {'link': 'https://testsite.com'})