def test_faulty_service_account(self):
     """pod creation should fail when service account does not exist"""
     service_account = "foobar"
     namespace = "default"
     k = KubernetesPodOperator(
         namespace=namespace,
         image="ubuntu:16.04",
         cmds=["bash", "-cx"],
         arguments=["echo 10"],
         labels={"foo": "bar"},
         name="test",
         task_id="task",
         in_cluster=False,
         do_xcom_push=False,
         startup_timeout_seconds=5,
         service_account_name=service_account,
     )
     context = create_context(k)
     pod = k.build_pod_request_obj(context)
     with pytest.raises(
             ApiException,
             match=
             f"error looking up service account {namespace}/{service_account}"
     ):
         k.get_or_create_pod(pod, context)
Beispiel #2
0
    def test_env_vars(self):
        # WHEN
        env_vars = [
            k8s.V1EnvVar(name="ENV1", value="val1"),
            k8s.V1EnvVar(name="ENV2", value="val2"),
            k8s.V1EnvVar(
                name="ENV3",
                value_from=k8s.V1EnvVarSource(
                    field_ref=k8s.V1ObjectFieldSelector(
                        field_path="status.podIP")),
            ),
        ]

        k = KubernetesPodOperator(
            namespace='default',
            image="ubuntu:16.04",
            cmds=["bash", "-cx"],
            arguments=["echo 10"],
            env_vars=env_vars,
            labels={"foo": "bar"},
            name="test-" + str(random.randint(0, 1000000)),
            task_id="task" + self.get_current_task_name(),
            in_cluster=False,
            do_xcom_push=False,
        )
        # THEN
        context = create_context(k)
        actual_pod = self.api_client.sanitize_for_serialization(
            k.build_pod_request_obj(context))
        self.expected_pod['spec']['containers'][0]['env'] = [
            {
                'name': 'ENV1',
                'value': 'val1'
            },
            {
                'name': 'ENV2',
                'value': 'val2'
            },
            {
                'name': 'ENV3',
                'valueFrom': {
                    'fieldRef': {
                        'fieldPath': 'status.podIP'
                    }
                }
            },
        ]
        assert self.expected_pod == actual_pod
Beispiel #3
0
 def test_faulty_service_account(self):
     bad_service_account_name = "foobar"
     k = KubernetesPodOperator(
         namespace='default',
         image="ubuntu:16.04",
         cmds=["bash", "-cx"],
         arguments=["echo 10"],
         labels={"foo": "bar"},
         name="test-" + str(random.randint(0, 1000000)),
         task_id="task" + self.get_current_task_name(),
         in_cluster=False,
         do_xcom_push=False,
         startup_timeout_seconds=5,
         service_account_name=bad_service_account_name,
     )
     context = create_context(k)
     pod = k.build_pod_request_obj(context)
     with pytest.raises(
             ApiException,
             match="error looking up service account default/foobar"):
         k.get_or_create_pod(pod, context)