github.com/hartzell/terraform@v0.8.6-0.20180503104400-0cc9e050ecd4/backend/local/testing.go (about)

     1  package local
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/backend"
    10  	"github.com/hashicorp/terraform/state"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  // TestLocal returns a configured Local struct with temporary paths and
    15  // in-memory ContextOpts.
    16  //
    17  // No operations will be called on the returned value, so you can still set
    18  // public fields without any locks.
    19  func TestLocal(t *testing.T) (*Local, func()) {
    20  	tempDir := testTempDir(t)
    21  	local := &Local{
    22  		StatePath:         filepath.Join(tempDir, "state.tfstate"),
    23  		StateOutPath:      filepath.Join(tempDir, "state.tfstate"),
    24  		StateBackupPath:   filepath.Join(tempDir, "state.tfstate.bak"),
    25  		StateWorkspaceDir: filepath.Join(tempDir, "state.tfstate.d"),
    26  		ContextOpts:       &terraform.ContextOpts{},
    27  	}
    28  	cleanup := func() {
    29  		if err := os.RemoveAll(tempDir); err != nil {
    30  			t.Fatal("error clecanup up test:", err)
    31  		}
    32  	}
    33  
    34  	return local, cleanup
    35  }
    36  
    37  // TestLocalProvider modifies the ContextOpts of the *Local parameter to
    38  // have a provider with the given name.
    39  func TestLocalProvider(t *testing.T, b *Local, name string) *terraform.MockResourceProvider {
    40  	// Build a mock resource provider for in-memory operations
    41  	p := new(terraform.MockResourceProvider)
    42  	p.DiffReturn = &terraform.InstanceDiff{}
    43  	p.RefreshFn = func(
    44  		info *terraform.InstanceInfo,
    45  		s *terraform.InstanceState) (*terraform.InstanceState, error) {
    46  		return s, nil
    47  	}
    48  	p.ResourcesReturn = []terraform.ResourceType{
    49  		terraform.ResourceType{
    50  			Name: "test_instance",
    51  		},
    52  	}
    53  
    54  	// Initialize the opts
    55  	if b.ContextOpts == nil {
    56  		b.ContextOpts = &terraform.ContextOpts{}
    57  	}
    58  
    59  	// Setup our provider
    60  	b.ContextOpts.ProviderResolver = terraform.ResourceProviderResolverFixed(
    61  		map[string]terraform.ResourceProviderFactory{
    62  			name: terraform.ResourceProviderFactoryFixed(p),
    63  		},
    64  	)
    65  
    66  	return p
    67  }
    68  
    69  // TestNewLocalSingle is a factory for creating a TestLocalSingleState.
    70  // This function matches the signature required for backend/init.
    71  func TestNewLocalSingle() backend.Backend {
    72  	return &TestLocalSingleState{}
    73  }
    74  
    75  // TestLocalSingleState is a backend implementation that wraps Local
    76  // and modifies it to only support single states (returns
    77  // ErrNamedStatesNotSupported for multi-state operations).
    78  //
    79  // This isn't an actual use case, this is exported just to provide a
    80  // easy way to test that behavior.
    81  type TestLocalSingleState struct {
    82  	Local
    83  }
    84  
    85  func (b *TestLocalSingleState) State(name string) (state.State, error) {
    86  	if name != backend.DefaultStateName {
    87  		return nil, backend.ErrNamedStatesNotSupported
    88  	}
    89  
    90  	return b.Local.State(name)
    91  }
    92  
    93  func (b *TestLocalSingleState) States() ([]string, error) {
    94  	return nil, backend.ErrNamedStatesNotSupported
    95  }
    96  
    97  func (b *TestLocalSingleState) DeleteState(string) error {
    98  	return backend.ErrNamedStatesNotSupported
    99  }
   100  
   101  func testTempDir(t *testing.T) string {
   102  	d, err := ioutil.TempDir("", "tf")
   103  	if err != nil {
   104  		t.Fatalf("err: %s", err)
   105  	}
   106  
   107  	return d
   108  }