github.com/kevinklinger/open_terraform@v1.3.6/noninternal/cloud/e2e/env_variables_test.go (about)

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