Exemple #1
0
    def test_update_jobs_and_delete_old(self, builder_mock, delete_job_mock,
                                        get_jobs_mock, is_job_mock):
        """
        Test update behaviour with --delete-old option

        Test update of jobs with the --delete-old option enabled, where only
        some jobs result in has_changed() to limit the number of times
        update_job is called, and have the get_jobs() method return additional
        jobs not in the input yaml to test that the code in cmd will call
        delete_job() after update_job() when '--delete-old' is set but only
        for the extra jobs.
        """
        # set up some test data
        jobs = ['old_job001', 'old_job002']
        extra_jobs = [{'name': name} for name in jobs]

        builder_obj = builder.Builder('http://jenkins.example.com',
                                      'doesnot',
                                      'matter',
                                      plugins_list={})

        # get the instance created by mock and redirect some of the method
        # mocks to call real methods on a the above test object.
        b_inst = builder_mock.return_value
        b_inst.plugins_list = builder_obj.plugins_list
        b_inst.update_job.side_effect = builder_obj.update_job
        b_inst.delete_old_managed.side_effect = builder_obj.delete_old_managed

        def _get_jobs():
            return builder_obj.parser.jobs + extra_jobs

        get_jobs_mock.side_effect = _get_jobs

        # override cache to ensure Jenkins.update_job called a limited number
        # of times
        self.cache_mock.return_value.has_changed.side_effect = ([True] * 2 +
                                                                [False] * 2)

        path = os.path.join(self.fixtures_path, 'cmd-002.yaml')
        args = self.parser.parse_args(['update', '--delete-old', path])

        with mock.patch('jenkins_jobs.builder.Jenkins.update_job') as update:
            with mock.patch('jenkins_jobs.builder.Jenkins.is_managed',
                            return_value=True):
                cmd.execute(args, self.config)
            self.assertEquals(
                2, update.call_count,
                "Expected Jenkins.update_job to be called '%d' "
                "times, got '%d' calls instead.\n"
                "Called with: %s" % (2, update.call_count, update.mock_calls))

        calls = [mock.call(name) for name in jobs]
        self.assertEquals(
            2, delete_job_mock.call_count,
            "Expected Jenkins.delete_job to be called '%d' "
            "times got '%d' calls instead.\n"
            "Called with: %s" %
            (2, delete_job_mock.call_count, delete_job_mock.mock_calls))
        delete_job_mock.assert_has_calls(calls, any_order=True)
    def test_multiple_same_anchor_in_multiple_toplevel_yaml(self):
        """
        Verify that anchors/aliases only span use of '!include' tag

        To ensure that any yaml loaded by the include tag is in the same
        space as the top level file, but individual top level yaml definitions
        are treated by the yaml loader as independent.
        """

        files = [
            "custom_same_anchor-001-part1.yaml",
            "custom_same_anchor-001-part2.yaml"
        ]

        b = builder.Builder("http://example.com",
                            "jenkins",
                            None,
                            plugins_list=[])
        b.load_files([os.path.join(self.fixtures_path, f) for f in files])
Exemple #3
0
    def _generate_jobs(self, config):
        module_path = config.arguments['module_path'].split(os.pathsep)

        library_path = config.arguments['library_path']
        if library_path is not None:
            library_path = library_path.split(os.pathsep)

        logging.debug("Module path: {0}".format(module_path))
        logging.debug("Library path: {0}".format(library_path))

        loader = jenkins_manager.loader.PythonLoader(module_path, library_path)
        logging.debug("Jobs loaded: {0}".format(pprint.pformat(loader.jobs)))

        jjbconfig = jjb_config.JJBConfig(config.arguments['conf'])
        jjbconfig.do_magical_things()
        jjbconfig.builder['ignore_cache'] = (not config.arguments['use_cache'])

        bldr = builder.Builder(jjbconfig)
        module_registry = registry.ModuleRegistry(jjbconfig, bldr.plugins_list)
        xml_generator = xml_config.XmlJobGenerator(module_registry)

        xml_jobs = xml_generator.generateXML(loader.jobs)

        return xml_jobs, bldr