Example #1
0
def test_cli_compact_settings_envs(
    cli: typer.Typer,
    fake_settings_class: Type[BaseCustomSettings],
    fake_granular_env_file_content: str,
    export_as_dict: Callable,
    cli_runner: CliRunner,
    monkeypatch: pytest.MonkeyPatch,
):

    with monkeypatch.context() as patch:
        mocked_envs_1: EnvVarsDict = setenvs_as_envfile(
            patch, fake_granular_env_file_content
        )

        settings_1 = fake_settings_class()

        settings_1_dict_wo_secrets = export_as_dict(settings_1)
        assert settings_1_dict_wo_secrets == {
            "APP_HOST": "localhost",
            "APP_PORT": 80,
            "APP_OPTIONAL_ADDON": {"MODULE_VALUE": 10, "MODULE_VALUE_DEFAULT": 42},
            "APP_REQUIRED_PLUGIN": {
                "POSTGRES_HOST": "localhost",
                "POSTGRES_PORT": 5432,
                "POSTGRES_USER": "******",
                "POSTGRES_PASSWORD": "******",
                "POSTGRES_DB": "foodb",
                "POSTGRES_MINSIZE": 1,
                "POSTGRES_MAXSIZE": 50,
                "POSTGRES_CLIENT_NAME": None,
            },
        }

        setting_env_content_compact = cli_runner.invoke(
            cli,
            ["settings", "--compact", "--show-secrets"],
            catch_exceptions=False,
        ).stdout

    # now we use these as env vars
    print(setting_env_content_compact)

    with monkeypatch.context() as patch:
        mocked_envs_2: EnvVarsDict = setenvs_as_envfile(
            patch,
            setting_env_content_compact,
        )

        assert mocked_envs_2 == {
            "APP_HOST": "localhost",
            "APP_PORT": "80",
            "APP_OPTIONAL_ADDON": '{"MODULE_VALUE": 10, "MODULE_VALUE_DEFAULT": 42}',
            "APP_REQUIRED_PLUGIN": '{"POSTGRES_HOST": "localhost", "POSTGRES_PORT": 5432, "POSTGRES_USER": "******", "POSTGRES_PASSWORD": "******", "POSTGRES_DB": "foodb", "POSTGRES_MINSIZE": 1, "POSTGRES_MAXSIZE": 50, "POSTGRES_CLIENT_NAME": null}',
        }

        settings_2 = fake_settings_class()
        assert settings_1 == settings_2
Example #2
0
def test_env_passing(monkeypatch: pytest.MonkeyPatch) -> None:
    """Confirm that MPI extension passes environment variables correctly."""
    config = MpiConfig(
        env_pass=["A", "B", "LONG_NAME"],
        env_pass_regex=["TOOLNAME", "MPI_.*_CONF"],
    )

    env: MutableMapping[str, str] = {}

    with monkeypatch.context() as m:
        m.setattr(os, "environ", {})
        env = {}
        config.pass_through_env_vars(env)
        assert env == {}

    with monkeypatch.context() as m:
        m.setattr(os, "environ", {"A": "a"})
        env = {}
        config.pass_through_env_vars(env)
        assert env == {"A": "a"}

    with monkeypatch.context() as m:
        m.setattr(os, "environ", {"A": "a", "C": "c"})
        env = {}
        config.pass_through_env_vars(env)
        assert env == {"A": "a"}

    with monkeypatch.context() as m:
        m.setattr(os, "environ", {"A": "a", "B": "b", "C": "c"})
        env = {"PATH": "one:two:three", "HOME": "/tmp/dir", "TMPDIR": "/tmp/dir"}
        config.pass_through_env_vars(env)
        assert env == {
            "PATH": "one:two:three",
            "HOME": "/tmp/dir",
            "TMPDIR": "/tmp/dir",
            "A": "a",
            "B": "b",
        }

    with monkeypatch.context() as m:
        m.setattr(os, "environ", {"TOOLNAME": "foobar"})
        env = {}
        config.pass_through_env_vars(env)
        assert env == {"TOOLNAME": "foobar"}

    with monkeypatch.context() as m:
        m.setattr(os, "environ", {"_TOOLNAME_": "foobar"})
        env = {}
        config.pass_through_env_vars(env)
        # Cos we are matching not searching
        assert env == {}

    with monkeypatch.context() as m:
        m.setattr(os, "environ", {"MPI_A_CONF": "A", "MPI_B_CONF": "B"})

        env = {}
        config.pass_through_env_vars(env)
        # Cos we are matching not searching
        assert env == {"MPI_A_CONF": "A", "MPI_B_CONF": "B"}
Example #3
0
def test_windows_warning(monkeypatch: pytest.MonkeyPatch) -> None:
    """Confirm that the windows warning is given."""
    with pytest.warns(UserWarning, match=r"Windows Subsystem for Linux 2"):
        # would normally just use the MonkeyPatch object directly
        # but if we don't use a context then os.name being "nt" causes problems
        # for pytest on non-Windows systems. So the context unravels the change
        # to os.name quickly, and then pytest will check for the desired warning
        with monkeypatch.context() as m:
            m.setattr(os, "name", "nt")
            main.windows_check()
Example #4
0
def use_fixed_time(
    monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture
) -> Iterator[None]:
    with monkeypatch.context() as m:
        m.setenv("TZ", "EST5EDT,M3.2.0,M11.1.0")
        time.tzset()
        mocker.patch("time.time", return_value=1612373696)
        # Time is now 2021-02-03T12:34:56-05:00
        yield
    time.tzset()
def test_value_from_two_concatenated_expressions_singularity(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    """Javascript test using Singularity."""
    sandboxjs.have_node_slim = False
    sandboxjs.localdata = threading.local()
    new_paths = hide_nodejs(tmp_path)
    factory = Factory()
    factory.loading_context.singularity = True
    factory.loading_context.debug = True
    factory.runtime_context.debug = True
    with monkeypatch.context() as m:
        m.setenv("PATH", new_paths)
        echo = factory.make(get_data("tests/wf/vf-concat.cwl"))
        file = {"class": "File", "location": get_data("tests/wf/whale.txt")}
        assert echo(file1=file) == {"out": "a string\n"}
Example #6
0
    def mark(cls, monkeypatch: pytest.MonkeyPatch) -> Generator[None, None, None]:
        """
        Mark connections under in that context.
        """

        orig_request = HTTPConnection.request

        def call_and_mark(target: Callable[..., None]) -> Callable[..., None]:
            def part(self: HTTPConnection, *args: Any, **kwargs: Any) -> None:
                target(self, *args, **kwargs)
                self.sock.sendall(cls._get_socket_mark(self.sock, False))

            return part

        with monkeypatch.context() as m:
            m.setattr(HTTPConnection, "request", call_and_mark(orig_request))
            yield