Example #1
0
    def testCanSubmitChangeDisallwedByParentConfigByDefault(self):
        """Test CanSubmitChangeInPreCq when sub-config allows it, but not root."""
        mock_checkout = mock.Mock()
        with osutils.TempDir(set_global=True) as tempdir:
            root_dir = os.path.join(tempdir, 'overlays')
            mock_checkout.GetPath.return_value = root_dir
            root_ini = os.path.join(root_dir, 'COMMIT-QUEUE.ini')
            osutils.WriteFile(root_ini, '[GENERAL]\n'
                              'union-pre-cq-sub-configs: yes\n',
                              makedirs=True)
            f_1 = self._CreateOverlayPaths(
                root_dir, 'overlay-lumpy',
                '[GENERAL]\nsubmit-in-pre-cq: yes\n')
            f_2 = self._CreateOverlayPaths(root_dir, 'overlay-link',
                                           '[GENERAL]\n')
            diff_dict = {f: 'M' for f in (f_1, f_2)}
            change = self._patch_factory.MockPatch()
            self.PatchObject(cros_patch.GerritPatch,
                             'GetDiffStatus',
                             return_value=diff_dict)

            parser = self.CreateCQConfigParser(change=change,
                                               common_config_file=root_ini,
                                               checkout=mock_checkout)
            self.assertFalse(parser.CanSubmitChangeInPreCQ())
Example #2
0
 def testGetOptionFromConfigFileOnGoodConfigFile(self):
     """Test if we can handle a good config file."""
     with osutils.TempDir(set_global=True) as tempdir:
         path = os.path.join(tempdir, 'foo.ini')
         osutils.WriteFile(path, '[a]\nb: bar baz\n')
         ignored = self.GetOption(path)
         self.assertEqual('bar baz', ignored)
Example #3
0
 def testGetConfigFlagReturnsFalse(self):
     """Test GetConfigFlag which returns False."""
     with osutils.TempDir(set_global=True) as tempdir:
         path = os.path.join(tempdir, 'foo.ini')
         osutils.WriteFile(path, '[a]\nc: yes\n')
         parser = self.CreateCQConfigParser(common_config_file=path)
         flag = parser.GetConfigFlag('a', 'b')
         self.assertFalse(flag)
Example #4
0
 def testGetUnionPreCQSubConfigsFlag(self):
     """Test ShouldUnionPreCQFromSubConfigs."""
     with osutils.TempDir(set_global=True) as tempdir:
         path = os.path.join(tempdir, 'foo.ini')
         osutils.WriteFile(path,
                           '[GENERAL]\nunion-pre-cq-sub-configs: yes\n')
         parser = self.CreateCQConfigParser(common_config_file=path)
         self.assertTrue(parser.GetUnionPreCQSubConfigsFlag())
Example #5
0
    def testGetSubsystems(self):
        """Test if we can get the subsystem label from a good config file."""
        with osutils.TempDir(set_global=True) as tempdir:
            path = os.path.join(tempdir, 'foo.ini')
            osutils.WriteFile(path, '[GENERAL]\nsubsystem: power light\n')
            parser = self.CreateCQConfigParser(common_config_file=path)
            subsystems = parser.GetSubsystems()

            self.assertItemsEqual(subsystems, ['power', 'light'])
Example #6
0
    def testGetStagesToIgnore(self):
        """Test if we can get the ignored stages from a good config file."""
        with osutils.TempDir(set_global=True) as tempdir:
            path = os.path.join(tempdir, 'foo.ini')
            osutils.WriteFile(path, '[GENERAL]\nignored-stages: bar baz\n')
            parser = self.CreateCQConfigParser(common_config_file=path)
            ignored = parser.GetStagesToIgnore()

            self.assertEqual(ignored, ['bar', 'baz'])
Example #7
0
    def testGetOptionForBadConfigFileWithTrueForgiven(self):
        """Test whether the return is None when handle a malformat config file."""
        with osutils.TempDir(set_global=True) as tempdir:
            path = os.path.join(tempdir, 'COMMIT-QUEUE.ini')
            osutils.WriteFile(path, 'foo\n')
            parser = self.CreateCQConfigParser(common_config_file=path)
            result = parser.GetOption('a', 'b')

            self.assertEqual(None, result)
Example #8
0
    def testGetOptionForBadConfigFileWithFalseForgiving(self):
        """Test whether exception is raised when handle a malformat config file."""
        with osutils.TempDir(set_global=True) as tempdir:
            path = os.path.join(tempdir, 'COMMIT-QUEUE.ini')
            osutils.WriteFile(path, 'foo\n')
            parser = self.CreateCQConfigParser(common_config_file=path,
                                               forgiving=False)

            with self.assertRaises(cq_config.MalformedCQConfigException):
                parser.GetOption('a', 'b')
Example #9
0
    def testGetPreCQConfigs(self):
        """Test GetPreCQConfigs."""
        with osutils.TempDir(set_global=True) as tempdir:
            path = os.path.join(tempdir, 'foo.ini')
            osutils.WriteFile(
                path, '[GENERAL]\npre-cq-configs: default binhost-pre-cq\n')
            parser = self.CreateCQConfigParser(common_config_file=path)
            pre_cq_configs = parser.GetPreCQConfigs()

            self.assertItemsEqual(pre_cq_configs,
                                  ['default', 'binhost-pre-cq'])
Example #10
0
    def testGetConfigFileForAffectedPathOnDeletedFiles(self):
        """Test _GetConfigFileForAffectedPath on deleted files."""
        # Create paths:
        # /tmpdir/overlays/COMMIT-QUEUE.ini
        parser = self.CreateCQConfigParser()
        with osutils.TempDir(set_global=True) as tempdir:
            root_dir = os.path.join(tempdir, 'overlays')
            root_ini = os.path.join(root_dir, 'COMMIT-QUEUE.ini')
            sub_dir = os.path.join(root_dir, 'overlay-lumpy')
            changed_file = os.path.join(sub_dir, 'test', 'test.py')
            config_path = parser._GetConfigFileForAffectedPath(
                changed_file, root_dir)

            self.assertEqual(config_path, root_ini)
Example #11
0
    def testGetConfigFileForAffectedPathReturnsRootPath(self):
        """Test _GetConfigFileForAffectedPath which returns root path."""
        # Create paths:
        # /tmpdir/overlays/COMMIT-QUEUE.ini
        # /tmpdir/overlays/overlay-lumpy/test/test.py
        parser = self.CreateCQConfigParser()
        with osutils.TempDir(set_global=True) as tempdir:
            root_dir = os.path.join(tempdir, 'overlays')
            root_ini = os.path.join(root_dir, 'COMMIT-QUEUE.ini')
            osutils.WriteFile(root_ini,
                              '[GENERAL]\npre-cq-configs: default\n',
                              makedirs=True)
            sub_dir = os.path.join(root_dir, 'overlay-lumpy')
            changed_file = os.path.join(sub_dir, 'test', 'test.py')
            osutils.WriteFile(changed_file, '#test#', makedirs=True)
            config_path = parser._GetConfigFileForAffectedPath(
                changed_file, root_dir)

            self.assertEqual(config_path, root_ini)
Example #12
0
    def testGetOption(self):
        """Test GetOption."""
        with osutils.TempDir(set_global=True) as tempdir:
            foo_path = os.path.join(tempdir, 'foo.ini')
            osutils.WriteFile(foo_path,
                              '[GENERAL]\npre-cq-configs: binhost-pre-cq\n')
            bar_path = os.path.join(tempdir, 'bar', 'bar.ini')
            osutils.WriteFile(bar_path,
                              '[GENERAL]\npre-cq-configs: lumpy-pre-cq\n',
                              makedirs=True)
            parser = self.CreateCQConfigParser(common_config_file=foo_path)

            self.assertEqual(
                parser.GetOption(constants.CQ_CONFIG_SECTION_GENERAL,
                                 constants.CQ_CONFIG_PRE_CQ_CONFIGS),
                'binhost-pre-cq')
            self.assertEqual(
                parser.GetOption(constants.CQ_CONFIG_SECTION_GENERAL,
                                 constants.CQ_CONFIG_PRE_CQ_CONFIGS,
                                 config_path=bar_path), 'lumpy-pre-cq')
Example #13
0
    def testGetUnionedPreCQConfigs(self):
        """Test GetUnionedPreCQConfigs."""
        mock_checkout = mock.Mock()
        with osutils.TempDir(set_global=True) as tempdir:
            root_dir = os.path.join(tempdir, 'overlays')
            mock_checkout.GetPath.return_value = root_dir
            root_ini = os.path.join(root_dir, 'COMMIT-QUEUE.ini')
            osutils.WriteFile(root_ini,
                              '[GENERAL]\npre-cq-configs: default\n',
                              makedirs=True)
            f_1 = self._CreateOverlayPaths(
                root_dir, 'overlay-lumpy',
                '[GENERAL]\npre-cq-configs: default lumpy-pre-cq\n')
            f_2 = self._CreateOverlayPaths(
                root_dir, 'overlay-stumpy',
                '[GENERAL]\npre-cq-configs: stumpy-pre-cq\n')
            f_3 = self._CreateOverlayPaths(
                root_dir, 'overlay-lakitu',
                '[GENERAL]\npre-cq-configs: lakitu-pre-cq\n')
            f_4 = self._CreateOverlayPaths(
                root_dir,
                'overlay-lakitu',
                '[GENERAL]\npre-cq-configs: lakitu-pre-cq\n',
                file_rel_path=os.path.join('test', 'test-copy.py'))
            diff_dict = {f: 'M' for f in (f_1, f_2, f_3, f_4)}
            change = self._patch_factory.MockPatch()
            self.PatchObject(cros_patch.GerritPatch,
                             'GetDiffStatus',
                             return_value=diff_dict)

            parser = self.CreateCQConfigParser(change=change,
                                               common_config_file=root_ini,
                                               checkout=mock_checkout)
            pre_cq_configs = parser.GetUnionedPreCQConfigs()

            self.assertItemsEqual(
                pre_cq_configs,
                {'default', 'lumpy-pre-cq', 'lakitu-pre-cq', 'stumpy-pre-cq'})
Example #14
0
    def testCanSubmitChangeInPreCqMissingSubConfigsOption(self):
        """Test CanSubmitChangeInPreCq when sub-configs option is missing."""
        mock_checkout = mock.Mock()
        with osutils.TempDir(set_global=True) as tempdir:
            root_dir = os.path.join(tempdir, 'overlays')
            mock_checkout.GetPath.return_value = root_dir
            root_ini = os.path.join(root_dir, 'COMMIT-QUEUE.ini')
            osutils.WriteFile(root_ini,
                              '[GENERAL]\nsubmit-in-pre-cq: no\n',
                              makedirs=True)
            f_1 = self._CreateOverlayPaths(
                root_dir, 'overlay-lumpy',
                '[GENERAL]\nsubmit-in-pre-cq: yes\n')
            diff_dict = {f_1: 'M'}
            change = self._patch_factory.MockPatch()
            self.PatchObject(cros_patch.GerritPatch,
                             'GetDiffStatus',
                             return_value=diff_dict)

            parser = self.CreateCQConfigParser(change=change,
                                               common_config_file=root_ini,
                                               checkout=mock_checkout)
            self.assertFalse(parser.CanSubmitChangeInPreCQ())
Example #15
0
 def setUp(self):
     self._tempdir_obj = osutils.TempDir(set_global=True)
     self.tempdir = self._tempdir_obj.tempdir
Example #16
0
 def testGetOptionFromConfigFileOnMissingConfigFile(self):
     """Test if we can handle a missing config file."""
     with osutils.TempDir(set_global=True) as tempdir:
         path = os.path.join(tempdir, 'foo.ini')
         self.assertEqual(None, self.GetOption(path))
Example #17
0
 def testGetOptionFromConfigFileOnBadConfigFile(self):
     """Test if we can handle an incorrectly formatted config file."""
     with osutils.TempDir(set_global=True) as tempdir:
         path = os.path.join(tempdir, 'foo.ini')
         osutils.WriteFile(path, 'foobar')
         self.assertRaises(ConfigParser.Error, self.GetOption, path)