github.com/opentofu/opentofu@v1.7.1/internal/cloud/e2e/apply_auto_approve_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 tfversion "github.com/opentofu/opentofu/version" 14 ) 15 16 func Test_terraform_apply_autoApprove(t *testing.T) { 17 t.Parallel() 18 skipIfMissingEnvVar(t) 19 skipWithoutRemoteTerraformVersion(t) 20 21 ctx := context.Background() 22 23 cases := testCases{ 24 "workspace manual apply, terraform apply without auto-approve, expect prompt": { 25 operations: []operationSets{ 26 { 27 prep: func(t *testing.T, orgName, dir string) { 28 wsName := "app" 29 _ = createWorkspace(t, orgName, tfe.WorkspaceCreateOptions{ 30 Name: tfe.String(wsName), 31 TerraformVersion: tfe.String(tfversion.String()), 32 AutoApply: tfe.Bool(false), 33 }) 34 tfBlock := terraformConfigCloudBackendName(orgName, wsName) 35 writeMainTF(t, tfBlock, dir) 36 }, 37 commands: []tfCommand{ 38 { 39 command: []string{"init"}, 40 expectedCmdOutput: `Terraform Cloud has been successfully initialized!`, 41 }, 42 { 43 command: []string{"apply"}, 44 expectedCmdOutput: `Do you want to perform these actions in workspace "app"?`, 45 userInput: []string{"yes"}, 46 postInputOutput: []string{`Apply complete!`}, 47 }, 48 }, 49 }, 50 }, 51 validations: func(t *testing.T, orgName string) { 52 workspace, err := tfeClient.Workspaces.ReadWithOptions(ctx, orgName, "app", &tfe.WorkspaceReadOptions{Include: []tfe.WSIncludeOpt{tfe.WSCurrentRun}}) 53 if err != nil { 54 t.Fatal(err) 55 } 56 if workspace.CurrentRun == nil { 57 t.Fatal("Expected workspace to have run, but got nil") 58 } 59 if workspace.CurrentRun.Status != tfe.RunApplied { 60 t.Fatalf("Expected run status to be `applied`, but is %s", workspace.CurrentRun.Status) 61 } 62 }, 63 }, 64 "workspace auto apply, terraform apply without auto-approve, expect prompt": { 65 operations: []operationSets{ 66 { 67 prep: func(t *testing.T, orgName, dir string) { 68 wsName := "app" 69 _ = createWorkspace(t, orgName, tfe.WorkspaceCreateOptions{ 70 Name: tfe.String(wsName), 71 TerraformVersion: tfe.String(tfversion.String()), 72 AutoApply: tfe.Bool(true), 73 }) 74 tfBlock := terraformConfigCloudBackendName(orgName, wsName) 75 writeMainTF(t, tfBlock, dir) 76 }, 77 commands: []tfCommand{ 78 { 79 command: []string{"init"}, 80 expectedCmdOutput: `Terraform Cloud has been successfully initialized!`, 81 }, 82 { 83 command: []string{"apply"}, 84 expectedCmdOutput: `Do you want to perform these actions in workspace "app"?`, 85 userInput: []string{"yes"}, 86 postInputOutput: []string{`Apply complete!`}, 87 }, 88 }, 89 }, 90 }, 91 validations: func(t *testing.T, orgName string) { 92 workspace, err := tfeClient.Workspaces.ReadWithOptions(ctx, orgName, "app", &tfe.WorkspaceReadOptions{Include: []tfe.WSIncludeOpt{tfe.WSCurrentRun}}) 93 if err != nil { 94 t.Fatal(err) 95 } 96 if workspace.CurrentRun == nil { 97 t.Fatal("Expected workspace to have run, but got nil") 98 } 99 if workspace.CurrentRun.Status != tfe.RunApplied { 100 t.Fatalf("Expected run status to be `applied`, but is %s", workspace.CurrentRun.Status) 101 } 102 }, 103 }, 104 "workspace manual apply, terraform apply with auto-approve, no prompt": { 105 operations: []operationSets{ 106 { 107 prep: func(t *testing.T, orgName, dir string) { 108 wsName := "app" 109 _ = createWorkspace(t, orgName, tfe.WorkspaceCreateOptions{ 110 Name: tfe.String(wsName), 111 TerraformVersion: tfe.String(tfversion.String()), 112 AutoApply: tfe.Bool(false), 113 }) 114 tfBlock := terraformConfigCloudBackendName(orgName, wsName) 115 writeMainTF(t, tfBlock, dir) 116 }, 117 commands: []tfCommand{ 118 { 119 command: []string{"init"}, 120 expectedCmdOutput: `Terraform Cloud has been successfully initialized!`, 121 }, 122 { 123 command: []string{"apply", "-auto-approve"}, 124 expectedCmdOutput: `Apply complete!`, 125 }, 126 }, 127 }, 128 }, 129 validations: func(t *testing.T, orgName string) { 130 workspace, err := tfeClient.Workspaces.ReadWithOptions(ctx, orgName, "app", &tfe.WorkspaceReadOptions{Include: []tfe.WSIncludeOpt{tfe.WSCurrentRun}}) 131 if err != nil { 132 t.Fatal(err) 133 } 134 if workspace.CurrentRun == nil { 135 t.Fatal("Expected workspace to have run, but got nil") 136 } 137 if workspace.CurrentRun.Status != tfe.RunApplied { 138 t.Fatalf("Expected run status to be `applied`, but is %s", workspace.CurrentRun.Status) 139 } 140 }, 141 }, 142 "workspace auto apply, terraform apply with auto-approve, no prompt": { 143 operations: []operationSets{ 144 { 145 prep: func(t *testing.T, orgName, dir string) { 146 wsName := "app" 147 _ = createWorkspace(t, orgName, tfe.WorkspaceCreateOptions{ 148 Name: tfe.String(wsName), 149 TerraformVersion: tfe.String(tfversion.String()), 150 AutoApply: tfe.Bool(true), 151 }) 152 tfBlock := terraformConfigCloudBackendName(orgName, wsName) 153 writeMainTF(t, tfBlock, dir) 154 }, 155 commands: []tfCommand{ 156 { 157 command: []string{"init"}, 158 expectedCmdOutput: `Terraform Cloud has been successfully initialized!`, 159 }, 160 { 161 command: []string{"apply", "-auto-approve"}, 162 expectedCmdOutput: `Apply complete!`, 163 }, 164 }, 165 }, 166 }, 167 validations: func(t *testing.T, orgName string) { 168 workspace, err := tfeClient.Workspaces.ReadWithOptions(ctx, orgName, "app", &tfe.WorkspaceReadOptions{Include: []tfe.WSIncludeOpt{tfe.WSCurrentRun}}) 169 if err != nil { 170 t.Fatal(err) 171 } 172 if workspace.CurrentRun == nil { 173 t.Fatal("Expected workspace to have run, but got nil") 174 } 175 if workspace.CurrentRun.Status != tfe.RunApplied { 176 t.Fatalf("Expected run status to be `applied`, but is %s", workspace.CurrentRun.Status) 177 } 178 }, 179 }, 180 } 181 182 testRunner(t, cases, 1) 183 }