github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/state/local_test.go (about)

     1  package state
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func TestLocalState(t *testing.T) {
    12  	ls := testLocalState(t)
    13  	defer os.Remove(ls.Path)
    14  	TestState(t, ls)
    15  }
    16  
    17  func TestLocalState_pathOut(t *testing.T) {
    18  	f, err := ioutil.TempFile("", "tf")
    19  	if err != nil {
    20  		t.Fatalf("err: %s", err)
    21  	}
    22  	f.Close()
    23  	defer os.Remove(f.Name())
    24  
    25  	ls := testLocalState(t)
    26  	ls.PathOut = f.Name()
    27  	defer os.Remove(ls.Path)
    28  
    29  	TestState(t, ls)
    30  }
    31  
    32  func TestLocalState_nonExist(t *testing.T) {
    33  	ls := &LocalState{Path: "ishouldntexist"}
    34  	if err := ls.RefreshState(); err != nil {
    35  		t.Fatalf("err: %s", err)
    36  	}
    37  
    38  	if state := ls.State(); state != nil {
    39  		t.Fatalf("bad: %#v", state)
    40  	}
    41  }
    42  
    43  func TestLocalState_impl(t *testing.T) {
    44  	var _ StateReader = new(LocalState)
    45  	var _ StateWriter = new(LocalState)
    46  	var _ StatePersister = new(LocalState)
    47  	var _ StateRefresher = new(LocalState)
    48  }
    49  
    50  func testLocalState(t *testing.T) *LocalState {
    51  	f, err := ioutil.TempFile("", "tf")
    52  	if err != nil {
    53  		t.Fatalf("err: %s", err)
    54  	}
    55  
    56  	err = terraform.WriteState(TestStateInitial(), f)
    57  	f.Close()
    58  	if err != nil {
    59  		t.Fatalf("err: %s", err)
    60  	}
    61  
    62  	ls := &LocalState{Path: f.Name()}
    63  	if err := ls.RefreshState(); err != nil {
    64  		t.Fatalf("bad: %s", err)
    65  	}
    66  
    67  	return ls
    68  }