Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SEC-19137 Add additional get_aws_credentials tests to spark-run #3919

Merged
merged 4 commits into from
Jul 18, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions tests/cli/test_cmds_spark_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -1450,3 +1450,75 @@ def test_get_aws_credentials():
RoleSessionName=mock.ANY,
WebIdentityToken="token-content",
)


@mock.patch("service_configuration_lib.spark_config.use_aws_profile", autospec=False)
@mock.patch("service_configuration_lib.spark_config.Session", autospec=True)
def test_get_aws_credentials_session(mock_boto3_session, mock_use_aws_profile):
# prioritize session over `profile_name` if both are provided
from service_configuration_lib.spark_config import get_aws_credentials
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to not have imports at the top? LGTM otherwise

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved all the import statements!


session = mock_boto3_session()

get_aws_credentials(
service="some-service",
session=session,
profile_name="some-profile",
)

mock_use_aws_profile.assert_called_once_with(session=session)
session.assert_not_called()


@mock.patch("service_configuration_lib.spark_config.use_aws_profile", autospec=False)
@mock.patch("service_configuration_lib.spark_config.Session", autospec=True)
def test_get_aws_credentials_profile(mock_boto3_session, mock_use_aws_profile):
# prioritize `profile_name` over `service` if both are provided
from service_configuration_lib.spark_config import get_aws_credentials

profile_name = "some-profile"

get_aws_credentials(service="some-service", profile_name=profile_name)

mock_use_aws_profile.assert_called_once_with(profile_name=profile_name)


@mock.patch("service_configuration_lib.spark_config.use_aws_profile", autospec=False)
@mock.patch("os.path.exists", autospec=False)
@mock.patch(
"service_configuration_lib.spark_config._load_aws_credentials_from_yaml",
autospec=True,
)
def test_get_aws_credentials_boto_cfg(
mock_load_aws_credentials_from_yaml, mock_os_path_exists, mock_use_aws_profile
):
# use `service` if profile_name is not provided
from service_configuration_lib.spark_config import (
get_aws_credentials,
AWS_CREDENTIALS_DIR,
)

service_name = "some-service"

get_aws_credentials(
service=service_name,
)

credentials_path = f"{AWS_CREDENTIALS_DIR}{service_name}.yaml"
mock_os_path_exists.return_value = True

mock_load_aws_credentials_from_yaml.assert_called_once_with(credentials_path)
mock_use_aws_profile.assert_not_called()


@mock.patch("service_configuration_lib.spark_config.use_aws_profile", autospec=False)
@mock.patch("service_configuration_lib.spark_config.Session", autospec=True)
def test_get_aws_credentials_default_profile(mock_boto3_session, mock_use_aws_profile):
# use `default` profile if no valid options are provided
from service_configuration_lib.spark_config import get_aws_credentials

get_aws_credentials(
service="spark",
)

mock_use_aws_profile.assert_called_once()
choww marked this conversation as resolved.
Show resolved Hide resolved
Loading