Skip to content

Commit 33817da

Browse files
committed
[windows] Increase test coverage
1 parent 0b1412a commit 33817da

File tree

4 files changed

+144
-6
lines changed

4 files changed

+144
-6
lines changed

postroj/winrunner.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,9 @@ def docker_context_online(self):
219219
return port_is_up(address.hostname, address.port)
220220

221221

222-
def hcmd(command, cwd=None, silent=False):
222+
def hcmd(command, cwd=None, use_stderr=True, silent=False):
223223
logger.debug(f"Running command: {command}")
224-
return cmd(command, cwd=cwd, use_stderr=True)
224+
return cmd(command, cwd=cwd, use_stderr=use_stderr)
225225

226226

227227
def hshell(command, cwd=None):
@@ -230,9 +230,10 @@ def hshell(command, cwd=None):
230230
return subprocess.check_output(command, shell=True, cwd=cwd).decode()
231231

232232

233-
def ccmd(command, use_pty=False):
233+
def ccmd(command, use_pty=False, capture=False):
234234
logger.debug(f"Running command: {command}")
235-
p = cmd(command=command, use_pty=use_pty)
235+
p = cmd(command=command, use_pty=use_pty, capture=capture)
236236
stdout = p.stdout
237-
fix_tty()
237+
if use_pty:
238+
fix_tty()
238239
return stdout

testing/postroj/test_winrunner.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# -*- coding: utf-8 -*-
2+
# (c) 2022 Andreas Motl <[email protected]>
3+
import pytest
4+
5+
from postroj.winrunner import ccmd, hshell, hcmd
6+
7+
8+
def test_hcmd_echo_newline(capfd):
9+
process = hcmd("/bin/echo foo", use_stderr=False)
10+
assert process.stdout is None
11+
assert process.stderr is None
12+
13+
result = capfd.readouterr()
14+
assert result.out == "foo\n"
15+
assert result.err == ""
16+
17+
18+
def test_hcmd_echo_no_newline(capfd):
19+
hcmd("/bin/echo -n foo", use_stderr=False)
20+
result = capfd.readouterr()
21+
assert result.out == "foo"
22+
23+
24+
def test_hshell_echo_newline():
25+
output = hshell("/bin/echo foo")
26+
assert output == "foo\n"
27+
28+
29+
def test_hshell_echo_no_newline():
30+
output = hshell("/bin/echo -n foo")
31+
assert output == "foo"
32+
33+
34+
def test_ccmd_echo_newline():
35+
output = ccmd("/bin/echo foo", capture=True)
36+
assert output == "foo\n"
37+
38+
39+
def test_ccmd_echo_newline_pty(capfd):
40+
output = ccmd("/bin/echo foo", capture=True, use_pty=True)
41+
assert output is None
42+
43+
result = capfd.readouterr()
44+
assert result.out == "foo\n"
45+
assert result.err == ""
46+
47+
48+
@pytest.mark.xfail
49+
def test_ccmd_echo_no_newline():
50+
output = ccmd("/bin/echo -n foo", capture=True)
51+
assert output == "foo"
52+
53+
54+
def test_ccmd_echo_no_newline_pty(capfd):
55+
output = ccmd("/bin/echo -n foo", capture=True, use_pty=True)
56+
assert output is None
57+
58+
result = capfd.readouterr()
59+
assert result.out == "foo"
60+
assert result.err == ""

testing/racker/test_run.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# -*- coding: utf-8 -*-
22
# (c) 2022 Andreas Motl <[email protected]>
3+
import re
34
import shlex
45
import subprocess
56
import sys
67
from pathlib import Path
8+
from unittest import mock
79

810
import pytest
911
from click._compat import strip_ansi
@@ -12,6 +14,9 @@
1214
from racker.cli import cli
1315

1416

17+
# Currently, this test module effectively runs well on Linux,
18+
# because it invokes machinery based on `systemd-nspawn`.
19+
1520
if sys.platform != "linux":
1621
pytest.skip("Skipping Linux-only tests", allow_module_level=True)
1722

@@ -104,6 +109,73 @@ def test_run_stdin_stdout(monkeypatch, capsys, delay):
104109
assert process.stdout == b"foo"
105110

106111

112+
@pytest.mark.xfail(reason="Not working within Linux VM on macOS. Reason: "
113+
"Cannot enable nested VT-x/AMD-V without nested-paging and unrestricted guest execution!")
114+
def test_run_windows_valid_image(capfd, delay):
115+
"""
116+
Request a valid Windows container image.
117+
118+
Note: This is currently not possible, because somehow VT-x does not work
119+
well enough to support this scenario, at least not on macOS Catalina.
120+
"""
121+
runner = CliRunner()
122+
123+
result = runner.invoke(cli, "run --rm --platform=windows/amd64 mcr.microsoft.com/windows/nanoserver:ltsc2022 -- cmd /C ver", catch_exceptions=False)
124+
assert result.exit_code == 0
125+
126+
captured = capfd.readouterr()
127+
assert "Microsoft Windows [Version 10.0.20348.707]" in captured.out
128+
129+
130+
def test_run_windows_invalid_image(caplog, delay):
131+
"""
132+
Request an invalid Windows container image and make sure it croaks correctly.
133+
"""
134+
runner = CliRunner()
135+
136+
result = runner.invoke(cli, "run --rm --platform=windows/amd64 images.example.org/foo/bar:special -- cmd /C ver", catch_exceptions=False)
137+
assert result.exit_code == 1
138+
139+
assert re.match(".*Inquiring information about OCI image .+ failed.*", caplog.text)
140+
assert re.match(".*Reason:.*Error parsing image name .* (error )?pinging (container|docker) registry images.example.org.*", caplog.text)
141+
142+
143+
def test_run_windows_mocked_noninteractive():
144+
"""
145+
Pretend to launch a Windows container, but don't.
146+
Reason: The `WinRunner` machinery has been mocked completely.
147+
"""
148+
runner = CliRunner()
149+
150+
with mock.patch("racker.cli.WinRunner") as winrunner:
151+
result = runner.invoke(cli, "run --rm --platform=windows/amd64 images.example.org/foo/bar:special -- cmd /C ver", catch_exceptions=False)
152+
assert result.exit_code == 0
153+
assert winrunner.mock_calls == [
154+
mock.call(image='images.example.org/foo/bar:special'),
155+
mock.call().setup(),
156+
mock.call().start(),
157+
mock.call().run('cmd /C ver', interactive=False, tty=False),
158+
]
159+
160+
161+
def test_run_windows_mocked_interactive():
162+
"""
163+
Pretend to launch a Windows container, but don't.
164+
Reason: The `WinRunner` machinery has been mocked completely.
165+
"""
166+
runner = CliRunner()
167+
168+
with mock.patch("racker.cli.WinRunner") as winrunner:
169+
result = runner.invoke(cli, "run -it --rm --platform=windows/amd64 images.example.org/foo/bar:special -- cmd /C ver", catch_exceptions=False)
170+
assert result.exit_code == 0
171+
assert winrunner.mock_calls == [
172+
mock.call(image='images.example.org/foo/bar:special'),
173+
mock.call().setup(),
174+
mock.call().start(),
175+
mock.call().run('cmd /C ver', interactive=True, tty=True),
176+
]
177+
178+
107179
# Unfortunately, this fails.
108180
"""
109181
def test_run_stdin_stdout_original(capfd):

testing/racker/test_run_windows.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@
1111
import pytest
1212

1313

14+
# Currently, this test module effectively runs well on macOS.
15+
# The following snippet skips invocation on both VirtualBox
16+
# and GitHub Actions.
17+
1418
if "rackerhost-debian11" in socket.gethostname():
15-
pytest.skip("Nested virtualization with VT-x fails on developer's macOS workstation", allow_module_level=True)
19+
pytest.skip("Nested virtualization with VT-x fails within "
20+
"VirtualBox environment on developer's macOS workstation", allow_module_level=True)
1621

1722
if "GITHUB_ACTIONS" in os.environ:
1823
pytest.skip("Installing the Vagrant filesystem image for Windows "

0 commit comments

Comments
 (0)