|
| 1 | +from ads.jobs import Job, DataScienceJob, ContainerRuntime |
| 2 | +from tests.unitary.default_setup.jobs.test_jobs_base import DataScienceJobPayloadTest |
| 3 | + |
| 4 | + |
| 5 | +class JobTagTestCase(DataScienceJobPayloadTest): |
| 6 | + @staticmethod |
| 7 | + def runtime() -> ContainerRuntime: |
| 8 | + return ContainerRuntime().with_image( |
| 9 | + "iad.ocir.io/my_namespace/my_ubuntu_image", |
| 10 | + entrypoint="/bin/sh", |
| 11 | + cmd="-c,echo Hello World", |
| 12 | + ) |
| 13 | + |
| 14 | + @staticmethod |
| 15 | + def infra() -> DataScienceJob: |
| 16 | + return ( |
| 17 | + DataScienceJob() |
| 18 | + .with_compartment_id("ocid1.compartment.oc1..<unique_ocid>") |
| 19 | + .with_project_id("ocid1.datascienceproject.oc1.iad.<unique_ocid>") |
| 20 | + ) |
| 21 | + |
| 22 | + def create_job(self, infra, runtime) -> dict: |
| 23 | + job = ( |
| 24 | + Job(name=self.__class__.__name__) |
| 25 | + .with_infrastructure(infra) |
| 26 | + .with_runtime(runtime) |
| 27 | + ) |
| 28 | + job = self.mock_create_job(job) |
| 29 | + return job.infrastructure.dsc_job.to_dict() |
| 30 | + |
| 31 | + def test_create_job_with_runtime_tags(self): |
| 32 | + runtime = ( |
| 33 | + self.runtime() |
| 34 | + .with_freeform_tag(freeform_tag="freeform_tag_val") |
| 35 | + .with_defined_tag(Operations={"CostCenter": "42"}) |
| 36 | + ) |
| 37 | + payload = self.create_job(self.infra(), runtime) |
| 38 | + self.assertEqual(payload["freeformTags"], dict(freeform_tag="freeform_tag_val")) |
| 39 | + self.assertEqual(payload["definedTags"], {"Operations": {"CostCenter": "42"}}) |
| 40 | + |
| 41 | + def test_create_job_with_infra_tags(self): |
| 42 | + infra = ( |
| 43 | + self.infra() |
| 44 | + .with_freeform_tag(freeform_tag="freeform_tag_val") |
| 45 | + .with_defined_tag(Operations={"CostCenter": "42"}) |
| 46 | + ) |
| 47 | + payload = self.create_job(infra, self.runtime()) |
| 48 | + self.assertEqual(payload["freeformTags"], dict(freeform_tag="freeform_tag_val")) |
| 49 | + self.assertEqual(payload["definedTags"], {"Operations": {"CostCenter": "42"}}) |
| 50 | + |
| 51 | + def test_create_job_with_infra_and_runtime_tags(self): |
| 52 | + # Tags defined in runtime will have higher priority |
| 53 | + infra = ( |
| 54 | + self.infra() |
| 55 | + .with_freeform_tag(freeform_tag="freeform_tag_val") |
| 56 | + .with_defined_tag(Operations={"CostCenter": "41"}) |
| 57 | + ) |
| 58 | + runtime = ( |
| 59 | + self.runtime() |
| 60 | + .with_freeform_tag(freeform_tag="freeform_tag_val") |
| 61 | + .with_defined_tag(Operations={"CostCenter": "42"}) |
| 62 | + ) |
| 63 | + payload = self.create_job(infra, runtime) |
| 64 | + self.assertEqual(payload["freeformTags"], dict(freeform_tag="freeform_tag_val")) |
| 65 | + self.assertEqual(payload["definedTags"], {"Operations": {"CostCenter": "42"}}) |
0 commit comments