forked from remind101/empire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
108 lines (89 loc) · 2.72 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package empiretest
import (
"io"
"io/ioutil"
"net/http/httptest"
"os"
"testing"
"text/template"
"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"
"github.com/remind101/empire/server/github"
"github.com/remind101/empire/server/middleware"
)
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)
}
if err := db.MigrateUp(); err != nil {
t.Fatal(err)
}
// Log queries if verbose mode is set.
if testing.Verbose() {
db.Debug()
}
e := empire.New(db, empire.DefaultOptions)
e.Scheduler = scheduler.NewFakeScheduler()
e.ProcfileExtractor = empire.ProcfileExtractorFunc(ExtractProcfile)
e.RunRecorder = empire.RecordTo(ioutil.Discard)
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 {
var opts server.Options
opts.GitHub.Webhooks.Secret = "abcd"
opts.Authenticator = auth.Anyone(&empire.User{Name: "fake"})
opts.GitHub.Deployments.Environments = []string{"test"}
opts.GitHub.Deployments.ImageBuilder = github.ImageFromTemplate(template.Must(template.New("image").Parse(github.DefaultTemplate)))
return NewTestServer(t, e, opts)
}
// NewTestServer returns a new httptest.Server for testing empire's http server.
func NewTestServer(t testing.TB, e *empire.Empire, opts server.Options) *httptest.Server {
if e == nil {
e = NewEmpire(t)
}
s := server.New(e, opts)
return httptest.NewServer(middleware.Handler(context.Background(), s))
}
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) ([]byte, error) {
p, err := procfile.Marshal(procfile.ExtendedProcfile{
"web": procfile.Process{
Command: []string{"./bin/web"},
},
})
if err != nil {
return nil, err
}
return p, dockerutil.FakePull(img, w)
}