Skip to content

Commit e8d5279

Browse files
machshevhcallahan-lowrisc
authored andcommitted
test: add initial CompileSim unittest
Add a `new()` static method to allow tests to test against the desired interface. The intention is to convert the deployment classes to Pydantic typed data classes. The deployment classes seem to be builders of fairly simple data classes once the construction process has been completed. The constructors take the data in other formats and then filter and transform to result in a new data class (deployment object). Pydantic data class constructors take in the final data, so we need a function to take the original data and do the transform. Having a `new()` static method is a fairly standard convention in languages such as Rust. For the moment this method just delegates to the existing constructor. The `CompileSim` unittests make use of this new method instead of the constructor directly. Which means they will be able to test the new code as well as the old code for A/B testing, without having to continually modify the tests. Signed-off-by: James McCorrie <[email protected]>
1 parent 9a7a08e commit e8d5279

File tree

2 files changed

+234
-0
lines changed

2 files changed

+234
-0
lines changed

src/dvsim/job/deploy.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
if TYPE_CHECKING:
3030
from dvsim.flow.sim import SimCfg
31+
from dvsim.modes import BuildMode
3132

3233

3334
class WorkspaceConfig(BaseModel):
@@ -392,6 +393,20 @@ def __init__(self, build_mode, sim_cfg) -> None:
392393
self.build_timeout_mins,
393394
)
394395

396+
@staticmethod
397+
def new(build_mode_obj: "BuildMode", sim_cfg: "SimCfg") -> "CompileSim":
398+
"""Create a new CompileSim object.
399+
400+
Args:
401+
build_mode_obj: build mode instance
402+
sim_cfg: Simulation config object
403+
404+
Returns:
405+
new CompileSim object.
406+
407+
"""
408+
return CompileSim(build_mode=build_mode_obj, sim_cfg=sim_cfg)
409+
395410
def _define_attrs(self) -> None:
396411
"""Define attributes."""
397412
super()._define_attrs()

tests/job/test_deploy.py

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
# Copyright lowRISC contributors (OpenTitan project).
2+
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
"""Test Job deployment models."""
6+
7+
from collections.abc import Mapping
8+
9+
import pytest
10+
from hamcrest import assert_that, equal_to
11+
12+
from dvsim.job.deploy import CompileSim
13+
14+
__all__ = ()
15+
16+
17+
class FakeCliArgs:
18+
"""Fake CLI args."""
19+
20+
def __init__(self) -> None:
21+
"""Initialise fake command line arguments."""
22+
self.build_timeout_mins = None
23+
self.timestamp = "timestamp"
24+
25+
26+
class FakeSimCfg:
27+
"""Fake sim configuration."""
28+
29+
def __init__(self) -> None:
30+
"""Initialise fake sim configuration."""
31+
self.name = "flow_name"
32+
self.variant = "variant"
33+
34+
self.args = FakeCliArgs()
35+
self.dry_run = True
36+
self.gui = False
37+
38+
self.scratch_path = "/scratch_path"
39+
self.scratch_root = "/scratch_root"
40+
self.proj_root = "/project"
41+
42+
self.exports = []
43+
44+
self.flow_makefile = "path/to/makefile"
45+
self.build_cmd = "path/to/{build_mode}/build_cmd"
46+
self.pre_build_cmds = ["A", "B"]
47+
self.post_build_cmds = ["C", "D"]
48+
self.build_dir = "build/dir"
49+
self.build_pass_patterns = None
50+
self.build_fail_patterns = None
51+
self.build_seed = 123
52+
53+
self.sv_flist_gen_cmd = "gen_cmd"
54+
self.sv_flist_gen_opts = []
55+
self.sv_flist_gen_dir = "path/to/gen"
56+
57+
self.cov = True
58+
self.cov_db_dir = "path"
59+
60+
61+
class FakeBuildMode:
62+
"""Fake BuildMode."""
63+
64+
def __init__(self) -> None:
65+
"""Initialise fake BuildMode."""
66+
self.name = "build_name"
67+
self.build_timeout_mins = 500
68+
self.build_mode = "build_mode"
69+
self.build_opts = ["-b path/here", '-a "Quoted"']
70+
71+
72+
def _build_compile_sim(
73+
*,
74+
build_overrides: Mapping | None = None,
75+
sim_overrides: Mapping | None = None,
76+
cli_args_overrides: Mapping | None = None,
77+
) -> CompileSim:
78+
"""Build CompileSim object.
79+
80+
Test helper that takes overrides to apply on top of the default values for
81+
the BuildMode and SimCfg fake objects.
82+
"""
83+
cli_args = FakeCliArgs()
84+
if cli_args_overrides:
85+
for arg, value in cli_args_overrides.items():
86+
setattr(cli_args, arg, value)
87+
88+
build_mode_obj = FakeBuildMode()
89+
if build_overrides:
90+
for arg, value in build_overrides.items():
91+
setattr(build_mode_obj, arg, value)
92+
93+
sim_cfg = FakeSimCfg()
94+
if sim_overrides:
95+
for arg, value in sim_overrides.items():
96+
setattr(sim_cfg, arg, value)
97+
98+
# Override the cli args in the sim configuration
99+
sim_cfg.args = cli_args
100+
101+
return CompileSim.new(
102+
build_mode_obj=build_mode_obj,
103+
sim_cfg=sim_cfg,
104+
)
105+
106+
107+
class TestCompileSim:
108+
"""Test CompileSim."""
109+
110+
@staticmethod
111+
@pytest.mark.parametrize(
112+
("build_overrides", "sim_overrides", "exp_cmd"),
113+
[
114+
(
115+
{"dry_run": True},
116+
{},
117+
"make -f path/to/makefile build "
118+
"-n "
119+
"build_cmd=path/to/build_name/build_cmd "
120+
"build_dir=build/dir "
121+
"build_opts='-b path/here -a \"Quoted\"' "
122+
"post_build_cmds='C && D' "
123+
"pre_build_cmds='A && B' "
124+
"proj_root=/project "
125+
"sv_flist_gen_cmd=gen_cmd "
126+
"sv_flist_gen_dir=path/to/gen "
127+
"sv_flist_gen_opts=''",
128+
),
129+
(
130+
{"dry_run": False},
131+
{},
132+
"make -f path/to/makefile build "
133+
"build_cmd=path/to/build_name/build_cmd "
134+
"build_dir=build/dir "
135+
"build_opts='-b path/here -a \"Quoted\"' "
136+
"post_build_cmds='C && D' "
137+
"pre_build_cmds='A && B' "
138+
"proj_root=/project "
139+
"sv_flist_gen_cmd=gen_cmd "
140+
"sv_flist_gen_dir=path/to/gen "
141+
"sv_flist_gen_opts=''",
142+
),
143+
],
144+
)
145+
def test_cmd(build_overrides: Mapping, sim_overrides: Mapping, exp_cmd: str) -> None:
146+
"""Test that a CompileSim has the expected cmd."""
147+
job = _build_compile_sim(
148+
build_overrides=build_overrides,
149+
sim_overrides=sim_overrides,
150+
)
151+
152+
assert_that(job.cmd, equal_to(exp_cmd))
153+
154+
@staticmethod
155+
@pytest.mark.parametrize(
156+
("build_overrides", "sim_overrides", "name", "full_name"),
157+
[
158+
({"name": "fred"}, {"variant": None}, "fred", "flow_name:fred"),
159+
({"name": "fred"}, {"variant": "v1"}, "fred", "flow_name_v1:fred"),
160+
({"name": "fred"}, {"name": "flow", "variant": None}, "fred", "flow:fred"),
161+
({"name": "george"}, {"variant": None}, "george", "flow_name:george"),
162+
({"name": "george"}, {"variant": "v2"}, "george", "flow_name_v2:george"),
163+
],
164+
)
165+
def test_names(
166+
build_overrides: Mapping,
167+
sim_overrides: Mapping,
168+
name: str,
169+
full_name: str,
170+
) -> None:
171+
"""Test that a CompileSim ends up with the expected names."""
172+
job = _build_compile_sim(
173+
build_overrides=build_overrides,
174+
sim_overrides=sim_overrides,
175+
)
176+
177+
assert_that(job.name, equal_to(name))
178+
assert_that(job.qual_name, equal_to(name))
179+
assert_that(job.full_name, equal_to(full_name))
180+
181+
@staticmethod
182+
@pytest.mark.parametrize(
183+
("sim_overrides", "seed"),
184+
[
185+
({"build_seed": 123}, 123),
186+
({"build_seed": 631}, 631),
187+
],
188+
)
189+
def test_seed(
190+
sim_overrides: Mapping,
191+
seed: int,
192+
) -> None:
193+
"""Test that a CompileSim ends up with the expected seed."""
194+
job = _build_compile_sim(
195+
sim_overrides=sim_overrides,
196+
)
197+
198+
assert_that(job.seed, equal_to(seed))
199+
200+
@staticmethod
201+
@pytest.mark.parametrize(
202+
("cli_args_overrides", "build_overrides", "timeout"),
203+
[
204+
({"build_timeout_mins": 111}, {}, 111),
205+
({}, {"build_timeout_mins": 112}, 112),
206+
],
207+
)
208+
def test_timeout(
209+
cli_args_overrides: Mapping,
210+
build_overrides: Mapping,
211+
timeout: int,
212+
) -> None:
213+
"""Test that a CompileSim ends up with the expected timeout."""
214+
job = _build_compile_sim(
215+
build_overrides=build_overrides,
216+
cli_args_overrides=cli_args_overrides,
217+
)
218+
219+
assert_that(job.build_timeout_mins, equal_to(timeout))

0 commit comments

Comments
 (0)