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

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