github.com/codeherentuk/terraform@v0.11.12-beta1/backend/init/init_test.go (about) 1 package init 2 3 import ( 4 "os" 5 "reflect" 6 "testing" 7 8 backendLocal "github.com/hashicorp/terraform/backend/local" 9 ) 10 11 func TestInit_backend(t *testing.T) { 12 // Initialize the backends map 13 Init(nil) 14 15 backends := []struct { 16 Name string 17 Type string 18 }{ 19 { 20 "local", 21 "*local.Local", 22 }, { 23 "remote", 24 "*remote.Remote", 25 }, { 26 "atlas", 27 "*atlas.Backend", 28 }, { 29 "azurerm", 30 "*azure.Backend", 31 }, { 32 "consul", 33 "*consul.Backend", 34 }, { 35 "etcdv3", 36 "*etcd.Backend", 37 }, { 38 "gcs", 39 "*gcs.Backend", 40 }, { 41 "inmem", 42 "*inmem.Backend", 43 }, { 44 "manta", 45 "*manta.Backend", 46 }, { 47 "s3", 48 "*s3.Backend", 49 }, { 50 "swift", 51 "*swift.Backend", 52 }, { 53 "azure", 54 "init.deprecatedBackendShim", 55 }, 56 } 57 58 // Make sure we get the requested backend 59 for _, b := range backends { 60 f := Backend(b.Name) 61 bType := reflect.TypeOf(f()).String() 62 63 if bType != b.Type { 64 t.Fatalf("expected backend %q to be %q, got: %q", b.Name, b.Type, bType) 65 } 66 } 67 } 68 69 func TestInit_forceLocalBackend(t *testing.T) { 70 // Initialize the backends map 71 Init(nil) 72 73 enhancedBackends := []struct { 74 Name string 75 Type string 76 }{ 77 { 78 "local", 79 "nil", 80 }, { 81 "remote", 82 "*remote.Remote", 83 }, 84 } 85 86 // Set the TF_FORCE_LOCAL_BACKEND flag so all enhanced backends will 87 // return a local.Local backend with themselves as embedded backend. 88 if err := os.Setenv("TF_FORCE_LOCAL_BACKEND", "1"); err != nil { 89 t.Fatalf("error setting environment variable TF_FORCE_LOCAL_BACKEND: %v", err) 90 } 91 92 // Make sure we always get the local backend. 93 for _, b := range enhancedBackends { 94 f := Backend(b.Name) 95 96 local, ok := f().(*backendLocal.Local) 97 if !ok { 98 t.Fatalf("expected backend %q to be \"*local.Local\", got: %T", b.Name, f()) 99 } 100 101 bType := "nil" 102 if local.Backend != nil { 103 bType = reflect.TypeOf(local.Backend).String() 104 } 105 106 if bType != b.Type { 107 t.Fatalf("expected local.Backend to be %s, got: %s", b.Type, bType) 108 } 109 } 110 }