Exemple #1
0
def test_run_module_command_called_process_error(fake_process: FakeProcess) -> None:
    """Test run_module_command raise CalledProcessError."""
    cmd = ["test"]
    fake_process.register_subprocess(cmd, returncode=1)  # type: ignore
    with pytest.raises(CalledProcessError):
        run_module_command(cmd, {}, exit_on_error=False)
    assert fake_process.call_count(cmd) == 1  # type: ignore
Exemple #2
0
    def test_run_build_steps_raise_file_not_found(
        self,
        caplog: LogCaptureFixture,
        fake_process: FakeProcess,
        platform_linux: None,
        runway_context: RunwayContext,
        tmp_path: Path,
    ) -> None:
        """Test run_build_steps."""
        caplog.set_level(logging.ERROR, MODULE)

        def _callback(process: FakePopen) -> None:
            process.returncode = 1
            raise FileNotFoundError

        fake_process.register_subprocess(["test", "step"], callback=_callback)
        with pytest.raises(FileNotFoundError):
            CloudDevelopmentKit(
                runway_context,
                module_root=tmp_path,
                options={
                    "build_steps": ["test step"]
                },
            ).run_build_steps()
        assert fake_process.call_count(["test", "step"]) == 1
        assert "failed to find it" in "\n".join(caplog.messages)
Exemple #3
0
 def test_npm_install_install(
     self,
     caplog: LogCaptureFixture,
     colorize: bool,
     fake_process: FakeProcess,
     is_noninteractive: bool,
     mocker: MockerFixture,
     runway_context: MockRunwayContext,
     tmp_path: Path,
     use_ci: bool,
 ) -> None:
     """Test npm_install install."""
     caplog.set_level(logging.INFO, logger=MODULE)
     mocker.patch(f"{MODULE}.use_npm_ci", return_value=use_ci)
     mocker.patch.object(RunwayModuleNpm, "check_for_npm")
     mocker.patch.object(RunwayModuleNpm, "warn_on_boto_env_vars")
     runway_context.env.ci = is_noninteractive
     runway_context.env.vars["RUNWAY_COLORIZE"] = str(colorize)
     cmd: List[Any] = [NPM_BIN, "install"]
     if not colorize:
         cmd.append("--no-color")
     fake_process.register_subprocess(cmd, returncode=0)
     RunwayModuleNpm(runway_context, module_root=tmp_path).npm_install()
     assert "running npm install..." in caplog.messages
     assert fake_process.call_count(cmd) == 1
Exemple #4
0
def test_run_module_command_exit_on_error_system_exit(
    fake_process: FakeProcess,
) -> None:
    """Test run_module_command raise SystemExit."""
    cmd = ["test"]
    fake_process.register_subprocess(cmd, returncode=1)  # type: ignore
    with pytest.raises(SystemExit):
        run_module_command(cmd, {})
    assert fake_process.call_count(cmd) == 1  # type: ignore
Exemple #5
0
 def test_get_version_from_executable_raise(self, fake_process: FakeProcess) -> None:
     """Test get_version_from_executable raise exception."""
     fake_process.register_subprocess(
         ["usr/tfenv/terraform", "-version"], returncode=1
     )
     with pytest.raises(
         subprocess.CalledProcessError, match="returned non-zero exit status 1"
     ):
         TFEnvManager.get_version_from_executable("usr/tfenv/terraform")
Exemple #6
0
 def test_get_version_from_executable(
     self,
     expected: Optional[VersionTuple],
     fake_process: FakeProcess,
     output: str,
 ) -> None:
     """Test get_version_from_executable."""
     fake_process.register_subprocess(
         ["usr/tfenv/terraform", "-version"], stdout=output
     )
     assert (
         TFEnvManager.get_version_from_executable("usr/tfenv/terraform") == expected
     )
Exemple #7
0
 def test_kubectl_kustomize_raise_called_process_error(
     self,
     fake_process: FakeProcess,
     mocker: MockerFixture,
     runway_context: MockRunwayContext,
     tmp_path: Path,
 ) -> None:
     """Test kubectl_kustomize."""
     gen_cmd = mocker.patch.object(K8s,
                                   "gen_cmd",
                                   return_value=["kubectl", "kustomize"])
     fake_process.register_subprocess(gen_cmd.return_value, returncode=1)
     with pytest.raises(CalledProcessError):
         assert K8s(runway_context,
                    module_root=tmp_path).kubectl_kustomize()
     assert fake_process.call_count(gen_cmd.return_value) == 1
Exemple #8
0
 def test_cdk_list_empty(
     self,
     fake_process: FakeProcess,
     mocker: MockerFixture,
     runway_context: RunwayContext,
     tmp_path: Path,
 ) -> None:
     """Test cdk_list empty."""
     mock_gen_cmd = mocker.patch.object(CloudDevelopmentKit,
                                        "gen_cmd",
                                        return_value=["list"])
     fake_process.register_subprocess(mock_gen_cmd.return_value,
                                      returncode=0,
                                      stdout="")
     obj = CloudDevelopmentKit(runway_context, module_root=tmp_path)
     assert obj.cdk_list() == [""]
     assert fake_process.call_count(mock_gen_cmd.return_value) == 1
Exemple #9
0
 def test_run_build_steps_raise_called_process_error(
     self,
     fake_process: FakeProcess,
     platform_linux: None,
     runway_context: RunwayContext,
     tmp_path: Path,
 ) -> None:
     """Test run_build_steps."""
     fake_process.register_subprocess(["test", "step"], returncode=1)
     with pytest.raises(CalledProcessError):
         CloudDevelopmentKit(
             runway_context,
             module_root=tmp_path,
             options={
                 "build_steps": ["test step"]
             },
         ).run_build_steps()
     assert fake_process.call_count(["test", "step"]) == 1
Exemple #10
0
 def test_cdk_list(
     self,
     fake_process: FakeProcess,
     mocker: MockerFixture,
     runway_context: RunwayContext,
     tmp_path: Path,
 ) -> None:
     """Test cdk_list."""
     mock_gen_cmd = mocker.patch.object(CloudDevelopmentKit,
                                        "gen_cmd",
                                        return_value=["list"])
     fake_process.register_subprocess(mock_gen_cmd.return_value,
                                      returncode=0,
                                      stdout="Stack0\nStack1")
     obj = CloudDevelopmentKit(runway_context, module_root=tmp_path)
     assert obj.cdk_list() == ["Stack0", "Stack1"]
     mock_gen_cmd.assert_called_once_with("list", include_context=True)
     assert fake_process.call_count(mock_gen_cmd.return_value) == 1
Exemple #11
0
 def test_cdk_list_raise_called_process_error(
     self,
     fake_process: FakeProcess,
     mocker: MockerFixture,
     runway_context: RunwayContext,
     tmp_path: Path,
 ) -> None:
     """Test cdk_list raise CalledProcessError."""
     mock_gen_cmd = mocker.patch.object(CloudDevelopmentKit,
                                        "gen_cmd",
                                        return_value=["list"])
     fake_process.register_subprocess(
         mock_gen_cmd.return_value,
         returncode=1,
     )
     with pytest.raises(CalledProcessError):
         CloudDevelopmentKit(runway_context,
                             module_root=tmp_path).cdk_list()
     assert fake_process.call_count(mock_gen_cmd.return_value) == 1
Exemple #12
0
def test_use_npm_ci(
    exit_code: int,
    expected: bool,
    fake_process: FakeProcess,
    has_lock: bool,
    has_shrinkwrap: bool,
    tmp_path: Path,
) -> None:
    """Test use_npm_ci."""
    if has_lock:
        (tmp_path / "package-lock.json").touch()
    if has_shrinkwrap:
        (tmp_path / "package-lock.json").touch()
    cmd: List[Any] = [NPM_BIN, "ci", "-h"]
    fake_process.register_subprocess(cmd, returncode=exit_code)

    assert use_npm_ci(tmp_path) is expected
    if has_lock or has_shrinkwrap:
        assert fake_process.call_count(cmd) == 1
    else:
        assert fake_process.call_count(cmd) == 0
Exemple #13
0
 def test_kubectl_kustomize(
     self,
     caplog: LogCaptureFixture,
     fake_process: FakeProcess,
     mocker: MockerFixture,
     runway_context: MockRunwayContext,
     tmp_path: Path,
 ) -> None:
     """Test kubectl_kustomize."""
     caplog.set_level(logging.DEBUG, logger=MODULE)
     data = {"key": "val"}
     data_string = yaml.dump(data, indent=2)
     gen_cmd = mocker.patch.object(K8s,
                                   "gen_cmd",
                                   return_value=["kubectl", "kustomize"])
     fake_process.register_subprocess(gen_cmd.return_value,
                                      stdout=data_string,
                                      returncode=0)
     assert (K8s(runway_context,
                 module_root=tmp_path).kubectl_kustomize() == data_string)
     assert fake_process.call_count(gen_cmd.return_value) == 1
     logs = "\n".join(caplog.messages)
     assert f"kustomized yaml generated by kubectl:\n\n{data_string}" in logs
Exemple #14
0
 def test_run_build_steps_linux(
     self,
     caplog: LogCaptureFixture,
     fake_process: FakeProcess,
     mocker: MockerFixture,
     platform_linux: None,
     runway_context: RunwayContext,
     tmp_path: Path,
 ) -> None:
     """Test run_build_steps."""
     caplog.set_level(logging.INFO, logger=MODULE)
     fix_windows_command_list = mocker.patch(
         f"{MODULE}.fix_windows_command_list")
     fake_process.register_subprocess(["test", "step"], returncode=0)
     obj = CloudDevelopmentKit(runway_context,
                               module_root=tmp_path,
                               options={"build_steps": ["test step"]})
     assert not obj.run_build_steps()
     fix_windows_command_list.assert_not_called()
     assert fake_process.call_count(["test", "step"]) == 1
     logs = "\n".join(caplog.messages)
     assert "build steps (in progress)" in logs
     assert "build steps (complete)" in logs
Exemple #15
0
def test_run_module_command(fake_process: FakeProcess) -> None:
    """Test run_module_command."""
    cmd = ["test"]
    fake_process.register_subprocess(cmd, returncode=0)  # type: ignore
    assert not run_module_command(cmd, {}, exit_on_error=False)
    assert fake_process.call_count(cmd) == 1  # type: ignore
Exemple #16
0
def test_typing() -> None:
    fp = FakeProcess()
    cmd = ["ls", "-l"]
    output = ["some", "lines", "of", "output"]
    fp.register_subprocess(cmd, stdout=output)