From 34bf4e31612ab33c3443711589b685bb3bb5266f Mon Sep 17 00:00:00 2001 From: Anthony Hoivik <38190936+hoivikaj@users.noreply.github.com> Date: Wed, 15 Apr 2026 22:08:57 -0400 Subject: [PATCH] feat(acc): Add provider_acceptance_tests input for automated acceptance test setup (#97) Signed-off-by: hoivikaj Signed-off-by: Anthony Hoivik <38190936+hoivikaj@users.noreply.github.com> --- README.md | 17 ++++++++ action.yml | 4 ++ dist/index.js | 11 +++++ lib/setup-tofu.js | 13 +++++- lib/test/setup-tofu.test.js | 85 ++++++++++++++++++++++++++++++++++++- package-lock.json | 24 +++++------ 6 files changed, 140 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 2479214..d4bd250 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,19 @@ steps: cache: true ``` +Provider acceptance test environment variables can be configured automatically by setting `provider_acceptance_tests` to `true`. This exports `TF_ACC`, `TF_ACC_PROVIDER_NAMESPACE`, `TF_ACC_PROVIDER_HOST`, and `TF_ACC_TERRAFORM_PATH` so you don't need to set them manually: + +```yaml +steps: +- uses: opentofu/setup-opentofu@v2 + with: + tofu_wrapper: false + provider_acceptance_tests: true +- run: go mod download +- run: go test -v -cover ./... + timeout-minutes: 10 +``` + Validation can be enabled to check the downloaded OpenTofu CLI SHA256 hash against a newline-delimited list of checksums: ```yaml @@ -285,6 +298,10 @@ The action supports the following inputs: the `tofu` binary and expose its STDOUT, STDERR, and exit code as outputs named `stdout`, `stderr`, and `exitcode` respectively. Defaults to `true`. - `cache` - (optional) Whether to use GitHub Actions tool-cache to store and reuse downloaded OpenTofu binaries. Defaults to `false`. +- `provider_acceptance_tests` - (optional) Whether to automatically set environment variables for running + OpenTofu provider acceptance tests. When set to `true`, the following environment variables are exported: + `TF_ACC=1`, `TF_ACC_PROVIDER_NAMESPACE=hashicorp`, `TF_ACC_PROVIDER_HOST=registry.opentofu.org`, and + `TF_ACC_TERRAFORM_PATH=`. Defaults to `false`. - `github_token` - (optional) Override the GitHub token read from the environment variable. Defaults to the value of the `GITHUB_TOKEN` environment variable unless running on Forgejo or Gitea. - `checksums` - (optional) A newline-delimited list of valid checksums (SHA256) for the downloaded OpenTofu CLI ZIP. When set, the action will verify the ZIP matches one of the checksums before proceeding. Defaults to `[]` diff --git a/action.yml b/action.yml index 575f48e..19a2d95 100644 --- a/action.yml +++ b/action.yml @@ -28,6 +28,10 @@ inputs: description: 'API token for GitHub to increase the rate limit. Defaults to the GITHUB_TOKEN environment variable unless running on Forgejo/Gitea.' default: '' required: false + provider_acceptance_tests: + description: 'Whether to automatically set environment variables for running OpenTofu provider acceptance tests (TF_ACC, TF_ACC_PROVIDER_NAMESPACE, TF_ACC_PROVIDER_HOST, TF_ACC_TERRAFORM_PATH). Defaults to `false`.' + default: 'false' + required: false checksums: description: Newline-delimited list of valid checksums (SHA256 hashes) for the downloaded OpenTofu binary. When set, the action will verify that the binary matches one of these checksums before proceeding. required: false diff --git a/dist/index.js b/dist/index.js index 1256151..5076b38 100644 --- a/dist/index.js +++ b/dist/index.js @@ -34811,6 +34811,7 @@ async function run () { const credentialsHostname = getInput('cli_config_credentials_hostname'); const credentialsToken = getInput('cli_config_credentials_token'); const wrapper = getInput('tofu_wrapper') === 'true'; + const providerAcceptanceTest = getInput('provider_acceptance_tests') === 'true'; const useCache = getInput('cache') === 'true'; const checksums = getMultilineInput('checksums'); let githubToken = getInput('github_token'); @@ -34884,6 +34885,16 @@ async function run () { // Add to path addPath(pathToCLI); + // Set up provider acceptance test environment variables + if (providerAcceptanceTest) { + const exeSuffix = (0,external_os_namespaceObject.platform)().startsWith('win') ? '.exe' : ''; + const binaryName = wrapper ? `tofu-bin${exeSuffix}` : `tofu${exeSuffix}`; + exportVariable('TF_ACC', '1'); + exportVariable('TF_ACC_PROVIDER_NAMESPACE', 'hashicorp'); + exportVariable('TF_ACC_PROVIDER_HOST', 'registry.opentofu.org'); + exportVariable('TF_ACC_TERRAFORM_PATH', (0,external_path_namespaceObject.join)(pathToCLI, binaryName)); + } + // Add credentials to file if they are provided if (credentialsHostname && credentialsToken) { await addCredentials(credentialsHostname, credentialsToken, osPlatform); diff --git a/lib/setup-tofu.js b/lib/setup-tofu.js index 3218dc4..43ca5ab 100644 --- a/lib/setup-tofu.js +++ b/lib/setup-tofu.js @@ -7,7 +7,7 @@ // Node.js core import { promises as fs } from 'fs'; import { platform, arch } from 'os'; -import { sep, resolve, dirname } from 'path'; +import { sep, join, resolve, dirname } from 'path'; import { fileURLToPath } from 'url'; // External @@ -177,6 +177,7 @@ async function run () { const credentialsHostname = getInput('cli_config_credentials_hostname'); const credentialsToken = getInput('cli_config_credentials_token'); const wrapper = getInput('tofu_wrapper') === 'true'; + const providerAcceptanceTest = getInput('provider_acceptance_tests') === 'true'; const useCache = getInput('cache') === 'true'; const checksums = getMultilineInput('checksums'); let githubToken = getInput('github_token'); @@ -250,6 +251,16 @@ async function run () { // Add to path addPath(pathToCLI); + // Set up provider acceptance test environment variables + if (providerAcceptanceTest) { + const exeSuffix = platform().startsWith('win') ? '.exe' : ''; + const binaryName = wrapper ? `tofu-bin${exeSuffix}` : `tofu${exeSuffix}`; + exportVariable('TF_ACC', '1'); + exportVariable('TF_ACC_PROVIDER_NAMESPACE', 'hashicorp'); + exportVariable('TF_ACC_PROVIDER_HOST', 'registry.opentofu.org'); + exportVariable('TF_ACC_TERRAFORM_PATH', join(pathToCLI, binaryName)); + } + // Add credentials to file if they are provided if (credentialsHostname && credentialsToken) { await addCredentials(credentialsHostname, credentialsToken, osPlatform); diff --git a/lib/test/setup-tofu.test.js b/lib/test/setup-tofu.test.js index 2ae4cb0..2093539 100644 --- a/lib/test/setup-tofu.test.js +++ b/lib/test/setup-tofu.test.js @@ -47,7 +47,7 @@ jest.unstable_mockModule('../util.js', () => ({ })); // Dynamic imports pick up the mocks -const { getInput, getMultilineInput, warning } = await import('@actions/core'); +const { getInput, warning, exportVariable, getMultilineInput } = await import('@actions/core'); const tc = await import('@actions/tool-cache'); const io = await import('@actions/io'); const { getRelease } = await import('../releases.js'); @@ -94,6 +94,7 @@ describe('setup-tofu', () => { cli_config_credentials_hostname: '', cli_config_credentials_token: '', tofu_wrapper: 'true', + provider_acceptance_tests: 'false', cache: 'false', github_token: '' }; @@ -277,6 +278,7 @@ describe('setup-tofu', () => { cli_config_credentials_hostname: '', cli_config_credentials_token: '', tofu_wrapper: 'true', + provider_acceptance_tests: 'false', cache: 'false', github_token: '' }; @@ -292,6 +294,87 @@ describe('setup-tofu', () => { }); }); + describe('provider_acceptance_tests functionality', () => { + it('should export acceptance test env vars when enabled without wrapper', async () => { + getInput.mockImplementation((name) => { + const defaults = { + tofu_version: fallbackVersion, + tofu_version_file: '', + cli_config_credentials_hostname: '', + cli_config_credentials_token: '', + tofu_wrapper: 'false', + provider_acceptance_tests: 'true', + cache: 'false', + github_token: '' + }; + return defaults[name] || ''; + }); + + await setup(); + + expect(exportVariable).toHaveBeenCalledWith('TF_ACC', '1'); + expect(exportVariable).toHaveBeenCalledWith('TF_ACC_PROVIDER_NAMESPACE', 'hashicorp'); + expect(exportVariable).toHaveBeenCalledWith('TF_ACC_PROVIDER_HOST', 'registry.opentofu.org'); + expect(exportVariable).toHaveBeenCalledWith( + 'TF_ACC_TERRAFORM_PATH', + expect.stringContaining('tofu') + ); + expect(exportVariable).not.toHaveBeenCalledWith( + 'TF_ACC_TERRAFORM_PATH', + expect.stringContaining('tofu-bin') + ); + }); + + it('should point TF_ACC_TERRAFORM_PATH to tofu-bin when wrapper is enabled', async () => { + getInput.mockImplementation((name) => { + const defaults = { + tofu_version: fallbackVersion, + tofu_version_file: '', + cli_config_credentials_hostname: '', + cli_config_credentials_token: '', + tofu_wrapper: 'true', + provider_acceptance_tests: 'true', + cache: 'false', + github_token: '' + }; + return defaults[name] || ''; + }); + + await setup(); + + expect(exportVariable).toHaveBeenCalledWith('TF_ACC', '1'); + expect(exportVariable).toHaveBeenCalledWith('TF_ACC_PROVIDER_NAMESPACE', 'hashicorp'); + expect(exportVariable).toHaveBeenCalledWith('TF_ACC_PROVIDER_HOST', 'registry.opentofu.org'); + expect(exportVariable).toHaveBeenCalledWith( + 'TF_ACC_TERRAFORM_PATH', + expect.stringContaining('tofu-bin') + ); + }); + + it('should not export acceptance test env vars when disabled', async () => { + getInput.mockImplementation((name) => { + const defaults = { + tofu_version: fallbackVersion, + tofu_version_file: '', + cli_config_credentials_hostname: '', + cli_config_credentials_token: '', + tofu_wrapper: 'false', + provider_acceptance_tests: 'false', + cache: 'false', + github_token: '' + }; + return defaults[name] || ''; + }); + + await setup(); + + expect(exportVariable).not.toHaveBeenCalledWith('TF_ACC', expect.anything()); + expect(exportVariable).not.toHaveBeenCalledWith('TF_ACC_PROVIDER_NAMESPACE', expect.anything()); + expect(exportVariable).not.toHaveBeenCalledWith('TF_ACC_PROVIDER_HOST', expect.anything()); + expect(exportVariable).not.toHaveBeenCalledWith('TF_ACC_TERRAFORM_PATH', expect.anything()); + }); + }); + describe('checksum validation', () => { const mockChecksum = 'a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6'; const mockChecksum2 = 'fcfac0a865b57e0711cb8bae47c8fc70ad93198fa6146a40020ea459715fda83'; diff --git a/package-lock.json b/package-lock.json index 85716cc..ca6b7cc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1115,9 +1115,9 @@ } }, "node_modules/@jest/reporters/node_modules/brace-expansion": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.2.tgz", - "integrity": "sha512-Jt0vHyM+jmUBqojB7E1NIYadt0vI0Qxjxd2TErW94wDz+E2LAm5vKMXXwg6ZZBTHPuUlDgQHKXvjGBdfcF1ZDQ==", + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.3.tgz", + "integrity": "sha512-MCV/fYJEbqx68aE58kv2cA/kiky1G8vux3OR6/jbS+jIMe/6fJWa0DTzJU7dqijOWYwHi1t29FlfYI9uytqlpA==", "dev": true, "license": "MIT", "dependencies": { @@ -2217,9 +2217,9 @@ } }, "node_modules/brace-expansion": { - "version": "1.1.12", - "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.12.tgz", - "integrity": "sha512-9T9UjW3r0UW5c1Q7GTwllptXwhvYmEzFhzMfZ9H7FQWt+uZePjZPjBP/W1ZEyZ1twGWom5/56TF4lPcqjnDHcg==", + "version": "1.1.13", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.13.tgz", + "integrity": "sha512-9ZLprWS6EENmhEOpjCYW2c8VkmOvckIJZfkr7rBW6dObmfgJ/L1GpSYW5Hpo9lDz4D1+n0Ckz8rU7FwHDQiG/w==", "dev": true, "license": "MIT", "dependencies": { @@ -4546,9 +4546,9 @@ } }, "node_modules/jest-config/node_modules/brace-expansion": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.2.tgz", - "integrity": "sha512-Jt0vHyM+jmUBqojB7E1NIYadt0vI0Qxjxd2TErW94wDz+E2LAm5vKMXXwg6ZZBTHPuUlDgQHKXvjGBdfcF1ZDQ==", + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.3.tgz", + "integrity": "sha512-MCV/fYJEbqx68aE58kv2cA/kiky1G8vux3OR6/jbS+jIMe/6fJWa0DTzJU7dqijOWYwHi1t29FlfYI9uytqlpA==", "dev": true, "license": "MIT", "dependencies": { @@ -4880,9 +4880,9 @@ } }, "node_modules/jest-runtime/node_modules/brace-expansion": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.2.tgz", - "integrity": "sha512-Jt0vHyM+jmUBqojB7E1NIYadt0vI0Qxjxd2TErW94wDz+E2LAm5vKMXXwg6ZZBTHPuUlDgQHKXvjGBdfcF1ZDQ==", + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.3.tgz", + "integrity": "sha512-MCV/fYJEbqx68aE58kv2cA/kiky1G8vux3OR6/jbS+jIMe/6fJWa0DTzJU7dqijOWYwHi1t29FlfYI9uytqlpA==", "dev": true, "license": "MIT", "dependencies": {