1 # Copyright (c) 2019 ARM Limited
4 # The license below extends only to copyright in the software and shall
5 # not be construed as granting a license to any other intellectual
6 # property including but not limited to intellectual property relating
7 # to a hardware implementation of the functionality of the software
8 # licensed hereunder. You may use the software subject to the license
9 # terms below provided that you ensure that this notice is replicated
10 # unmodified and in its entirety in all distributions of the software,
11 # modified or unmodified, in source code or in binary form.
13 # Copyright (c) 2017 Mark D. Hill and David A. Wood
14 # All rights reserved.
16 # Redistribution and use in source and binary forms, with or without
17 # modification, are permitted provided that the following conditions are
18 # met: redistributions of source code must retain the above copyright
19 # notice, this list of conditions and the following disclaimer;
20 # redistributions in binary form must reproduce the above copyright
21 # notice, this list of conditions and the following disclaimer in the
22 # documentation and/or other materials provided with the distribution;
23 # neither the name of the copyright holders nor the names of its
24 # contributors may be used to endorse or promote products derived from
25 # this software without specific prior written permission.
27 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 from testlib
.fixture
import Fixture
51 from testlib
.configuration
import config
, constants
52 from testlib
.helper
import log_call
, cacheresult
, joinpath
, absdirpath
53 import testlib
.log
as log
54 from testlib
.state
import Result
57 class VariableFixture(Fixture
):
58 def __init__(self
, value
=None, name
=None):
59 super(VariableFixture
, self
).__init
__(name
=name
)
63 class TempdirFixture(Fixture
):
66 super(TempdirFixture
, self
).__init
__(
67 name
=constants
.tempdir_fixture_name
)
69 def setup(self
, testitem
):
70 self
.path
= tempfile
.mkdtemp(prefix
='gem5out')
72 def post_test_procedure(self
, testitem
):
73 suiteUID
= testitem
.metadata
.uid
.suite
74 testUID
= testitem
.metadata
.name
75 testing_result_folder
= os
.path
.join(config
.result_path
,
76 "SuiteUID:" + suiteUID
,
79 # Copy the output files of the run from /tmp to testing-results
80 # We want to wipe the entire result folder for this test first. Why?
81 # If the result folder exists (probably from the previous run), if
82 # this run emits fewer files, there'll be files from the previous
83 # run in this folder, which would cause confusion if one does not
84 # check the timestamp of the file.
85 if os
.path
.exists(testing_result_folder
):
86 shutil
.rmtree(testing_result_folder
)
87 shutil
.copytree(self
.path
, testing_result_folder
)
89 def teardown(self
, testitem
):
90 if testitem
.result
== Result
.Passed
:
91 shutil
.rmtree(self
.path
)
93 class UniqueFixture(Fixture
):
95 Base class for fixtures that generate a target in the
96 filesystem. If the same fixture is used by more than one
97 test/suite, rather than creating a copy of the fixture, it returns
98 the same object and makes sure that setup is only executed
99 once. Devired classses should override the _init and _setup
102 :param target: The absolute path of the target in the filesystem.
107 def __new__(cls
, target
):
108 if target
in cls
.fixtures
:
109 obj
= cls
.fixtures
[target
]
111 obj
= super(UniqueFixture
, cls
).__new
__(cls
)
112 obj
.lock
= threading
.Lock()
114 cls
.fixtures
[target
] = obj
117 def __init__(self
, *args
, **kwargs
):
119 if hasattr(self
, '_init_done'):
121 super(UniqueFixture
, self
).__init
__(self
, **kwargs
)
122 self
._init
(*args
, **kwargs
)
123 self
._init
_done
= True
125 def setup(self
, testitem
):
127 if hasattr(self
, '_setup_done'):
129 self
._setup
_done
= True
130 self
._setup
(testitem
)
133 class SConsFixture(UniqueFixture
):
135 Fixture will wait until all SCons targets are collected and tests are
136 about to be ran, then will invocate a single instance of SCons for all
139 :param directory: The directory which scons will -C (cd) into before
140 executing. If None is provided, will choose the config base_dir.
143 def __new__(cls
, target
):
144 obj
= super(SConsFixture
, cls
).__new
__(cls
, target
)
147 def _setup(self
, testitem
):
148 if config
.skip_build
:
152 'scons', '-C', self
.directory
,
153 '-j', str(config
.threads
),
155 '--no-compress-debug'
160 'No SCons targets specified, this will'
161 ' build the default all target.\n'
162 'This is likely unintended, and you'
163 ' may wish to kill testlib and reconfigure.')
165 log
.test_log
.message(
166 'Building the following targets.'
167 ' This may take a while.')
168 log
.test_log
.message('%s' % (', '.join(self
.targets
)))
169 log
.test_log
.message(
170 "You may want to run with only a single ISA"
171 "(--isa=), use --skip-build, or use 'rerun'.")
173 command
.extend(self
.targets
)
175 command
.extend(self
.options
)
176 log_call(log
.test_log
, command
, time
=None, stderr
=sys
.stderr
)
178 class Gem5Fixture(SConsFixture
):
179 def __new__(cls
, isa
, variant
, protocol
=None):
180 target_dir
= joinpath(config
.build_dir
, isa
.upper())
182 target_dir
+= '_' + protocol
183 target
= joinpath(target_dir
, 'gem5.%s' % variant
)
184 obj
= super(Gem5Fixture
, cls
).__new
__(cls
, target
)
187 def _init(self
, isa
, variant
, protocol
=None):
188 self
.name
= constants
.gem5_binary_fixture_name
190 self
.targets
= [self
.target
]
191 self
.path
= self
.target
192 self
.directory
= config
.base_dir
196 self
.options
= [ '--default=' + isa
.upper(),
197 'PROTOCOL=' + protocol
]
200 class MakeFixture(Fixture
):
201 def __init__(self
, directory
, *args
, **kwargs
):
202 name
= 'make -C %s' % directory
203 super(MakeFixture
, self
).__init
__(build_once
=True, lazy_init
=False,
207 self
.directory
= directory
210 super(MakeFixture
, self
).setup()
211 targets
= set(self
.required_by
)
212 command
= ['make', '-C', self
.directory
]
213 command
.extend([target
.target
for target
in targets
])
214 log_call(log
.test_log
, command
, time
=None, stderr
=sys
.stderr
)
217 class MakeTarget(Fixture
):
218 def __init__(self
, target
, make_fixture
=None, *args
, **kwargs
):
220 :param make_fixture: The make invocation we will be attached to.
221 Since we don't have a single global instance of make in gem5 like we do
222 scons we need to know what invocation to attach to. If none given,
225 super(MakeTarget
, self
).__init
__(name
=target
, *args
, **kwargs
)
226 self
.target
= self
.name
228 if make_fixture
is None:
229 make_fixture
= MakeFixture(
234 self
.make_fixture
= make_fixture
236 # Add our self to the required targets of the main MakeFixture
237 self
.require(self
.make_fixture
)
239 def setup(self
, testitem
):
240 super(MakeTarget
, self
).setup()
241 self
.make_fixture
.setup()
244 class TestProgram(MakeTarget
):
245 def __init__(self
, program
, isa
, os
, recompile
=False):
246 make_dir
= joinpath(config
.bin_dir
, program
)
247 make_fixture
= MakeFixture(make_dir
)
248 target
= joinpath('bin', isa
, os
, program
)
249 super(TestProgram
, self
).__init
__(target
, make_fixture
)
250 self
.path
= joinpath(make_dir
, target
)
251 self
.recompile
= recompile
253 def setup(self
, testitem
):
254 # Check if the program exists if it does then only compile if
255 # recompile was given.
257 super(MakeTarget
, self
).setup()
258 elif not os
.path
.exists(self
.path
):
259 super(MakeTarget
, self
).setup()
261 class DownloadedProgram(UniqueFixture
):
262 """ Like TestProgram, but checks the version in the gem5 binary repository
263 and downloads an updated version if it is needed.
266 def __new__(cls
, url
, path
, filename
, gzip_decompress
=False):
267 target
= joinpath(path
, filename
)
268 return super(DownloadedProgram
, cls
).__new
__(cls
, target
)
270 def _init(self
, url
, path
, filename
, gzip_decompress
=False, **kwargs
):
273 The url of the archive
275 The absolute path of the directory containing the archive
277 The name of the archive
278 gzip_decompress: boolean
279 True if this target resource have been compressed using gzip and
280 is to be decompressed prior to usage.
285 self
.filename
= joinpath(path
, filename
)
286 self
.name
= "Downloaded:" + self
.filename
287 self
.gzip_decompress
= gzip_decompress
291 log
.test_log
.debug("Downloading " + self
.url
+ " to " + self
.path
)
292 if not os
.path
.exists(self
.path
):
294 os
.makedirs(self
.path
)
296 if e
.errno
!= errno
.EEXIST
:
298 if self
.gzip_decompress
:
299 gzipped_filename
= self
.filename
+ ".gz"
300 urllib
.request
.urlretrieve(self
.url
, gzipped_filename
)
302 with
open(self
.filename
, 'wb') as outfile
:
303 with gzip
.open(gzipped_filename
, 'r') as infile
:
304 shutil
.copyfileobj(infile
, outfile
)
306 os
.remove(gzipped_filename
)
308 urllib
.request
.urlretrieve(self
.url
, self
.filename
)
310 def _getremotetime(self
):
311 import datetime
, time
312 import _strptime
# Needed for python threading bug
314 u
= urllib
.request
.urlopen(self
.url
, timeout
=10)
316 return time
.mktime(datetime
.datetime
.strptime( \
317 u
.info()["Last-Modified"],
318 "%a, %d %b %Y %X GMT").timetuple())
320 def _setup(self
, testitem
):
321 # Check to see if there is a file downloaded
322 if not os
.path
.exists(self
.filename
):
326 t
= self
._getremotetime
()
327 except (urllib
.error
.URLError
, socket
.timeout
):
328 # Problem checking the server, use the old files.
329 log
.test_log
.debug("Could not contact server. Binaries may be old.")
331 # If the server version is more recent, download it
332 if t
> os
.path
.getmtime(self
.filename
):
335 class DownloadedArchive(DownloadedProgram
):
336 """ Like TestProgram, but checks the version in the gem5 binary repository
337 and downloads an updated version if it is needed.
342 with tarfile
.open(self
.filename
) as tf
:
343 tf
.extractall(self
.path
)
345 def _setup(self
, testitem
):
346 # Check to see if there is a file downloaded
347 if not os
.path
.exists(self
.filename
):
352 t
= self
._getremotetime
()
353 except (urllib
.error
.URLError
, socket
.timeout
):
354 # Problem checking the server, use the old files.
355 log
.test_log
.debug("Could not contact server. "
356 "Binaries may be old.")
358 # If the server version is more recent, download it
359 if t
> os
.path
.getmtime(self
.filename
):