From a9b69ee055d7b82ba5da0e4dbfaecd42be41de6b Mon Sep 17 00:00:00 2001 From: "Bobby R. Bruce" Date: Thu, 19 Jan 2023 14:30:19 +0000 Subject: [PATCH] stdlib: Add null/None versioning in resources.json This patch allows for the "version" field in the resources.json file to be `null` (translated to `None` in the Python JSON package) or not declared. In this case the resources.json file will be used regardless as to what version the gem5 binary is set. This is useful for testing purposes. Tests have been updated to utilize this where possible. Change-Id: I9d8ae18cb3e61d58bc822bad30853fa3442cb33f Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/67337 Maintainer: Jason Lowe-Power Tested-by: kokoro Reviewed-by: Jason Lowe-Power --- src/python/gem5/resources/downloader.py | 7 +- .../resources/pyunit_workload_checks.py | 99 +++---------------- .../refs/workload-checks-custom-workload.json | 17 ++++ .../resources/refs/workload-checks.json | 40 ++++++++ 4 files changed, 76 insertions(+), 87 deletions(-) create mode 100644 tests/pyunit/stdlib/resources/refs/workload-checks-custom-workload.json create mode 100644 tests/pyunit/stdlib/resources/refs/workload-checks.json 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" + } + } + ] +}