Skip to content

Use fixtures for test credentials and region name #10207

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

Merged
merged 24 commits into from
Feb 16, 2024
Merged

Conversation

viren-nadkarni
Copy link
Member

@viren-nadkarni viren-nadkarni commented Feb 9, 2024

Background

This PR makes some code improvements for test credentials and region.

Changes

  • Remove redundant is_aws() helper and use is_aws_cloud() everywhere. Added by Extract snapshot lib #10164
  • Across our tests, instead of directly importing constants for test account ID and test region name, fixtures are used. New fixtures for secondary test account ID and secondary region name are also introduced.

@viren-nadkarni viren-nadkarni self-assigned this Feb 9, 2024
@viren-nadkarni viren-nadkarni added the semver: patch Non-breaking changes which can be included in patch releases label Feb 9, 2024
Copy link

github-actions bot commented Feb 9, 2024

LocalStack Community integration with Pro

    2 files  ±0      2 suites  ±0   1h 22m 20s ⏱️ + 2m 1s
2 641 tests +1  2 394 ✅ +1  247 💤 ±0  0 ❌ ±0 
2 643 runs  +1  2 394 ✅ +1  249 💤 ±0  0 ❌ ±0 

Results for commit f56c80d. ± Comparison against base commit b5c5603.

♻️ This comment has been updated with latest results.

Copy link

github-actions bot commented Feb 9, 2024

S3 Image Test Results (AMD64 / ARM64)

  2 files  ±0    2 suites  ±0   3m 16s ⏱️ -1s
386 tests ±0  336 ✅ ±0   50 💤 ±0  0 ❌ ±0 
772 runs  ±0  672 ✅ ±0  100 💤 ±0  0 ❌ ±0 

Results for commit f56c80d. ± Comparison against base commit b5c5603.

♻️ This comment has been updated with latest results.

Comment on lines 106 to 111
@pytest.fixture(name="region", scope="session")
def fixture_region(aws_client):
if is_aws() or is_api_enabled("sts"):
return aws_client.sts.meta.region_name
else:
return TEST_AWS_REGION_NAME
Copy link
Member Author

Choose a reason for hiding this comment

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

This is moved to localstack.testing.pytest.fixtures alongside its companion fixtures

@coveralls
Copy link

coveralls commented Feb 13, 2024

Coverage Status

coverage: 83.83% (-0.004%) from 83.834%
when pulling f56c80d on unify-fixtures
into b5c5603 on master.

@viren-nadkarni viren-nadkarni force-pushed the unify-fixtures branch 2 times, most recently from 424a97c to 934e5bd Compare February 13, 2024 09:22
ex.AWSAccessKeyId = TEST_AWS_ACCESS_KEY_ID # todo maybe return access key used for the request
ex.AWSAccessKeyId = (
constants.TEST_AWS_ACCESS_KEY_ID
) # todo maybe return access key used for the request
Copy link
Member

Choose a reason for hiding this comment

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

In this and the other cases in this file, I'd suggest we just remove it completely (basically implementing the TODO)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I plan to remove all uses of the TEST_* variables outside of the tests themselves. I've made some of it in #8204, would it be okay if I take it forward there?

Copy link
Contributor

Choose a reason for hiding this comment

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

For the presigned URLs, we will need a bit more logic especially when creating the Credentials to validate the URL and remove those constants. Right now, we explicitly say that we need the credentials to be test / test in order to validate them (so technically the test should fail when using other credentials, which is why I believe we're using TEST_* and not DEFAULT_*).

The secret access key is not passed in the signature, so we will need a network request to fetch it like IAM does, in order to properly validate it. But I agree that implementing this TODO would be really great!

Let me know if there's anything I can do there.


@pytest.fixture(scope="session")
def secondary_region_name():
return constants.SECONDARY_TEST_AWS_REGION_NAME
Copy link
Member

Choose a reason for hiding this comment

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

Not a particular fan of using the constants for the fixtures. One of the longer-running efforts will be moving to a state where we won't have access to localstack internals from the test code (e.g. because we're running LS in a container and test against that). I also think constants should generally not be a place where we put values used in testing.

How about we use environment variables to configure these and falling back to a static default in the fixture?

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, this value already gives precedence to the env variable

https://github.com/localstack/localstack/blob/unify-fixtures/localstack/constants.py#L159-L163

I admit that its place in localstack.constants is misleading.

Would it be okay if I add a todo note and make the changes in a follow up PR, given how large this PR is already?

Copy link
Member

@dominikschubert dominikschubert left a comment

Choose a reason for hiding this comment

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

LGTM, didn't spot any obvious issues.

I have a bit of a concern in terms of using this fixture for unit tests, though. I think keeping those static is fine

region_name="us-east-1",
region_name=region_name,
Copy link
Member

Choose a reason for hiding this comment

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

I'm not so sure we want to do this with unit tests. The behavior of the underlying region lookup seems to be not as well defined here 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right, I picked this from #8204 but I'm unable to reproduce the original reason for the change. Reverted for now, I'll revisit this if it reoccurs.

# Boto still does not support chunk encoding, which means we can't test with the client nor
# aws_http_client_factory. See open issue: https://github.com/boto/boto3/issues/751
# Test for https://github.com/localstack/localstack/issues/1571
object_key = "data"
body = "Hello\r\n\r\n\r\n\r\n"
headers = {
"Authorization": mock_aws_request_headers(
"s3", aws_access_key_id=TEST_AWS_ACCESS_KEY_ID, region_name=TEST_AWS_REGION_NAME
"s3",
aws_access_key_id=TEST_AWS_ACCESS_KEY_ID,
Copy link
Member

Choose a reason for hiding this comment

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

we should be able to get this via STS as well,so might be worth to make this a fixture too

Copy link
Member Author

@viren-nadkarni viren-nadkarni Feb 16, 2024

Choose a reason for hiding this comment

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

Do you mean STS:GetSessionToken? I'll make a note, thanks!

@viren-nadkarni viren-nadkarni merged commit f58f922 into master Feb 16, 2024
@viren-nadkarni viren-nadkarni deleted the unify-fixtures branch February 16, 2024 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver: patch Non-breaking changes which can be included in patch releases
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants