mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-28 18:02:38 +02:00

The platform where we run our tests directly affects the isolation providers we can choose. For instance, we cannot run Qubes tests on a Windows/macOS platform, nor can we spawn containers in a Qubes platform, if the `QUBES_CONVERSION` envvar has been specified. This platform incompatibility was never an issue before, because Dangerzone is capable of selecting the proper isolation provider under the hood. However, with the addition of tests that target specific isolation providers, it's possible that we may run by mistake a test that does not apply to our platform. To counter this, we employed `pytest.skipif()` guards around classes, but we may omit those by mistake. Case in point, the `TestContainer` class does not have such a guard, which means that we attempt to run this test case on Qubes and it fails. Add module-level guards in our isolation provider tests using pytest's `pytest.skip("...", allow_module_level=True)` function, so that we make such restrictions more explicit, and less easy to forget when we add a new class.
52 lines
1.4 KiB
Python
52 lines
1.4 KiB
Python
import os
|
|
import subprocess
|
|
|
|
import pytest
|
|
from pytest_mock import MockerFixture
|
|
|
|
from dangerzone.conversion import errors
|
|
from dangerzone.document import Document
|
|
from dangerzone.isolation_provider.base import IsolationProvider
|
|
from dangerzone.isolation_provider.dummy import Dummy
|
|
|
|
from .base import IsolationProviderTermination
|
|
|
|
# Run the tests in this module only if dummy conversion is enabled.
|
|
if not os.environ.get("DUMMY_CONVERSION", False):
|
|
pytest.skip("Dummy conversion is not enabled", allow_module_level=True)
|
|
|
|
|
|
class DummyWait(Dummy):
|
|
"""Dummy isolation provider that spawns a blocking process."""
|
|
|
|
def start_doc_to_pixels_proc(self, document: Document) -> subprocess.Popen:
|
|
return subprocess.Popen(
|
|
["python3"],
|
|
stdin=subprocess.PIPE,
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE,
|
|
)
|
|
|
|
def terminate_doc_to_pixels_proc(
|
|
self, document: Document, p: subprocess.Popen
|
|
) -> None:
|
|
p.terminate()
|
|
|
|
|
|
@pytest.fixture
|
|
def provider_wait() -> DummyWait:
|
|
return DummyWait()
|
|
|
|
|
|
class TestDummyTermination(IsolationProviderTermination):
|
|
def test_failed(
|
|
self,
|
|
provider_wait: IsolationProvider,
|
|
mocker: MockerFixture,
|
|
) -> None:
|
|
mocker.patch.object(
|
|
provider_wait,
|
|
"get_proc_exception",
|
|
return_value=errors.DocFormatUnsupported(),
|
|
)
|
|
super().test_failed(provider_wait, mocker)
|