github.com/opentofu/opentofu@v1.7.1/internal/cloud/e2e/backend_apply_before_init_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  	"testing"
    10  )
    11  
    12  func Test_backend_apply_before_init(t *testing.T) {
    13  	t.Parallel()
    14  	skipIfMissingEnvVar(t)
    15  	skipWithoutRemoteTerraformVersion(t)
    16  
    17  	cases := testCases{
    18  		"terraform apply with cloud block - blank state": {
    19  			operations: []operationSets{
    20  				{
    21  					prep: func(t *testing.T, orgName, dir string) {
    22  						wsName := "new-workspace"
    23  						tfBlock := terraformConfigCloudBackendName(orgName, wsName)
    24  						writeMainTF(t, tfBlock, dir)
    25  					},
    26  					commands: []tfCommand{
    27  						{
    28  							command:           []string{"apply"},
    29  							expectedCmdOutput: `Terraform Cloud initialization required: please run "terraform init"`,
    30  							expectError:       true,
    31  						},
    32  					},
    33  				},
    34  			},
    35  		},
    36  		"terraform apply with cloud block - local state": {
    37  			operations: []operationSets{
    38  				{
    39  					prep: func(t *testing.T, orgName, dir string) {
    40  						tfBlock := terraformConfigLocalBackend()
    41  						writeMainTF(t, tfBlock, dir)
    42  					},
    43  					commands: []tfCommand{
    44  						{
    45  							command:           []string{"init"},
    46  							expectedCmdOutput: `Successfully configured the backend "local"!`,
    47  						},
    48  						{
    49  							command:         []string{"apply", "-auto-approve"},
    50  							postInputOutput: []string{`Apply complete!`},
    51  						},
    52  					},
    53  				},
    54  				{
    55  					prep: func(t *testing.T, orgName, dir string) {
    56  						wsName := "new-workspace"
    57  						tfBlock := terraformConfigCloudBackendName(orgName, wsName)
    58  						writeMainTF(t, tfBlock, dir)
    59  					},
    60  					commands: []tfCommand{
    61  						{
    62  							command:           []string{"apply"},
    63  							expectedCmdOutput: `Terraform Cloud initialization required: please run "terraform init"`,
    64  							expectError:       true,
    65  						},
    66  					},
    67  				},
    68  			},
    69  		},
    70  	}
    71  
    72  	testRunner(t, cases, 1)
    73  }