github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/command/state_test.go (about)

     1  package command
     2  
     3  import (
     4  	"path/filepath"
     5  	"regexp"
     6  	"sort"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/state"
    10  )
    11  
    12  // testStateBackups returns the list of backups in order of creation
    13  // (oldest first) in the given directory.
    14  func testStateBackups(t *testing.T, dir string) []string {
    15  	// Find all the backups
    16  	list, err := filepath.Glob(filepath.Join(dir, "*"+DefaultBackupExtension))
    17  	if err != nil {
    18  		t.Fatalf("err: %s", err)
    19  	}
    20  
    21  	// Sort them which will put them naturally in the right order
    22  	sort.Strings(list)
    23  
    24  	return list
    25  }
    26  
    27  func TestStateDefaultBackupExtension(t *testing.T) {
    28  	tmp, cwd := testCwd(t)
    29  	defer testFixCwd(t, tmp, cwd)
    30  
    31  	s, err := (&StateMeta{}).State()
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  
    36  	backupPath := s.(*state.BackupState).Path
    37  	match := regexp.MustCompile(`terraform\.tfstate\.\d+\.backup$`).MatchString
    38  	if !match(backupPath) {
    39  		t.Fatal("Bad backup path:", backupPath)
    40  	}
    41  }