Skip to content

Commit 388bcbd

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 11a1875 commit 388bcbd

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
@@ -4752,7 +4752,7 @@ func TestInit_stateStore_providerUpgrade(t *testing.T) {
47524752
}
47534753
})
47544754

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

4774-
// Allow the test to respond to the pause in provider installation for
4775-
// checking the state storage provider.
4776-
_ = testInputMap(t, map[string]string{
4777-
"approve": "yes",
4778-
})
4779-
4774+
// INIT #1 - fail upgrade due to needing -safe-init flag
47804775
ui := new(cli.MockUi)
47814776
view, done := testView(t)
47824777
meta := Meta{
@@ -4798,28 +4793,46 @@ func TestInit_stateStore_providerUpgrade(t *testing.T) {
47984793
"-enable-pluggable-state-storage-experiment=true",
47994794
"-migrate-state=true",
48004795
"-upgrade",
4801-
"-safe-init",
4796+
// -safe-init not present
48024797
}
48034798
code := c.Run(args)
48044799
testOutput := done(t)
4805-
if code != 0 {
4806-
t.Fatalf("expected 0 exit code, got %d, output: \n%s", code, testOutput.All())
4800+
if code != 1 {
4801+
t.Fatalf("expected 1 exit code, got %d, output: \n%s", code, testOutput.All())
48074802
}
4808-
4809-
// Check output
48104803
output := testOutput.All()
4811-
expectedMsg := "Terraform has been successfully initialized!"
4804+
expectedMsg := "Error: State storage providers must be downloaded using -safe-init flag"
48124805
if !strings.Contains(output, expectedMsg) {
48134806
t.Fatalf("expected output to include %q, but got':\n %s", expectedMsg, output)
48144807
}
4815-
expectedReason := "State store provider \"test\" (hashicorp/test) version changed from 1.2.3 to 9.9.9"
4816-
if !strings.Contains(output, expectedReason) {
4817-
t.Fatalf("expected output to include reason %q, but got':\n %s", expectedReason, output)
4818-
}
48194808

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

0 commit comments

Comments
 (0)