-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
139 lines (118 loc) · 2.65 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"flag"
"fmt"
"io"
"os"
"testing"
"time"
"github.com/flynn/flynn/pkg/postgres"
"github.com/jackc/pgx"
)
var db *postgres.DB
func TestMain(m *testing.M) {
flag.Parse()
var err error
db, err = setupTestDb()
if err != nil {
panic(err)
}
defer db.Close()
os.Exit(m.Run())
}
func TestKeepingBackups(t *testing.T) {
// daily
d := time.Now().Add(-3 * 24 * time.Hour)
b := &Backup{StartedAt: &d}
if shouldDeleteBackup(b) {
t.Error("should keep the daily backup")
}
// weekly
d = time.Now().Add(-8 * 24 * time.Hour)
for d.Weekday() != time.Sunday {
d = d.Add(24 * time.Hour)
}
b = &Backup{StartedAt: &d}
if shouldDeleteBackup(b) {
t.Error("should keep the weely backup")
}
// monthly
d, _ = time.Parse(time.RFC822, "01 Jan 15 01:00 UTC")
b = &Backup{StartedAt: &d}
if shouldDeleteBackup(b) {
t.Error("should keep the monthly backup")
}
// other
d, _ = time.Parse(time.RFC822, "02 Jan 14 01:00 UTC")
b = &Backup{StartedAt: &d}
if !shouldDeleteBackup(b) {
t.Error("should delete other backup")
}
}
func setupTestDb() (*postgres.DB, error) {
dbname := "pgbackupstest"
if err := setupDatabase(dbname); err != nil {
return nil, err
}
return getDatabase(dbname)
}
func getDatabase(dbname string) (*postgres.DB, error) {
pgxpool, err := pgx.NewConnPool(pgx.ConnPoolConfig{
ConnConfig: pgx.ConnConfig{
Host: os.Getenv("PGHOST"),
Database: dbname,
User: os.Getenv("PGUSER"),
Password: os.Getenv("PGPASSWORD"),
},
})
if err != nil {
return nil, err
}
db := postgres.New(pgxpool, nil)
return db, nil
}
func setupDatabase(dbname string) error {
if os.Getenv("PGDATABASE") != "" {
dbname = os.Getenv("PGDATABASE")
} else {
os.Setenv("PGDATABASE", dbname)
}
if os.Getenv("PGSSLMODE") == "" {
os.Setenv("PGSSLMODE", "disable")
}
if os.Getenv("PGHOST") == "" {
os.Setenv("PGHOST", "localhost")
}
connConfig := pgx.ConnConfig{
Host: os.Getenv("PGHOST"),
Database: "postgres",
User: os.Getenv("PGUSER"),
Password: os.Getenv("PGPASSWORD"),
}
db, err := pgx.Connect(connConfig)
if err != nil {
return err
}
defer db.Close()
if _, err := db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", dbname)); err != nil {
return err
}
if _, err := db.Exec(fmt.Sprintf("CREATE DATABASE %s", dbname)); err != nil {
return err
}
return nil
}
type dummyStore struct {
}
func newDummyStore() *dummyStore {
return &dummyStore{}
}
func (*dummyStore) DownloadUrl(string, string) (string, error) {
return "", nil
}
func (*dummyStore) Put(appId string, backupId string, r io.Reader) error {
return nil
}
func (*dummyStore) Delete(appId string, backupId string) error {
return nil
}