github.com/cbroglie/terraform@v0.7.0-rc3.0.20170410193827-735dfc416d46/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 StateEnvDir: filepath.Join(tempDir, "state.tfstate.d"), 25 ContextOpts: &terraform.ContextOpts{}, 26 } 27 } 28 29 // TestLocalProvider modifies the ContextOpts of the *Local parameter to 30 // have a provider with the given name. 31 func TestLocalProvider(t *testing.T, b *Local, name string) *terraform.MockResourceProvider { 32 // Build a mock resource provider for in-memory operations 33 p := new(terraform.MockResourceProvider) 34 p.DiffReturn = &terraform.InstanceDiff{} 35 p.RefreshFn = func( 36 info *terraform.InstanceInfo, 37 s *terraform.InstanceState) (*terraform.InstanceState, error) { 38 return s, nil 39 } 40 p.ResourcesReturn = []terraform.ResourceType{ 41 terraform.ResourceType{ 42 Name: "test_instance", 43 }, 44 } 45 46 // Initialize the opts 47 if b.ContextOpts == nil { 48 b.ContextOpts = &terraform.ContextOpts{} 49 } 50 if b.ContextOpts.Providers == nil { 51 b.ContextOpts.Providers = make(map[string]terraform.ResourceProviderFactory) 52 } 53 54 // Setup our provider 55 b.ContextOpts.Providers[name] = func() (terraform.ResourceProvider, error) { 56 return p, nil 57 } 58 59 return p 60 } 61 62 // TestNewLocalSingle is a factory for creating a TestLocalSingleState. 63 // This function matches the signature required for backend/init. 64 func TestNewLocalSingle() backend.Backend { 65 return &TestLocalSingleState{} 66 } 67 68 // TestLocalSingleState is a backend implementation that wraps Local 69 // and modifies it to only support single states (returns 70 // ErrNamedStatesNotSupported for multi-state operations). 71 // 72 // This isn't an actual use case, this is exported just to provide a 73 // easy way to test that behavior. 74 type TestLocalSingleState struct { 75 Local 76 } 77 78 func (b *TestLocalSingleState) State(name string) (state.State, error) { 79 if name != backend.DefaultStateName { 80 return nil, backend.ErrNamedStatesNotSupported 81 } 82 83 return b.Local.State(name) 84 } 85 86 func (b *TestLocalSingleState) States() ([]string, error) { 87 return nil, backend.ErrNamedStatesNotSupported 88 } 89 90 func (b *TestLocalSingleState) DeleteState(string) error { 91 return backend.ErrNamedStatesNotSupported 92 } 93 94 func testTempDir(t *testing.T) string { 95 d, err := ioutil.TempDir("", "tf") 96 if err != nil { 97 t.Fatalf("err: %s", err) 98 } 99 100 return d 101 }