Ejemplo n.º 1
0
def on_init(environment: Environment, runner: RunnerType, **_kwargs) -> None:
    """
    Event hook that gets run after the locust environment has been set up. See docs for more info:
    https://docs.locust.io/en/stable/api.html?#locust.event.Events.init

    In our case, we're setting up certs.
    :param environment: locust environment.
    :param runner: locust runner that can be used to shut down the test run.
    :param _kwargs: Other kwargs we aren't using that are passed to hook functions.
    :return: None
    """
    try:
        milmove_env = MilMoveEnv(value=environment.host)
    except ValueError as err:
        # For some reason exceptions don't stop the runner automatically, so we have to do it
        # ourselves.
        runner.quit()

        raise err

    try:
        set_up_certs(env=milmove_env)
    except ImplementationError as err:
        runner.quit()

        raise err
Ejemplo n.º 2
0
    def test_no_file_is_created_if_running_locally(self,
                                                   tmp_path: Path) -> None:
        cert_key_pem = tmp_path / DP3_CERT_KEY_PEM_FILENAME

        with mock.patch("utils.auth.DP3_CERT_KEY_PEM", str(cert_key_pem)):
            set_up_certs(env=MilMoveEnv.LOCAL)

            assert len(list(tmp_path.iterdir())) == 0
Ejemplo n.º 3
0
    def run_entire_flow(self, milmove_env: MilMoveEnv) -> None:
        try:
            set_up_certs(env=milmove_env)

            flow_session_manager = FlowSessionManager(milmove_env, None)

            while self.run(flow_session_manager):
                pass
        finally:
            remove_certs(env=milmove_env)
Ejemplo n.º 4
0
    def test_cert_created_for_deployed_host_with_env_vars(
            self, tmp_path: Path, monkeypatch: MonkeyPatch):
        fake_cert = "fake cert"
        fake_key = "fake key"
        monkeypatch.setenv("MOVE_MIL_DP3_TLS_CERT", fake_cert)
        monkeypatch.setenv("MOVE_MIL_DP3_TLS_KEY", fake_key)

        cert_key_pem = tmp_path / DP3_CERT_KEY_PEM_FILENAME

        with mock.patch("utils.auth.DP3_CERT_KEY_PEM", str(cert_key_pem)):
            set_up_certs(env=MilMoveEnv.DP3)

            assert len(list(tmp_path.iterdir())) == 1

            assert cert_key_pem.exists()

            assert f"{fake_cert}\n{fake_key}" == cert_key_pem.read_text()
Ejemplo n.º 5
0
    def test_missing_env_tls_var_raises_exception_for_deployed_host(
            self, tls_env_var: str, tmp_path: Path,
            monkeypatch: MonkeyPatch) -> None:
        monkeypatch.delenv(tls_env_var, raising=False)

        cert_key_pem = tmp_path / DP3_CERT_KEY_PEM_FILENAME

        with mock.patch("utils.auth.DP3_CERT_KEY_PEM", str(cert_key_pem)):
            with pytest.raises(
                    ImplementationError,
                    match=
                    "Cannot run load testing in a deployed environment without the matching certificate and key.",
            ):
                set_up_certs(env=MilMoveEnv.DP3)

            assert len(list(tmp_path.iterdir())) == 0

            assert not cert_key_pem.exists()