github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/db/db_local/backup_test.go (about) 1 package db_local 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "testing" 8 "time" 9 10 filehelpers "github.com/turbot/go-kit/files" 11 12 "github.com/turbot/steampipe/pkg/constants" 13 "github.com/turbot/steampipe/pkg/filepaths" 14 ) 15 16 func TestTrimBackups(t *testing.T) { 17 filepaths.SteampipeDir, _ = filehelpers.Tildefy("~/.steampipe") 18 // create backups more than MaxBackups 19 backupDir := filepaths.EnsureBackupsDir() 20 filesCreated := []string{} 21 for i := 0; i < constants.MaxBackups; i++ { 22 // make sure the files that get created end up to really old 23 // this way we won't end up deleting any actual backup files 24 timeLastYear := time.Now().Add(12 * 30 * 24 * time.Hour) 25 26 fileName := fmt.Sprintf("database-%s-%2d", timeLastYear.Format("2006-01-02-15-04"), i) 27 createFile := filepath.Join(backupDir, fileName) 28 if err := os.WriteFile(filepath.Join(backupDir, fileName), []byte(""), 0644); err != nil { 29 filesCreated = append(filesCreated, createFile) 30 } 31 } 32 33 trimBackups() 34 35 for _, f := range filesCreated { 36 if filehelpers.FileExists(f) { 37 t.Errorf("did not remove test backup file: %s", f) 38 } 39 } 40 41 }