2018-10-31 16:45:03 +01:00
|
|
|
package remote
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2019-02-18 16:01:06 +01:00
|
|
|
"path"
|
2018-10-31 16:45:03 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
tfe "github.com/hashicorp/go-tfe"
|
2019-11-13 17:34:09 +01:00
|
|
|
svchost "github.com/hashicorp/terraform-svchost"
|
2019-10-11 11:34:26 +02:00
|
|
|
"github.com/hashicorp/terraform-svchost/auth"
|
|
|
|
"github.com/hashicorp/terraform-svchost/disco"
|
2018-10-31 16:45:03 +01:00
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
|
|
"github.com/hashicorp/terraform/configs"
|
2018-11-15 20:26:46 +01:00
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
2019-10-11 11:34:26 +02:00
|
|
|
"github.com/hashicorp/terraform/httpclient"
|
2018-11-15 20:26:46 +01:00
|
|
|
"github.com/hashicorp/terraform/providers"
|
2020-08-11 17:43:01 +02:00
|
|
|
"github.com/hashicorp/terraform/states/remote"
|
2018-10-31 16:45:03 +01:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
2019-10-11 11:34:26 +02:00
|
|
|
"github.com/hashicorp/terraform/version"
|
2018-10-31 16:45:03 +01:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
2018-11-15 20:26:46 +01:00
|
|
|
|
|
|
|
backendLocal "github.com/hashicorp/terraform/backend/local"
|
2018-10-31 16:45:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
testCred = "test-auth-token"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
tfeHost = svchost.Hostname(defaultHostname)
|
|
|
|
credsSrc = auth.StaticCredentialsSource(map[svchost.Hostname]map[string]interface{}{
|
|
|
|
tfeHost: {"token": testCred},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
func testInput(t *testing.T, answers map[string]string) *mockInput {
|
|
|
|
return &mockInput{answers: answers}
|
|
|
|
}
|
|
|
|
|
2019-02-06 09:36:42 +01:00
|
|
|
func testBackendDefault(t *testing.T) (*Remote, func()) {
|
2018-10-31 16:45:03 +01:00
|
|
|
obj := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"hostname": cty.NullVal(cty.String),
|
|
|
|
"organization": cty.StringVal("hashicorp"),
|
|
|
|
"token": cty.NullVal(cty.String),
|
|
|
|
"workspaces": cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"name": cty.StringVal("prod"),
|
|
|
|
"prefix": cty.NullVal(cty.String),
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
return testBackend(t, obj)
|
|
|
|
}
|
|
|
|
|
2019-02-06 09:36:42 +01:00
|
|
|
func testBackendNoDefault(t *testing.T) (*Remote, func()) {
|
2018-10-31 16:45:03 +01:00
|
|
|
obj := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"hostname": cty.NullVal(cty.String),
|
|
|
|
"organization": cty.StringVal("hashicorp"),
|
|
|
|
"token": cty.NullVal(cty.String),
|
|
|
|
"workspaces": cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"name": cty.NullVal(cty.String),
|
|
|
|
"prefix": cty.StringVal("my-app-"),
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
return testBackend(t, obj)
|
|
|
|
}
|
|
|
|
|
2019-02-06 09:36:42 +01:00
|
|
|
func testBackendNoOperations(t *testing.T) (*Remote, func()) {
|
2018-12-05 12:29:08 +01:00
|
|
|
obj := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"hostname": cty.NullVal(cty.String),
|
|
|
|
"organization": cty.StringVal("no-operations"),
|
|
|
|
"token": cty.NullVal(cty.String),
|
|
|
|
"workspaces": cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"name": cty.StringVal("prod"),
|
|
|
|
"prefix": cty.NullVal(cty.String),
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
return testBackend(t, obj)
|
|
|
|
}
|
|
|
|
|
2018-10-31 16:45:03 +01:00
|
|
|
func testRemoteClient(t *testing.T) remote.Client {
|
2019-02-06 09:36:42 +01:00
|
|
|
b, bCleanup := testBackendDefault(t)
|
|
|
|
defer bCleanup()
|
|
|
|
|
2018-10-31 16:45:03 +01:00
|
|
|
raw, err := b.StateMgr(backend.DefaultStateName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error: %v", err)
|
|
|
|
}
|
2019-02-06 09:36:42 +01:00
|
|
|
|
|
|
|
return raw.(*remote.State).Client
|
2018-10-31 16:45:03 +01:00
|
|
|
}
|
|
|
|
|
2019-02-06 09:36:42 +01:00
|
|
|
func testBackend(t *testing.T, obj cty.Value) (*Remote, func()) {
|
2018-10-31 16:45:03 +01:00
|
|
|
s := testServer(t)
|
|
|
|
b := New(testDisco(s))
|
|
|
|
|
|
|
|
// Configure the backend so the client is created.
|
2019-02-26 00:37:20 +01:00
|
|
|
newObj, valDiags := b.PrepareConfig(obj)
|
2018-10-31 16:45:03 +01:00
|
|
|
if len(valDiags) != 0 {
|
|
|
|
t.Fatal(valDiags.ErrWithWarnings())
|
|
|
|
}
|
2019-02-26 00:37:20 +01:00
|
|
|
obj = newObj
|
2018-10-31 16:45:03 +01:00
|
|
|
|
|
|
|
confDiags := b.Configure(obj)
|
|
|
|
if len(confDiags) != 0 {
|
|
|
|
t.Fatal(confDiags.ErrWithWarnings())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a new mock client.
|
|
|
|
mc := newMockClient()
|
|
|
|
|
|
|
|
// Replace the services we use with our mock services.
|
|
|
|
b.CLI = cli.NewMockUi()
|
|
|
|
b.client.Applies = mc.Applies
|
|
|
|
b.client.ConfigurationVersions = mc.ConfigurationVersions
|
2019-09-27 23:22:34 +02:00
|
|
|
b.client.CostEstimates = mc.CostEstimates
|
2018-10-31 16:45:03 +01:00
|
|
|
b.client.Organizations = mc.Organizations
|
|
|
|
b.client.Plans = mc.Plans
|
|
|
|
b.client.PolicyChecks = mc.PolicyChecks
|
|
|
|
b.client.Runs = mc.Runs
|
|
|
|
b.client.StateVersions = mc.StateVersions
|
2019-11-13 17:34:09 +01:00
|
|
|
b.client.Variables = mc.Variables
|
2018-10-31 16:45:03 +01:00
|
|
|
b.client.Workspaces = mc.Workspaces
|
|
|
|
|
|
|
|
b.ShowDiagnostics = func(vals ...interface{}) {
|
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
for _, diag := range diags.Append(vals...) {
|
|
|
|
b.CLI.Error(diag.Description().Summary)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 20:26:46 +01:00
|
|
|
// Set local to a local test backend.
|
|
|
|
b.local = testLocalBackend(t, b)
|
|
|
|
|
2018-10-31 16:45:03 +01:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// Create the organization.
|
|
|
|
_, err := b.client.Organizations.Create(ctx, tfe.OrganizationCreateOptions{
|
|
|
|
Name: tfe.String(b.organization),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the default workspace if required.
|
|
|
|
if b.workspace != "" {
|
|
|
|
_, err = b.client.Workspaces.Create(ctx, b.organization, tfe.WorkspaceCreateOptions{
|
|
|
|
Name: tfe.String(b.workspace),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-06 09:36:42 +01:00
|
|
|
return b, s.Close
|
2018-10-31 16:45:03 +01:00
|
|
|
}
|
|
|
|
|
2018-11-15 20:26:46 +01:00
|
|
|
func testLocalBackend(t *testing.T, remote *Remote) backend.Enhanced {
|
|
|
|
b := backendLocal.NewWithBackend(remote)
|
|
|
|
|
|
|
|
b.CLI = remote.CLI
|
|
|
|
b.ShowDiagnostics = remote.ShowDiagnostics
|
|
|
|
|
|
|
|
// Add a test provider to the local backend.
|
|
|
|
p := backendLocal.TestLocalProvider(t, b, "null", &terraform.ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"null_resource": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Computed: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
p.ApplyResourceChangeResponse = providers.ApplyResourceChangeResponse{NewState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.StringVal("yes"),
|
|
|
|
})}
|
|
|
|
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2018-10-31 16:45:03 +01:00
|
|
|
// testServer returns a *httptest.Server used for local testing.
|
|
|
|
func testServer(t *testing.T) *httptest.Server {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
|
|
|
// Respond to service discovery calls.
|
|
|
|
mux.HandleFunc("/well-known/terraform.json", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2018-12-15 21:28:35 +01:00
|
|
|
io.WriteString(w, `{
|
2019-02-18 16:01:06 +01:00
|
|
|
"state.v2": "/api/v2/",
|
2018-12-15 21:28:35 +01:00
|
|
|
"tfe.v2.1": "/api/v2/",
|
|
|
|
"versions.v1": "/v1/versions/"
|
|
|
|
}`)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Respond to service version constraints calls.
|
|
|
|
mux.HandleFunc("/v1/versions/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2019-02-18 16:01:06 +01:00
|
|
|
io.WriteString(w, fmt.Sprintf(`{
|
|
|
|
"service": "%s",
|
2018-12-15 21:28:35 +01:00
|
|
|
"product": "terraform",
|
2019-02-06 09:36:42 +01:00
|
|
|
"minimum": "0.1.0",
|
|
|
|
"maximum": "10.0.0"
|
2019-02-18 16:01:06 +01:00
|
|
|
}`, path.Base(r.URL.Path)))
|
2018-10-31 16:45:03 +01:00
|
|
|
})
|
|
|
|
|
2020-05-15 05:01:55 +02:00
|
|
|
// Respond to pings to get the API version header.
|
|
|
|
mux.HandleFunc("/api/v2/ping", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("TFP-API-Version", "2.3")
|
|
|
|
})
|
|
|
|
|
2018-12-05 12:29:08 +01:00
|
|
|
// Respond to the initial query to read the hashicorp org entitlements.
|
|
|
|
mux.HandleFunc("/api/v2/organizations/hashicorp/entitlement-set", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/vnd.api+json")
|
|
|
|
io.WriteString(w, `{
|
|
|
|
"data": {
|
|
|
|
"id": "org-GExadygjSbKP8hsY",
|
|
|
|
"type": "entitlement-sets",
|
|
|
|
"attributes": {
|
|
|
|
"operations": true,
|
|
|
|
"private-module-registry": true,
|
|
|
|
"sentinel": true,
|
|
|
|
"state-storage": true,
|
|
|
|
"teams": true,
|
|
|
|
"vcs-integrations": true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Respond to the initial query to read the no-operations org entitlements.
|
|
|
|
mux.HandleFunc("/api/v2/organizations/no-operations/entitlement-set", func(w http.ResponseWriter, r *http.Request) {
|
2018-11-15 20:26:46 +01:00
|
|
|
w.Header().Set("Content-Type", "application/vnd.api+json")
|
|
|
|
io.WriteString(w, `{
|
|
|
|
"data": {
|
2018-12-05 12:29:08 +01:00
|
|
|
"id": "org-ufxa3y8jSbKP8hsT",
|
|
|
|
"type": "entitlement-sets",
|
2018-11-15 20:26:46 +01:00
|
|
|
"attributes": {
|
2018-12-05 12:29:08 +01:00
|
|
|
"operations": false,
|
|
|
|
"private-module-registry": true,
|
|
|
|
"sentinel": true,
|
|
|
|
"state-storage": true,
|
|
|
|
"teams": true,
|
|
|
|
"vcs-integrations": true
|
2018-11-15 20:26:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
})
|
|
|
|
|
|
|
|
// All tests that are assumed to pass will use the hashicorp organization,
|
|
|
|
// so for all other organization requests we will return a 404.
|
|
|
|
mux.HandleFunc("/api/v2/organizations/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(404)
|
|
|
|
io.WriteString(w, `{
|
|
|
|
"errors": [
|
|
|
|
{
|
|
|
|
"status": "404",
|
|
|
|
"title": "not found"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}`)
|
|
|
|
})
|
|
|
|
|
2018-10-31 16:45:03 +01:00
|
|
|
return httptest.NewServer(mux)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testDisco returns a *disco.Disco mapping app.terraform.io and
|
|
|
|
// localhost to a local test server.
|
|
|
|
func testDisco(s *httptest.Server) *disco.Disco {
|
|
|
|
services := map[string]interface{}{
|
2019-02-18 16:01:06 +01:00
|
|
|
"state.v2": fmt.Sprintf("%s/api/v2/", s.URL),
|
2018-12-15 21:28:35 +01:00
|
|
|
"tfe.v2.1": fmt.Sprintf("%s/api/v2/", s.URL),
|
|
|
|
"versions.v1": fmt.Sprintf("%s/v1/versions/", s.URL),
|
2018-10-31 16:45:03 +01:00
|
|
|
}
|
|
|
|
d := disco.NewWithCredentialsSource(credsSrc)
|
2019-10-11 11:34:26 +02:00
|
|
|
d.SetUserAgent(httpclient.TerraformUserAgent(version.String()))
|
2018-10-31 16:45:03 +01:00
|
|
|
|
|
|
|
d.ForceHostServices(svchost.Hostname(defaultHostname), services)
|
|
|
|
d.ForceHostServices(svchost.Hostname("localhost"), services)
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
type unparsedVariableValue struct {
|
|
|
|
value string
|
|
|
|
source terraform.ValueSourceType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *unparsedVariableValue) ParseVariableValue(mode configs.VariableParsingMode) (*terraform.InputValue, tfdiags.Diagnostics) {
|
|
|
|
return &terraform.InputValue{
|
|
|
|
Value: cty.StringVal(v.value),
|
|
|
|
SourceType: v.source,
|
|
|
|
}, tfdiags.Diagnostics{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// testVariable returns a backend.UnparsedVariableValue used for testing.
|
|
|
|
func testVariables(s terraform.ValueSourceType, vs ...string) map[string]backend.UnparsedVariableValue {
|
|
|
|
vars := make(map[string]backend.UnparsedVariableValue, len(vs))
|
|
|
|
for _, v := range vs {
|
|
|
|
vars[v] = &unparsedVariableValue{
|
|
|
|
value: v,
|
|
|
|
source: s,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return vars
|
|
|
|
}
|