github.com/cycloidio/terraform@v1.1.10-0.20220513142504-76d5c768dc63/cloud/e2e/migrate_state_single_to_tfc_test.go (about)

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