11package test
22
33import (
4- "testing"
5- "path/filepath"
64 "fmt"
7- "sync"
8- "github.com/gruntwork-io/terratest/modules/test-structure"
5+ "github.com/gruntwork-io/terratest/modules/aws"
96 "github.com/gruntwork-io/terratest/modules/random"
107 "github.com/gruntwork-io/terratest/modules/terraform"
11- "github.com/gruntwork-io/terratest/modules/aws "
12- "github.com/gruntwork-io/terratest/modules/files "
13- "os "
14- "github.com/gruntwork-io/terratest/modules/logger "
8+ "github.com/gruntwork-io/terratest/modules/test-structure "
9+ "path/filepath "
10+ "sync "
11+ "testing "
1512)
1613
1714const clusterNamePrimaryVarName = "cluster_name_primary"
@@ -105,8 +102,6 @@ func testCouchbaseMultiDataCenterReplication(t *testing.T, osName string, editio
105102 terraformOptions := test_structure .LoadTerraformOptions (t , couchbaseMultiClusterDir )
106103 terraform .Destroy (t , terraformOptions )
107104
108- restoreProvider (t , couchbaseMultiClusterDir )
109-
110105 amiIdPrimary := test_structure .LoadString (t , couchbaseMultiClusterDir , savedAmiIdPrimary )
111106 amiIdReplica := test_structure .LoadString (t , couchbaseMultiClusterDir , savedAmiIdReplica )
112107
@@ -136,15 +131,15 @@ func testCouchbaseMultiDataCenterReplication(t *testing.T, osName string, editio
136131 uniqueIdPrimary := test_structure .LoadString (t , couchbaseMultiClusterDir , savedUniqueIdPrimary )
137132 uniqueIdReplica := test_structure .LoadString (t , couchbaseMultiClusterDir , savedUniqueIdReplica )
138133
139- overrideProvider (t , couchbaseMultiClusterDir , awsRegionPrimary , awsRegionReplica )
140-
141134 terraformOptions := & terraform.Options {
142135 TerraformDir : couchbaseMultiClusterDir ,
143- Vars : map [string ]interface {} {
144- "ami_id_primary" : amiIdPrimary ,
145- "ami_id_replica" : amiIdReplica ,
146- clusterNamePrimaryVarName : formatCouchbaseClusterName ("primary" , uniqueIdPrimary ),
147- clusterNameReplicaVarName : formatCouchbaseClusterName ("replica" , uniqueIdReplica ),
136+ Vars : map [string ]interface {}{
137+ "primary_region" : awsRegionPrimary ,
138+ "replica_region" : awsRegionReplica ,
139+ "ami_id_primary" : amiIdPrimary ,
140+ "ami_id_replica" : amiIdReplica ,
141+ clusterNamePrimaryVarName : formatCouchbaseClusterName ("primary" , uniqueIdPrimary ),
142+ clusterNameReplicaVarName : formatCouchbaseClusterName ("replica" , uniqueIdReplica ),
148143 },
149144 }
150145
@@ -168,55 +163,3 @@ func testCouchbaseMultiDataCenterReplication(t *testing.T, osName string, editio
168163 checkReplicationIsWorking (t , consoleUrlPrimary , consoleUrlReplica , "test-bucket" , "test-bucket-replica" )
169164 })
170165}
171-
172- const providerOverrideTemplate = `
173- # This file temporarily overrides the providers at test time. The original providers file should be restored at the
174- # end of the test!
175-
176- provider "aws" {
177- alias = "primary"
178- region = "%s"
179- }
180-
181- provider "aws" {
182- alias = "replica"
183- region = "%s"
184- }
185- `
186-
187- // In order for the examples to work well with the Terraform Registry, where they are wrapped in a module, we cannot
188- // define the AWS regions in those providers. This works OK for manual usage, where the user can specify the region
189- // interactively, but not at test time. Therefore, as a workaround, we override the providers.tf file at test time
190- // with the regions fully defined, and then put it back at the end of the test in the restoreProvider function.
191- func overrideProvider (t * testing.T , couchbaseMultiClusterDir string , awsRegionPrimary string , awsRegionReplica string ) {
192- providersFilePath := filepath .Join (couchbaseMultiClusterDir , providersFile )
193- providersFileBackupPath := filepath .Join (couchbaseMultiClusterDir , providersFileBackup )
194-
195- logger .Logf (t , "Backing up %s to %s" , providersFilePath , providersFileBackupPath )
196- if err := files .CopyFile (providersFilePath , providersFileBackupPath ); err != nil {
197- t .Fatal (err )
198- }
199-
200- newProvidersFileContents := fmt .Sprintf (providerOverrideTemplate , awsRegionPrimary , awsRegionReplica )
201-
202- logger .Logf (t , "Creating override proviers file at %s with contents:\n %s" , providersFilePath , newProvidersFileContents )
203- if err := files .WriteFileWithSamePermissions (providersFilePath , providersFilePath , []byte (newProvidersFileContents )); err != nil {
204- t .Fatal (err )
205- }
206- }
207-
208- // See the overrideProvider method for details
209- func restoreProvider (t * testing.T , couchbaseMultiClusterDir string ) {
210- providersFilePath := filepath .Join (couchbaseMultiClusterDir , providersFile )
211- providersFileBackupPath := filepath .Join (couchbaseMultiClusterDir , providersFileBackup )
212-
213- logger .Logf (t , "Restoring %s from %s" , providersFilePath , providersFileBackupPath )
214- if err := files .CopyFile (providersFileBackupPath , providersFilePath ); err != nil {
215- t .Fatal (err )
216- }
217-
218- logger .Logf (t , "Deleting %s" , providersFileBackupPath )
219- if err := os .Remove (providersFileBackupPath ); err != nil {
220- t .Fatal (err )
221- }
222- }
0 commit comments