Skip to content

Commit

Permalink
Merge pull request #8 from Novakov/fix-qemu-memory-arg
Browse files Browse the repository at this point in the history
fix: Actually pass memory setting to QEMU
  • Loading branch information
Novakov authored May 30, 2023
2 parents 9538047 + 02fb044 commit 8e69f1a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 22 deletions.
4 changes: 4 additions & 0 deletions src/qemu_runner/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def apply_general() -> GeneralSettings:
halted=other.halted if other.halted is not None else self._general.halted,
gdb=other.gdb if other.gdb is not None else self._general.gdb,
gdb_dev=other.gdb_dev if other.gdb_dev is not None else self._general.gdb_dev,
memory=other.memory if other.memory is not None else self._general.memory
)

def apply_arguments() -> Iterable[Argument]:
Expand Down Expand Up @@ -153,6 +154,9 @@ def read_general_settings() -> GeneralSettings:
if 'halted' in section:
result = replace(result, halted=config_parser.getboolean('general', 'halted'))

if 'memory' in section:
result = replace(result, memory=section['memory'])

return result

return Layer(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[general]
engine = qemu-system-arm
memory = 128M

[machine]
@ = virt-cortex-m
Expand Down
15 changes: 15 additions & 0 deletions tests/test_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ def test_create_layer():
Layer(GeneralSettings(engine='e2', kernel='k2', kernel_cmdline='c2', halted=True, gdb=True, gdb_dev='gdb2')),
Layer(GeneralSettings(engine='e2', kernel='k2', kernel_cmdline='c1 c2', halted=True, gdb=True, gdb_dev='gdb2')),
),
(
Layer(GeneralSettings(memory='12')),
Layer(GeneralSettings()),
Layer(GeneralSettings(memory='12')),
),
(
Layer(GeneralSettings(memory='12')),
Layer(GeneralSettings(memory='24')),
Layer(GeneralSettings(memory='24')),
),
(
Layer(GeneralSettings()),
Layer(GeneralSettings(memory='24')),
Layer(GeneralSettings(memory='24')),
),
(
Layer(MY_ENGINE, [Argument('device', 'd1', {'id': 'id1', 'p': 'a'})]),
Layer(MY_ENGINE, [Argument('chardev', 'c1', {'id': 'id2', 'b': 'a'})]),
Expand Down
7 changes: 7 additions & 0 deletions tests/test_layer_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
""",
Layer(general=GeneralSettings(engine='my-engine'))
),
(
"""
[general]
memory = 128M
""",
Layer(general=GeneralSettings(memory='128M'))
),
(
"""
[general]
Expand Down
63 changes: 41 additions & 22 deletions tests/test_runner_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,49 @@ def capture_runner_cmdline(runner: Path, *args: CmdlineArg, cwd: Optional[os.Pat
]


def test_runner_flow(tmp_path: Path):
@pytest.fixture()
def test_layer(tmp_path: Path) -> Path:
with open(tmp_path / 'test-layer', 'w') as f:
f.write("""
[general]
engine = qemu-system-arm
memory = 128M
[machine]
@ = virt_cortex_m
flash_kb = 1024
""")

return tmp_path / 'test-layer'


def test_runner_flow(tmp_path: Path, test_layer: Path):
engine = place_echo_args(tmp_path / 'qemu' / 'qemu-system-arm')

run_make_runner('-l', 'virt-cortex-m.ini', '-o', tmp_path / 'test.pyz', cwd=tmp_path)
run_make_runner('-l', test_layer, '-o', tmp_path / 'test.pyz', cwd=tmp_path)
with with_cwd(tmp_path):
cmdline = capture_runner_cmdline(tmp_path / 'test.pyz', 'abc.elf', 'arg1', 'arg2')

assert cmdline == [
engine,
'-machine', 'virt_cortex_m,flash_kb=1024',
'-m', '128M',
'-kernel', str(tmp_path / 'abc.elf'),
'-append', 'arg1 arg2'
]


def test_runner_flow_no_args(tmp_path: Path):
def test_runner_flow_no_args(tmp_path: Path, test_layer: Path):
engine = place_echo_args(tmp_path / 'qemu' / 'qemu-system-arm')

run_make_runner('-l', 'virt-cortex-m.ini', '-o', tmp_path / 'test.pyz', cwd=tmp_path)
run_make_runner('-l', test_layer, '-o', tmp_path / 'test.pyz', cwd=tmp_path)
with with_cwd(tmp_path):
cmdline = capture_runner_cmdline(tmp_path / 'test.pyz', 'abc.elf')

assert cmdline == [
engine,
'-machine', 'virt_cortex_m,flash_kb=1024',
'-m', '128M',
'-kernel', str(tmp_path / 'abc.elf'),
]

Expand All @@ -89,26 +107,26 @@ def assert_arg_set_in_cmdline(arg_set: List[str], cmdline: List[str]):
(['--debug'], [['-s']]),
(['--debug', '--debug-listen', 'abc'], [['-gdb', 'abc']]),
])
def test_builtin_args(tmpdir: Path, runner_args: List[str], qemu_args: List[List[str]]):
def test_builtin_args(tmpdir: Path, runner_args: List[str], qemu_args: List[List[str]], test_layer: Path):
place_echo_args(tmpdir / 'qemu' / 'qemu-system-arm')

run_make_runner('-l', 'virt-cortex-m.ini', '-o', tmpdir / 'test.pyz', cwd=tmpdir)
run_make_runner('-l', test_layer, '-o', tmpdir / 'test.pyz', cwd=tmpdir)
cmdline = capture_runner_cmdline(tmpdir / 'test.pyz', *runner_args, 'abc.elf', 'arg1', 'arg2')

for arg_set in qemu_args:
assert_arg_set_in_cmdline(arg_set, cmdline)


def test_debug_listen_no_debug(tmp_path: Path):
def test_debug_listen_no_debug(tmp_path: Path, test_layer: Path):
place_echo_args(tmp_path / 'qemu' / 'qemu-system-arm')

run_make_runner('-l', 'virt-cortex-m.ini', '-o', tmp_path / 'test.pyz', cwd=tmp_path)
run_make_runner('-l', test_layer, '-o', tmp_path / 'test.pyz', cwd=tmp_path)
cmdline = capture_runner_cmdline(tmp_path / 'test.pyz', '--debug-listen', 'MARK-DEBUG', 'abc.elf', 'arg1', 'arg2')

assert 'MARK-DEBUG' not in cmdline


def test_layers_are_embbedded_in_runner(tmp_path: Path):
def test_layers_are_embbedded_in_runner(tmp_path: Path, test_layer: Path):
place_echo_args(tmp_path / 'qemu' / 'qemu-system-arm')

with open(tmp_path / 'my-layer.ini', 'w') as f:
Expand All @@ -118,7 +136,7 @@ def test_layers_are_embbedded_in_runner(tmp_path: Path):
addr=12
""")

run_make_runner('-l', 'virt-cortex-m.ini', 'my-layer.ini', '-o', tmp_path / 'test.pyz', cwd=tmp_path)
run_make_runner('-l', test_layer, 'my-layer.ini', '-o', tmp_path / 'test.pyz', cwd=tmp_path)

os.unlink(tmp_path / 'my-layer.ini')

Expand All @@ -127,40 +145,41 @@ def test_layers_are_embbedded_in_runner(tmp_path: Path):
assert_arg_set_in_cmdline(['-device', 'test_device,id=test_id,addr=12'], cmdline)


def test_extract_base_command_line_with_kernel(tmp_path: Path):
def test_extract_base_command_line_with_kernel(tmp_path: Path, test_layer: Path):
with open(tmp_path / 'my-layer.ini', 'w') as f:
f.write("""
[device:test_id]
@=test_device
addr=12
""")

run_make_runner('-l', 'virt-cortex-m.ini', 'my-layer.ini', '-o', tmp_path / 'test.pyz', cwd=tmp_path)
run_make_runner('-l', test_layer, 'my-layer.ini', '-o', tmp_path / 'test.pyz', cwd=tmp_path)

with with_env({'QEMU_DEV': 'my-qemu'}), with_cwd(tmp_path):
cp = execute_runner(tmp_path / 'test.pyz', ['--debug', '--dry-run', 'abc.elf', 'a', 'b', 'c'])

kernel_path = tmp_path / 'abc.elf'

assert cp.stdout.strip().replace("-kernel '", '-kernel ').replace("' -append", ' -append') == (
"my-qemu -machine virt_cortex_m,flash_kb=1024 -device test_device,id=test_id,addr=12 " +
"my-qemu -machine virt_cortex_m,flash_kb=1024 -device test_device,id=test_id,addr=12 -m 128M " +
f"-s -kernel {kernel_path} -append 'a b c'")


def test_extract_base_command_line_no_kernel(tmp_path: Path):
def test_extract_base_command_line_no_kernel(tmp_path: Path, test_layer: Path):
with open(tmp_path / 'my-layer.ini', 'w') as f:
f.write("""
[device:test_id]
@=test_device
addr=12
""")

run_make_runner('-l', 'virt-cortex-m.ini', 'my-layer.ini', '-o', tmp_path / 'test.pyz', cwd=tmp_path)
run_make_runner('-l', test_layer, 'my-layer.ini', '-o', tmp_path / 'test.pyz', cwd=tmp_path)

with with_env({'QEMU_DEV': 'my-qemu'}):
cp = execute_runner(tmp_path / 'test.pyz', ['--debug', '--dry-run'])

assert cp.stdout.strip() == "my-qemu -machine virt_cortex_m,flash_kb=1024 -device test_device,id=test_id,addr=12 -s"
assert cp.stdout.strip() == ("my-qemu -machine virt_cortex_m,flash_kb=1024 -device test_device,id=test_id,addr=12 " +
"-m 128M -s")


def test_derive_runner(tmp_path: Path):
Expand Down Expand Up @@ -356,28 +375,28 @@ def test_env_runner_flags(tmp_path: Path):
['--debug'],
['--debug-listen', 'abc']
])
def test_invalid_args(tmp_path: Path, args: List[str]) -> None:
run_make_runner('-l', 'virt-cortex-m.ini', '-o', tmp_path / 'test.pyz', cwd=tmp_path)
def test_invalid_args(tmp_path: Path, args: List[str], test_layer: Path) -> None:
run_make_runner('-l', test_layer, '-o', tmp_path / 'test.pyz', cwd=tmp_path)

cp = execute_runner(tmp_path / 'test.pyz', args, check=False, cwd=tmp_path)
assert 'test.pyz: error:' in cp.stderr
assert cp.returncode != 0


def test_explicit_qemu_dir(tmp_path: Path) -> None:
def test_explicit_qemu_dir(tmp_path: Path, test_layer: Path) -> None:
engine = place_echo_args(tmp_path / 'my-qemu' / 'qemu-system-arm')

run_make_runner('-l', 'virt-cortex-m.ini', '-o', tmp_path / 'test.pyz', cwd=tmp_path)
run_make_runner('-l', test_layer, '-o', tmp_path / 'test.pyz', cwd=tmp_path)

args = capture_runner_cmdline(tmp_path / 'test.pyz', '--qemu-dir', tmp_path / 'my-qemu', 'abc.elf')

assert args[0] == engine


def test_explicit_qemu_executable(tmp_path: Path) -> None:
def test_explicit_qemu_executable(tmp_path: Path, test_layer: Path) -> None:
engine = place_echo_args(tmp_path / 'my-qemu' / 'qemu')

run_make_runner('-l', 'virt-cortex-m.ini', '-o', tmp_path / 'test.pyz', cwd=tmp_path)
run_make_runner('-l', test_layer, '-o', tmp_path / 'test.pyz', cwd=tmp_path)

args = capture_runner_cmdline(tmp_path / 'test.pyz', '--qemu', engine, 'abc.elf')

Expand Down

0 comments on commit 8e69f1a

Please sign in to comment.