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())
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)
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)
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())
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'])
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'])
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)
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')
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'])
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)
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)
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')
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'})
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())
def setUp(self): self._tempdir_obj = osutils.TempDir(set_global=True) self.tempdir = self._tempdir_obj.tempdir
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))
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)