diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000000..95561cf18c --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,64 @@ +##############################################################################bl +# MIT License +# +# Copyright (c) 2021 - 2023 Advanced Micro Devices, Inc. All Rights Reserved. +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +##############################################################################el +# Common helper routines for testing collateral + + +import inspect +import os +import shutil + + +def check_resource_allocation(): + """Check if CTEST resource allocation is enabled for parallel testing and set + HIP_VISIBLE_DEVICES variable accordingly with assigned gpu index. + """ + + if "CTEST_RESOURCE_GROUP_COUNT" not in os.environ: + return + + if "CTEST_RESOURCE_GROUP_0_GPUS" in os.environ: + resource = os.environ["CTEST_RESOURCE_GROUP_0_GPUS"] + # extract assigned gpu id from env var: example format -> 'id:0,slots:1' + for item in resource.split(","): + key, value = item.split(":") + if key == "id": + os.environ["HIP_VISIBLE_DEVICES"] = value + return + + return + + +def get_output_dir(suffix="_output", clean_existing=True): + """Provides a unique output directory based on the name of the calling test function with a suffix applied. + + Args: + suffix (str, optional): suffix to append to output_dir. Defaults to "_output". + clean_existing (bool, optional): Whether to remove existing directory if exists. Defaults to True. + """ + + output_dir = inspect.stack()[1].function + suffix + if clean_existing: + if os.path.exists(output_dir): + shutil.rmtree(output_dir) + return output_dir