github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/cloud/e2e/backend_apply_before_init_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package main
     5  
     6  import (
     7  	"testing"
     8  )
     9  
    10  func Test_backend_apply_before_init(t *testing.T) {
    11  	t.Parallel()
    12  	skipIfMissingEnvVar(t)
    13  	skipWithoutRemoteTerraformVersion(t)
    14  
    15  	cases := testCases{
    16  		"terraform apply with cloud block - blank state": {
    17  			operations: []operationSets{
    18  				{
    19  					prep: func(t *testing.T, orgName, dir string) {
    20  						wsName := "new-workspace"
    21  						tfBlock := terraformConfigCloudBackendName(orgName, wsName)
    22  						writeMainTF(t, tfBlock, dir)
    23  					},
    24  					commands: []tfCommand{
    25  						{
    26  							command:           []string{"apply"},
    27  							expectedCmdOutput: `Terraform Cloud initialization required: please run "terraform init"`,
    28  							expectError:       true,
    29  						},
    30  					},
    31  				},
    32  			},
    33  		},
    34  		"terraform apply with cloud block - local state": {
    35  			operations: []operationSets{
    36  				{
    37  					prep: func(t *testing.T, orgName, dir string) {
    38  						tfBlock := terraformConfigLocalBackend()
    39  						writeMainTF(t, tfBlock, dir)
    40  					},
    41  					commands: []tfCommand{
    42  						{
    43  							command:           []string{"init"},
    44  							expectedCmdOutput: `Successfully configured the backend "local"!`,
    45  						},
    46  						{
    47  							command:         []string{"apply", "-auto-approve"},
    48  							postInputOutput: []string{`Apply complete!`},
    49  						},
    50  					},
    51  				},
    52  				{
    53  					prep: func(t *testing.T, orgName, dir string) {
    54  						wsName := "new-workspace"
    55  						tfBlock := terraformConfigCloudBackendName(orgName, wsName)
    56  						writeMainTF(t, tfBlock, dir)
    57  					},
    58  					commands: []tfCommand{
    59  						{
    60  							command:           []string{"apply"},
    61  							expectedCmdOutput: `Terraform Cloud initialization required: please run "terraform init"`,
    62  							expectError:       true,
    63  						},
    64  					},
    65  				},
    66  			},
    67  		},
    68  	}
    69  
    70  	testRunner(t, cases, 1)
    71  }