Exemple #1
0
    def create(user,
               project,
               config,
               code_reference,
               config_map_refs=None,
               secret_refs=None,
               nocache=False) -> Tuple['BuildJob', bool]:
        build_spec = BuildSpecification.create_specification(config,
                                                             configmap_refs=config_map_refs,
                                                             secret_refs=secret_refs,
                                                             to_dict=False)
        if not nocache and build_spec.config.nocache is not None:
            # Set the config's nocache rebuild
            nocache = build_spec.config.nocache
        # Check if image is not using latest tag, then we can reuse a previous build
        rebuild_cond = (
            nocache or
            (conf.get(BUILD_JOBS_ALWAYS_PULL_LATEST) and
             build_spec.config.image_tag == LATEST_IMAGE_TAG)
        )
        if not rebuild_cond:
            job = BuildJob.objects.filter(project=project,
                                          content=build_spec.raw_data,
                                          code_reference=code_reference).last()
            if job:
                return job, False

        return BuildJob.objects.create(user=user,
                                       project=project,
                                       content=build_spec.raw_data,
                                       code_reference=code_reference), True
Exemple #2
0
    def test_scheduler_create_build_job_of_already_done_job(self):
        """Check the case when the job is already done and
        we need to create a new job."""
        config = {'image': 'busybox:tag'}
        build_job = BuildJobFactory(
            project=self.project,
            user=self.project.user,
            code_reference=self.code_reference,
            content=BuildSpecification.create_specification(
                config, to_dict=False).raw_data)
        build_job.set_status(JobLifeCycle.STOPPED)

        assert BuildJob.objects.count() == 1
        with patch('scheduler.dockerizer_scheduler.start_dockerizer'
                   ) as mock_start:
            mock_start.return_value = True
            build_job, image_exists, build_status = dockerizer_scheduler.create_build_job(
                user=self.project.user,
                project=self.project,
                config=config,
                code_reference=self.code_reference)
        assert mock_start.call_count == 1
        assert image_exists is False
        assert build_status is True
        assert BuildJob.objects.count() == 2
Exemple #3
0
class BuildJobFactory(factory.DjangoModelFactory):
    content = BuildSpecification.create_specification({
        'image': 'busybox'
    },
                                                      to_dict=False).raw_data

    user = factory.SubFactory(UserFactory)
    project = factory.SubFactory(ProjectFactory)
    code_reference = factory.SubFactory(CodeReferenceFactory)

    class Meta:
        model = BuildJob
Exemple #4
0
    def test_scheduler_create_build_job_of_already_running_job(self):
        """Check the case when the job is already running and
        we just set the requesting service to running."""
        config = {'image': 'busybox:tag'}
        build_job = BuildJobFactory(
            project=self.project,
            user=self.project.user,
            code_reference=self.code_reference,
            content=BuildSpecification.create_specification(
                config, to_dict=False).raw_data)
        build_job.set_status(JobLifeCycle.RUNNING)

        assert BuildJob.objects.count() == 1
        with patch('scheduler.dockerizer_scheduler.start_dockerizer'
                   ) as mock_start:
            build_job, image_exists, build_status = dockerizer_scheduler.create_build_job(
                user=self.project.user,
                project=self.project,
                config=config,
                code_reference=self.code_reference)
        assert mock_start.call_count == 0
        assert image_exists is False
        assert build_status is True
        assert BuildJob.objects.count() == 1