|
| 1 | +# Copyright (c) ZenML GmbH 2024. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at: |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 12 | +# or implied. See the License for the specific language governing |
| 13 | +# permissions and limitations under the License. |
| 14 | + |
| 15 | +from typing import Callable |
| 16 | +from uuid import UUID |
| 17 | + |
| 18 | +import pytest |
| 19 | +from typing_extensions import Annotated |
| 20 | + |
| 21 | +from zenml import pipeline, step |
| 22 | +from zenml.client import Client |
| 23 | +from zenml.enums import ModelStages |
| 24 | +from zenml.model.model import Model |
| 25 | + |
| 26 | + |
| 27 | +@step(enable_cache=True) |
| 28 | +def simple_producer_step() -> Annotated[int, "trackable_artifact"]: |
| 29 | + return 42 |
| 30 | + |
| 31 | + |
| 32 | +@step(enable_cache=False) |
| 33 | +def keep_pipeline_alive() -> None: |
| 34 | + pass |
| 35 | + |
| 36 | + |
| 37 | +@pipeline |
| 38 | +def cacheable_pipeline_which_always_run(): |
| 39 | + simple_producer_step() |
| 40 | + keep_pipeline_alive() |
| 41 | + |
| 42 | + |
| 43 | +@pipeline |
| 44 | +def cacheable_pipeline_which_can_be_fully_cached(): |
| 45 | + simple_producer_step() |
| 46 | + |
| 47 | + |
| 48 | +@pipeline |
| 49 | +def cacheable_pipeline_where_second_step_is_cached(): |
| 50 | + simple_producer_step(id="simple_producer_step_1") |
| 51 | + simple_producer_step(id="simple_producer_step_2") |
| 52 | + |
| 53 | + |
| 54 | +def _validate_artifacts_state( |
| 55 | + clean_client: Client, |
| 56 | + pr_id: UUID, |
| 57 | + producer_pr_id: UUID, |
| 58 | + expected_version: int, |
| 59 | + step_name: str = "simple_producer_step", |
| 60 | + artifact_name: str = "trackable_artifact", |
| 61 | +): |
| 62 | + pr = clean_client.get_pipeline_run(pr_id) |
| 63 | + outputs_1 = pr.steps[step_name].outputs |
| 64 | + step = clean_client.get_run_step(pr.steps[step_name].id) |
| 65 | + outputs_2 = step.outputs |
| 66 | + for outputs in [outputs_1, outputs_2]: |
| 67 | + assert len(outputs) == 1 |
| 68 | + assert int(outputs[artifact_name].version) == expected_version |
| 69 | + # producer ID is always the original PR |
| 70 | + assert ( |
| 71 | + outputs[artifact_name].producer_pipeline_run_id == producer_pr_id |
| 72 | + ) |
| 73 | + |
| 74 | + artifact = clean_client.get_artifact_version(artifact_name) |
| 75 | + assert artifact.name == artifact_name |
| 76 | + assert int(artifact.version) == expected_version |
| 77 | + # producer ID is always the original PR |
| 78 | + assert artifact.producer_pipeline_run_id == producer_pr_id |
| 79 | + |
| 80 | + |
| 81 | +# TODO: remove clean client, ones clean env for REST is available |
| 82 | +@pytest.mark.parametrize( |
| 83 | + "pipeline", |
| 84 | + [ |
| 85 | + cacheable_pipeline_which_always_run, |
| 86 | + cacheable_pipeline_which_can_be_fully_cached, |
| 87 | + ], |
| 88 | +) |
| 89 | +def test_that_cached_artifact_versions_are_created_properly( |
| 90 | + pipeline: Callable, clean_client: Client |
| 91 | +): |
| 92 | + pr_orig = pipeline() |
| 93 | + |
| 94 | + _validate_artifacts_state( |
| 95 | + clean_client=clean_client, |
| 96 | + pr_id=pr_orig.id, |
| 97 | + producer_pr_id=pr_orig.id, |
| 98 | + expected_version=1, |
| 99 | + ) |
| 100 | + |
| 101 | + pr = pipeline() |
| 102 | + |
| 103 | + pr = clean_client.get_pipeline_run(pr.id) |
| 104 | + _validate_artifacts_state( |
| 105 | + clean_client=clean_client, |
| 106 | + pr_id=pr.id, |
| 107 | + producer_pr_id=pr_orig.id, |
| 108 | + expected_version=1, # cached artifact doesn't produce new version |
| 109 | + ) |
| 110 | + |
| 111 | + |
| 112 | +# TODO: remove clean client, ones clean env for REST is available |
| 113 | +def test_that_cached_artifact_versions_are_created_properly_for_second_step( |
| 114 | + clean_client: Client, |
| 115 | +): |
| 116 | + pr_orig = cacheable_pipeline_where_second_step_is_cached() |
| 117 | + |
| 118 | + _validate_artifacts_state( |
| 119 | + clean_client=clean_client, |
| 120 | + pr_id=pr_orig.id, |
| 121 | + producer_pr_id=pr_orig.id, |
| 122 | + step_name="simple_producer_step_1", |
| 123 | + expected_version=1, |
| 124 | + ) |
| 125 | + _validate_artifacts_state( |
| 126 | + clean_client=clean_client, |
| 127 | + pr_id=pr_orig.id, |
| 128 | + producer_pr_id=pr_orig.id, |
| 129 | + step_name="simple_producer_step_2", |
| 130 | + expected_version=1, |
| 131 | + ) |
| 132 | + |
| 133 | + pr = cacheable_pipeline_where_second_step_is_cached() |
| 134 | + |
| 135 | + pr = clean_client.get_pipeline_run(pr.id) |
| 136 | + _validate_artifacts_state( |
| 137 | + clean_client=clean_client, |
| 138 | + pr_id=pr.id, |
| 139 | + producer_pr_id=pr_orig.id, |
| 140 | + step_name="simple_producer_step_1", |
| 141 | + expected_version=1, # cached artifact doesn't produce new version |
| 142 | + ) |
| 143 | + _validate_artifacts_state( |
| 144 | + clean_client=clean_client, |
| 145 | + pr_id=pr.id, |
| 146 | + producer_pr_id=pr_orig.id, |
| 147 | + step_name="simple_producer_step_2", |
| 148 | + expected_version=1, # cached artifact doesn't produce new version |
| 149 | + ) |
| 150 | + |
| 151 | + |
| 152 | +def test_that_cached_artifact_versions_are_created_properly_for_model_version( |
| 153 | + clean_client: Client, |
| 154 | +): |
| 155 | + pr_orig = cacheable_pipeline_which_always_run.with_options( |
| 156 | + model=Model(name="foo") |
| 157 | + )() |
| 158 | + |
| 159 | + mv = clean_client.get_model_version("foo", ModelStages.LATEST) |
| 160 | + assert ( |
| 161 | + mv.data_artifacts["trackable_artifact"]["1"].producer_pipeline_run_id |
| 162 | + == pr_orig.id |
| 163 | + ) |
| 164 | + |
| 165 | + cacheable_pipeline_which_always_run.with_options(model=Model(name="foo"))() |
| 166 | + |
| 167 | + mv = clean_client.get_model_version("foo", ModelStages.LATEST) |
| 168 | + assert ( |
| 169 | + mv.data_artifacts["trackable_artifact"]["1"].producer_pipeline_run_id |
| 170 | + == pr_orig.id |
| 171 | + ) |
0 commit comments