github.com/paybyphone/terraform@v0.9.5-0.20170613192930-9706042ddd51/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 StateWorkspaceDir: 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 51 // Setup our provider 52 b.ContextOpts.ProviderResolver = terraform.ResourceProviderResolverFixed( 53 map[string]terraform.ResourceProviderFactory{ 54 name: terraform.ResourceProviderFactoryFixed(p), 55 }, 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 }