forked from romshark/dgraph_graphql_go
/
setup.go
117 lines (100 loc) · 2.81 KB
/
setup.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
package setup
import (
"context"
ctx "context"
"testing"
"time"
"github.com/dgraph-io/dgo"
dbapi "github.com/dgraph-io/dgo/protos/api"
"github.com/romshark/dgraph_graphql_go/api"
"github.com/romshark/dgraph_graphql_go/api/config"
trn "github.com/romshark/dgraph_graphql_go/api/transport"
thttp "github.com/romshark/dgraph_graphql_go/api/transport/http"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
)
// TestContext represents a test context
type TestContext struct {
Stats *StatisticsRecorder
DBHost string
SrvHost string
}
// TestSetup represents the Dgraph-based server setup of an individual test
type TestSetup struct {
t *testing.T
stats *StatisticsRecorder
apiServer api.Server
serverTransport trn.Server
debugUsername string
debugPassword string
}
// T returns the test reference
func (ts *TestSetup) T() *testing.T {
return ts.t
}
// New creates a new test setup
func New(t *testing.T, context TestContext) *TestSetup {
start := time.Now()
debugUsername := "test"
debugPassword := "test"
// Clear database
conn, err := grpc.Dial(context.DBHost, grpc.WithInsecure())
require.NoError(t, err)
db := dgo.NewDgraphClient(dbapi.NewDgraphClient(conn))
require.NoError(t, db.Alter(
ctx.Background(),
&dbapi.Operation{DropAll: true},
))
require.NoError(t, conn.Close())
serverTransport, err := thttp.NewServer(thttp.ServerConfig{
Host: context.SrvHost,
Playground: false,
})
require.NoError(t, err)
serverConfig := &config.ServerConfig{
Mode: config.ModeDebug,
DBHost: context.DBHost,
DebugUser: config.DebugUserConfig{
// Enable the debug user in read-write mode
Mode: config.DebugUserRW,
Username: debugUsername,
Password: debugPassword,
},
Shield: config.ShieldConfig{
WhitelistEnabled: false,
},
Transport: []trn.Server{
serverTransport,
},
}
apiServer, err := api.NewServer(serverConfig)
require.NoError(t, err)
require.NoError(t, apiServer.Launch())
testSetup := &TestSetup{
t: t,
stats: context.Stats,
apiServer: apiServer,
serverTransport: serverTransport,
debugUsername: debugUsername,
debugPassword: debugPassword,
}
// Record setup time
context.Stats.Set(t, func(stat *TestStatistics) {
stat.SetupTime = time.Since(start)
})
return testSetup
}
// Teardown gracefully terminates the test,
// this method MUST BE DEFERRED until the end of the test!
func (ts *TestSetup) Teardown() {
start := time.Now()
// Stop the API server instance
if err := ts.apiServer.Shutdown(context.Background()); err != nil {
// Don't break on shutdown failure, remove database before quitting!
ts.t.Errorf("API server shutdown: %s", err)
}
// Record teardown time
ts.stats.Set(ts.t, func(stat *TestStatistics) {
stat.TeardownTime = time.Since(start)
})
}