test: Enable mocking http provider sources in tests#38206
Conversation
…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.
…e test to assert those calls.
| httpGetter := getter.HttpGetter{ | ||
| Client: httpclient.New(), | ||
| Client: client, | ||
| Netrc: true, |
There was a problem hiding this comment.
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.
|
|
||
| // 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) { |
There was a problem hiding this comment.
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.
mildwonkey
left a comment
There was a problem hiding this comment.
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!
…ion signature to NewMockSource function
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
testingOverridesneeds to be set in theMetafor a provider to be used, i.e. for a provider factory to be obtainable by calling code.The existing
newMockProviderSourcehelper 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:
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
installFromHTTPURLfunction. Before the client was always generated there usinghttpclient.New(), but now the provider source can supply its own client for use instead of httpclient.New().Target Release
N/A
Rollback Plan
Changes to Security Controls
Are there any changes to security controls (access controls, encryption, logging) in this pull request? If so, explain.
CHANGELOG entry