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