tests: Add tests for the resource downloader
Change-Id: Ibc2685670dc50234b9b216226c03ed24c8bc9925 Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/59089 Maintainer: Bobby Bruce <bbruce@ucdavis.edu> Reviewed-by: Bobby Bruce <bbruce@ucdavis.edu> Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
committed by
Bobby Bruce
parent
e552295620
commit
cc6aa4c173
139
tests/pyunit/stdlib/resources/pyunit_downloader_checks.py
Normal file
139
tests/pyunit/stdlib/resources/pyunit_downloader_checks.py
Normal file
@@ -0,0 +1,139 @@
|
||||
# Copyright (c) 2022 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
|
||||
|
||||
from gem5.resources.downloader import(
|
||||
_get_resources_json_at_path,
|
||||
_get_resources_json,
|
||||
_resources_json_version_required,
|
||||
)
|
||||
|
||||
class MD5FileTestSuite(unittest.TestCase):
|
||||
"""Test cases for gem5.resources.downloader"""
|
||||
|
||||
def create_temp_resources_json(self) -> str:
|
||||
"""
|
||||
This creates a simple resource.json temp file for testing purposes.
|
||||
"""
|
||||
|
||||
file_contents = \
|
||||
"{" + f"\"version\" : \"{_resources_json_version_required()}\"," \
|
||||
+ """
|
||||
"url_base" : "http://dist.gem5.org/dist/v21-2",
|
||||
"previous-versions" : {},
|
||||
"resources": [
|
||||
{
|
||||
"type": "resource",
|
||||
"name" : "riscv-disk-img",
|
||||
"documentation" : "A simple RISCV disk image based on busybox.",
|
||||
"architecture": "RISCV",
|
||||
"is_zipped" : true,
|
||||
"md5sum" : "d6126db9f6bed7774518ae25aa35f153",
|
||||
"url": "{url_base}/images/riscv/busybox/riscv-disk.img.gz",
|
||||
"source" : "src/riscv-fs",
|
||||
"additional_metadata" : {
|
||||
"root_partition": null
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "resource",
|
||||
"name" : "riscv-lupio-busybox-img",
|
||||
"documentation" : "A RISCV disk image, based on busybox, to ...",
|
||||
"architecture": "RISCV",
|
||||
"is_zipped" : true,
|
||||
"md5sum" : "e5bee8a31f45f4803f87c0d781553ccc",
|
||||
"url": "{url_base}/images/riscv/busybox/riscv-lupio-busybox.img",
|
||||
"source" : "src/lupv",
|
||||
"additional_metadata" : {
|
||||
"root_partition": "1"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
"""
|
||||
file = tempfile.NamedTemporaryFile(mode="w", delete=False)
|
||||
file.write(file_contents)
|
||||
file.close()
|
||||
return file.name
|
||||
|
||||
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.assertTrue("resources" in json)
|
||||
self.assertEquals(2, len(json["resources"]))
|
||||
self.assertTrue("name" in json["resources"][0])
|
||||
self.assertEquals("riscv-disk-img", json["resources"][0]["name"])
|
||||
self.assertTrue("name" in json["resources"][1])
|
||||
self.assertEquals("riscv-lupio-busybox-img",
|
||||
json["resources"][1]["name"])
|
||||
|
||||
def test_get_resources_json_at_path(self) -> None:
|
||||
# Tests the gem5.resources.downloader._get_resources_json_at_path()
|
||||
# function.
|
||||
|
||||
path = self.create_temp_resources_json()
|
||||
json = _get_resources_json_at_path(path = path)
|
||||
|
||||
self.verify_json(json=json)
|
||||
|
||||
# Cleanup the temp file
|
||||
os.remove(path)
|
||||
|
||||
def test_get_resources_json(self) -> None:
|
||||
# Tests the gem5.resources.downloader._get_resources_json() function.
|
||||
|
||||
path = self.create_temp_resources_json()
|
||||
|
||||
# We set the "GEM5_RESOURCE_JSON" environment variable to allow using
|
||||
# our test temp resources.json.
|
||||
os.environ["GEM5_RESOURCE_JSON"] = path
|
||||
json = _get_resources_json()
|
||||
self.verify_json(json=json)
|
||||
|
||||
# Cleanup the temp file
|
||||
os.remove(path)
|
||||
|
||||
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)
|
||||
)
|
||||
Reference in New Issue
Block a user