2016-05-10 19:03:58 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
2016-11-22 14:39:00 -06:00
|
|
|
"regexp"
|
2016-05-10 19:03:58 -05:00
|
|
|
"sort"
|
|
|
|
"testing"
|
2016-11-22 14:39:00 -06:00
|
|
|
|
2018-09-30 11:29:51 -05:00
|
|
|
"github.com/hashicorp/terraform/states/statemgr"
|
2016-05-10 19:03:58 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// testStateBackups returns the list of backups in order of creation
|
|
|
|
// (oldest first) in the given directory.
|
|
|
|
func testStateBackups(t *testing.T, dir string) []string {
|
|
|
|
// Find all the backups
|
|
|
|
list, err := filepath.Glob(filepath.Join(dir, "*"+DefaultBackupExtension))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort them which will put them naturally in the right order
|
|
|
|
sort.Strings(list)
|
|
|
|
|
|
|
|
return list
|
|
|
|
}
|
2016-11-22 14:39:00 -06:00
|
|
|
|
|
|
|
func TestStateDefaultBackupExtension(t *testing.T) {
|
2017-02-07 11:11:48 -06:00
|
|
|
tmp, cwd := testCwd(t)
|
|
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
|
2017-07-26 12:08:09 -05:00
|
|
|
s, err := (&StateMeta{}).State()
|
2016-11-22 14:39:00 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-09-30 11:29:51 -05:00
|
|
|
backupPath := s.(*statemgr.Filesystem).BackupPath()
|
2016-11-22 14:39:00 -06:00
|
|
|
match := regexp.MustCompile(`terraform\.tfstate\.\d+\.backup$`).MatchString
|
|
|
|
if !match(backupPath) {
|
|
|
|
t.Fatal("Bad backup path:", backupPath)
|
|
|
|
}
|
|
|
|
}
|