forked from remind101/empire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
86 lines (69 loc) · 2.08 KB
/
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
package empiretest
import (
"io"
"net/http/httptest"
"os"
"testing"
"golang.org/x/net/context"
"github.com/ejholmes/flock"
"github.com/remind101/empire"
"github.com/remind101/empire/pkg/dockerutil"
"github.com/remind101/empire/pkg/image"
"github.com/remind101/empire/procfile"
"github.com/remind101/empire/scheduler"
"github.com/remind101/empire/server"
"github.com/remind101/empire/server/auth"
)
var (
// DatabaseURL is a connection string for the postgres database to use
// during integration tests.
DatabaseURL = "postgres://localhost/empire?sslmode=disable"
)
// NewEmpire returns a new Empire instance suitable for testing. It ensures that
// the database is clean before returning.
func NewEmpire(t testing.TB) *empire.Empire {
db, err := empire.OpenDB(DatabaseURL)
if err != nil {
t.Fatal(err)
}
// TODO: Run db.MigrateUp once migrations are in the binary.
// Log queries if verbose mode is set.
if testing.Verbose() {
db.Debug()
}
e := empire.New(db, empire.DefaultOptions)
e.Scheduler = scheduler.NewFakeScheduler()
e.ExtractProcfile = ExtractProcfile
if err := e.Reset(); err != nil {
t.Fatal(err)
}
return e
}
// NewServer builds a new empire.Empire instance and returns an httptest.Server
// running the empire API.
func NewServer(t testing.TB, e *empire.Empire) *httptest.Server {
if e == nil {
e = NewEmpire(t)
}
server.DefaultOptions.GitHub.Webhooks.Secret = "abcd"
server.DefaultOptions.Authenticator = auth.Anyone(&empire.User{Name: "fake"})
server.DefaultOptions.GitHub.Deployments.Environments = []string{"test"}
return httptest.NewServer(server.New(e, server.DefaultOptions))
}
var dblock = "/tmp/empire.lock"
// Run runs testing.M after aquiring a lock against the database.
func Run(m *testing.M) {
l, err := flock.NewPath(dblock)
if err != nil {
panic(err)
}
l.Lock()
defer l.Unlock()
os.Exit(m.Run())
}
// ExtractProcfile extracts a fake procfile.
func ExtractProcfile(ctx context.Context, img image.Image, w io.Writer) (procfile.Procfile, error) {
return procfile.Procfile{
"web": "./bin/web",
}, dockerutil.FakePull(img, w)
}