2017-01-19 05:47:56 +01:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2018-03-28 16:54:08 +02:00
|
|
|
"os"
|
2017-01-19 05:47:56 +01:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2017-03-01 19:59:17 +01:00
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
|
|
"github.com/hashicorp/terraform/state"
|
2017-01-19 05:47:56 +01:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2018-03-21 02:43:02 +01:00
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
2017-01-19 05:47:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestLocal returns a configured Local struct with temporary paths and
|
|
|
|
// in-memory ContextOpts.
|
|
|
|
//
|
|
|
|
// No operations will be called on the returned value, so you can still set
|
|
|
|
// public fields without any locks.
|
2018-03-28 16:54:08 +02:00
|
|
|
func TestLocal(t *testing.T) (*Local, func()) {
|
2018-03-21 02:43:02 +01:00
|
|
|
t.Helper()
|
2018-07-04 12:11:35 +02:00
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
tempDir := testTempDir(t)
|
|
|
|
var local *Local
|
|
|
|
local = &Local{
|
|
|
|
StatePath: filepath.Join(tempDir, "state.tfstate"),
|
|
|
|
StateOutPath: filepath.Join(tempDir, "state.tfstate"),
|
|
|
|
StateBackupPath: filepath.Join(tempDir, "state.tfstate.bak"),
|
|
|
|
StateWorkspaceDir: filepath.Join(tempDir, "state.tfstate.d"),
|
|
|
|
ContextOpts: &terraform.ContextOpts{},
|
|
|
|
ShowDiagnostics: func(vals ...interface{}) {
|
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
diags = diags.Append(vals...)
|
|
|
|
for _, diag := range diags {
|
|
|
|
t.Log(diag.Description().Summary)
|
|
|
|
if local.CLI != nil {
|
|
|
|
local.CLI.Error(diag.Description().Summary)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2018-03-28 16:54:08 +02:00
|
|
|
cleanup := func() {
|
|
|
|
if err := os.RemoveAll(tempDir); err != nil {
|
|
|
|
t.Fatal("error clecanup up test:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return local, cleanup
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestLocalProvider modifies the ContextOpts of the *Local parameter to
|
|
|
|
// have a provider with the given name.
|
2018-05-23 04:57:04 +02:00
|
|
|
func TestLocalProvider(t *testing.T, b *Local, name string, schema *terraform.ProviderSchema) *terraform.MockResourceProvider {
|
2017-01-19 05:47:56 +01:00
|
|
|
// Build a mock resource provider for in-memory operations
|
|
|
|
p := new(terraform.MockResourceProvider)
|
2018-05-23 04:57:04 +02:00
|
|
|
p.GetSchemaReturn = schema
|
2017-01-19 05:47:56 +01:00
|
|
|
p.DiffReturn = &terraform.InstanceDiff{}
|
|
|
|
p.RefreshFn = func(
|
|
|
|
info *terraform.InstanceInfo,
|
|
|
|
s *terraform.InstanceState) (*terraform.InstanceState, error) {
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
p.ResourcesReturn = []terraform.ResourceType{
|
|
|
|
terraform.ResourceType{
|
|
|
|
Name: "test_instance",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the opts
|
|
|
|
if b.ContextOpts == nil {
|
|
|
|
b.ContextOpts = &terraform.ContextOpts{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup our provider
|
2017-04-22 02:40:46 +02:00
|
|
|
b.ContextOpts.ProviderResolver = terraform.ResourceProviderResolverFixed(
|
|
|
|
map[string]terraform.ResourceProviderFactory{
|
|
|
|
name: terraform.ResourceProviderFactoryFixed(p),
|
|
|
|
},
|
|
|
|
)
|
2017-01-19 05:47:56 +01:00
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2017-03-01 19:59:17 +01:00
|
|
|
// TestNewLocalSingle is a factory for creating a TestLocalSingleState.
|
|
|
|
// This function matches the signature required for backend/init.
|
|
|
|
func TestNewLocalSingle() backend.Backend {
|
2018-03-21 02:43:02 +01:00
|
|
|
return &TestLocalSingleState{Local: &Local{}}
|
2017-03-01 19:59:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestLocalSingleState is a backend implementation that wraps Local
|
|
|
|
// and modifies it to only support single states (returns
|
|
|
|
// ErrNamedStatesNotSupported for multi-state operations).
|
|
|
|
//
|
|
|
|
// This isn't an actual use case, this is exported just to provide a
|
|
|
|
// easy way to test that behavior.
|
|
|
|
type TestLocalSingleState struct {
|
2018-07-04 12:11:35 +02:00
|
|
|
*Local
|
2017-03-01 19:59:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *TestLocalSingleState) State(name string) (state.State, error) {
|
|
|
|
if name != backend.DefaultStateName {
|
|
|
|
return nil, backend.ErrNamedStatesNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Local.State(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *TestLocalSingleState) States() ([]string, error) {
|
|
|
|
return nil, backend.ErrNamedStatesNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *TestLocalSingleState) DeleteState(string) error {
|
|
|
|
return backend.ErrNamedStatesNotSupported
|
|
|
|
}
|
|
|
|
|
2018-07-04 17:24:49 +02:00
|
|
|
// TestNewLocalNoDefault is a factory for creating a TestLocalNoDefaultState.
|
|
|
|
// This function matches the signature required for backend/init.
|
|
|
|
func TestNewLocalNoDefault() backend.Backend {
|
2018-03-21 02:43:02 +01:00
|
|
|
return &TestLocalNoDefaultState{Local: &Local{}}
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestLocalNoDefaultState is a backend implementation that wraps
|
|
|
|
// Local and modifies it to support named states, but not the
|
|
|
|
// default state. It returns ErrDefaultStateNotSupported when the
|
|
|
|
// DefaultStateName is used.
|
|
|
|
type TestLocalNoDefaultState struct {
|
|
|
|
*Local
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *TestLocalNoDefaultState) State(name string) (state.State, error) {
|
|
|
|
if name == backend.DefaultStateName {
|
|
|
|
return nil, backend.ErrDefaultStateNotSupported
|
|
|
|
}
|
|
|
|
return b.Local.State(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *TestLocalNoDefaultState) States() ([]string, error) {
|
|
|
|
states, err := b.Local.States()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
filtered := states[:0]
|
|
|
|
for _, name := range states {
|
|
|
|
if name != backend.DefaultStateName {
|
|
|
|
filtered = append(filtered, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filtered, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *TestLocalNoDefaultState) DeleteState(name string) error {
|
|
|
|
if name == backend.DefaultStateName {
|
|
|
|
return backend.ErrDefaultStateNotSupported
|
|
|
|
}
|
|
|
|
return b.Local.DeleteState(name)
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
func testTempDir(t *testing.T) string {
|
|
|
|
d, err := ioutil.TempDir("", "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return d
|
|
|
|
}
|