예제 #1
0
    def test_get_env_vars_with_kv_env_vars(self):
        # Check wrong kv env vars
        with self.assertRaises(PolypodException):
            get_env_vars(
                contexts=None,
                log_level=None,
                kv_env_vars=["x", "y", "z"],
                artifacts_store_name=None,
                connections=None,
                secrets=None,
                config_maps=None,
            )
        with self.assertRaises(PolypodException):
            get_env_vars(
                contexts=None,
                log_level=None,
                kv_env_vars={"x": "y"},
                artifacts_store_name=None,
                connections=None,
                secrets=None,
                config_maps=None,
            )

        # Valid kv env vars
        assert (get_env_vars(
            contexts=None,
            log_level=None,
            kv_env_vars=[["key1", "val1"], ["key2", "val2"]],
            artifacts_store_name=None,
            connections=None,
            secrets=None,
            config_maps=None,
        ) == get_kv_env_vars([["key1", "val1"], ["key2", "val2"]]))
예제 #2
0
    def test_get_env_vars_with_all(self):
        connection = V1ConnectionType(
            name="test_s3",
            kind=V1ConnectionKind.S3,
            schema=V1BucketConnection(bucket="s3//:foo"),
            secret=self.resource6.schema,
        )

        env_vars = get_env_vars(
            contexts=PluginsContextsSpec.from_config(
                V1Plugins(collect_logs=False,
                          collect_artifacts=True,
                          collect_resources=True)),
            log_level="info",
            kv_env_vars=[["key1", "val1"], ["key2", "val2"]],
            artifacts_store_name="test",
            connections=[connection],
            secrets=[
                self.resource1,
                self.resource2,
                self.resource3,
                self.resource4,
                self.resource6,
            ],
            config_maps=[
                self.resource1,
                self.resource2,
                self.resource3,
                self.resource4,
            ],
        )
        expected = [
            get_env_var(name=POLYAXON_KEYS_LOG_LEVEL, value="info"),
            get_env_var(name=POLYAXON_KEYS_COLLECT_ARTIFACTS, value=True),
            get_env_var(name=POLYAXON_KEYS_COLLECT_RESOURCES, value=True),
            get_env_var(name=POLYAXON_KEYS_ARTIFACTS_STORE_NAME, value="test"),
        ]
        expected += get_connection_env_var(connection=connection,
                                           secret=self.resource6)
        expected += get_kv_env_vars([["key1", "val1"], ["key2", "val2"]])
        expected += get_env_vars_from_k8s_resources(
            secrets=[
                self.resource1,
                self.resource2,
                self.resource3,
                self.resource4,
                self.resource6,
            ],
            config_maps=[
                self.resource1,
                self.resource2,
                self.resource3,
                self.resource4,
            ],
        )

        assert env_vars == expected
예제 #3
0
def get_env_vars(
    contexts: PluginsContextsSpec,
    log_level: str,
    kv_env_vars: List[List],
    artifacts_store_name: str,
    connections: Iterable[V1ConnectionType],
    secrets: Iterable[V1K8sResourceType],
    config_maps: Iterable[V1K8sResourceType],
) -> List[k8s_schemas.V1EnvVar]:
    env_vars = []
    connections = connections or []

    if log_level:
        env_vars.append(get_env_var(name=POLYAXON_KEYS_LOG_LEVEL, value=log_level))

    if contexts and contexts.collect_artifacts:
        env_vars.append(get_env_var(name=POLYAXON_KEYS_COLLECT_ARTIFACTS, value=True))

    if contexts and contexts.collect_resources:
        env_vars.append(get_env_var(name=POLYAXON_KEYS_COLLECT_RESOURCES, value=True))

    if artifacts_store_name:
        env_vars.append(
            get_env_var(
                name=POLYAXON_KEYS_ARTIFACTS_STORE_NAME, value=artifacts_store_name
            )
        )

    # Add connection env vars information
    for connection in connections:
        try:
            secret = connection.get_secret()
            env_vars += to_list(
                get_connection_env_var(connection=connection, secret=secret),
                check_none=True,
            )
        except PolyaxonSchemaError as e:
            raise PolypodException("Error resolving secrets: %s" % e) from e

    env_vars += get_kv_env_vars(kv_env_vars)
    env_vars += get_env_vars_from_k8s_resources(
        secrets=secrets, config_maps=config_maps
    )
    return env_vars
예제 #4
0
 def test_get_kv_env_var(self):
     # Empty value
     assert get_kv_env_vars([]) == []
     # Non valid value
     with self.assertRaises(PolypodException):
         get_kv_env_vars([[123, "foo", "bar"]])
     with self.assertRaises(PolypodException):
         get_kv_env_vars([[123]])
     # Valid value
     env_vars = get_kv_env_vars([["foo", {
         "moo": "bar"
     }], ("foo", "bar"), ["foo", 1]])
     assert env_vars[0].name == "foo"
     assert env_vars[0].value == '{"moo": "bar"}'
     assert env_vars[1].name == "foo"
     assert env_vars[1].value == "bar"
     assert env_vars[2].name == "foo"
     assert env_vars[2].value == "1"