def test_kbenv(self, mocker: MockerFixture, runway_context: MockRunwayContext, tmp_path: Path) -> None: """Test kbenv.""" mock_env_mgr = mocker.patch(f"{MODULE}.KBEnvManager", return_value="success") assert (K8s(runway_context, module_root=tmp_path).kbenv == mock_env_mgr.return_value) mock_env_mgr.assert_called_once_with(tmp_path)
def test_skip(self, runway_context: MockRunwayContext, tmp_path: Path) -> None: """Test skip.""" obj = K8s(runway_context, module_root=tmp_path) # type: ignore assert obj.skip obj.options.kustomize_config.parent.mkdir(parents=True, exist_ok=True) obj.options.kustomize_config.touch() assert not obj.skip
def test_kubectl_bin(self, mocker: MockerFixture, runway_context: MockRunwayContext, tmp_path: Path) -> None: """Test kubectl_bin.""" obj = K8s(runway_context, module_root=tmp_path) mock_install = mocker.patch.object(obj.kbenv, "install", return_value="success") assert obj.kubectl_bin == mock_install.return_value mock_install.assert_called_once_with(None)
def test_kubectl_bin_handle_version_not_specified( self, mocker: MockerFixture, runway_context: MockRunwayContext, tmp_path: Path) -> None: """Test kubectl_bin.""" which = mocker.patch(f"{MODULE}.which", return_value=True) obj = K8s(runway_context, module_root=tmp_path) mocker.patch.object(obj.kbenv, "install", side_effect=KubectlVersionNotSpecified) assert obj.kubectl_bin == "kubectl" which.assert_called_once_with("kubectl")
def test_init( self, caplog: LogCaptureFixture, runway_context: MockRunwayContext, tmp_path: Path, ) -> None: """Test init.""" caplog.set_level(logging.WARNING, logger=MODULE) obj = K8s(runway_context, module_root=tmp_path) assert not obj.init() assert f"init not currently supported for {K8s.__name__}" in caplog.messages
def test_kubectl_delete_raise_called_process_error( self, mocker: MockerFixture, runway_context: MockRunwayContext, tmp_path: Path, ) -> None: """Test kubectl_delete raise CalledProcessError.""" mocker.patch.object(K8s, "gen_cmd") mocker.patch(f"{MODULE}.run_module_command", side_effect=CalledProcessError(1, "")) with pytest.raises(CalledProcessError): K8s(runway_context, module_root=tmp_path).kubectl_delete()
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
def test_destroy( self, mocker: MockerFixture, runway_context: MockRunwayContext, skip: bool, tmp_path: Path, ) -> None: """Test destroy.""" mocker.patch.object(K8s, "skip", skip) kubectl_kustomize = mocker.patch.object(K8s, "kubectl_kustomize") kubectl_delete = mocker.patch.object(K8s, "kubectl_delete") assert not K8s(runway_context, module_root=tmp_path).destroy() if skip: kubectl_kustomize.assert_not_called() kubectl_delete.assert_not_called() else: kubectl_kustomize.assert_called_once_with() kubectl_delete.assert_called_once_with()
def test_gen_cmd( self, args_list: Optional[List[str]], command: KubectlCommandTypeDef, expected: List[str], mocker: MockerFixture, runway_context: MockRunwayContext, tmp_path: Path, ) -> None: """Test gen_command.""" mocker.patch.object(K8s, "kubectl_bin", "kubectl") expected.insert(0, "kubectl") assert (K8s( runway_context, module_root=tmp_path, options={ "overlay_path": "overlay_path" }, ).gen_cmd(command, args_list) == expected)
def test_plan( self, caplog: LogCaptureFixture, mocker: MockerFixture, runway_context: MockRunwayContext, skip: bool, tmp_path: Path, ) -> None: """Test plan.""" caplog.set_level(logging.INFO, logger=MODULE) mocker.patch.object(K8s, "skip", skip) kubectl_kustomize = mocker.patch.object(K8s, "kubectl_kustomize", return_value="success") assert not K8s(runway_context, module_root=tmp_path).plan() if skip: kubectl_kustomize.assert_not_called() else: kubectl_kustomize.assert_called_once_with() logs = "\n".join(caplog.messages) assert ( f"kustomized yaml generated by kubectl:\n\n{kubectl_kustomize.return_value}" in logs)
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
def test_kubectl_delete( self, caplog: LogCaptureFixture, mocker: MockerFixture, runway_context: MockRunwayContext, tmp_path: Path, ) -> None: """Test kubectl_delete.""" caplog.set_level(logging.INFO, logger=MODULE) mock_gen_cmd = mocker.patch.object(K8s, "gen_cmd", return_value=["delete"]) mock_run_module_command = mocker.patch(f"{MODULE}.run_module_command") obj = K8s(runway_context, module_root=tmp_path) assert not obj.kubectl_delete() mock_gen_cmd.assert_called_once_with("delete") mock_run_module_command.assert_called_once_with( cmd_list=mock_gen_cmd.return_value, env_vars=runway_context.env.vars, logger=obj.logger, ) logs = "\n".join(caplog.messages) assert "destroy (in progress)" in logs assert "destroy (complete)" in logs