terraform/internal/command/workdir/testing.go
Sarah French efb2a1c567
PSS: Let the init command recognise when there are no changes in configuration. (#37777)
* Pull determining of PSS provider's version from current locks into a separate method

* Add code for identifying when config and provider version match existing backend state (i.e. no changes)

* Update test - locks are now needed before it hits expected error diag return

* Add test showing successful init when no config changes are detected.

* Update `getStateStorageProviderVersion` to return nil versions for builtin and re-attached providers.

This makes comparison easier when determining if config has changed since last init.

* Add test coverage for `getStateStorageProviderVersion`

* Move testing fixtures around, preparing for different types of changed state_store config changes being tested

* Add test showing that changing the state_store config is detected as a change, but handling this scenario isn't implemented yet

* Update hashes in test fixture backend state file to be accurate

Previously dummy values were fine, but as tests using hashes to identify changes these values need to be accurate!

* Update existing test cases so that Terraform uses the same test provider version as described in the backend state file fixture for the test.

* Add test showing that changing the PSS provider's config is detected as a change, but handling this scenario isn't implemented yet

* Add test showing that swapping to a different state storage implementation in the same provider is detected as a change, but handling this scenario isn't implemented yet

* Add test showing that changing the provider used for PSS is detected as a change, but handling this scenario isn't implemented yet

* Add test showing that upgrading a provider is detected as a change, but handling this scenario isn't implemented yet

* Update test to use v1.2.3 for consistency with other tests

Just to avoid any confusion if copy-pasting happens in future.

* More corrections to existing test fixtures - unset config should be null, and replace dummy hash values with correct values.

* Fix test for using -reconfigure with state_store; the default workspace would already exist in this scenario

* Update TestInit_stateStore_configUnchanged to assert that init was a no-op for backend state

* Remove unused fixture

* Remove test that's replaced by new tests in command/init_test.go

* Replace old references to deleted "state-store-changed" test fixture & update test to not expect a value for region attr in provider config

* Make test fixture coupling a little more understandable

* Refactor detection of no need to migrate into a function

* Add TODO about more involved provider version change tests

We will allow downgrades to succeed as long as the schema version number is unchanged

* Update (configs.StateStore)Hash method to return a single hash that's impacted by: state store config, provider config, state store type, provider source

* Update calling code and test helper code to reflect that the nested provider block no longer has its own hash

* Remove test; there is now a single hash that SHOULD be affected by the provider block!

* Also use provider name, from config, in hash

* Update tests to reflect changes in how hashes are made

* Remove unused `stateStoreConfigNeedsMigration` function

* Remove duplicate isProviderReattached function.

* Fixes to affected tests

* Allow provider version to impact the state storage hash, update impacted tests and test fixtures

* Update tests that now require locks data to be present in test setup

* Update comment for accuracy

* Fixes to other test fixtures - remove excess hash field, set hash to 0 to indicate they're not set accurately.

* Make upgrade test actually use upgrade code path

* Add lock files to test fixture directories that represent a project that's had a successful prior init using PSS
2025-10-20 16:51:45 +01:00

41 lines
1 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package workdir
import (
"testing"
version "github.com/hashicorp/go-version"
tfaddr "github.com/hashicorp/terraform-registry-address"
svchost "github.com/hashicorp/terraform-svchost"
)
// getTestProviderState is a test helper that returns a state representation
// of a provider used for managing state via pluggable state storage.
// The Hash is always hardcoded at 12345.
func getTestProviderState(t *testing.T, semVer, hostname, namespace, typeName, config string) *ProviderConfigState {
t.Helper()
var ver *version.Version
if semVer == "" {
// Allow passing no version in; leave ver nil
ver = nil
} else {
var err error
ver, err = version.NewSemver(semVer)
if err != nil {
t.Fatalf("test setup failed when creating version.Version: %s", err)
}
}
return &ProviderConfigState{
Version: ver,
Source: &tfaddr.Provider{
Hostname: svchost.Hostname(hostname),
Namespace: namespace,
Type: typeName,
},
ConfigRaw: []byte(config),
}
}