Files
gem5/tests/pyunit/stdlib/resources/pyunit_downloader_checks.py
Kunal Pai ffbf73db1d stdlib, tests, configs: Introduce gem5 Vision to resources
This patch makes changes to the stdlib based on the gem5 Vision project.
Firstly, a MongoDB database is supported.
A JSON database's support is continued.
The JSON can either be a local path or a raw GitHub link.

The data for these databases is stored in src/python
under "gem5-config.json".
This will be used by default.
However, the configuration can be overridden:
- by providing a path using the GEM5_CONFIG env variable.
- by placing a gem5-config.json file in the current working directory.

An AbstractClient is an abstract class that implements
searching and sorting relevant to the databases.

Clients is an optional list that can be passed
while defining any Resource class and obtain_resource.
These databases can be defined in the config JSON.

Resources now have versions. This allows for a
single version, e.g., 'x86-ubuntu-boot', to have
multiple versions. As such, the key of a resource is
its ID and Version (e.g., 'x86-ubuntu-boot/v2.1.0').
Different versions of a resource might be compatible
with different versions of gem5.

By default, it picks the latest version compatible with the gem5 Version
of the user.

A gem5 resource schema now has additional fields.
These are:
- source_url: Stores URL of GitHub Source of the resource.
- license: License information of the resource.
- tags: Words to identify a resource better, like hello for hello-world
- example_usage: How to use the resource in a simulation.
- gem5_versions: List of gem5 versions that resource is compatible with.
- resource_version: The version of the resource itself.
- size: The download size of the resource, if it exists.
- code_examples: List of objects.
These objects contain the path to where a resource is
used in gem5 example config scripts,
and if the resource itself is used in tests or not.
- category: Category of the resource, as defined by classes in
src/python/gem5/resources/resource.py.

Some fields have been renamed:
- "name" is changed to "id"
- "documentation" is changed to "description"

Besides these, the schema also supports resource specialization.
It adds fields relevant to a specific resource as specified in
src/python/gem5/resources/resource.py
These changes have been made to better present
information on the new gem5 Resources website.

But, they do not affect the way resources are used by a gem5 user.
This patch is also backwards compatible.
Existing code doesn't break with this new infrastructure.

Also, refs in the tests have been changed to match this new schema.
Tests have been changed to work with the two clients.

Change-Id: Ia9bf47f7900763827fd5e873bcd663cc3ecdba40
Co-authored-by: Kunal Pai <kunpai@ucdavis.edu>
Co-authored-by: Parth Shah <helloparthshah@gmail.com>
Co-authored-by: Harshil Patel <harshilp2107@gmail.com>
Co-authored-by: aarsli <arsli@ucdavis.edu>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/70858
Tested-by: kokoro <noreply+kokoro@google.com>
Maintainer: Jason Lowe-Power <power.jg@gmail.com>
Reviewed-by: Jason Lowe-Power <power.jg@gmail.com>
2023-06-06 03:30:50 +00:00

199 lines
8.5 KiB
Python

# Copyright (c) 2023 The Regents of the University of California
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met: redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer;
# redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution;
# neither the name of the copyright holders nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import unittest
import tempfile
import os
from typing import Dict
import json
from gem5.resources.downloader import (
_get_resources_json_at_path,
_get_resources_json,
_resources_json_version_required,
)
class ResourceDownloaderTestSuite(unittest.TestCase):
"""Test cases for gem5.resources.downloader"""
@classmethod
def setUpClass(cls) -> str:
"""
This creates a simple resources collection for testing
"""
file_contents = [
{
"category": "binary",
"id": "this-is-a-test-resource",
"description": "This is a test resource",
"architecture": "X86",
"size": 13816,
"tags": ["asmtest", "testing", "riscv", "testing"],
"is_zipped": False,
"md5sum": "4e70a98b6976969deffff91eed17fba1",
"source": "src/asmtest",
"url": "http://dist.gem5.org/dist/develop/test-progs/asmtest/bin/rv64mi-p-sbreak",
"code_examples": [],
"license": " BSD-3-Clause",
"author": [],
"source_url": "https://github.com/gem5/gem5-resources/tree/develop/src/asmtest",
"resource_version": "1.0.0",
"gem5_versions": ["23.0"],
"example_usage": 'get_resource(resource_name="rv64mi-p-sbreak")',
},
{
"category": "binary",
"id": "this-is-a-test-resource",
"description": "This is a test resource but double newer",
"architecture": "X86",
"size": 13816,
"tags": ["asmtest"],
"is_zipped": False,
"md5sum": "4e70a98b6976969deffff91eed17fba1",
"source": "src/asmtest",
"url": "http://dist.gem5.org/dist/develop/test-progs/asmtest/bin/rv64mi-p-sbreak",
"code_examples": [],
"license": " BSD-3-Clause",
"author": [],
"source_url": "https://github.com/gem5/gem5-resources/tree/develop/src/asmtest",
"resource_version": "2.0.0",
"gem5_versions": ["23.1"],
"example_usage": 'get_resource(resource_name="rv64mi-p-sbreak")',
},
{
"category": "simpoint",
"id": "test-version",
"description": "Simpoints for running the 'x86-print-this' resource with the parameters `\"print this\" 15000`. This is encapsulated in the 'x86-print-this-15000-with-simpoints' workload.",
"architecture": "X86",
"size": 10240,
"tags": [],
"is_zipped": False,
"md5sum": "3fcffe3956c8a95e3fb82e232e2b41fb",
"is_tar_archive": True,
"url": "http://dist.gem5.org/dist/develop/simpoints/x86-print-this-15000-simpoints-20221013.tar",
"simpoint_interval": 1000000,
"warmup_interval": 1000000,
"code_examples": [],
"license": "",
"author": [],
"source_url": "",
"resource_version": "1.0.0",
"gem5_versions": ["23.0"],
"workload_name": "x86-print-this-15000-with-simpoints",
"example_usage": 'get_resource(resource_name="x86-print-this-1500-simpoints")',
"workloads": [
"x86-print-this-15000-with-simpoints",
"x86-print-this-15000-with-simpoints-and-checkpoint",
],
},
{
"category": "file",
"id": "test-version",
"description": "Simpoints for running the 'x86-print-this' resource with the parameters `\"print this\" 15000`. This is encapsulated in the 'x86-print-this-15000-with-simpoints' workload.",
"architecture": "X86",
"size": 10240,
"tags": [],
"is_zipped": False,
"md5sum": "3fcffe3956c8a95e3fb82e232e2b41fb",
"is_tar_archive": True,
"url": "http://dist.gem5.org/dist/develop/simpoints/x86-print-this-15000-simpoints-20221013.tar",
"simpoint_interval": 1000000,
"warmup_interval": 1000000,
"code_examples": [],
"license": "",
"author": [],
"source_url": "",
"resource_version": "0.2.0",
"gem5_versions": ["23.0"],
"workload_name": "x86-print-this-15000-with-simpoints",
"example_usage": 'get_resource(resource_name="x86-print-this-1500-simpoints")',
"workloads": [
"x86-print-this-15000-with-simpoints",
"x86-print-this-15000-with-simpoints-and-checkpoint",
],
},
]
file = tempfile.NamedTemporaryFile(mode="w", delete=False)
file.write(json.dumps(file_contents))
file.close()
cls.file_path = file.name
os.environ["GEM5_RESOURCE_JSON"] = cls.file_path
@classmethod
def tearDownClass(cls) -> None:
os.remove(cls.file_path)
del os.environ["GEM5_RESOURCE_JSON"]
def verify_json(self, json: Dict) -> None:
"""
This verifies the JSON file created in created in
"create_temp_resources_json" has been loaded correctly into a Python
dictionary.
"""
self.assertEquals(4, len(json))
self.assertTrue("id" in json[0])
self.assertEquals("this-is-a-test-resource", json[0]["id"])
self.assertEquals("binary", json[0]["category"])
self.assertTrue("id" in json[1])
self.assertEquals("this-is-a-test-resource", json[1]["id"])
self.assertTrue("id" in json[2])
self.assertEquals("test-version", json[2]["id"])
self.assertTrue("id" in json[3])
self.assertEquals("test-version", json[3]["id"])
def test_get_resources_json_at_path(self) -> None:
# Tests the gem5.resources.downloader._get_resources_json_at_path()
# function.
json = _get_resources_json_at_path(path=self.file_path)
self.verify_json(json=json)
def test_get_resources_json(self) -> None:
# Tests the gem5.resources.downloader._get_resources_json() function.
json = _get_resources_json()
self.verify_json(json=json)
def test_get_resources_json_invalid_url(self) -> None:
# Tests the gem5.resources.downloader._get_resources_json() function in
# case where an invalid url is passed as the URL/PATH of the
# resources.json file.
path = "NotAURLorFilePath"
os.environ["GEM5_RESOURCE_JSON"] = path
with self.assertRaises(Exception) as context:
_get_resources_json()
self.assertTrue(
f"Resources location '{path}' is not a valid path or URL."
in str(context.exception)
)
# Set back to the old path
os.environ["GEM5_RESOURCE_JSON"] = self.file_path