Files
gem5/tests/pyunit/stdlib/resources/pyunit_obtain_resources_check.py
Bobby R. Bruce 82587ce71b stdlib: Refactor gem5 Vision/gem5-resources code
This patch includes several changes to the gem5 tools interface to the
gem5-resources infrastructure. These are:

* The old download and JSON query functions have been removed from the
  downloader module. These functions were used for directly downloading
  and inspecting the resource JSON file, hosted at
  https://resources.gem5.org/resources. This information is now obtained
  via `gem5.client`. If a resources JSON file is specified as a client,
  it should conform to the new schema:
  https//resources.gem5.org/gem5-resources-schema.json. The old schema
  (pre-v23.0) is no longer valid. Tests have been updated to reflect
  this change. Those which tested these old functions have been removed.
* Unused imports have been removed.
* For the resource query functions, and those tasked with obtaining the
  resources, the parameter `gem5_version` has been added. In all cases
  it does the same thing:
    * It will filter results based on compatibility to the
      `gem5_version` specified. If no resources are compatible the
      latest version of that resource is chosen (though a warning is
      thrown).
    * By default it is set to the current gem5 version.
    * It is optional. If `None`, this filtering functionality is not
      carried out.
    * Tests have been updated to fix the version to “develop” so the
      they do not break between versions.
* The `gem5_version` parameters will filter using a logic which will
  base compatibility on the specificity of the gem5-version specified in
  a resource’s data. If a resource has a compatible gem5-version of
  “v18.4” it will be compatible with any minor/hotfix version within the
  v18.4 release (this can be seen as matching on “v18.4.*.*”.) Likewise,
  if a resource has a compatible gem5-version of “v18.4.1” then it’s
  only compatible with the v18.4.1 release but any of it’s hot fix
  releases (“v18.4.1.*”).
* The ‘list_resources’ function has been updated to use the
  “gem5.client” APIs to get resource information from the clients
  (MongoDB or a JSON file). This has been designed to remain backwards
  compatible to as much as is possible, though, due to schema changes,
  the function does search across all versions of gem5.
* `get_resources` function was added to the `AbstractClient`. This is a
   more general function than `get_resource_by_id`. It was
  primarily created to handle the `list_resources` update but is a
  useful update to the API. The `get_resource_by_id` function has been
  altered to function as a wrapped to the `get_resources` function.
* Removed “GEM5_RESOURCE_JSON” code has been removed. This is no longer
  used.
* Tests have been cleaned up a little bit to be easier to read.
* Some docstrings have been updated.

Things that are left TODO with this code:

* The client_wrapper/client/abstract_client abstractions are rather
  pointless. In particular the client_wrapper and client classes could
  be merged.
* The downloader module no longer does much and should have its
  functions merged into other modules.
* With the addition of the `get_resources` function, much of the code in
  the `AbstractClient` could be simplified.

Change-Id: I0ce48e88b93a2b9db53d4749861fa0b5f9472053
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/71506
Reviewed-by: Kunal Pai <kunpai@ucdavis.edu>
Maintainer: Jason Lowe-Power <power.jg@gmail.com>
Tested-by: kokoro <noreply+kokoro@google.com>
2023-06-15 20:14:55 +00:00

172 lines
6.8 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 os
import io
import contextlib
from pathlib import Path
from gem5.resources.resource import obtain_resource, BinaryResource
from gem5.isas import ISA
from _m5 import core
from gem5.resources.client_api.client_wrapper import ClientWrapper
from unittest.mock import patch
mock_json_path = Path(__file__).parent / "refs/obtain-resource.json"
mock_config_json = {
"sources": {
"baba": {
"url": mock_json_path,
"isMongo": False,
}
},
}
@patch(
"gem5.resources.client.clientwrapper",
new=ClientWrapper(mock_config_json),
)
class TestObtainResourcesCheck(unittest.TestCase):
def get_resource_dir(cls) -> str:
"""To ensure the resources are cached to the same directory as all
other tests, this function returns the location of the testing
directories "resources" directory.
"""
return os.path.join(
os.path.realpath(os.path.dirname(__file__)),
os.pardir,
os.pardir,
os.pardir,
"gem5",
"resources",
)
def test_obtain_resources_no_version(self):
"""Test that the resource loader returns latest version compatible with that version of gem5 when no version is specified."""
gem5Version = core.gem5Version
resource = obtain_resource(
resource_id="test-binary-resource",
resource_directory=self.get_resource_dir(),
gem5_version="develop",
)
self.assertEquals("1.7.0", resource.get_resource_version())
self.assertIsInstance(resource, BinaryResource)
self.assertEquals(
"test description v1.7.0", resource.get_description()
)
self.assertEquals("src/test-source", resource.get_source())
self.assertEquals(ISA.ARM, resource.get_architecture())
def test_obtain_resources_with_version_compatible(self):
resource = obtain_resource(
resource_id="test-binary-resource",
resource_directory=self.get_resource_dir(),
resource_version="1.5.0",
gem5_version="develop",
)
self.assertEquals("1.5.0", resource.get_resource_version())
self.assertIsInstance(resource, BinaryResource)
self.assertEquals(
"test description for 1.5.0", resource.get_description()
)
self.assertEquals("src/test-source", resource.get_source())
self.assertEquals(ISA.ARM, resource.get_architecture())
def test_obtain_resources_with_version_incompatible(self):
resource = None
f = io.StringIO()
with contextlib.redirect_stderr(f):
resource = obtain_resource(
resource_id="test-binary-resource",
resource_directory=self.get_resource_dir(),
resource_version="1.5.0",
)
self.assertTrue(
f"warn: Resource test-binary-resource with version 1.5.0 is not known to be compatible with gem5 version {core.gem5Version}. "
"This may cause problems with your simulation. This resource's compatibility with different gem5 versions can be found here: "
f"https://resources.gem5.org/resources/test-binary-resource/versions"
in f.getvalue()
)
resource = obtain_resource(
resource_id="test-binary-resource",
resource_directory=self.get_resource_dir(),
resource_version="1.5.0",
gem5_version="develop",
)
self.assertEquals("1.5.0", resource.get_resource_version())
self.assertIsInstance(resource, BinaryResource)
self.assertEquals(
"test description for 1.5.0", resource.get_description()
)
self.assertEquals("src/test-source", resource.get_source())
self.assertEquals(ISA.ARM, resource.get_architecture())
def test_obtain_resources_no_version_invalid_id(self):
with self.assertRaises(Exception) as context:
obtain_resource(
resource_id="invalid-id",
resource_directory=self.get_resource_dir(),
gem5_version="develop",
)
self.assertTrue(
"Resource with ID 'invalid-id' not found."
in str(context.exception)
)
def test_obtain_resources_with_version_invalid_id(self):
with self.assertRaises(Exception) as context:
obtain_resource(
resource_id="invalid-id",
resource_directory=self.get_resource_dir(),
resource_version="1.7.0",
gem5_version="develop",
)
self.assertTrue(
"Resource with ID 'invalid-id' not found."
in str(context.exception)
)
def test_obtain_resources_with_version_invalid_version(self):
with self.assertRaises(Exception) as context:
obtain_resource(
resource_id="test-binary-resource",
resource_directory=self.get_resource_dir(),
resource_version="3.0.0",
)
self.assertTrue(
f"Resource test-binary-resource with version '3.0.0'"
" not found.\nResource versions can be found at: "
f"https://resources.gem5.org/resources/test-binary-resource/versions"
in str(context.exception)
)