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