forked from stellar-deprecated/horizon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
130 lines (102 loc) · 3.31 KB
/
main.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
// Package test contains simple test helpers that should not
// have any dependencies on horizon's packages. think constants,
// custom matchers, generic helpers etc.
package test
import (
"bytes"
"fmt"
"log"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"github.com/Sirupsen/logrus"
"github.com/jmoiron/sqlx"
glog "github.com/stellar/horizon/log"
"golang.org/x/net/context"
)
//go:generate go get github.com/jteeuwen/go-bindata/go-bindata
//go:generate go-bindata -pkg test scenarios
const (
DefaultTestDatabaseUrl = "postgres://localhost:5432/horizon_test?sslmode=disable"
DefaultTestStellarCoreDatabaseUrl = "postgres://localhost:5432/stellar-core_test?sslmode=disable"
)
// DatabaseUrl returns the database connection the url any test
// use when connecting to the history/horizon database
func DatabaseUrl() string {
databaseURL := os.Getenv("DATABASE_URL")
if databaseURL == "" {
databaseURL = DefaultTestDatabaseUrl
}
return databaseURL
}
// StellarCoreDatabaseUrl returns the database connection the url any test
// use when connecting to the stellar-core database
func StellarCoreDatabaseUrl() string {
databaseURL := os.Getenv("STELLAR_CORE_DATABASE_URL")
if databaseURL == "" {
databaseURL = DefaultTestStellarCoreDatabaseUrl
}
return databaseURL
}
// OpenDatabase opens a database, panicing if it cannot
func OpenDatabase(dsn string) *sqlx.DB {
db, err := sqlx.Open("postgres", dsn)
if err != nil {
log.Panic(err)
}
return db
}
// LoadScenario populates the test databases with pre-created scenarios. Each
// scenario is in the scenarios subfolder of this package and are a pair of
// sql files, one per database.
func LoadScenario(scenarioName string) {
scenarioBasePath := "scenarios/" + scenarioName
horizonPath := scenarioBasePath + "-horizon.sql"
stellarCorePath := scenarioBasePath + "-core.sql"
loadSqlFile(DatabaseUrl(), horizonPath)
loadSqlFile(StellarCoreDatabaseUrl(), stellarCorePath)
}
func loadSqlFile(url string, path string) {
sql, err := Asset(path)
if err != nil {
log.Panic(err)
}
reader := bytes.NewReader(sql)
cmd := exec.Command("psql", url)
cmd.Stdin = reader
err = cmd.Run()
if err != nil {
log.Panic(err)
}
}
// Context provides a context suitable for testing in tests that do not create
// a full App instance (in which case your tests should be using the app's
// context). This context has a logger bound to it suitable for testing.
func Context() context.Context {
return glog.Set(context.Background(), testLogger)
}
// ContextWithLogBuffer returns a context and a buffer into which the new, bound
// logger will write into. This method allows you to inspect what data was
// logged more easily in your tests.
func ContextWithLogBuffer() (context.Context, *bytes.Buffer) {
output := new(bytes.Buffer)
l, _ := glog.New()
l.Logger.Out = output
l.Logger.Formatter.(*logrus.TextFormatter).DisableColors = true
l.Logger.Level = logrus.DebugLevel
ctx := glog.Set(context.Background(), l)
return ctx, output
}
type StaticMockServer struct {
*httptest.Server
LastRequest *http.Request
}
func NewStaticMockServer(response string) *StaticMockServer {
result := &StaticMockServer{}
result.Server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
result.LastRequest = r
fmt.Fprintln(w, response)
}))
return result
}