diff --git a/src/python/gem5/resources/downloader.py b/src/python/gem5/resources/downloader.py index 0b67ecdebd..4a2ed5d332 100644 --- a/src/python/gem5/resources/downloader.py +++ b/src/python/gem5/resources/downloader.py @@ -154,8 +154,13 @@ def _get_resources_json() -> Dict: # If the current version pulled is not correct, look up the # "previous-versions" field to find the correct one. + # If the resource JSON file does not have a "version" field or it's + # null/None, then we will use this resource JSON file (this is usefull for + # testing purposes). version = _resources_json_version_required() - if to_return["version"] != version: + json_version = None if "version" not in to_return else to_return["version"] + + if json_version and json_version != version: if version in to_return["previous-versions"].keys(): to_return = _get_resources_json_at_path( path=to_return["previous-versions"][version] diff --git a/tests/pyunit/stdlib/resources/pyunit_workload_checks.py b/tests/pyunit/stdlib/resources/pyunit_workload_checks.py index fab0bbfbf1..2bc31f5a3f 100644 --- a/tests/pyunit/stdlib/resources/pyunit_workload_checks.py +++ b/tests/pyunit/stdlib/resources/pyunit_workload_checks.py @@ -25,7 +25,6 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import unittest -import tempfile import os from gem5.resources.workload import Workload, CustomWorkload @@ -34,7 +33,6 @@ from gem5.resources.resource import ( DiskImageResource, obtain_resource, ) -from gem5.resources.downloader import _resources_json_version_required from typing import Dict @@ -46,33 +44,12 @@ class CustomWorkloadTestSuite(unittest.TestCase): @classmethod def setUpClass(cls) -> None: - file_contents = ( - "{" - + f'"version" : "{_resources_json_version_required()}",' - + """ - "url_base" : "http://dist.gem5.org/dist/v22-0", - "previous-versions" : {}, - "resources": [ - { - "type" : "binary", - "name" : "x86-hello64-static", - "documentation" : "A 'Hello World!' binary.", - "architecture" : "X86", - "is_zipped" : false, - "md5sum" : "dbf120338b37153e3334603970cebd8c", - "url" : "{url_base}/test-progs/hello/bin/x86/linux/hello64-static", - "source" : "src/simple" - } - ] -} - """ - ) - file = tempfile.NamedTemporaryFile(mode="w", delete=False) - file.write(file_contents) - file.close() - cls.test_json = file.name - os.environ["GEM5_RESOURCE_JSON"] = cls.test_json + os.environ["GEM5_RESOURCE_JSON"] = os.path.join( + os.path.realpath(os.path.dirname(__file__)), + "refs", + "workload-checks-custom-workload.json", + ) cls.custom_workload = CustomWorkload( function="set_se_binary_workload", @@ -84,9 +61,8 @@ class CustomWorkloadTestSuite(unittest.TestCase): @classmethod def tearDownClass(cls): - # Remove the test json file and unset the environment variable so this - # test does not interfere with others. - os.remove(cls.test_json) + # Unset the environment variable so this test does not interfere with + # others. os.environ["GEM5_RESOURCE_JSON"] def test_get_function_str(self) -> None: @@ -149,67 +125,18 @@ class WorkloadTestSuite(unittest.TestCase): @classmethod def setUpClass(cls): - # In this constructor we create a json file to load then create a test - # workload. - file_contents = ( - "{" - + f'"version" : "{_resources_json_version_required()}",' - + """ - "url_base" : "http://dist.gem5.org/dist/v22-0", - "previous-versions" : {}, - "resources": [ - { - "type" : "kernel", - "name" : "x86-linux-kernel-5.2.3", - "documentation" : "The linux kernel (v5.2.3), compiled to X86.", - "architecture" : "X86", - "is_zipped" : false, - "md5sum" : "4838c99b77d33c8307b939c16624e4ac", - "url" : "{url_base}/kernels/x86/static/vmlinux-5.2.3", - "source" : "src/linux-kernel" - }, - { - "type" : "disk-image", - "name" : "x86-ubuntu-18.04-img", - "documentation" : "A disk image containing Ubuntu 18.04 for x86..", - "architecture" : "X86", - "is_zipped" : true, - "md5sum" : "90e363abf0ddf22eefa2c7c5c9391c49", - "url" : "{url_base}/images/x86/ubuntu-18-04/x86-ubuntu.img.gz", - "source" : "src/x86-ubuntu", - "root_partition": "1" - }, - { - "type" : "workload", - "name" : "simple-boot", - "documentation" : "Description of workload here", - "function" : "set_kernel_disk_workload", - "resources" : { - "kernel" : "x86-linux-kernel-5.2.3", - "disk_image" : "x86-ubuntu-18.04-img" - }, - "additional_params" : { - "readfile_contents" : "echo 'Boot successful'; m5 exit" - } - } - ] -} - """ + os.environ["GEM5_RESOURCE_JSON"] = os.path.join( + os.path.realpath(os.path.dirname(__file__)), + "refs", + "workload-checks.json", ) - file = tempfile.NamedTemporaryFile(mode="w", delete=False) - file.write(file_contents) - file.close() - - cls.test_json = file.name - os.environ["GEM5_RESOURCE_JSON"] = cls.test_json cls.workload = Workload("simple-boot") @classmethod def tearDownClass(cls): - # Remove the test json file and unset the environment variable so this - # test does not interfere with others. - os.remove(cls.test_json) + # Unset the environment variable so this test does not interfere with + # others. os.environ["GEM5_RESOURCE_JSON"] def test_get_function_str(self) -> None: diff --git a/tests/pyunit/stdlib/resources/refs/workload-checks-custom-workload.json b/tests/pyunit/stdlib/resources/refs/workload-checks-custom-workload.json new file mode 100644 index 0000000000..a9dd2aaa46 --- /dev/null +++ b/tests/pyunit/stdlib/resources/refs/workload-checks-custom-workload.json @@ -0,0 +1,17 @@ +{ + "version" : null, + "url_base" : "http://dist.gem5.org/dist/v22-0", + "previous-versions" : {}, + "resources": [ + { + "type" : "binary", + "name" : "x86-hello64-static", + "documentation" : "A 'Hello World!' binary.", + "architecture" : "X86", + "is_zipped" : false, + "md5sum" : "dbf120338b37153e3334603970cebd8c", + "url" : "{url_base}/test-progs/hello/bin/x86/linux/hello64-static", + "source" : "src/simple" + } + ] +} diff --git a/tests/pyunit/stdlib/resources/refs/workload-checks.json b/tests/pyunit/stdlib/resources/refs/workload-checks.json new file mode 100644 index 0000000000..4f7e76bfb5 --- /dev/null +++ b/tests/pyunit/stdlib/resources/refs/workload-checks.json @@ -0,0 +1,40 @@ +{ + "url_base" : "http://dist.gem5.org/dist/v22-0", + "previous-versions" : {}, + "resources": [ + { + "type" : "kernel", + "name" : "x86-linux-kernel-5.2.3", + "documentation" : "The linux kernel (v5.2.3), compiled to X86.", + "architecture" : "X86", + "is_zipped" : false, + "md5sum" : "4838c99b77d33c8307b939c16624e4ac", + "url" : "{url_base}/kernels/x86/static/vmlinux-5.2.3", + "source" : "src/linux-kernel" + }, + { + "type" : "disk-image", + "name" : "x86-ubuntu-18.04-img", + "documentation" : "A disk image containing Ubuntu 18.04 for x86..", + "architecture" : "X86", + "is_zipped" : true, + "md5sum" : "90e363abf0ddf22eefa2c7c5c9391c49", + "url" : "{url_base}/images/x86/ubuntu-18-04/x86-ubuntu.img.gz", + "source" : "src/x86-ubuntu", + "root_partition": "1" + }, + { + "type" : "workload", + "name" : "simple-boot", + "documentation" : "Description of workload here", + "function" : "set_kernel_disk_workload", + "resources" : { + "kernel" : "x86-linux-kernel-5.2.3", + "disk_image" : "x86-ubuntu-18.04-img" + }, + "additional_params" : { + "readfile_contents" : "echo 'Boot successful'; m5 exit" + } + } + ] +}