github.com/opentofu/opentofu@v1.7.1/internal/cloud/e2e/env_variables_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 "fmt" 11 "testing" 12 13 "github.com/hashicorp/go-tfe" 14 ) 15 16 func Test_cloud_organization_env_var(t *testing.T) { 17 t.Parallel() 18 skipIfMissingEnvVar(t) 19 20 ctx := context.Background() 21 org, cleanup := createOrganization(t) 22 t.Cleanup(cleanup) 23 24 cases := testCases{ 25 "with TF_CLOUD_ORGANIZATION set": { 26 operations: []operationSets{ 27 { 28 prep: func(t *testing.T, orgName, dir string) { 29 remoteWorkspace := "cloud-workspace" 30 tfBlock := terraformConfigCloudBackendOmitOrg(remoteWorkspace) 31 writeMainTF(t, tfBlock, dir) 32 }, 33 commands: []tfCommand{ 34 { 35 command: []string{"init"}, 36 expectedCmdOutput: `Terraform Cloud has been successfully initialized!`, 37 }, 38 { 39 command: []string{"apply", "-auto-approve"}, 40 postInputOutput: []string{`Apply complete!`}, 41 }, 42 }, 43 }, 44 }, 45 validations: func(t *testing.T, orgName string) { 46 expectedName := "cloud-workspace" 47 ws, err := tfeClient.Workspaces.Read(ctx, org.Name, expectedName) 48 if err != nil { 49 t.Fatal(err) 50 } 51 if ws == nil { 52 t.Fatalf("Expected workspace %s to be present, but is not.", expectedName) 53 } 54 }, 55 }, 56 } 57 58 testRunner(t, cases, 0, fmt.Sprintf("TF_CLOUD_ORGANIZATION=%s", org.Name)) 59 } 60 61 func Test_cloud_workspace_name_env_var(t *testing.T) { 62 t.Parallel() 63 skipIfMissingEnvVar(t) 64 65 org, orgCleanup := createOrganization(t) 66 t.Cleanup(orgCleanup) 67 68 wk := createWorkspace(t, org.Name, tfe.WorkspaceCreateOptions{ 69 Name: tfe.String("cloud-workspace"), 70 }) 71 72 validCases := testCases{ 73 "a workspace that exists": { 74 operations: []operationSets{ 75 { 76 prep: func(t *testing.T, orgName, dir string) { 77 tfBlock := terraformConfigCloudBackendOmitWorkspaces(org.Name) 78 writeMainTF(t, tfBlock, dir) 79 }, 80 commands: []tfCommand{ 81 { 82 command: []string{"init"}, 83 expectedCmdOutput: `Terraform Cloud has been successfully initialized!`, 84 }, 85 { 86 command: []string{"apply", "-auto-approve"}, 87 postInputOutput: []string{`Apply complete!`}, 88 }, 89 }, 90 }, 91 { 92 prep: func(t *testing.T, orgName, dir string) { 93 tfBlock := terraformConfigCloudBackendOmitWorkspaces(org.Name) 94 writeMainTF(t, tfBlock, dir) 95 }, 96 commands: []tfCommand{ 97 { 98 command: []string{"init"}, 99 expectedCmdOutput: `Terraform Cloud has been successfully initialized!`, 100 }, 101 { 102 command: []string{"workspace", "show"}, 103 expectedCmdOutput: wk.Name, 104 }, 105 }, 106 }, 107 }, 108 }, 109 } 110 111 errCases := testCases{ 112 "a workspace that doesn't exist": { 113 operations: []operationSets{ 114 { 115 prep: func(t *testing.T, orgName, dir string) { 116 tfBlock := terraformConfigCloudBackendOmitWorkspaces(org.Name) 117 writeMainTF(t, tfBlock, dir) 118 }, 119 commands: []tfCommand{ 120 { 121 command: []string{"init"}, 122 expectError: true, 123 }, 124 }, 125 }, 126 }, 127 }, 128 } 129 130 testRunner(t, validCases, 0, fmt.Sprintf(`TF_WORKSPACE=%s`, wk.Name)) 131 testRunner(t, errCases, 0, fmt.Sprintf(`TF_WORKSPACE=%s`, "the-fires-of-mt-doom")) 132 } 133 134 func Test_cloud_workspace_tags_env_var(t *testing.T) { 135 t.Parallel() 136 skipIfMissingEnvVar(t) 137 138 org, orgCleanup := createOrganization(t) 139 t.Cleanup(orgCleanup) 140 141 wkValid := createWorkspace(t, org.Name, tfe.WorkspaceCreateOptions{ 142 Name: tfe.String("cloud-workspace"), 143 Tags: []*tfe.Tag{ 144 {Name: "cloud"}, 145 }, 146 }) 147 148 // this will be a workspace that won't have a tag listed in our test configuration 149 wkInvalid := createWorkspace(t, org.Name, tfe.WorkspaceCreateOptions{ 150 Name: tfe.String("cloud-workspace-2"), 151 }) 152 153 validCases := testCases{ 154 "a workspace with valid tag": { 155 operations: []operationSets{ 156 { 157 prep: func(t *testing.T, orgName, dir string) { 158 tfBlock := terraformConfigCloudBackendTags(org.Name, wkValid.TagNames[0]) 159 writeMainTF(t, tfBlock, dir) 160 }, 161 commands: []tfCommand{ 162 { 163 command: []string{"init"}, 164 expectedCmdOutput: `Terraform Cloud has been successfully initialized!`, 165 }, 166 { 167 command: []string{"apply", "-auto-approve"}, 168 postInputOutput: []string{`Apply complete!`}, 169 }, 170 }, 171 }, 172 { 173 prep: func(t *testing.T, orgName, dir string) { 174 tfBlock := terraformConfigCloudBackendTags(org.Name, wkValid.TagNames[0]) 175 writeMainTF(t, tfBlock, dir) 176 }, 177 commands: []tfCommand{ 178 { 179 command: []string{"init"}, 180 expectedCmdOutput: `Terraform Cloud has been successfully initialized!`, 181 }, 182 { 183 command: []string{"workspace", "show"}, 184 expectedCmdOutput: wkValid.Name, 185 }, 186 }, 187 }, 188 }, 189 }, 190 } 191 192 errCases := testCases{ 193 "a workspace not specified by tags": { 194 operations: []operationSets{ 195 { 196 prep: func(t *testing.T, orgName, dir string) { 197 tfBlock := terraformConfigCloudBackendTags(org.Name, wkValid.TagNames[0]) 198 writeMainTF(t, tfBlock, dir) 199 }, 200 commands: []tfCommand{ 201 { 202 command: []string{"init"}, 203 expectError: true, 204 }, 205 }, 206 }, 207 }, 208 }, 209 } 210 211 testRunner(t, validCases, 0, fmt.Sprintf(`TF_WORKSPACE=%s`, wkValid.Name)) 212 testRunner(t, errCases, 0, fmt.Sprintf(`TF_WORKSPACE=%s`, wkInvalid.Name)) 213 } 214 215 func Test_cloud_null_config(t *testing.T) { 216 t.Parallel() 217 skipIfMissingEnvVar(t) 218 219 org, cleanup := createOrganization(t) 220 t.Cleanup(cleanup) 221 222 wk := createWorkspace(t, org.Name, tfe.WorkspaceCreateOptions{ 223 Name: tfe.String("cloud-workspace"), 224 }) 225 226 cases := testCases{ 227 "with all env vars set": { 228 operations: []operationSets{ 229 { 230 prep: func(t *testing.T, orgName, dir string) { 231 tfBlock := terraformConfigCloudBackendOmitConfig() 232 writeMainTF(t, tfBlock, dir) 233 }, 234 commands: []tfCommand{ 235 { 236 command: []string{"init"}, 237 expectedCmdOutput: `Terraform Cloud has been successfully initialized!`, 238 }, 239 { 240 command: []string{"apply", "-auto-approve"}, 241 postInputOutput: []string{`Apply complete!`}, 242 }, 243 }, 244 }, 245 { 246 prep: func(t *testing.T, orgName, dir string) { 247 tfBlock := terraformConfigCloudBackendOmitConfig() 248 writeMainTF(t, tfBlock, dir) 249 }, 250 commands: []tfCommand{ 251 { 252 command: []string{"init"}, 253 expectedCmdOutput: `Terraform Cloud has been successfully initialized!`, 254 }, 255 { 256 command: []string{"workspace", "show"}, 257 expectedCmdOutput: wk.Name, 258 }, 259 }, 260 }, 261 }, 262 }, 263 } 264 265 testRunner(t, cases, 1, 266 fmt.Sprintf(`TF_CLOUD_ORGANIZATION=%s`, org.Name), 267 fmt.Sprintf(`TF_CLOUD_HOSTNAME=%s`, tfeHostname), 268 fmt.Sprintf(`TF_WORKSPACE=%s`, wk.Name)) 269 }