Skip to content

refactor: Separate concerns in mock provider sources logic, handle callbacks using t.Cleanup#38446

Merged
SarahFrench merged 4 commits intomainfrom
sarah/refactor-mock-provider-sources
Apr 27, 2026
Merged

refactor: Separate concerns in mock provider sources logic, handle callbacks using t.Cleanup#38446
SarahFrench merged 4 commits intomainfrom
sarah/refactor-mock-provider-sources

Conversation

@SarahFrench
Copy link
Copy Markdown
Member

@SarahFrench SarahFrench commented Apr 27, 2026

For a separate PR I've been working on implementing a test helper that returns an http server that acts as a network mirror. For that to work the server needs to be able to serve data from files that 'look like' provider archives and have associated checksums.

That logic already exists but it was inside of getproviders.FakeInstallablePackageMeta. That function both created temporary files and returned PackageMeta data. I've split the file creation out into a new CreateFakeFileWithChecksumForProvider function that in future I will reuse when implementing the mock network mirror.

During this refactoring I realised that there a lot of callbacks returned to calling code for cleanup. I've updated existing helpers and made the new helper use the passed in testing.T argument to handle cleanup internally via t.Cleanup. I don't believe there's a special reason why this isn't already used in provider source-related helpers; when these helpers were originally added in March 2020 (#24477) the t.Cleanup function had only just been introduced to Go the month before and I imagine hadn't been adopted in the codebase yet.

Note to reviewer:

Please go commit-by-commit to understand the diffs! I've split things up.

That final commit dramatically increased the footprint of this PR 😅 - we could avoid this by reverting the last commit and using the approach in 5c6fe51 , returning a no-op callback and leaving calling code unchanged. If we chose that approach we'd need to avoid copy-paste of existing tests and instead have new tests ignore the returned no-op callback. In my opinion it's better to rip the bandaid off now.

Target Release

1.16.x

Rollback Plan

  • If a change needs to be reverted, we will roll out an update to the code within 7 days.

Changes to Security Controls

Are there any changes to security controls (access controls, encryption, logging) in this pull request? If so, explain.

CHANGELOG entry

  • This change is user-facing and I added a changelog entry.
  • This change is not user-facing.

@SarahFrench SarahFrench added the no-changelog-needed Add this to your PR if the change does not require a changelog entry label Apr 27, 2026
@SarahFrench SarahFrench force-pushed the sarah/refactor-mock-provider-sources branch from 8ddc6e7 to 4aab9d9 Compare April 27, 2026 10:11
// The caller is responsible for calling the close callback to clean up the temporary file.
// The temporary file is only used to calculate checksums and isn't actually used to install the provider in the test.
func FakePackageMetaViaHTTP(provider addrs.Provider, version Version, protocols VersionList, target Platform, locationBaseUrl string, execFilename string) (PackageMeta, func(), error) {
func FakePackageMetaViaHTTP(t *testing.T, provider addrs.Provider, version Version, protocols VersionList, target Platform, locationBaseUrl string) (PackageMeta, error) {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'd recommend going commit-by-commit in this PR, but figured I'd highlight that this is the central part of the changes.

@SarahFrench SarahFrench marked this pull request as ready for review April 27, 2026 10:33
@SarahFrench SarahFrench requested a review from a team as a code owner April 27, 2026 10:33
Copy link
Copy Markdown
Member

@radeksimko radeksimko left a comment

Choose a reason for hiding this comment

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

Nice cleanup, thanks

Copy link
Copy Markdown
Member

@matejrisek matejrisek left a comment

Choose a reason for hiding this comment

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

TIL about t.Cleanup()

🚀

@SarahFrench SarahFrench merged commit 650fd34 into main Apr 27, 2026
13 checks passed
@SarahFrench SarahFrench deleted the sarah/refactor-mock-provider-sources branch April 27, 2026 11:13
austinvalle pushed a commit that referenced this pull request Apr 28, 2026
…llbacks using `t.Cleanup` (#38446)

* refactor: Split temp file creation logic out of FakeInstallablePackageMeta & FakePackageMetaViaHTTP. Handle cleanup internally.

* refactor: Use 'Must' helpers in `newMockProviderSourceUsingTestHttpServer`

* refactor: Update tests using `newMockProviderSource` to not handle 'close' callback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-changelog-needed Add this to your PR if the change does not require a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants