/
config.go
111 lines (97 loc) · 2.84 KB
/
config.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
// Copyright 2018 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package integration
import (
"fmt"
"io/ioutil"
"strings"
"github.com/pingcap/pd/pkg/tempurl"
"github.com/pingcap/pd/server"
)
// ConfigOption is used to define customize settings in test.
type ConfigOption func(conf *server.Config)
type serverConfig struct {
Name string
DataDir string
ClientURLs string
AdvertiseClientURLs string
PeerURLs string
AdvertisePeerURLs string
ClusterConfig *clusterConfig
Join bool
}
func newServerConfig(name string, cc *clusterConfig, join bool) *serverConfig {
tempDir, _ := ioutil.TempDir("/tmp", "pd-integration-test")
return &serverConfig{
Name: name,
DataDir: tempDir,
ClientURLs: tempurl.Alloc(),
PeerURLs: tempurl.Alloc(),
ClusterConfig: cc,
Join: join,
}
}
func (c *serverConfig) Generate(opts ...ConfigOption) (*server.Config, error) {
arguments := []string{
"--name=" + c.Name,
"--data-dir=" + c.DataDir,
"--client-urls=" + c.ClientURLs,
"--advertise-client-urls=" + c.AdvertiseClientURLs,
"--peer-urls=" + c.PeerURLs,
"--advertise-peer-urls=" + c.AdvertisePeerURLs,
}
if c.Join {
arguments = append(arguments, "--join="+c.ClusterConfig.GetJoinAddr())
} else {
arguments = append(arguments, "--initial-cluster="+c.ClusterConfig.GetServerAddrs())
}
cfg := server.NewConfig()
err := cfg.Parse(arguments)
if err != nil {
return nil, err
}
for _, opt := range opts {
opt(cfg)
}
return cfg, nil
}
type clusterConfig struct {
InitialServers []*serverConfig
JoinServers []*serverConfig
}
func newClusterConfig(n int) *clusterConfig {
var cc clusterConfig
for i := 0; i < n; i++ {
c := newServerConfig(cc.nextServerName(), &cc, false)
cc.InitialServers = append(cc.InitialServers, c)
}
return &cc
}
func (c *clusterConfig) Join() *serverConfig {
sc := newServerConfig(c.nextServerName(), c, true)
c.JoinServers = append(c.JoinServers, sc)
return sc
}
func (c *clusterConfig) nextServerName() string {
return fmt.Sprintf("pd%d", len(c.InitialServers)+len(c.JoinServers)+1)
}
func (c *clusterConfig) GetServerAddrs() string {
var addrs []string
for _, s := range c.InitialServers {
addrs = append(addrs, fmt.Sprintf("%s=%s", s.Name, s.PeerURLs))
}
return strings.Join(addrs, ",")
}
func (c *clusterConfig) GetJoinAddr() string {
return c.InitialServers[0].PeerURLs
}