forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
90 lines (79 loc) · 2.2 KB
/
server.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
// Copyright 2015, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package framework
import (
"fmt"
"net"
"net/http"
"time"
"github.com/youtube/vitess/go/sqldb"
"github.com/youtube/vitess/go/vt/dbconfigs"
"github.com/youtube/vitess/go/vt/mysqlctl"
querypb "github.com/youtube/vitess/go/vt/proto/query"
topodatapb "github.com/youtube/vitess/go/vt/proto/topodata"
"github.com/youtube/vitess/go/vt/tabletserver"
)
var (
// BaseConfig is the base config of the server.
BaseConfig tabletserver.Config
// Target is the target info for the server.
Target querypb.Target
// Server is the TabletServer for the framework.
Server *tabletserver.TabletServer
// ServerAddress is the http URL for the server.
ServerAddress string
)
// StartServer starts the server and initializes
// all the global variables. This function should only be called
// once at the beginning of the test.
func StartServer(connParams sqldb.ConnParams) error {
dbcfgs := dbconfigs.DBConfigs{
App: dbconfigs.DBConfig{
ConnParams: connParams,
Keyspace: "vttest",
Shard: "0",
},
}
mysqld := mysqlctl.NewMysqld(
"Dba",
"App",
&mysqlctl.Mycnf{},
&dbcfgs.Dba,
&dbcfgs.App.ConnParams,
&dbcfgs.Repl)
BaseConfig = tabletserver.DefaultQsConfig
BaseConfig.EnableAutoCommit = true
BaseConfig.StrictTableAcl = true
Target = querypb.Target{
Keyspace: "vttest",
Shard: "0",
TabletType: topodatapb.TabletType_MASTER,
}
Server = tabletserver.NewTabletServer(BaseConfig)
Server.Register()
err := Server.StartService(Target, dbcfgs, mysqld)
if err != nil {
return fmt.Errorf("could not start service: %v\n", err)
}
// Start http service.
ln, err := net.Listen("tcp", ":0")
if err != nil {
return fmt.Errorf("could not start listener: %v\n", err)
}
ServerAddress = fmt.Sprintf("http://%s", ln.Addr().String())
go http.Serve(ln, nil)
for {
time.Sleep(10 * time.Millisecond)
response, err := http.Get(fmt.Sprintf("%s/debug/vars", ServerAddress))
if err == nil {
response.Body.Close()
break
}
}
return nil
}
// StopServer must be called once all the tests are done.
func StopServer() {
Server.StopService()
}