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
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
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 validate_build_spec_config(config, raise_for_rest: bool = False): try: spec = BuildSpecification.read(config) except (MarshmallowValidationError, PolyaxonfileError, PolyaxonConfigurationError) as e: message_error = 'Received non valid build specification config. %s' % e if raise_for_rest: raise ValidationError(message_error) else: raise DjangoValidationError(message_error) return spec
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_get_with_environment(self): spec_content = """--- version: 1 kind: build environment: node_selector: foo: bar tolerations: - key: "key" operator: "Equal" value: "value" effect: "NoSchedule" affinity: foo: bar resources: gpu: requests: 1 limits: 1 tpu: requests: 1 limits: 1 build: image: my_image """ spec_parsed_content = BuildSpecification.read(spec_content) project = ProjectFactory(user=self.auth_client.user) obj = self.factory_class(project=project, config=spec_parsed_content.parsed_data) url = '/{}/{}/{}/builds/{}/'.format(API_V1, project.user.username, project.name, obj.id) obj_query = queries.builds_details.get(id=obj.id) resp = self.auth_client.get(url) assert resp.status_code == status.HTTP_200_OK assert resp.data == self.serializer_class(obj_query).data
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
def specification(self) -> 'BuildSpecification': return BuildSpecification(values=self.config)
cpu: requests: 1 limits: 1 memory: requests: 100 limits: 200 build: image: my_image run: cmd: test """ job_spec_resources_parsed_content = JobSpecification.read( job_spec_resources_content) build_spec_content = """--- version: 1 kind: build tags: [fixtures] build: image: my_image build_steps: ['step1', 'step2'] """ build_spec_parsed_content = BuildSpecification.read(build_spec_content)