def create(user, project, config, code_reference, configmap_refs=None, secret_refs=None, nocache=False) -> Tuple['BuildJob', bool]: build_config = BuildSpecification.create_specification( config, configmap_refs=configmap_refs, secret_refs=secret_refs, to_dict=False) if not nocache and build_config.build.nocache is not None: # Set the config's nocache rebuild nocache = build_config.build.nocache # Check if image is not using latest tag, then we can reuse a previous build rebuild_cond = (nocache or (conf.get('BUILD_ALWAYS_PULL_LATEST') and build_config.build.image_tag == LATEST_IMAGE_TAG)) if not rebuild_cond: job = BuildJob.objects.filter( project=project, config=build_config.parsed_data, code_reference=code_reference).last() if job: return job, False return BuildJob.objects.create(user=user, project=project, config=build_config.parsed_data, code_reference=code_reference), True
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, config=BuildSpecification.create_specification(config)) build_job.set_status(JobLifeCycle.STOPPED) assert BuildJob.objects.count() == 1 with patch('scheduler.dockerizer_scheduler.start_dockerizer') as mock_start: with patch('scheduler.dockerizer_scheduler.check_image') as mock_check: mock_start.return_value = True mock_check.return_value = False 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 mock_check.call_count == 1 assert image_exists is False assert build_status is True assert BuildJob.objects.count() == 2
class BuildJobFactory(factory.DjangoModelFactory): config = BuildSpecification.create_specification({'image': 'busybox'}) user = factory.SubFactory(UserFactory) project = factory.SubFactory(ProjectFactory) code_reference = factory.SubFactory(CodeReferenceFactory) class Meta: model = BuildJob
def create(user, project, config, code_reference, nocache=False): build_config = BuildSpecification.create_specification(config, to_dict=False) if not nocache and build_config.build.nocache is not None: # Set the config's nocache rebuild nocache = build_config.build.nocache # Check if image is not using latest tag, then we can reuse a previous build if not nocache and build_config.build.image_tag != LATEST_IMAGE_TAG: job = BuildJob.objects.filter( project=project, config=build_config.parsed_data, code_reference=code_reference).last() if job: return job return BuildJob.objects.create(user=user, project=project, config=build_config.parsed_data, code_reference=code_reference)
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, config=BuildSpecification.create_specification(config)) 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
def test_scheduler_create_build_job_image_already_exists(self): """Check the case when the image is already built.""" config = {'image': 'busybox:tag'} BuildJobFactory(project=self.project, user=self.project.user, code_reference=self.code_reference, config=BuildSpecification.create_specification(config)) assert BuildJob.objects.count() == 1 with patch('scheduler.dockerizer_scheduler.start_dockerizer') as mock_start: with patch('scheduler.dockerizer_scheduler.check_image') as mock_check: mock_check.return_value = True _, 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 mock_check.call_count == 1 assert image_exists is True assert build_status is False assert BuildJob.objects.count() == 1