Exemplo n.º 1
0
 def test_log_name(self, derived_from, expected, caplog, monkeypatch):
     """Test log_name."""
     caplog.set_level(logging.INFO, logger="runway")
     monkeypatch.setattr(DeployEnvironment, "name", "test")
     obj = DeployEnvironment()
     obj.name_derived_from = derived_from
     obj.log_name()
     assert caplog.messages == expected
Exemplo n.º 2
0
    def test_max_concurrent_cfngin_stacks(self) -> None:
        """Test max_concurrent_cfngin_stacks."""
        obj = DeployEnvironment(environ={})

        assert obj.max_concurrent_cfngin_stacks == 0

        obj.max_concurrent_cfngin_stacks = 5
        assert obj.max_concurrent_cfngin_stacks == 5
        assert obj.vars["RUNWAY_MAX_CONCURRENT_CFNGIN_STACKS"] == "5"
Exemplo n.º 3
0
    def test_aws_profile(self) -> None:
        """Test aws_profile."""
        env_vars = {"key": "val"}
        profile_name = "something"
        obj = DeployEnvironment(environ=env_vars)

        assert not obj.aws_profile

        obj.aws_profile = profile_name
        assert obj.aws_profile == profile_name
        assert obj.vars["AWS_PROFILE"] == profile_name
Exemplo n.º 4
0
    def test_copy(self, monkeypatch, tmp_path):
        """Test copy."""
        monkeypatch.setattr(DeployEnvironment, "name", "test")
        obj = DeployEnvironment(root_dir=tmp_path)
        obj_copy = obj.copy()

        assert obj_copy != obj
        assert obj_copy._ignore_git_branch == obj._ignore_git_branch
        assert obj_copy.name == obj.name
        assert obj_copy.name_derived_from == obj.name_derived_from
        assert obj_copy.root_dir == obj.root_dir
        assert obj_copy.vars == obj.vars
Exemplo n.º 5
0
    def test_name(self) -> None:
        """Test name."""
        obj = DeployEnvironment(explicit_name="test")
        assert obj.name == "test"
        assert obj.name_derived_from == "explicit"

        obj.name = "test2"  # type: ignore
        assert obj.name == "test2"

        del obj.name
        obj.name = "test3"  # type: ignore
        assert obj.name == "test3"
Exemplo n.º 6
0
    def test_copy(self, mocker: MockerFixture, tmp_path: Path) -> None:
        """Test copy."""
        mocker.patch.object(DeployEnvironment, "name", "test")
        obj = DeployEnvironment(root_dir=tmp_path)
        obj_copy = obj.copy()

        assert obj_copy != obj
        assert obj_copy._ignore_git_branch == obj._ignore_git_branch
        assert obj_copy.name == obj.name
        assert obj_copy.name_derived_from == obj.name_derived_from
        assert obj_copy.root_dir == obj.root_dir
        assert obj_copy.vars == obj.vars
Exemplo n.º 7
0
    def test_debug(self) -> None:
        """Test debug."""
        obj = DeployEnvironment(environ={})

        assert not obj.debug

        obj.debug = True
        assert obj.debug
        assert obj.vars["DEBUG"] == "1"

        obj.debug = False
        assert not obj.debug
        assert "DEBUG" not in obj.vars
Exemplo n.º 8
0
    def test_verbose(self) -> None:
        """Test verbose."""
        obj = DeployEnvironment(environ={})

        assert not obj.verbose

        obj.verbose = True
        assert obj.verbose
        assert obj.vars["VERBOSE"] == "1"

        obj.verbose = False
        assert not obj.verbose
        assert "VERBOSE" not in obj.vars
Exemplo n.º 9
0
    def test_max_concurrent_regions(self, mock_proc):
        """Test max_concurrent_regions."""
        mock_proc.cpu_count.return_value = 4
        obj = DeployEnvironment(environ={})

        assert obj.max_concurrent_regions == 4

        mock_proc.cpu_count.return_value = 62
        assert obj.max_concurrent_regions == 61

        obj.max_concurrent_regions = 12
        assert obj.max_concurrent_regions == 12
        assert obj.vars["RUNWAY_MAX_CONCURRENT_REGIONS"] == 12
Exemplo n.º 10
0
    def test_ci(self) -> None:
        """Test ci."""
        obj = DeployEnvironment(environ={})

        assert not obj.ci

        obj.ci = True
        assert obj.ci
        assert obj.vars["CI"] == "1"

        obj.ci = False
        assert not obj.ci
        assert "CI" not in obj.vars
Exemplo n.º 11
0
 def test_log_name(
     self,
     derived_from: str,
     expected: List[str],
     caplog: LogCaptureFixture,
     mocker: MockerFixture,
 ) -> None:
     """Test log_name."""
     caplog.set_level(logging.INFO, logger="runway")
     mocker.patch.object(DeployEnvironment, "name", "test")
     obj = DeployEnvironment()
     obj.name_derived_from = derived_from
     obj.log_name()
     assert caplog.messages == expected
Exemplo n.º 12
0
    def test_max_concurrent_regions(self, mocker: MockerFixture) -> None:
        """Test max_concurrent_regions."""
        mock_cpu_count = MagicMock(return_value=4)
        mocker.patch(f"{MODULE}.os.cpu_count", mock_cpu_count)
        obj = DeployEnvironment(environ={})

        assert obj.max_concurrent_regions == 4

        mock_cpu_count.return_value = 62
        assert obj.max_concurrent_regions == 61

        obj.max_concurrent_regions = 12
        assert obj.max_concurrent_regions == 12
        assert obj.vars["RUNWAY_MAX_CONCURRENT_REGIONS"] == "12"
Exemplo n.º 13
0
 def test_name_from_directory(self, root_dir: Path, expected: str,
                              mocker: MockerFixture) -> None:
     """Test name from directory."""
     mocker.patch.object(DeployEnvironment, "branch_name", None)
     obj = DeployEnvironment(ignore_git_branch=True, root_dir=root_dir)
     assert obj.name == expected
     assert obj.name_derived_from == "directory"
Exemplo n.º 14
0
 def get_context(name: str = "test",
                 region: str = "us-east-1") -> MockRunwayContext:
     """Create a basic Runway context object."""
     context = MockRunwayContext(deploy_environment=DeployEnvironment(
         explicit_name=name))
     context.env.aws_region = region
     return context
Exemplo n.º 15
0
    def test_init_defaults(self, cd_tmp_path: Path) -> None:
        """Test attributes set by init default values."""
        obj = DeployEnvironment()

        assert not obj._ignore_git_branch
        assert obj.name_derived_from is None
        assert obj.root_dir == cd_tmp_path
        assert obj.vars == os.environ
Exemplo n.º 16
0
    def test_aws_region(self):
        """Test aws_region."""
        env_vars = {"AWS_REGION": "us-east-1", "AWS_DEFAULT_REGION": "us-west-2"}
        obj = DeployEnvironment(environ=env_vars)

        assert obj.aws_region == "us-east-1"

        del obj.vars["AWS_REGION"]
        assert obj.aws_region == "us-west-2"

        del obj.vars["AWS_DEFAULT_REGION"]
        assert not obj.aws_region

        obj.aws_region = "us-east-1"
        assert obj.aws_region == "us-east-1"
        assert obj.vars["AWS_REGION"] == "us-east-1"
        assert obj.vars["AWS_DEFAULT_REGION"] == "us-east-1"
Exemplo n.º 17
0
    def test_branch_name_invalid_repo(self, mocker: MockerFixture) -> None:
        """Test branch_name handle InvalidGitRepositoryError."""
        mock_git = mocker.patch(f"{MODULE}.git")
        mock_git.Repo.side_effect = InvalidGitRepositoryError

        obj = DeployEnvironment()
        assert obj.branch_name is None
        mock_git.Repo.assert_called_once_with(os.getcwd(),
                                              search_parent_directories=True)
Exemplo n.º 18
0
 def __init__(
     self, *, command: Optional[str] = None, deploy_environment: Any = None, **_: Any
 ) -> None:
     """Instantiate class."""
     if not deploy_environment:
         deploy_environment = DeployEnvironment(environ={}, explicit_name="test")
     super().__init__(command=command, deploy_environment=deploy_environment)
     self._boto3_test_client = MutableMap()
     self._boto3_test_stubber = MutableMap()
     self._use_concurrent = True
Exemplo n.º 19
0
    def test_branch_name_no_git(self, mocker: MockerFixture,
                                caplog: LogCaptureFixture) -> None:
        """Test branch_name git ImportError."""
        caplog.set_level(logging.DEBUG, logger="runway.core.components")
        mocker.patch(f"{MODULE}.git", object)
        obj = DeployEnvironment()

        assert obj.branch_name is None
        assert ("failed to import git; ensure git is your path and executable "
                "to read the branch name") in caplog.messages
Exemplo n.º 20
0
    def test_branch_name_type_error(self, mock_git, caplog):
        """Test branch_name handle TypeError."""
        caplog.set_level(logging.WARNING, logger="runway")
        mock_git.Repo.side_effect = TypeError

        with pytest.raises(SystemExit) as excinfo:
            obj = DeployEnvironment()
            assert not obj.branch_name

        assert excinfo.value.code == 1
        assert "Unable to retrieve the current git branch name!" in caplog.messages
Exemplo n.º 21
0
    def test_branch_name_no_git(self, monkeypatch, caplog):
        """Test branch_name git ImportError."""
        caplog.set_level(logging.DEBUG, logger="runway.core.components")
        monkeypatch.setattr(MODULE + ".git", object)
        obj = DeployEnvironment()

        assert obj.branch_name is None
        assert (
            "failed to import git; ensure git is your path and executable "
            "to read the branch name"
        ) in caplog.messages
Exemplo n.º 22
0
 def __init__(self, command=None, deploy_environment=None):
     """Instantiate class."""
     if not deploy_environment:
         deploy_environment = DeployEnvironment(environ={},
                                                explicit_name="test")
     super(MockRunwayContext,
           self).__init__(command=command,
                          deploy_environment=deploy_environment)
     self._boto3_test_client = MutableMap()
     self._boto3_test_stubber = MutableMap()
     self._use_concurrent = True
Exemplo n.º 23
0
    def test_branch_name(self, mocker: MockerFixture) -> None:
        """Test branch_name."""
        mock_git = mocker.patch(f"{MODULE}.git")
        branch_name = "test"
        mock_repo = MagicMock()
        mock_repo.active_branch.name = branch_name
        mock_git.Repo.return_value = mock_repo

        obj = DeployEnvironment()
        assert obj.branch_name == branch_name
        mock_git.Repo.assert_called_once_with(os.getcwd(),
                                              search_parent_directories=True)
Exemplo n.º 24
0
    def test_branch_name(self, mock_git):
        """Test branch_name."""
        branch_name = "test"
        mock_repo = MagicMock()
        mock_repo.active_branch.name = branch_name
        mock_git.Repo.return_value = mock_repo

        obj = DeployEnvironment()
        assert obj.branch_name == branch_name
        mock_git.Repo.assert_called_once_with(
            os.getcwd(), search_parent_directories=True
        )
Exemplo n.º 25
0
    def test_init(self, cd_tmp_path: Path) -> None:
        """Test attributes set by init."""
        new_dir = cd_tmp_path / "new_dir"
        obj = DeployEnvironment(
            environ={"key": "val"},
            explicit_name="test",
            ignore_git_branch=True,
            root_dir=new_dir,
        )

        assert obj._ignore_git_branch
        assert obj.root_dir == new_dir
        assert obj.vars == {"key": "val"}
Exemplo n.º 26
0
    def test_name_from_branch(self, branch, environ, expected, monkeypatch):
        """Test name from branch."""
        mock_prompt = MagicMock(return_value="user_value")
        monkeypatch.setattr(MODULE + ".click.prompt", mock_prompt)
        monkeypatch.setattr(DeployEnvironment, "branch_name", branch)
        obj = DeployEnvironment(environ=environ)
        assert obj.name == expected
        if expected == "user_value":
            assert obj.name_derived_from == "explicit"
        else:
            assert obj.name_derived_from == "branch"

        if obj.ci:
            mock_prompt.assert_not_called()
Exemplo n.º 27
0
    def test_name_from_branch(self, branch: str, environ: Dict[str, str],
                              expected: str, mocker: MockerFixture) -> None:
        """Test name from branch."""
        mock_prompt = MagicMock(return_value="user_value")
        mocker.patch(f"{MODULE}.click.prompt", mock_prompt)
        mocker.patch.object(DeployEnvironment, "branch_name", branch)
        obj = DeployEnvironment(environ=environ)
        assert obj.name == expected
        if expected == "user_value":
            assert obj.name_derived_from == "explicit"
        else:
            assert obj.name_derived_from == "branch"

        if obj.ci:
            mock_prompt.assert_not_called()
Exemplo n.º 28
0
def runway_context(request: FixtureRequest) -> MockRunwayContext:
    """Create a mock Runway context object."""
    env_vars = {
        "AWS_REGION": getattr(
            cast("Module", request.module), "AWS_REGION", "us-east-1"
        ),
        "DEFAULT_AWS_REGION": getattr(
            cast("Module", request.module), "AWS_REGION", "us-east-1"
        ),
        "DEPLOY_ENVIRONMENT": getattr(
            cast("Module", request.module), "DEPLOY_ENVIRONMENT", "test"
        ),
    }
    creds = {
        "AWS_ACCESS_KEY_ID": "test_access_key",
        "AWS_SECRET_ACCESS_KEY": "test_secret_key",
        "AWS_SESSION_TOKEN": "test_session_token",
    }
    env_vars.update(getattr(cast("Module", request.module), "AWS_CREDENTIALS", creds))
    env_vars.update(getattr(cast("Module", request.module), "ENV_VARS", {}))  # type: ignore
    return MockRunwayContext(
        command="test",
        deploy_environment=DeployEnvironment(environ=env_vars, explicit_name="test"),
    )
Exemplo n.º 29
0
    def test_ignore_git_branch(self) -> None:
        """Test ignore_git_branch."""
        obj = DeployEnvironment(environ={}, explicit_name="first")

        assert not obj.ignore_git_branch
        assert obj.name == "first"

        obj._DeployEnvironment__name = "second"  # type: ignore
        obj.ignore_git_branch = False
        assert obj.name == "first"
        assert not obj.ignore_git_branch

        obj.ignore_git_branch = True
        assert obj.name == "second"
        assert obj.ignore_git_branch

        # delete attr before setting new val to force AttributeError
        del obj.name
        obj.ignore_git_branch = False
        assert obj.name == "second"
Exemplo n.º 30
0
 def test_boto3_credentials(self) -> None:
     """Test boto3_credentials."""
     obj = DeployEnvironment(environ=TEST_CREDENTIALS)
     assert obj.aws_credentials == TEST_CREDENTIALS