Пример #1
0
    def test_label_selector(self):
        signature = "woahhhh"
        signer = JobSigner(signature)

        assert signer.label_selector() == f"{JobSigner.LABEL_KEY}={signature}"

        job_definition_name = "jdphd"
        assert signer.label_selector(job_definition_name).split(",") == [
            signer.label_selector(),
            f"{JobSigner.JOB_DEFINITION_NAME_KEY}={job_definition_name}",
        ]
Пример #2
0
    def test_fetch_jobs_filters(self, mock_batch_client):
        mock_batch_client.list_namespaced_job.return_value = V1JobList(
            items=[V1Job(metadata=V1ObjectMeta(name="1"))], metadata=V1ListMeta()
        )
        namespace = "hellomoto"
        signer = JobSigner("foo")
        manager = JobManager(
            namespace=namespace, signer=signer, register=StaticJobDefinitionsRegister()
        )

        assert len(list(manager.fetch_jobs(extra="filter"))) == 1
        mock_batch_client.list_namespaced_job.assert_called_once_with(
            namespace=namespace, label_selector=signer.label_selector(extra="filter")
        )
Пример #3
0
    def test_fetch_jobs_job_definition_name(self, mock_batch_client):
        namespace = "phd"
        signer = JobSigner("school")
        manager = JobManager(
            namespace=namespace, signer=signer, register=StaticJobDefinitionsRegister()
        )
        job_definition_name = "jd"
        mock_batch_client.list_namespaced_job.return_value = V1JobList(
            items=[], metadata=V1ListMeta()
        )

        list(manager.fetch_jobs(job_definition_name))

        mock_batch_client.list_namespaced_job.assert_called_once_with(
            namespace=namespace,
            label_selector=signer.label_selector(job_definition_name),
        )
Пример #4
0
    def test_label_selector_extra(self):
        signature = "woahhhh"
        signer = JobSigner(signature)

        assert signer.label_selector() == f"{JobSigner.LABEL_KEY}={signature}"

        job_definition_name = "jdphd"
        assert (
            signer.label_selector(extra="label", another=2)
            == f"{signer.label_selector()},extra=label,another=2"
        )

        assert (
            signer.label_selector(job_definition_name, extra="label", another=2)
            == f"{signer.label_selector()},{JobSigner.JOB_DEFINITION_NAME_KEY}={job_definition_name},extra=label,another=2"
        )

        # Various permutations are safe
        assert (
            signer.label_selector(
                extra="label", another=2, job_definition_name=job_definition_name
            )
            == f"{signer.label_selector()},{JobSigner.JOB_DEFINITION_NAME_KEY}={job_definition_name},extra=label,another=2"
        )
        assert (
            signer.label_selector(
                job_definition_name=job_definition_name, extra="label", another=2
            )
            == f"{signer.label_selector()},{JobSigner.JOB_DEFINITION_NAME_KEY}={job_definition_name},extra=label,another=2"
        )
Пример #5
0
 def manager(self) -> JobManager:
     """
     Returns a Job manager based on the config
     """
     return JobManager(
         namespace=self.namespace,
         signer=JobSigner(self.signature),
         register=self.register,
     )
Пример #6
0
def manager(request, register):
    # Add randomness here to make it so we can re-run locally without cleaning up
    signer = JobSigner(secrets.token_hex(16))
    manager = JobManager("default", signer, register=register)
    try:
        yield manager
    finally:
        # But try to clean them up anyway
        for job in manager.fetch_jobs():
            manager.delete_job(job)
Пример #7
0
    def test_sets_label_dict(self):
        signature = "hehehe"
        signer = JobSigner(signature)
        job = {"metadata": {}}

        signer.sign(job)

        assert (
            job["metadata"]["labels"][JobSigner.LABEL_KEY] == signature
        ), "Metadata label not set"

        job_definition_name = "tbirdaway"
        signer.sign(job, job_definition_name)
        assert (
            job["metadata"]["labels"][JobSigner.JOB_DEFINITION_NAME_KEY]
            == job_definition_name
        ), "Job Definition label not set"
Пример #8
0
    def test_sets_label_job(self):
        signature = "hehehe"
        signer = JobSigner(signature)
        job = V1Job(metadata=V1ObjectMeta())

        signer.sign(job)

        assert (
            job.metadata.labels[JobSigner.LABEL_KEY] == signature
        ), "Metadata label not set"

        job_definition_name = "funfun"
        signer.sign(job, job_definition_name)
        assert (
            job.metadata.labels[JobSigner.JOB_DEFINITION_NAME_KEY]
            == job_definition_name
        ), "Job Definition label not set"