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