| Daniel Arndt | 0a881a3 | 2023-07-04 18:54:42 -0300 | [diff] [blame] | 1 | ####################################################################################### |
| 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 | |
| 17 | from contextlib import contextmanager |
| 18 | from typing import Type |
| 19 | import unittest |
| 20 | from temporalio.client import WorkflowFailureError |
| 21 | from temporalio.exceptions import RetryState |
| 22 | |
| 23 | from osm_lcm.temporal.utils import get_root_cause |
| 24 | |
| 25 | |
| 26 | @contextmanager |
| 27 | def 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 |