Skip to content

Commit bf0f17d

Browse files
committed
test: Update test to show that init -upgrade fails when -safe-init isn't provided, and passes when the flag is present.
1 parent 71fd051 commit bf0f17d

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

internal/command/init_test.go

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4756,7 +4756,7 @@ func TestInit_stateStore_providerUpgrade(t *testing.T) {
47564756
}
47574757
})
47584758

4759-
t.Run("upgrading the provider used for state storage via HTTP", func(t *testing.T) {
4759+
t.Run("upgrading the provider used for state storage via HTTP requires -safe-init flag", func(t *testing.T) {
47604760
// Create a temporary working directory with state store configuration
47614761
// that doesn't match the backend state file
47624762
td := t.TempDir()
@@ -4775,12 +4775,7 @@ func TestInit_stateStore_providerUpgrade(t *testing.T) {
47754775
// In this test scenario we perform an upgrade. This causes v9.9.9 to be downloaded.
47764776
source := newMockProviderSourceUsingTestHttpServer(t, addrs.NewDefaultProvider("test"), getproviders.MustParseVersion("9.9.9"))
47774777

4778-
// Allow the test to respond to the pause in provider installation for
4779-
// checking the state storage provider.
4780-
_ = testInputMap(t, map[string]string{
4781-
"approve": "yes",
4782-
})
4783-
4778+
// INIT #1 - fail upgrade due to needing -safe-init flag
47844779
ui := new(cli.MockUi)
47854780
view, done := testView(t)
47864781
meta := Meta{
@@ -4802,28 +4797,46 @@ func TestInit_stateStore_providerUpgrade(t *testing.T) {
48024797
"-enable-pluggable-state-storage-experiment=true",
48034798
"-migrate-state=true",
48044799
"-upgrade",
4805-
"-safe-init",
4800+
// -safe-init not present
48064801
}
48074802
code := c.Run(args)
48084803
testOutput := done(t)
4809-
if code != 0 {
4810-
t.Fatalf("expected 0 exit code, got %d, output: \n%s", code, testOutput.All())
4804+
if code != 1 {
4805+
t.Fatalf("expected 1 exit code, got %d, output: \n%s", code, testOutput.All())
48114806
}
4812-
4813-
// Check output
48144807
output := testOutput.All()
4815-
expectedMsg := "Terraform has been successfully initialized!"
4808+
expectedMsg := "Error: State storage providers must be downloaded using -safe-init flag"
48164809
if !strings.Contains(output, expectedMsg) {
48174810
t.Fatalf("expected output to include %q, but got':\n %s", expectedMsg, output)
48184811
}
4819-
expectedReason := "State store provider \"test\" (hashicorp/test) version changed from 1.2.3 to 9.9.9"
4820-
if !strings.Contains(output, expectedReason) {
4821-
t.Fatalf("expected output to include reason %q, but got':\n %s", expectedReason, output)
4822-
}
48234812

4824-
// check state remains accessible after migration
4825-
if _, exists := mockProvider.MockStates[backend.DefaultStateName]; !exists {
4826-
t.Fatal("expected the default workspace to exist after migration, but it is missing")
4813+
// INIT #2 - successful upgrade due to presence of -safe-init flag
4814+
//
4815+
// Allow the test to respond to the pause in provider installation for
4816+
// checking the state storage provider.
4817+
_ = testInputMap(t, map[string]string{
4818+
"approve": "yes",
4819+
})
4820+
4821+
ui = new(cli.MockUi)
4822+
view, done = testView(t)
4823+
c.Meta.View = view
4824+
c.Meta.Ui = ui
4825+
args = []string{
4826+
"-enable-pluggable-state-storage-experiment=true",
4827+
"-migrate-state=true",
4828+
"-upgrade",
4829+
"-safe-init",
4830+
}
4831+
code = c.Run(args)
4832+
testOutput = done(t)
4833+
if code != 0 {
4834+
t.Fatalf("expected 0 exit code, got %d, output: \n%s", code, testOutput.All())
4835+
}
4836+
output = testOutput.All()
4837+
expectedMsg = "Terraform has been successfully initialized!"
4838+
if !strings.Contains(output, expectedMsg) {
4839+
t.Fatalf("expected output to include %q, but got':\n %s", expectedMsg, output)
48274840
}
48284841
})
48294842
}

0 commit comments

Comments
 (0)