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