Skip to content

test: Enable mocking http provider sources in tests#38206

Merged
SarahFrench merged 10 commits intomainfrom
sarah/allow-mocking-http-provider-source
Feb 26, 2026
Merged

test: Enable mocking http provider sources in tests#38206
SarahFrench merged 10 commits intomainfrom
sarah/allow-mocking-http-provider-source

Conversation

@SarahFrench
Copy link
Member

@SarahFrench SarahFrench commented Feb 24, 2026

This is part of breaking #38205 into smaller PRs.

This PR includes work to enable tests where Terraform is led to believe it's downloading a provider via HTTP. This is necessary in the linked PR because we want Terraform to behave differently based on where a provider is downloaded from.

Context:

The helper newMockProviderSource is used when Terraform's provider installation logic is under test. The provider source is used by Terraform when 'installing' providers during init, but testingOverrides needs to be set in the Meta for a provider to be used, i.e. for a provider factory to be obtainable by calling code.

The existing newMockProviderSource helper always makes Terraform believe the provider is downloaded from a local archive (see this line in the helper, and then this line from the invoked function).

In this PR I have updated the getproviders.MockSource mock to be able to mock download via HTTP, by allowing a mock source to use a paired test HTTP server.

The HTTP server needs to:

  • Enable TLS
  • Have a handler for the correct path for a given provider that returns data matching checksums for that provider.
    • Note that the data is not used beyond comparison to a checksum; testingOverrides is still what's used when launching a provider child process, as noted above.

To enable TLS in the tests I've made changes (improved via pairing with @dsa0x, thnx!) to how the HTTP client is obtained in the installFromHTTPURL function. Before the client was always generated there using httpclient.New(), but now the provider source can supply its own client for use instead of httpclient.New().

Target Release

N/A

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.

…talled via HTTP, add `newMockProviderSourceViaHTTP` for direct use in tests.

Compare newMockProviderSourceViaHTTP to the existing newMockProviderSource. newMockProviderSource uses FakeInstallablePackageMeta, which creates metadata for providers that says it's being sourced from a local archive. In newMockProviderSourceViaHTTP the metadata reports the provider is downloaded via HTTP. The URL is created using a base URL that should be obtained from a test http server created in the test.
…from a mock source that uses an http server. Allowing the mock server to provide a client helps navigate self-signed cert issues with TLS. Update impacted tests and calling code (rpcapi).
…act away making the http test server. Add test showing it in use.
@SarahFrench SarahFrench added the no-changelog-needed Add this to your PR if the change does not require a changelog entry label Feb 24, 2026
httpGetter := getter.HttpGetter{
Client: httpclient.New(),
Client: client,
Netrc: true,
Copy link
Member Author

Choose a reason for hiding this comment

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

These changes are necessary to enable TLS using the self-signed cert from the httptest.Server made in the test setup. The client needs to be configured with InsecureSkipVerify set to true so that the self-signed cert doesn't cause issues, and we want to supply that cert in a test-specific way.

Only the mock provider source implements the ClientReturningSource interface, so only mock provider sources will lead to this method receiving a non-nil client. For end users of Terraform the incoming *http.Client value will always be nil, so the client will be created in the original and secure way here in this function.

@SarahFrench SarahFrench marked this pull request as ready for review February 24, 2026 14:32
@SarahFrench SarahFrench requested a review from a team as a code owner February 24, 2026 14:32

// Testing init's behaviors with `state_store` when run in an empty working directory
func TestInit_stateStore_newWorkingDir(t *testing.T) {
t.Run("temporary: test showing use of HTTP server in mock provider source", func(t *testing.T) {
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 test is here to illustrate using the new helpers. My plan is to delete it in a subsequent PR that makes use of the new helpers while implementing new features.

I'm happy to remove them in this PR before merging, once they're no longer needed for reference by the reviewer.

Copy link
Contributor

@mildwonkey mildwonkey left a comment

Choose a reason for hiding this comment

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

I've left a bunch of nitpicky comments, and I feel at least somewhat strongly that ClientReturningSource should have Test or Mock or Fake in the name since it's showing up in "production" codepaths, but definitely nothing in here I'd block you over!

@SarahFrench SarahFrench merged commit 9f5f21f into main Feb 26, 2026
7 checks passed
@SarahFrench SarahFrench deleted the sarah/allow-mocking-http-provider-source branch February 26, 2026 10:24
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