def _testForgiveness(self, forgiveness, expected_result):
        class ForgivenessTest(image_test_lib.ImageTestCase):
            """A dummy test that is sometime forgiving, sometime not.

      Its only test (testFail) always fail.
      """

            _forgiving = True

            def SetForgiving(self, value):
                self._forgiving = value

            def IsForgiving(self):
                return self._forgiving

            def testFail(self):
                self.fail()

        test = ForgivenessTest('testFail')
        test.SetForgiving(forgiveness)
        suite = image_test_lib.ImageTestSuite()
        suite.addTest(test)
        self.PatchObject(unittest.TestLoader,
                         'loadTestsFromName',
                         autospec=True,
                         return_value=[suite])
        argv = [self.tempdir]
        self.assertEqual(expected_result, test_image.main(argv))
Exemplo n.º 2
0
def main(args):
    opts = ParseArgs(args)

    # Build up test suites.
    loader = unittest.TestLoader()
    loader.suiteClass = image_test_lib.ImageTestSuite
    # We use a different prefix here so that unittest DO NOT pick up the
    # image tests automatically because they depend on a proper environment.
    loader.testMethodPrefix = 'Test'
    all_tests = loader.loadTestsFromName('chromite.cros.test.image_test')
    forgiving = image_test_lib.ImageTestSuite()
    non_forgiving = image_test_lib.ImageTestSuite()
    for suite in all_tests:
        for test in suite.GetTests():
            if test.IsForgiving():
                forgiving.addTest(test)
            else:
                non_forgiving.addTest(test)

    # Run them in the image directory.
    runner = image_test_lib.ImageTestRunner()
    runner.SetBoard(opts.board)
    runner.SetResultDir(opts.test_results_root)
    image_file = FindImage(opts.image_dir)
    tmp_in_chroot = path_util.FromChrootPath('/tmp')
    with osutils.TempDir(base_dir=tmp_in_chroot) as temp_dir:
        with osutils.MountImageContext(image_file, temp_dir):
            with osutils.ChdirContext(temp_dir):
                # Run non-forgiving tests first so that exceptions in forgiving tests
                # do not skip any required tests.
                logging.info('Running NON-forgiving tests.')
                result = runner.run(non_forgiving)
                logging.info('Running forgiving tests.')
                runner.run(forgiving)

    if result and not result.wasSuccessful():
        return 1
    return 0
    def testBoardAndDirectory(self):
        """Verify that "--board", "--test_results_root" are passed to the tests."""
        class AttributeTest(image_test_lib.ForgivingImageTestCase):
            """Dummy test class to hold board and directory."""
            def testOkay(self):
                pass

        test = AttributeTest('testOkay')
        suite = image_test_lib.ImageTestSuite()
        suite.addTest(test)
        self.PatchObject(unittest.TestLoader,
                         'loadTestsFromName',
                         autospec=True,
                         return_value=[suite])
        argv = [
            '--board', 'my-board', '--test_results_root', 'your-root',
            self.tempdir
        ]
        test_image.main(argv)
        # pylint: disable=W0212
        self.assertEqual('my-board', test._board)
        # pylint: disable=W0212
        self.assertEqual('your-root', os.path.basename(test._result_dir))
    def testChdir(self):
        """Verify the CWD is in a temp directory."""
        class CwdTest(image_test_lib.NonForgivingImageTestCase):
            """A dummy test class to verify current working directory."""

            _expected_dir = None

            def SetCwd(self, cwd):
                self._expected_dir = cwd

            def testExpectedCwd(self):
                self.assertEqual(self._expected_dir, os.getcwd())

        self.assertNotEqual('/tmp', os.getcwd())
        os.chdir('/tmp')

        test = CwdTest('testExpectedCwd')
        suite = image_test_lib.ImageTestSuite()
        suite.addTest(test)
        self.PatchObject(unittest.TestLoader,
                         'loadTestsFromName',
                         autospec=True,
                         return_value=[suite])

        # Set up the expected directory.
        expected_dir = os.path.join(self.tempdir, 'my-subdir')
        os.mkdir(expected_dir)
        test.SetCwd(expected_dir)
        self.PatchObject(tempfile,
                         'mkdtemp',
                         autospec=True,
                         return_value=expected_dir)

        argv = [self.tempdir]
        self.assertEqual(0, test_image.main(argv))
        self.assertEqual('/tmp', os.getcwd())