github.com/jameswoolfenden/terraform@v0.11.12-beta1/state/backup_test.go (about)

     1  package state
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"sync"
     7  	"testing"
     8  )
     9  
    10  func TestBackupState_locker(t *testing.T) {
    11  	var _ Locker = new(BackupState)
    12  }
    13  
    14  func TestBackupState(t *testing.T) {
    15  	f, err := ioutil.TempFile("", "tf")
    16  	if err != nil {
    17  		t.Fatalf("err: %s", err)
    18  	}
    19  	f.Close()
    20  	defer os.Remove(f.Name())
    21  
    22  	ls := testLocalState(t)
    23  	defer os.Remove(ls.Path)
    24  	TestState(t, &BackupState{
    25  		Real: ls,
    26  		Path: f.Name(),
    27  	})
    28  
    29  	if fi, err := os.Stat(f.Name()); err != nil {
    30  		t.Fatalf("err: %s", err)
    31  	} else if fi.Size() == 0 {
    32  		t.Fatalf("bad: %d", fi.Size())
    33  	}
    34  }
    35  
    36  func TestBackupStateRace(t *testing.T) {
    37  	f, err := ioutil.TempFile("", "tf")
    38  	if err != nil {
    39  		t.Fatalf("err: %s", err)
    40  	}
    41  	f.Close()
    42  	defer os.Remove(f.Name())
    43  
    44  	ls := testLocalState(t)
    45  	defer os.Remove(ls.Path)
    46  	bs := &BackupState{
    47  		Real: ls,
    48  		Path: f.Name(),
    49  	}
    50  
    51  	current := TestStateInitial()
    52  
    53  	var wg sync.WaitGroup
    54  	for i := 0; i < 100; i++ {
    55  		wg.Add(1)
    56  		go func() {
    57  			defer wg.Done()
    58  			bs.WriteState(current)
    59  			bs.PersistState()
    60  			bs.RefreshState()
    61  		}()
    62  	}
    63  
    64  	wg.Wait()
    65  }