blob: 8081fa21bd976e04d689acb7d3753703ffb40935 [file] [log] [blame]
Daniel Arndt0a881a32023-07-04 18:54:42 -03001#######################################################################################
2# Copyright ETSI Contributors and Others.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13# implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17from contextlib import contextmanager
18from typing import Type
19import unittest
20from temporalio.client import WorkflowFailureError
21from temporalio.exceptions import RetryState
22
23from osm_lcm.temporal.utils import get_root_cause
24
25
26@contextmanager
27def validate_workflow_failure_error_type(
28 test_case: unittest.TestCase, cause_type: Type[Exception]
29):
30 """Validates that the workflow failed with the given cause type.
31
32 args:
33 cause_type: The type of the exception that caused the workflow to fail.
34 """
35 with test_case.assertRaises(WorkflowFailureError) as e_info:
36 yield e_info
37 exception = e_info.exception
38 test_case.assertNotEquals(
39 exception.cause.retry_state, # type: ignore
40 RetryState.TIMEOUT,
41 "Workflow timed out. You may need to increase the execution timeout",
42 )
43 exception = get_root_cause(exception)
44 test_case.assertEqual(exception.type, cause_type.__name__) # type: ignore