Skip to content

Commit e2d8dbd

Browse files
committed
ref(tests+database) use ImagePrefix for database image, proper error handling in CreateVolume
ref(tests) obey the gofmt-lord
1 parent 617416b commit e2d8dbd

2 files changed

Lines changed: 51 additions & 50 deletions

File tree

database/tests/recovery_test.go

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,68 @@
11
package tests
22

33
import (
4+
"database/sql"
45
"fmt"
5-
"testing"
6-
"time"
76
"github.com/deis/deis/tests/dockercli"
87
"github.com/deis/deis/tests/mock"
98
"github.com/deis/deis/tests/utils"
10-
"database/sql"
11-
"github.com/lib/pq"
9+
"github.com/lib/pq"
10+
"testing"
11+
"time"
1212
)
1313

14-
func OpenDeisDatabase(t *testing.T, host string, port string) (*sql.DB) {
15-
db, err := sql.Open("postgres", "postgres://deis:changeme123@" + host + ":" + port + "/deis?sslmode=disable");
14+
func OpenDeisDatabase(t *testing.T, host string, port string) *sql.DB {
15+
db, err := sql.Open("postgres", "postgres://deis:changeme123@"+host+":"+port+"/deis?sslmode=disable")
1616
if err != nil {
17-
t.Fatal(err)
18-
}
17+
t.Fatal(err)
18+
}
1919
WaitForDatabase(t, db)
2020
return db
2121
}
2222

2323
func WaitForDatabase(t *testing.T, db *sql.DB) {
24-
fmt.Printf("--- Waiting for pg to be ready")
24+
fmt.Printf("--- Waiting for pg to be ready")
2525
for {
26-
_, err := db.Query("select 1")
27-
if err, ok := err.(*pq.Error); ok {
28-
if err.Code.Name() == "cannot_connect_now" {
29-
fmt.Printf(".")
30-
time.Sleep(500 * time.Millisecond)
31-
continue
32-
}
33-
t.Fatal(err)
34-
}
35-
fmt.Printf("\n")
36-
break
26+
_, err := db.Query("select 1")
27+
if err, ok := err.(*pq.Error); ok {
28+
if err.Code.Name() == "cannot_connect_now" {
29+
fmt.Printf(".")
30+
time.Sleep(500 * time.Millisecond)
31+
continue
32+
}
33+
t.Fatal(err)
34+
}
35+
fmt.Printf("\n")
36+
break
3737
}
3838
}
3939

4040
func TryTableSelect(t *testing.T, db *sql.DB, tableName string, expectFailure bool) {
41-
_, err := db.Query("select * from " + tableName)
41+
_, err := db.Query("select * from " + tableName)
4242

4343
if expectFailure {
44-
if err == nil {
45-
t.Fatal("The table should not exist")
46-
}
44+
if err == nil {
45+
t.Fatal("The table should not exist")
46+
}
4747
} else {
48-
if err != nil {
49-
t.Fatal(err)
50-
}
51-
}
48+
if err != nil {
49+
t.Fatal(err)
50+
}
51+
}
5252
}
5353

5454
func execSql(t *testing.T, db *sql.DB, q string) {
5555
_, err := db.Query(q)
5656
if err != nil {
57-
t.Fatal(err)
57+
t.Fatal(err)
5858
}
5959
}
6060

6161
func TestDatabaseRecovery(t *testing.T) {
6262
var err error
6363
tag, etcdPort := utils.BuildTag(), utils.RandomPort()
6464
cli, stdout, _ := dockercli.NewClient()
65+
imageName := utils.ImagePrefix() + "database" + ":" + tag
6566

6667
// start etcd container
6768
etcdName := "deis-etcd-" + tag
@@ -79,14 +80,14 @@ func TestDatabaseRecovery(t *testing.T) {
7980
defer cli.CmdRm("-f", databaseVolumeA)
8081
defer cli.CmdRm("-f", databaseVolumeB)
8182
go func() {
82-
fmt.Printf("--- Creating Volume A\n")
83+
fmt.Printf("--- Creating Volume A\n")
8384
_ = cli.CmdRm("-f", "-v", databaseVolumeA)
84-
dockercli.CreateVolume(cli, databaseVolumeA, "/var/lib/postgresql")
85+
dockercli.CreateVolume(t, cli, databaseVolumeA, "/var/lib/postgresql")
8586

8687
fmt.Printf("--- Creating Volume B\n")
8788

8889
_ = cli.CmdRm("-f", databaseVolumeB)
89-
dockercli.CreateVolume(cli, databaseVolumeB, "/var/lib/postgresql")
90+
dockercli.CreateVolume(t, cli, databaseVolumeB, "/var/lib/postgresql")
9091
}()
9192
dockercli.WaitForLine(t, stdout, databaseVolumeB, true)
9293

@@ -99,7 +100,7 @@ func TestDatabaseRecovery(t *testing.T) {
99100
_ = cli.CmdRm("-f", name)
100101
err = dockercli.RunContainer(cli,
101102
"--name", name,
102-
"--volumes-from", volumeName,
103+
"--volumes-from", volumeName,
103104
"--rm",
104105
"-p", port+":5432",
105106
"-e", "EXTERNAL_PORT="+port,
@@ -108,7 +109,7 @@ func TestDatabaseRecovery(t *testing.T) {
108109
"-e", "ETCD_TTL=2",
109110
"-e", "BACKUP_FREQUENCY=0",
110111
"-e", "BACKUPS_TO_RETAIN=100",
111-
"deis/database:"+tag)
112+
imageName)
112113
}
113114

114115
stopDatabase := func() {
@@ -120,52 +121,48 @@ func TestDatabaseRecovery(t *testing.T) {
120121
fmt.Println("Done")
121122
}
122123

123-
124124
//ACTION
125125

126126
//STEP 1: start db with volume A and wait for init to complete
127-
fmt.Print("--- Starting database with Volume A... ")
127+
fmt.Print("--- Starting database with Volume A... ")
128128
go startDatabase(databaseVolumeA)
129129
dockercli.WaitForLine(t, stdout, "database: postgres is running...", true)
130-
fmt.Println("Done")
130+
fmt.Println("Done")
131131

132132
db := OpenDeisDatabase(t, host, port)
133133
TryTableSelect(t, db, "api_foo", true)
134-
134+
135135
stopDatabase()
136136

137137
//STEP 2a: start db with volume B, wait for init and create the table
138138
cli, stdout, _ = dockercli.NewClient()
139-
fmt.Printf("--- Starting database with Volume B... ")
139+
fmt.Printf("--- Starting database with Volume B... ")
140140
go startDatabase(databaseVolumeB)
141141
dockercli.WaitForLine(t, stdout, "database: postgres is running...", true)
142-
fmt.Println("Done")
142+
fmt.Println("Done")
143143

144-
145144
db = OpenDeisDatabase(t, host, port)
146145
TryTableSelect(t, db, "api_foo", true)
147146

148-
fmt.Println("--- Creating the table")
147+
fmt.Println("--- Creating the table")
149148
execSql(t, db, "create table api_foo(t text)")
150149

151150
//STEP 2b: make sure we observed full backup cycle after forced checkpoint
152-
fmt.Print("--- Waiting for the change to be backed up... ")
151+
fmt.Print("--- Waiting for the change to be backed up... ")
153152
dockercli.WaitForLine(t, stdout, "database: performing a backup...", true)
154153
dockercli.WaitForLine(t, stdout, "database: backup has been completed.", true)
155-
fmt.Println("Done")
154+
fmt.Println("Done")
156155

157156
stopDatabase()
158157

159158
//STEP 3: start db with volume A again and assert table existence
160159
cli, stdout, _ = dockercli.NewClient()
161-
fmt.Printf("--- Starting database with Volume A again... ")
160+
fmt.Printf("--- Starting database with Volume A again... ")
162161
go startDatabase(databaseVolumeA)
163162
dockercli.WaitForLine(t, stdout, "database: postgres is running...", true)
164-
fmt.Println("Done")
163+
fmt.Println("Done")
165164

166165
db = OpenDeisDatabase(t, host, port)
167166
TryTableSelect(t, db, "api_foo", false)
168167

169-
170-
171168
}

tests/dockercli/dockercli.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,15 @@ func NewClient() (
125125
return
126126
}
127127

128-
func CreateVolume(cli *client.DockerCli, name string, path string) {
129-
RunContainer(cli,
128+
func CreateVolume(t *testing.T, cli *client.DockerCli, name string, path string) {
129+
err := RunContainer(cli,
130130
"--name", name,
131131
"-v", path,
132132
"ubuntu-debootstrap:14.04", "/bin/true")
133+
134+
if err != nil {
135+
t.Fatal(err)
136+
}
133137
}
134138

135139
// PrintToStdout prints a string to stdout.

0 commit comments

Comments
 (0)