Example #1
0
    def test_handle_backend_remote_undetermined(
        self,
        caplog: LogCaptureFixture,
        monkeypatch: MonkeyPatch,
        runway_context: MockRunwayContext,
        tmp_path: Path,
    ) -> None:
        """Test handle_backend for remote backend with workspace undetermined."""
        caplog.set_level(LogLevels.WARNING, logger=MODULE)
        monkeypatch.delenv("TF_WORKSPACE", raising=False)
        mock_get_full_configuration = MagicMock(return_value={})
        backend: Dict[str, Union[Dict[str, Any], str]] = {
            "type": "remote",
            "config": {},
        }

        obj = Terraform(runway_context, module_root=tmp_path)
        monkeypatch.setattr(obj, "tfenv", MagicMock(backend=backend))
        monkeypatch.setattr(
            obj.options.backend_config,
            "get_full_configuration",
            mock_get_full_configuration,
        )

        assert not obj.handle_backend()
        mock_get_full_configuration.assert_called_once_with()
        assert '"workspaces" not defined in backend config' in "\n".join(
            caplog.messages)
Example #2
0
    def test_handle_backend_remote_prefix(
        self,
        caplog: LogCaptureFixture,
        monkeypatch: MonkeyPatch,
        runway_context: MockRunwayContext,
        tmp_path: Path,
    ) -> None:
        """Test handle_backend for remote backend with workspace prefix."""
        caplog.set_level(LogLevels.DEBUG, logger=MODULE)
        monkeypatch.delenv("TF_WORKSPACE", raising=False)
        mock_get_full_configuration = MagicMock(return_value={})
        backend = {
            "type": "remote",
            "config": {
                "workspaces": {
                    "prefix": "test"
                }
            }
        }

        obj = Terraform(runway_context, module_root=tmp_path)
        monkeypatch.setattr(obj, "tfenv", MagicMock(backend=backend))
        monkeypatch.setattr(
            obj.options.backend_config,
            "get_full_configuration",
            mock_get_full_configuration,
        )

        assert not obj.handle_backend()
        mock_get_full_configuration.assert_called_once_with()
        assert obj.ctx.env.vars["TF_WORKSPACE"] == obj.ctx.env.name
        assert 'set environment variable "TF_WORKSPACE" to avoid prompt' in "\n".join(
            caplog.messages)
Example #3
0
    def test_handle_backend_remote_name(
        self,
        caplog: LogCaptureFixture,
        monkeypatch: MonkeyPatch,
        runway_context: MockRunwayContext,
        tmp_path: Path,
    ) -> None:
        """Test handle_backend for remote backend with workspace prefix."""
        caplog.set_level(LogLevels.DEBUG, logger=MODULE)
        monkeypatch.setenv("TF_WORKSPACE", "anything")
        mock_get_full_configuration = MagicMock(return_value={})
        backend = {
            "type": "remote",
            "config": {
                "workspaces": {
                    "name": "test"
                }
            }
        }

        obj = Terraform(runway_context, module_root=tmp_path)
        monkeypatch.setattr(obj, "tfenv", MagicMock(backend=backend))
        monkeypatch.setattr(
            obj.options.backend_config,
            "get_full_configuration",
            mock_get_full_configuration,
        )

        assert not obj.handle_backend()
        mock_get_full_configuration.assert_called_once_with()
        assert "TF_WORKSPACE" not in obj.ctx.env.vars
        assert obj.required_workspace == "default"
        assert 'forcing use of static workspace "default"' in "\n".join(
            caplog.messages)
Example #4
0
    def test_handle_backend_no_handler(
        self,
        caplog: LogCaptureFixture,
        mocker: MockerFixture,
        runway_context: MockRunwayContext,
        tmp_path: Path,
    ) -> None:
        """Test handle_backend with no handler."""
        caplog.set_level(LogLevels.DEBUG, logger=MODULE)
        mock_get_full_configuration = MagicMock(return_value={})
        backend: Dict[str, Union[Dict[str, Any], str]] = {
            "type": "unsupported",
            "config": {},
        }

        obj = Terraform(runway_context, module_root=tmp_path)
        mocker.patch.object(obj, "tfenv", MagicMock(backend=backend))
        mocker.patch.object(
            obj.options.backend_config,
            "get_full_configuration",
            mock_get_full_configuration,
        )
        assert not obj.handle_backend()
        mock_get_full_configuration.assert_not_called()
        assert 'backed "unsupported" does not require special handling' in "\n".join(
            caplog.messages)
Example #5
0
 def test_handle_backend_no_type(
     self,
     caplog: LogCaptureFixture,
     mocker: MockerFixture,
     runway_context: MockRunwayContext,
     tmp_path: Path,
 ) -> None:
     """Test handle_backend with no type."""
     caplog.set_level(LogLevels.INFO, logger=MODULE)
     obj = Terraform(runway_context, module_root=tmp_path)
     mocker.patch.object(obj, "tfenv", MagicMock(backend={"type": None}))
     assert not obj.handle_backend()
     assert "unable to determine backend for module" in "\n".join(
         caplog.messages)