-
Notifications
You must be signed in to change notification settings - Fork 496
/
t.go
167 lines (138 loc) · 4.21 KB
/
t.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//lint:file-ignore U1001 Ignore all unused code, thinks the code is unused because of the test skips
package test
import (
"io"
"testing"
"encoding/json"
"github.com/jmoiron/sqlx"
"github.com/sirupsen/logrus"
"github.com/stellar/go/services/horizon/internal/db2/schema"
"github.com/stellar/go/services/horizon/internal/ledger"
"github.com/stellar/go/services/horizon/internal/operationfeestats"
tdb "github.com/stellar/go/services/horizon/internal/test/db"
"github.com/stellar/go/services/horizon/internal/test/scenarios"
"github.com/stellar/go/support/db"
"github.com/stellar/go/support/render/hal"
)
// TODO - remove ref to core db once scenario tests are removed.
func (t *T) CoreSession() *db.Session {
return &db.Session{
DB: t.CoreDB,
}
}
// Finish finishes the test, logging any accumulated horizon logs to the logs
// output
func (t *T) Finish() {
logEntries := t.testLogs()
operationfeestats.ResetState()
for _, entry := range logEntries {
logString, err := entry.String()
if err != nil {
t.T.Logf("Error from entry.String: %v", err)
} else {
t.T.Log(logString)
}
}
}
// HorizonSession returns a db.Session instance pointing at the horizon test
// database
func (t *T) HorizonSession() *db.Session {
return &db.Session{
DB: t.HorizonDB,
}
}
func (t *T) loadScenario(scenarioName string, includeHorizon bool) {
stellarCorePath := scenarioName + "-core.sql"
scenarios.Load(tdb.StellarCoreURL(), stellarCorePath)
if includeHorizon {
horizonPath := scenarioName + "-horizon.sql"
scenarios.Load(tdb.HorizonURL(), horizonPath)
}
}
// Scenario loads the named sql scenario into the database
func (t *T) Scenario(name string) ledger.Status {
clearHorizonDB(t.T, t.HorizonDB)
t.loadScenario(name, true)
return t.LoadLedgerStatus()
}
// ScenarioWithoutHorizon loads the named sql scenario into the database
func (t *T) ScenarioWithoutHorizon(name string) ledger.Status {
t.loadScenario(name, false)
ResetHorizonDB(t.T, t.HorizonDB)
return t.LoadLedgerStatus()
}
// ResetHorizonDB sets up a new horizon database with empty tables
func ResetHorizonDB(t *testing.T, db *sqlx.DB) {
clearHorizonDB(t, db)
_, err := schema.Migrate(db.DB, schema.MigrateUp, 0)
if err != nil {
t.Fatalf("could not run migrations up on test db: %v", err)
}
}
func clearHorizonDB(t *testing.T, db *sqlx.DB) {
_, err := schema.Migrate(db.DB, schema.MigrateDown, 0)
if err != nil {
t.Fatalf("could not run migrations down on test db: %v", err)
}
}
// UnmarshalPage populates dest with the records contained in the json-encoded page in r
func (t *T) UnmarshalPage(r io.Reader, dest interface{}) hal.Links {
var env struct {
Embedded struct {
Records json.RawMessage `json:"records"`
} `json:"_embedded"`
Links struct {
Self hal.Link `json:"self"`
Next hal.Link `json:"next"`
Prev hal.Link `json:"prev"`
} `json:"_links"`
}
err := json.NewDecoder(r).Decode(&env)
t.Require.NoError(err, "failed to decode page")
err = json.Unmarshal(env.Embedded.Records, dest)
t.Require.NoError(err, "failed to decode records")
return env.Links
}
// UnmarshalNext extracts and returns the next link
func (t *T) UnmarshalNext(r io.Reader) string {
var env struct {
Links struct {
Next struct {
Href string `json:"href"`
} `json:"next"`
} `json:"_links"`
}
err := json.NewDecoder(r).Decode(&env)
t.Require.NoError(err, "failed to decode page")
return env.Links.Next.Href
}
// UnmarshalExtras extracts and returns extras content
func (t *T) UnmarshalExtras(r io.Reader) map[string]string {
var resp struct {
Extras map[string]string `json:"extras"`
}
err := json.NewDecoder(r).Decode(&resp)
t.Require.NoError(err, "failed to decode page")
return resp.Extras
}
// LoadLedgerStatus loads ledger state from the core db(or panicing on failure).
func (t *T) LoadLedgerStatus() ledger.Status {
var next ledger.Status
err := t.HorizonSession().GetRaw(t.Ctx, &next, `
SELECT
COALESCE(MIN(sequence), 0) as history_elder,
COALESCE(MAX(sequence), 0) as history_latest
FROM history_ledgers
`)
if err != nil {
panic(err)
}
return next
}
// retrieves entries from test logger instance
func (t *T) testLogs() []logrus.Entry {
if t.EndLogTest == nil {
return []logrus.Entry{}
}
return t.EndLogTest()
}