|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | # (c) 2022 Andreas Motl <[email protected]> |
| 3 | +import re |
3 | 4 | import shlex |
4 | 5 | import subprocess |
5 | 6 | import sys |
6 | 7 | from pathlib import Path |
| 8 | +from unittest import mock |
7 | 9 |
|
8 | 10 | import pytest |
9 | 11 | from click._compat import strip_ansi |
|
12 | 14 | from racker.cli import cli |
13 | 15 |
|
14 | 16 |
|
| 17 | +# Currently, this test module effectively runs well on Linux, |
| 18 | +# because it invokes machinery based on `systemd-nspawn`. |
| 19 | + |
15 | 20 | if sys.platform != "linux": |
16 | 21 | pytest.skip("Skipping Linux-only tests", allow_module_level=True) |
17 | 22 |
|
@@ -104,6 +109,73 @@ def test_run_stdin_stdout(monkeypatch, capsys, delay): |
104 | 109 | assert process.stdout == b"foo" |
105 | 110 |
|
106 | 111 |
|
| 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 | + |
107 | 179 | # Unfortunately, this fails. |
108 | 180 | """ |
109 | 181 | def test_run_stdin_stdout_original(capfd): |
|
0 commit comments