github.com/opentofu/opentofu@v1.7.1/internal/cloud/e2e/migrate_state_single_to_tfc_test.go (about) 1 // Copyright (c) The OpenTofu Authors 2 // SPDX-License-Identifier: MPL-2.0 3 // Copyright (c) 2023 HashiCorp, Inc. 4 // SPDX-License-Identifier: MPL-2.0 5 6 package main 7 8 import ( 9 "context" 10 "testing" 11 12 tfe "github.com/hashicorp/go-tfe" 13 ) 14 15 func Test_migrate_single_to_tfc(t *testing.T) { 16 t.Parallel() 17 skipIfMissingEnvVar(t) 18 skipWithoutRemoteTerraformVersion(t) 19 20 ctx := context.Background() 21 22 cases := testCases{ 23 "migrate using cloud workspace name strategy": { 24 operations: []operationSets{ 25 { 26 prep: func(t *testing.T, orgName, dir string) { 27 tfBlock := terraformConfigLocalBackend() 28 writeMainTF(t, tfBlock, dir) 29 }, 30 commands: []tfCommand{ 31 { 32 command: []string{"init"}, 33 expectedCmdOutput: `Successfully configured the backend "local"!`, 34 }, 35 { 36 command: []string{"apply", "-auto-approve"}, 37 postInputOutput: []string{`Apply complete!`}, 38 }, 39 }, 40 }, 41 { 42 prep: func(t *testing.T, orgName, dir string) { 43 wsName := "new-workspace" 44 tfBlock := terraformConfigCloudBackendName(orgName, wsName) 45 writeMainTF(t, tfBlock, dir) 46 }, 47 commands: []tfCommand{ 48 { 49 command: []string{"init"}, 50 expectedCmdOutput: `Migrating from backend "local" to Terraform Cloud.`, 51 userInput: []string{"yes", "yes"}, 52 postInputOutput: []string{ 53 `Should Terraform migrate your existing state?`, 54 `Terraform Cloud has been successfully initialized!`}, 55 }, 56 { 57 command: []string{"workspace", "list"}, 58 expectedCmdOutput: `new-workspace`, 59 }, 60 }, 61 }, 62 }, 63 validations: func(t *testing.T, orgName string) { 64 wsList, err := tfeClient.Workspaces.List(ctx, orgName, nil) 65 if err != nil { 66 t.Fatal(err) 67 } 68 ws := wsList.Items[0] 69 if ws.Name != "new-workspace" { 70 t.Fatalf("Expected workspace to be `new-workspace`, but is %s", ws.Name) 71 } 72 }, 73 }, 74 "migrate using cloud workspace tags strategy": { 75 operations: []operationSets{ 76 { 77 prep: func(t *testing.T, orgName, dir string) { 78 tfBlock := terraformConfigLocalBackend() 79 writeMainTF(t, tfBlock, dir) 80 }, 81 commands: []tfCommand{ 82 { 83 command: []string{"init"}, 84 expectedCmdOutput: `Successfully configured the backend "local"!`, 85 }, 86 { 87 command: []string{"apply", "-auto-approve"}, 88 postInputOutput: []string{`Apply complete!`}, 89 }, 90 }, 91 }, 92 { 93 prep: func(t *testing.T, orgName, dir string) { 94 tag := "app" 95 tfBlock := terraformConfigCloudBackendTags(orgName, tag) 96 writeMainTF(t, tfBlock, dir) 97 }, 98 commands: []tfCommand{ 99 { 100 command: []string{"init"}, 101 expectedCmdOutput: `Migrating from backend "local" to Terraform Cloud.`, 102 userInput: []string{"yes", "new-workspace", "yes"}, 103 postInputOutput: []string{ 104 `Should Terraform migrate your existing state?`, 105 `Terraform Cloud requires all workspaces to be given an explicit name.`, 106 `Terraform Cloud has been successfully initialized!`}, 107 }, 108 { 109 command: []string{"workspace", "list"}, 110 expectedCmdOutput: `new-workspace`, 111 }, 112 }, 113 }, 114 }, 115 validations: func(t *testing.T, orgName string) { 116 wsList, err := tfeClient.Workspaces.List(ctx, orgName, &tfe.WorkspaceListOptions{ 117 Tags: "app", 118 }) 119 if err != nil { 120 t.Fatal(err) 121 } 122 ws := wsList.Items[0] 123 if ws.Name != "new-workspace" { 124 t.Fatalf("Expected workspace to be `new-workspace`, but is %s", ws.Name) 125 } 126 }, 127 }, 128 } 129 130 testRunner(t, cases, 1) 131 }