2024-04-07 19:31:23 -04:00
|
|
|
// Copyright IBM Corp. 2014, 2026
|
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
|
|
|
|
package arguments
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2024-04-16 02:09:09 -04:00
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2024-04-07 19:31:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestParseInit_basicValid(t *testing.T) {
|
2024-04-16 02:09:09 -04:00
|
|
|
var flagNameValue []FlagNameValue
|
2024-04-07 19:31:23 -04:00
|
|
|
testCases := map[string]struct {
|
|
|
|
|
args []string
|
|
|
|
|
want *Init
|
|
|
|
|
}{
|
|
|
|
|
"with default options": {
|
|
|
|
|
nil,
|
|
|
|
|
&Init{
|
|
|
|
|
FromModule: "",
|
|
|
|
|
Lockfile: "",
|
|
|
|
|
TestsDirectory: "tests",
|
|
|
|
|
ViewType: ViewHuman,
|
|
|
|
|
Backend: true,
|
|
|
|
|
Cloud: true,
|
|
|
|
|
Get: true,
|
|
|
|
|
ForceInitCopy: false,
|
|
|
|
|
StateLock: true,
|
|
|
|
|
StateLockTimeout: 0,
|
|
|
|
|
Reconfigure: false,
|
|
|
|
|
MigrateState: false,
|
|
|
|
|
Upgrade: false,
|
|
|
|
|
Json: false,
|
|
|
|
|
IgnoreRemoteVersion: false,
|
2024-04-16 02:09:09 -04:00
|
|
|
BackendConfig: FlagNameValueSlice{
|
|
|
|
|
FlagName: "-backend-config",
|
|
|
|
|
Items: &flagNameValue,
|
|
|
|
|
},
|
PSS: Implement initialisation of new working directory (or use of `-reconfigure` flag) while using `state_store` (#37732)
* Minor fixes in diagnostics
This can only be done once modules have been parsed and the required providers data is available. There are multiple places where config is parsed, into either Config or Module structs, so this needs to be implemented in multiple places.
* Rename test to make it specific to use of backend block in config
* Update initBackend to accept whole initArgs collection
* Only process --backend-config data, when setting up a `backend`, if that data isn't empty
* Simplify how mock provider factories are made in tests
* Update mock provider's default logic to track and manage existing workspaces
* Add `ProviderSchema` method to `Pluggable` structs. This allows calling code to access the provider schema when using provider configuration data.
* Add function for converting a providerreqs.Version to a hashicorp/go-version Version.
This is needed for using locks when creating the backend state file.
* Implement initial version of init new working directories using `stateStore_C_s`. Default to creating the default workspace if no workspaces exist.
* Update test fixtures to match the hashicorp/test mock provider used in PSS tests
* Allow tests to obtain locks that include `testingOverrides` providers.
The `testingOverrides` field will only be set in tests, so this should not impact end users.
* Add tests showing TF can initialize a working directory for the first time (and do the same when forced by -reconfigure flag). Remove replaced tests.
* Add -create-default-workspace flag, to be used to disable creating the default workspace by default when -input=false (i.e for use in CI). Refactor creation of default workspace logic. Add tests.
* Allow reattached providers to be used during init for PSS
* Rename variable to `backendHash` so relation to `backend` is clearer
* Allow `(m *Meta) Backend` to return warning diagnostics
* Protect against nil testingOverrides in providerFactoriesFromLocks
* Add test case seeing what happens if default workspace selected, doesn't exist, but other workspaces do exist.
The consequences here are due to using `selectWorkspace` in `stateStore_C_s`, matching what's done in `backend_C_r_s`.
* Address code consistency check failure on PR
* Refactor use of mock in test that's experiencing EOF error...
* Remove test that requires test to supply input for user prompt
This test passes when run in isolation but fails when run alongside other tests, even when skipping all other tests using `testStdinPipe`. I don't think the value of this test is great enough to start changing how we test stdin input.
* Allow -create-default-workspace to be used regardless of whether input is enabled or disabled
* Add TF_SKIP_CREATE_DEFAULT_WORKSPACE environment variable
* Responses to feedback, including making testStdinPipe helper log details of errors copying data to stdin.
Note: We cannot call t.Fatal from a non-test goroutine.
* Use Errorf instead
* Allow backend state files to not include version data when a builtin or reattached provider is in use.
* Add clarifying comment about re-attached providers when finding the matching entry in required_providers
* Report that the default workspace was created to the view
* Refactor: use error comparison via `errors.Is` to identify when no workspaces exist.
* Move handling of TF_ENABLE_PLUGGABLE_STATE_STORAGE into init's ParseInit func.
* Validate that PSS-related flags can only be used when experiments are enabled, enforce coupling of PSS-related flags when in use.
* Slight rewording of output message about default workspace
* Update test to assert new output about default workspace
2025-10-15 05:44:21 -04:00
|
|
|
Vars: &Vars{},
|
|
|
|
|
InputEnabled: true,
|
|
|
|
|
CompactWarnings: false,
|
|
|
|
|
TargetFlags: nil,
|
|
|
|
|
CreateDefaultWorkspace: true,
|
2024-04-07 19:31:23 -04:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
"setting multiple options": {
|
|
|
|
|
[]string{"-backend=false", "-force-copy=true",
|
|
|
|
|
"-from-module=./main-dir", "-json", "-get=false",
|
|
|
|
|
"-lock=false", "-lock-timeout=10s", "-reconfigure=true",
|
2024-04-16 02:09:09 -04:00
|
|
|
"-upgrade=true", "-lockfile=readonly", "-compact-warnings=true",
|
2024-04-07 19:31:23 -04:00
|
|
|
"-ignore-remote-version=true", "-test-directory=./test-dir"},
|
|
|
|
|
&Init{
|
|
|
|
|
FromModule: "./main-dir",
|
|
|
|
|
Lockfile: "readonly",
|
|
|
|
|
TestsDirectory: "./test-dir",
|
|
|
|
|
ViewType: ViewJSON,
|
|
|
|
|
Backend: false,
|
|
|
|
|
Cloud: false,
|
|
|
|
|
Get: false,
|
|
|
|
|
ForceInitCopy: true,
|
|
|
|
|
StateLock: false,
|
|
|
|
|
StateLockTimeout: time.Duration(10) * time.Second,
|
|
|
|
|
Reconfigure: true,
|
|
|
|
|
MigrateState: false,
|
|
|
|
|
Upgrade: true,
|
|
|
|
|
Json: true,
|
|
|
|
|
IgnoreRemoteVersion: true,
|
2024-04-16 02:09:09 -04:00
|
|
|
BackendConfig: FlagNameValueSlice{
|
|
|
|
|
FlagName: "-backend-config",
|
|
|
|
|
Items: &flagNameValue,
|
|
|
|
|
},
|
PSS: Implement initialisation of new working directory (or use of `-reconfigure` flag) while using `state_store` (#37732)
* Minor fixes in diagnostics
This can only be done once modules have been parsed and the required providers data is available. There are multiple places where config is parsed, into either Config or Module structs, so this needs to be implemented in multiple places.
* Rename test to make it specific to use of backend block in config
* Update initBackend to accept whole initArgs collection
* Only process --backend-config data, when setting up a `backend`, if that data isn't empty
* Simplify how mock provider factories are made in tests
* Update mock provider's default logic to track and manage existing workspaces
* Add `ProviderSchema` method to `Pluggable` structs. This allows calling code to access the provider schema when using provider configuration data.
* Add function for converting a providerreqs.Version to a hashicorp/go-version Version.
This is needed for using locks when creating the backend state file.
* Implement initial version of init new working directories using `stateStore_C_s`. Default to creating the default workspace if no workspaces exist.
* Update test fixtures to match the hashicorp/test mock provider used in PSS tests
* Allow tests to obtain locks that include `testingOverrides` providers.
The `testingOverrides` field will only be set in tests, so this should not impact end users.
* Add tests showing TF can initialize a working directory for the first time (and do the same when forced by -reconfigure flag). Remove replaced tests.
* Add -create-default-workspace flag, to be used to disable creating the default workspace by default when -input=false (i.e for use in CI). Refactor creation of default workspace logic. Add tests.
* Allow reattached providers to be used during init for PSS
* Rename variable to `backendHash` so relation to `backend` is clearer
* Allow `(m *Meta) Backend` to return warning diagnostics
* Protect against nil testingOverrides in providerFactoriesFromLocks
* Add test case seeing what happens if default workspace selected, doesn't exist, but other workspaces do exist.
The consequences here are due to using `selectWorkspace` in `stateStore_C_s`, matching what's done in `backend_C_r_s`.
* Address code consistency check failure on PR
* Refactor use of mock in test that's experiencing EOF error...
* Remove test that requires test to supply input for user prompt
This test passes when run in isolation but fails when run alongside other tests, even when skipping all other tests using `testStdinPipe`. I don't think the value of this test is great enough to start changing how we test stdin input.
* Allow -create-default-workspace to be used regardless of whether input is enabled or disabled
* Add TF_SKIP_CREATE_DEFAULT_WORKSPACE environment variable
* Responses to feedback, including making testStdinPipe helper log details of errors copying data to stdin.
Note: We cannot call t.Fatal from a non-test goroutine.
* Use Errorf instead
* Allow backend state files to not include version data when a builtin or reattached provider is in use.
* Add clarifying comment about re-attached providers when finding the matching entry in required_providers
* Report that the default workspace was created to the view
* Refactor: use error comparison via `errors.Is` to identify when no workspaces exist.
* Move handling of TF_ENABLE_PLUGGABLE_STATE_STORAGE into init's ParseInit func.
* Validate that PSS-related flags can only be used when experiments are enabled, enforce coupling of PSS-related flags when in use.
* Slight rewording of output message about default workspace
* Update test to assert new output about default workspace
2025-10-15 05:44:21 -04:00
|
|
|
Vars: &Vars{},
|
|
|
|
|
InputEnabled: true,
|
|
|
|
|
Args: []string{},
|
|
|
|
|
CompactWarnings: true,
|
|
|
|
|
TargetFlags: nil,
|
|
|
|
|
CreateDefaultWorkspace: true,
|
2024-04-07 19:31:23 -04:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
"with cloud option": {
|
2024-04-16 02:09:09 -04:00
|
|
|
[]string{"-cloud=false", "-input=false", "-target=foo_bar.baz", "-backend-config", "backend.config"},
|
2024-04-07 19:31:23 -04:00
|
|
|
&Init{
|
|
|
|
|
FromModule: "",
|
|
|
|
|
Lockfile: "",
|
|
|
|
|
TestsDirectory: "tests",
|
|
|
|
|
ViewType: ViewHuman,
|
|
|
|
|
Backend: false,
|
|
|
|
|
Cloud: false,
|
|
|
|
|
Get: true,
|
|
|
|
|
ForceInitCopy: false,
|
|
|
|
|
StateLock: true,
|
|
|
|
|
StateLockTimeout: 0,
|
|
|
|
|
Reconfigure: false,
|
|
|
|
|
MigrateState: false,
|
|
|
|
|
Upgrade: false,
|
|
|
|
|
Json: false,
|
|
|
|
|
IgnoreRemoteVersion: false,
|
2024-04-16 02:09:09 -04:00
|
|
|
BackendConfig: FlagNameValueSlice{
|
|
|
|
|
FlagName: "-backend-config",
|
|
|
|
|
Items: &[]FlagNameValue{{Name: "-backend-config", Value: "backend.config"}},
|
|
|
|
|
},
|
PSS: Implement initialisation of new working directory (or use of `-reconfigure` flag) while using `state_store` (#37732)
* Minor fixes in diagnostics
This can only be done once modules have been parsed and the required providers data is available. There are multiple places where config is parsed, into either Config or Module structs, so this needs to be implemented in multiple places.
* Rename test to make it specific to use of backend block in config
* Update initBackend to accept whole initArgs collection
* Only process --backend-config data, when setting up a `backend`, if that data isn't empty
* Simplify how mock provider factories are made in tests
* Update mock provider's default logic to track and manage existing workspaces
* Add `ProviderSchema` method to `Pluggable` structs. This allows calling code to access the provider schema when using provider configuration data.
* Add function for converting a providerreqs.Version to a hashicorp/go-version Version.
This is needed for using locks when creating the backend state file.
* Implement initial version of init new working directories using `stateStore_C_s`. Default to creating the default workspace if no workspaces exist.
* Update test fixtures to match the hashicorp/test mock provider used in PSS tests
* Allow tests to obtain locks that include `testingOverrides` providers.
The `testingOverrides` field will only be set in tests, so this should not impact end users.
* Add tests showing TF can initialize a working directory for the first time (and do the same when forced by -reconfigure flag). Remove replaced tests.
* Add -create-default-workspace flag, to be used to disable creating the default workspace by default when -input=false (i.e for use in CI). Refactor creation of default workspace logic. Add tests.
* Allow reattached providers to be used during init for PSS
* Rename variable to `backendHash` so relation to `backend` is clearer
* Allow `(m *Meta) Backend` to return warning diagnostics
* Protect against nil testingOverrides in providerFactoriesFromLocks
* Add test case seeing what happens if default workspace selected, doesn't exist, but other workspaces do exist.
The consequences here are due to using `selectWorkspace` in `stateStore_C_s`, matching what's done in `backend_C_r_s`.
* Address code consistency check failure on PR
* Refactor use of mock in test that's experiencing EOF error...
* Remove test that requires test to supply input for user prompt
This test passes when run in isolation but fails when run alongside other tests, even when skipping all other tests using `testStdinPipe`. I don't think the value of this test is great enough to start changing how we test stdin input.
* Allow -create-default-workspace to be used regardless of whether input is enabled or disabled
* Add TF_SKIP_CREATE_DEFAULT_WORKSPACE environment variable
* Responses to feedback, including making testStdinPipe helper log details of errors copying data to stdin.
Note: We cannot call t.Fatal from a non-test goroutine.
* Use Errorf instead
* Allow backend state files to not include version data when a builtin or reattached provider is in use.
* Add clarifying comment about re-attached providers when finding the matching entry in required_providers
* Report that the default workspace was created to the view
* Refactor: use error comparison via `errors.Is` to identify when no workspaces exist.
* Move handling of TF_ENABLE_PLUGGABLE_STATE_STORAGE into init's ParseInit func.
* Validate that PSS-related flags can only be used when experiments are enabled, enforce coupling of PSS-related flags when in use.
* Slight rewording of output message about default workspace
* Update test to assert new output about default workspace
2025-10-15 05:44:21 -04:00
|
|
|
Vars: &Vars{},
|
|
|
|
|
InputEnabled: false,
|
|
|
|
|
Args: []string{},
|
|
|
|
|
CompactWarnings: false,
|
|
|
|
|
TargetFlags: []string{"foo_bar.baz"},
|
|
|
|
|
CreateDefaultWorkspace: true,
|
2024-04-07 19:31:23 -04:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-16 02:09:09 -04:00
|
|
|
cmpOpts := cmpopts.IgnoreUnexported(Vars{})
|
|
|
|
|
|
2024-04-07 19:31:23 -04:00
|
|
|
for name, tc := range testCases {
|
|
|
|
|
t.Run(name, func(t *testing.T) {
|
PSS: Implement initialisation of new working directory (or use of `-reconfigure` flag) while using `state_store` (#37732)
* Minor fixes in diagnostics
This can only be done once modules have been parsed and the required providers data is available. There are multiple places where config is parsed, into either Config or Module structs, so this needs to be implemented in multiple places.
* Rename test to make it specific to use of backend block in config
* Update initBackend to accept whole initArgs collection
* Only process --backend-config data, when setting up a `backend`, if that data isn't empty
* Simplify how mock provider factories are made in tests
* Update mock provider's default logic to track and manage existing workspaces
* Add `ProviderSchema` method to `Pluggable` structs. This allows calling code to access the provider schema when using provider configuration data.
* Add function for converting a providerreqs.Version to a hashicorp/go-version Version.
This is needed for using locks when creating the backend state file.
* Implement initial version of init new working directories using `stateStore_C_s`. Default to creating the default workspace if no workspaces exist.
* Update test fixtures to match the hashicorp/test mock provider used in PSS tests
* Allow tests to obtain locks that include `testingOverrides` providers.
The `testingOverrides` field will only be set in tests, so this should not impact end users.
* Add tests showing TF can initialize a working directory for the first time (and do the same when forced by -reconfigure flag). Remove replaced tests.
* Add -create-default-workspace flag, to be used to disable creating the default workspace by default when -input=false (i.e for use in CI). Refactor creation of default workspace logic. Add tests.
* Allow reattached providers to be used during init for PSS
* Rename variable to `backendHash` so relation to `backend` is clearer
* Allow `(m *Meta) Backend` to return warning diagnostics
* Protect against nil testingOverrides in providerFactoriesFromLocks
* Add test case seeing what happens if default workspace selected, doesn't exist, but other workspaces do exist.
The consequences here are due to using `selectWorkspace` in `stateStore_C_s`, matching what's done in `backend_C_r_s`.
* Address code consistency check failure on PR
* Refactor use of mock in test that's experiencing EOF error...
* Remove test that requires test to supply input for user prompt
This test passes when run in isolation but fails when run alongside other tests, even when skipping all other tests using `testStdinPipe`. I don't think the value of this test is great enough to start changing how we test stdin input.
* Allow -create-default-workspace to be used regardless of whether input is enabled or disabled
* Add TF_SKIP_CREATE_DEFAULT_WORKSPACE environment variable
* Responses to feedback, including making testStdinPipe helper log details of errors copying data to stdin.
Note: We cannot call t.Fatal from a non-test goroutine.
* Use Errorf instead
* Allow backend state files to not include version data when a builtin or reattached provider is in use.
* Add clarifying comment about re-attached providers when finding the matching entry in required_providers
* Report that the default workspace was created to the view
* Refactor: use error comparison via `errors.Is` to identify when no workspaces exist.
* Move handling of TF_ENABLE_PLUGGABLE_STATE_STORAGE into init's ParseInit func.
* Validate that PSS-related flags can only be used when experiments are enabled, enforce coupling of PSS-related flags when in use.
* Slight rewording of output message about default workspace
* Update test to assert new output about default workspace
2025-10-15 05:44:21 -04:00
|
|
|
experimentsEnabled := false
|
|
|
|
|
got, diags := ParseInit(tc.args, experimentsEnabled)
|
2024-04-07 19:31:23 -04:00
|
|
|
if len(diags) > 0 {
|
|
|
|
|
t.Fatalf("unexpected diags: %v", diags)
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-16 02:09:09 -04:00
|
|
|
if diff := cmp.Diff(tc.want, got, cmpOpts); diff != "" {
|
2024-04-07 19:31:23 -04:00
|
|
|
t.Errorf("unexpected result\n%s", diff)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestParseInit_invalid(t *testing.T) {
|
|
|
|
|
testCases := map[string]struct {
|
2024-04-16 02:09:09 -04:00
|
|
|
args []string
|
|
|
|
|
wantErr string
|
|
|
|
|
wantViewType ViewType
|
2024-04-07 19:31:23 -04:00
|
|
|
}{
|
|
|
|
|
"with unsupported options": {
|
2024-04-16 02:09:09 -04:00
|
|
|
args: []string{"-raw"},
|
|
|
|
|
wantErr: "flag provided but not defined",
|
|
|
|
|
wantViewType: ViewHuman,
|
2024-04-07 19:31:23 -04:00
|
|
|
},
|
|
|
|
|
"with both -backend and -cloud options set": {
|
2024-04-16 02:09:09 -04:00
|
|
|
args: []string{"-backend=false", "-cloud=false"},
|
|
|
|
|
wantErr: "The -backend and -cloud options are aliases of one another and mutually-exclusive in their use",
|
|
|
|
|
wantViewType: ViewHuman,
|
|
|
|
|
},
|
|
|
|
|
"with both -migrate-state and -json options set": {
|
|
|
|
|
args: []string{"-migrate-state", "-json"},
|
|
|
|
|
wantErr: "Terraform cannot ask for interactive approval when -json is set. To use the -migrate-state option, disable the -json option.",
|
|
|
|
|
wantViewType: ViewJSON,
|
|
|
|
|
},
|
|
|
|
|
"with both -migrate-state and -reconfigure options set": {
|
|
|
|
|
args: []string{"-migrate-state", "-reconfigure"},
|
|
|
|
|
wantErr: "The -migrate-state and -reconfigure options are mutually-exclusive.",
|
|
|
|
|
wantViewType: ViewHuman,
|
2024-04-07 19:31:23 -04:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
|
t.Run(name, func(t *testing.T) {
|
PSS: Implement initialisation of new working directory (or use of `-reconfigure` flag) while using `state_store` (#37732)
* Minor fixes in diagnostics
This can only be done once modules have been parsed and the required providers data is available. There are multiple places where config is parsed, into either Config or Module structs, so this needs to be implemented in multiple places.
* Rename test to make it specific to use of backend block in config
* Update initBackend to accept whole initArgs collection
* Only process --backend-config data, when setting up a `backend`, if that data isn't empty
* Simplify how mock provider factories are made in tests
* Update mock provider's default logic to track and manage existing workspaces
* Add `ProviderSchema` method to `Pluggable` structs. This allows calling code to access the provider schema when using provider configuration data.
* Add function for converting a providerreqs.Version to a hashicorp/go-version Version.
This is needed for using locks when creating the backend state file.
* Implement initial version of init new working directories using `stateStore_C_s`. Default to creating the default workspace if no workspaces exist.
* Update test fixtures to match the hashicorp/test mock provider used in PSS tests
* Allow tests to obtain locks that include `testingOverrides` providers.
The `testingOverrides` field will only be set in tests, so this should not impact end users.
* Add tests showing TF can initialize a working directory for the first time (and do the same when forced by -reconfigure flag). Remove replaced tests.
* Add -create-default-workspace flag, to be used to disable creating the default workspace by default when -input=false (i.e for use in CI). Refactor creation of default workspace logic. Add tests.
* Allow reattached providers to be used during init for PSS
* Rename variable to `backendHash` so relation to `backend` is clearer
* Allow `(m *Meta) Backend` to return warning diagnostics
* Protect against nil testingOverrides in providerFactoriesFromLocks
* Add test case seeing what happens if default workspace selected, doesn't exist, but other workspaces do exist.
The consequences here are due to using `selectWorkspace` in `stateStore_C_s`, matching what's done in `backend_C_r_s`.
* Address code consistency check failure on PR
* Refactor use of mock in test that's experiencing EOF error...
* Remove test that requires test to supply input for user prompt
This test passes when run in isolation but fails when run alongside other tests, even when skipping all other tests using `testStdinPipe`. I don't think the value of this test is great enough to start changing how we test stdin input.
* Allow -create-default-workspace to be used regardless of whether input is enabled or disabled
* Add TF_SKIP_CREATE_DEFAULT_WORKSPACE environment variable
* Responses to feedback, including making testStdinPipe helper log details of errors copying data to stdin.
Note: We cannot call t.Fatal from a non-test goroutine.
* Use Errorf instead
* Allow backend state files to not include version data when a builtin or reattached provider is in use.
* Add clarifying comment about re-attached providers when finding the matching entry in required_providers
* Report that the default workspace was created to the view
* Refactor: use error comparison via `errors.Is` to identify when no workspaces exist.
* Move handling of TF_ENABLE_PLUGGABLE_STATE_STORAGE into init's ParseInit func.
* Validate that PSS-related flags can only be used when experiments are enabled, enforce coupling of PSS-related flags when in use.
* Slight rewording of output message about default workspace
* Update test to assert new output about default workspace
2025-10-15 05:44:21 -04:00
|
|
|
experimentsEnabled := false
|
|
|
|
|
got, diags := ParseInit(tc.args, experimentsEnabled)
|
2024-04-07 19:31:23 -04:00
|
|
|
if len(diags) == 0 {
|
|
|
|
|
t.Fatal("expected diags but got none")
|
|
|
|
|
}
|
|
|
|
|
if got, want := diags.Err().Error(), tc.wantErr; !strings.Contains(got, want) {
|
|
|
|
|
t.Fatalf("wrong diags\n got: %s\nwant: %s", got, want)
|
|
|
|
|
}
|
2024-04-16 02:09:09 -04:00
|
|
|
if got.ViewType != tc.wantViewType {
|
2024-04-07 19:31:23 -04:00
|
|
|
t.Fatalf("wrong view type, got %#v, want %#v", got.ViewType, ViewHuman)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-16 02:09:09 -04:00
|
|
|
|
PSS: Implement initialisation of new working directory (or use of `-reconfigure` flag) while using `state_store` (#37732)
* Minor fixes in diagnostics
This can only be done once modules have been parsed and the required providers data is available. There are multiple places where config is parsed, into either Config or Module structs, so this needs to be implemented in multiple places.
* Rename test to make it specific to use of backend block in config
* Update initBackend to accept whole initArgs collection
* Only process --backend-config data, when setting up a `backend`, if that data isn't empty
* Simplify how mock provider factories are made in tests
* Update mock provider's default logic to track and manage existing workspaces
* Add `ProviderSchema` method to `Pluggable` structs. This allows calling code to access the provider schema when using provider configuration data.
* Add function for converting a providerreqs.Version to a hashicorp/go-version Version.
This is needed for using locks when creating the backend state file.
* Implement initial version of init new working directories using `stateStore_C_s`. Default to creating the default workspace if no workspaces exist.
* Update test fixtures to match the hashicorp/test mock provider used in PSS tests
* Allow tests to obtain locks that include `testingOverrides` providers.
The `testingOverrides` field will only be set in tests, so this should not impact end users.
* Add tests showing TF can initialize a working directory for the first time (and do the same when forced by -reconfigure flag). Remove replaced tests.
* Add -create-default-workspace flag, to be used to disable creating the default workspace by default when -input=false (i.e for use in CI). Refactor creation of default workspace logic. Add tests.
* Allow reattached providers to be used during init for PSS
* Rename variable to `backendHash` so relation to `backend` is clearer
* Allow `(m *Meta) Backend` to return warning diagnostics
* Protect against nil testingOverrides in providerFactoriesFromLocks
* Add test case seeing what happens if default workspace selected, doesn't exist, but other workspaces do exist.
The consequences here are due to using `selectWorkspace` in `stateStore_C_s`, matching what's done in `backend_C_r_s`.
* Address code consistency check failure on PR
* Refactor use of mock in test that's experiencing EOF error...
* Remove test that requires test to supply input for user prompt
This test passes when run in isolation but fails when run alongside other tests, even when skipping all other tests using `testStdinPipe`. I don't think the value of this test is great enough to start changing how we test stdin input.
* Allow -create-default-workspace to be used regardless of whether input is enabled or disabled
* Add TF_SKIP_CREATE_DEFAULT_WORKSPACE environment variable
* Responses to feedback, including making testStdinPipe helper log details of errors copying data to stdin.
Note: We cannot call t.Fatal from a non-test goroutine.
* Use Errorf instead
* Allow backend state files to not include version data when a builtin or reattached provider is in use.
* Add clarifying comment about re-attached providers when finding the matching entry in required_providers
* Report that the default workspace was created to the view
* Refactor: use error comparison via `errors.Is` to identify when no workspaces exist.
* Move handling of TF_ENABLE_PLUGGABLE_STATE_STORAGE into init's ParseInit func.
* Validate that PSS-related flags can only be used when experiments are enabled, enforce coupling of PSS-related flags when in use.
* Slight rewording of output message about default workspace
* Update test to assert new output about default workspace
2025-10-15 05:44:21 -04:00
|
|
|
func TestParseInit_experimentalFlags(t *testing.T) {
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
|
args []string
|
|
|
|
|
envs map[string]string
|
|
|
|
|
wantErr string
|
|
|
|
|
experimentsEnabled bool
|
|
|
|
|
}{
|
|
|
|
|
"error: -enable-pluggable-state-storage-experiment and experiments are disabled": {
|
|
|
|
|
args: []string{"-enable-pluggable-state-storage-experiment"},
|
|
|
|
|
experimentsEnabled: false,
|
|
|
|
|
wantErr: "Cannot use -enable-pluggable-state-storage-experiment flag without experiments enabled",
|
|
|
|
|
},
|
|
|
|
|
"error: TF_ENABLE_PLUGGABLE_STATE_STORAGE is set and experiments are disabled": {
|
|
|
|
|
envs: map[string]string{
|
|
|
|
|
"TF_ENABLE_PLUGGABLE_STATE_STORAGE": "1",
|
|
|
|
|
},
|
|
|
|
|
experimentsEnabled: false,
|
|
|
|
|
wantErr: "Cannot use -enable-pluggable-state-storage-experiment flag without experiments enabled",
|
|
|
|
|
},
|
|
|
|
|
"error: -create-default-workspace=false and experiments are disabled": {
|
|
|
|
|
args: []string{"-create-default-workspace=false"},
|
|
|
|
|
experimentsEnabled: false,
|
|
|
|
|
wantErr: "Cannot use -create-default-workspace flag without experiments enabled",
|
|
|
|
|
},
|
|
|
|
|
"error: TF_SKIP_CREATE_DEFAULT_WORKSPACE is set and experiments are disabled": {
|
|
|
|
|
envs: map[string]string{
|
|
|
|
|
"TF_SKIP_CREATE_DEFAULT_WORKSPACE": "1",
|
|
|
|
|
},
|
|
|
|
|
experimentsEnabled: false,
|
|
|
|
|
wantErr: "Cannot use -create-default-workspace flag without experiments enabled",
|
|
|
|
|
},
|
|
|
|
|
"error: -create-default-workspace=false used without -enable-pluggable-state-storage-experiment, while experiments are enabled": {
|
|
|
|
|
args: []string{"-create-default-workspace=false"},
|
|
|
|
|
experimentsEnabled: true,
|
|
|
|
|
wantErr: "Cannot use -create-default-workspace=false flag unless the pluggable state storage experiment is enabled",
|
|
|
|
|
},
|
|
|
|
|
"error: TF_SKIP_CREATE_DEFAULT_WORKSPACE used without -enable-pluggable-state-storage-experiment, while experiments are enabled": {
|
|
|
|
|
envs: map[string]string{
|
|
|
|
|
"TF_SKIP_CREATE_DEFAULT_WORKSPACE": "1",
|
|
|
|
|
},
|
|
|
|
|
experimentsEnabled: true,
|
|
|
|
|
wantErr: "Cannot use -create-default-workspace=false flag unless the pluggable state storage experiment is enabled",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
|
for k, v := range tc.envs {
|
|
|
|
|
t.Setenv(k, v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, diags := ParseInit(tc.args, tc.experimentsEnabled)
|
|
|
|
|
if len(diags) == 0 {
|
|
|
|
|
t.Fatal("expected diags but got none")
|
|
|
|
|
}
|
|
|
|
|
if got, want := diags.Err().Error(), tc.wantErr; !strings.Contains(got, want) {
|
|
|
|
|
t.Fatalf("wrong diags\n got: %s\nwant: %s", got, want)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-16 02:09:09 -04:00
|
|
|
func TestParseInit_vars(t *testing.T) {
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
|
args []string
|
|
|
|
|
want []FlagNameValue
|
|
|
|
|
}{
|
|
|
|
|
"no var flags by default": {
|
|
|
|
|
args: nil,
|
|
|
|
|
want: nil,
|
|
|
|
|
},
|
|
|
|
|
"one var": {
|
|
|
|
|
args: []string{"-var", "foo=bar"},
|
|
|
|
|
want: []FlagNameValue{
|
|
|
|
|
{Name: "-var", Value: "foo=bar"},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
"one var-file": {
|
|
|
|
|
args: []string{"-var-file", "cool.tfvars"},
|
|
|
|
|
want: []FlagNameValue{
|
|
|
|
|
{Name: "-var-file", Value: "cool.tfvars"},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
"ordering preserved": {
|
|
|
|
|
args: []string{
|
|
|
|
|
"-var", "foo=bar",
|
|
|
|
|
"-var-file", "cool.tfvars",
|
|
|
|
|
"-var", "boop=beep",
|
|
|
|
|
},
|
|
|
|
|
want: []FlagNameValue{
|
|
|
|
|
{Name: "-var", Value: "foo=bar"},
|
|
|
|
|
{Name: "-var-file", Value: "cool.tfvars"},
|
|
|
|
|
{Name: "-var", Value: "boop=beep"},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
|
t.Run(name, func(t *testing.T) {
|
PSS: Implement initialisation of new working directory (or use of `-reconfigure` flag) while using `state_store` (#37732)
* Minor fixes in diagnostics
This can only be done once modules have been parsed and the required providers data is available. There are multiple places where config is parsed, into either Config or Module structs, so this needs to be implemented in multiple places.
* Rename test to make it specific to use of backend block in config
* Update initBackend to accept whole initArgs collection
* Only process --backend-config data, when setting up a `backend`, if that data isn't empty
* Simplify how mock provider factories are made in tests
* Update mock provider's default logic to track and manage existing workspaces
* Add `ProviderSchema` method to `Pluggable` structs. This allows calling code to access the provider schema when using provider configuration data.
* Add function for converting a providerreqs.Version to a hashicorp/go-version Version.
This is needed for using locks when creating the backend state file.
* Implement initial version of init new working directories using `stateStore_C_s`. Default to creating the default workspace if no workspaces exist.
* Update test fixtures to match the hashicorp/test mock provider used in PSS tests
* Allow tests to obtain locks that include `testingOverrides` providers.
The `testingOverrides` field will only be set in tests, so this should not impact end users.
* Add tests showing TF can initialize a working directory for the first time (and do the same when forced by -reconfigure flag). Remove replaced tests.
* Add -create-default-workspace flag, to be used to disable creating the default workspace by default when -input=false (i.e for use in CI). Refactor creation of default workspace logic. Add tests.
* Allow reattached providers to be used during init for PSS
* Rename variable to `backendHash` so relation to `backend` is clearer
* Allow `(m *Meta) Backend` to return warning diagnostics
* Protect against nil testingOverrides in providerFactoriesFromLocks
* Add test case seeing what happens if default workspace selected, doesn't exist, but other workspaces do exist.
The consequences here are due to using `selectWorkspace` in `stateStore_C_s`, matching what's done in `backend_C_r_s`.
* Address code consistency check failure on PR
* Refactor use of mock in test that's experiencing EOF error...
* Remove test that requires test to supply input for user prompt
This test passes when run in isolation but fails when run alongside other tests, even when skipping all other tests using `testStdinPipe`. I don't think the value of this test is great enough to start changing how we test stdin input.
* Allow -create-default-workspace to be used regardless of whether input is enabled or disabled
* Add TF_SKIP_CREATE_DEFAULT_WORKSPACE environment variable
* Responses to feedback, including making testStdinPipe helper log details of errors copying data to stdin.
Note: We cannot call t.Fatal from a non-test goroutine.
* Use Errorf instead
* Allow backend state files to not include version data when a builtin or reattached provider is in use.
* Add clarifying comment about re-attached providers when finding the matching entry in required_providers
* Report that the default workspace was created to the view
* Refactor: use error comparison via `errors.Is` to identify when no workspaces exist.
* Move handling of TF_ENABLE_PLUGGABLE_STATE_STORAGE into init's ParseInit func.
* Validate that PSS-related flags can only be used when experiments are enabled, enforce coupling of PSS-related flags when in use.
* Slight rewording of output message about default workspace
* Update test to assert new output about default workspace
2025-10-15 05:44:21 -04:00
|
|
|
experimentsEnabled := false
|
|
|
|
|
got, diags := ParseInit(tc.args, experimentsEnabled)
|
2024-04-16 02:09:09 -04:00
|
|
|
if len(diags) > 0 {
|
|
|
|
|
t.Fatalf("unexpected diags: %v", diags)
|
|
|
|
|
}
|
|
|
|
|
if vars := got.Vars.All(); !cmp.Equal(vars, tc.want) {
|
|
|
|
|
t.Fatalf("unexpected result\n%s", cmp.Diff(vars, tc.want))
|
|
|
|
|
}
|
|
|
|
|
if got, want := got.Vars.Empty(), len(tc.want) == 0; got != want {
|
|
|
|
|
t.Fatalf("expected Empty() to return %t, but was %t", want, got)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|