def test_task_failure_with_upstream_secrets_doesnt_store_secret_value_and_recompute_if_necessary( client, ): @prefect.task(max_retries=2, retry_delay=timedelta(minutes=100)) def is_p_three(p): if p == 3: raise ValueError("No thank you.") return p with prefect.Flow("test", result=PrefectResult()) as f: p = prefect.tasks.secrets.PrefectSecret("p") res = is_p_three(p) with prefect.context(secrets=dict(p=3)): state = CloudFlowRunner(flow=f).run(return_tasks=[res]) assert state.is_running() assert isinstance(state.result[res], Retrying) assert state.result[res].cached_inputs["p"].location is None ## here we set the result of the secret to an empty result, ensuring ## it will get converted to a "true" result; ## we expect that the upstream value will actually get recomputed from context ## through the SecretResultHandler safe = SecretResult(p) state.result[p] = Success(result=safe) state.result[res].start_time = pendulum.now("utc") state.result[res].cached_inputs = dict(p=safe) with prefect.context(secrets=dict(p=4)): new_state = CloudFlowRunner(flow=f).run(return_tasks=[res], task_states=state.result) assert new_state.is_successful() assert new_state.result[res].result == 4
def test_reads_by_rerunning_task(self): task = PrefectSecret("foo") task.run = lambda *args, **kwargs: 42 result = SecretResult(task) result.location == "foo" new_result = result.read("foo") assert new_result.value == 42 new_result.location == "foo"
def test_load_results_from_upstream_reads_secret_results(self, cloud_api): secret_result = SecretResult( prefect.tasks.secrets.PrefectSecret(name="foo")) state = Success(result=PrefectResult(location="foo")) with prefect.context(secrets=dict(foo=42)): edge = Edge(Task(result=secret_result), 2, key="x") new_state, upstreams = CloudTaskRunner(task=Task()).load_results( state=Pending(), upstream_states={edge: state}) assert upstreams[edge].result == 42
def test_reads_with_new_name(self): task = PrefectSecret("foo") result = SecretResult(task) with prefect.context(secrets=dict(x=99, foo="bar")): res1 = result.read("x") res2 = result.read("foo") assert res1.value == 99 assert res1.location == "x" assert res2.value == "bar" assert res2.location == "foo"
def __init__(self, **kwargs): if kwargs.get("result"): raise ValueError("Result types for Secrets are not configurable.") kwargs["checkpoint"] = False super().__init__(**kwargs) self.result = SecretResult(secret_task=self)
def test_cant_write_to_secret_task(self): task = PrefectSecret("foo") result = SecretResult(task) with pytest.raises(ValueError): result.write("new")
def test_instantiates_with_task(self): task = PrefectSecret("foo") result = SecretResult(task) assert result.secret_task is task assert result.location == "foo"
def test_cant_pass_serializer_to_secret_result(self): with pytest.raises(ValueError, match="Can't pass a serializer"): SecretResult(PrefectSecret("foo"), serializer=None)