github.com/pdecat/terraform@v0.11.9-beta1/backend/remote/testing.go (about) 1 package remote 2 3 import ( 4 "context" 5 "fmt" 6 "io" 7 "net/http" 8 "net/http/httptest" 9 "testing" 10 11 tfe "github.com/hashicorp/go-tfe" 12 "github.com/hashicorp/terraform/backend" 13 "github.com/hashicorp/terraform/state/remote" 14 "github.com/hashicorp/terraform/svchost" 15 "github.com/hashicorp/terraform/svchost/auth" 16 "github.com/hashicorp/terraform/svchost/disco" 17 "github.com/mitchellh/cli" 18 ) 19 20 const ( 21 testCred = "test-auth-token" 22 ) 23 24 var ( 25 tfeHost = svchost.Hostname(defaultHostname) 26 credsSrc = auth.StaticCredentialsSource(map[svchost.Hostname]map[string]interface{}{ 27 tfeHost: {"token": testCred}, 28 }) 29 ) 30 31 func testInput(t *testing.T, answers map[string]string) *mockInput { 32 return &mockInput{answers: answers} 33 } 34 35 func testBackendDefault(t *testing.T) *Remote { 36 c := map[string]interface{}{ 37 "organization": "hashicorp", 38 "workspaces": []interface{}{ 39 map[string]interface{}{ 40 "name": "prod", 41 }, 42 }, 43 } 44 return testBackend(t, c) 45 } 46 47 func testBackendNoDefault(t *testing.T) *Remote { 48 c := map[string]interface{}{ 49 "organization": "hashicorp", 50 "workspaces": []interface{}{ 51 map[string]interface{}{ 52 "prefix": "my-app-", 53 }, 54 }, 55 } 56 return testBackend(t, c) 57 } 58 59 func testRemoteClient(t *testing.T) remote.Client { 60 b := testBackendDefault(t) 61 raw, err := b.State(backend.DefaultStateName) 62 if err != nil { 63 t.Fatalf("error: %v", err) 64 } 65 s := raw.(*remote.State) 66 return s.Client 67 } 68 69 func testBackend(t *testing.T, c map[string]interface{}) *Remote { 70 s := testServer(t) 71 b := New(testDisco(s)) 72 73 // Configure the backend so the client is created. 74 backend.TestBackendConfig(t, b, c) 75 76 // Get a new mock client. 77 mc := newMockClient() 78 79 // Replace the services we use with our mock services. 80 b.CLI = cli.NewMockUi() 81 b.client.Applies = mc.Applies 82 b.client.ConfigurationVersions = mc.ConfigurationVersions 83 b.client.Organizations = mc.Organizations 84 b.client.Plans = mc.Plans 85 b.client.PolicyChecks = mc.PolicyChecks 86 b.client.Runs = mc.Runs 87 b.client.StateVersions = mc.StateVersions 88 b.client.Workspaces = mc.Workspaces 89 90 ctx := context.Background() 91 92 // Create the organization. 93 _, err := b.client.Organizations.Create(ctx, tfe.OrganizationCreateOptions{ 94 Name: tfe.String(b.organization), 95 }) 96 if err != nil { 97 t.Fatalf("error: %v", err) 98 } 99 100 // Create the default workspace if required. 101 if b.workspace != "" { 102 _, err = b.client.Workspaces.Create(ctx, b.organization, tfe.WorkspaceCreateOptions{ 103 Name: tfe.String(b.workspace), 104 }) 105 if err != nil { 106 t.Fatalf("error: %v", err) 107 } 108 } 109 110 return b 111 } 112 113 // testServer returns a *httptest.Server used for local testing. 114 func testServer(t *testing.T) *httptest.Server { 115 mux := http.NewServeMux() 116 117 // Respond to service discovery calls. 118 mux.HandleFunc("/well-known/terraform.json", func(w http.ResponseWriter, r *http.Request) { 119 w.Header().Set("Content-Type", "application/json") 120 io.WriteString(w, `{"tfe.v2":"/api/v2/"}`) 121 }) 122 123 return httptest.NewServer(mux) 124 } 125 126 // testDisco returns a *disco.Disco mapping app.terraform.io and 127 // localhost to a local test server. 128 func testDisco(s *httptest.Server) *disco.Disco { 129 services := map[string]interface{}{ 130 "tfe.v2": fmt.Sprintf("%s/api/v2/", s.URL), 131 } 132 d := disco.NewWithCredentialsSource(credsSrc) 133 134 d.ForceHostServices(svchost.Hostname(defaultHostname), services) 135 d.ForceHostServices(svchost.Hostname("localhost"), services) 136 return d 137 }