This repository has been archived by the owner on Sep 2, 2020. It is now read-only.
/
testdb.go
111 lines (98 loc) · 2.42 KB
/
testdb.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
package storage
import (
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
)
type testDB struct {
*MongoDB
}
type testDBError struct {
err string
cmd string
}
var (
TestDB *testDB // singleton
)
func newTestDBError(err string, cmd string) testDBError {
return testDBError{err, cmd}
}
func (e testDBError) Error() string {
if len(e.cmd) > 0 {
return "Test database: " + e.err + " - Command output:\n" + e.cmd
} else {
return "Test database: " + e.err
}
}
// Using a MongoDB backend, export a testing database (done so we don't have
// to export the testDB struct).
func NewTestDBMongo(m *MongoDB) *testDB {
return &testDB{m}
}
func (db *testDB) StartMongoTesting() (err error) {
log.Println("starting testing mongo")
// TODO if database is running, restart it
// check for supervisor, if it doesn't exist, we can't do testing
if !supervisorExists() {
return newTestDBError("could not find the supervisor daemon", "")
}
// start supervisor/mongo with the script
err = run("cd ../storage/mongo_test && ./run.sh start")
if err != nil {
return newTestDBError("could not start the test database", err.(testDBError).cmd)
}
// Open connection
err = TestDB.OpenConnection()
if err != nil {
return newTestDBError("could not open the database connection: "+err.Error(), "")
}
return nil
}
func (db *testDB) Reset() (err error) {
log.Println("reset testing mongo")
s := db.CloneSession()
dbs, err := s.DatabaseNames()
if err != nil {
return newTestDBError(err.Error(), "")
}
for i := range dbs {
s.DB(dbs[i]).DropDatabase()
}
return nil
}
func (db *testDB) StopMongoTesting() (err error) {
log.Println("stopping testing mongo")
// stop supervisor/mongo
err = run("cd ../storage/mongo_test && ./run.sh stop")
if err != nil {
return newTestDBError("failed to stop the testing database", err.(testDBError).cmd)
}
return nil
}
func supervisorExists() bool {
env := os.Getenv("PATH")
if len(env) == 0 {
return false
}
for _, p := range filepath.SplitList(env) {
fn := p + "/supervisord"
if _, err := os.Stat(fn); err == nil {
return true
}
}
return false
}
func run(command string) (err error) {
var output []byte
if runtime.GOOS == "windows" {
output, err = exec.Command("cmd", "/C", command).CombinedOutput()
} else {
output, err = exec.Command("/bin/sh", "-c", command).CombinedOutput()
}
if err != nil {
return newTestDBError("failed to execute command "+command, string(output))
}
return nil
}