Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

playground: fix bug which can't specify db port #1511

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/playground/instance/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewTiDBInstance(binPath string, dir, host, configPath string, id int, pds [
ID: id,
Dir: dir,
Host: host,
Port: utils.MustGetFreePort(host, 4000),
Port: utils.MustGetFreePort(host, GetTiDBPort(configPath)),
StatusPort: utils.MustGetFreePort("0.0.0.0", 10080),
ConfigPath: configPath,
},
Expand Down
19 changes: 19 additions & 0 deletions components/playground/instance/tidb_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package instance

import (
"github.com/BurntSushi/toml"
"github.com/pingcap/tidb/config"
)

// GetTiDBPort custom db port for playground using config file
func GetTiDBPort(configPath string) int {
// default port
port := 4000
// read from config file if possible
config := &config.Config{}
_, err := toml.DecodeFile(configPath, config)
if err == nil && config.Port > 0 {
port = int(config.Port)
}
return port
}
33 changes: 33 additions & 0 deletions components/playground/instance/tidb_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package instance

import (
"bytes"
"os"
"testing"

"github.com/BurntSushi/toml"
"github.com/pingcap/tidb/config"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are avoiding dependency upon tidb and other components by intend, please use general interface objects (map[string]interface{} with type assertion) to decode the config.

Or simply provide a command line argument in playground to specify TiDB port and overwrite the default.

"github.com/stretchr/testify/assert"
)

func TestGetTiDBPort(t *testing.T) {
defaultPort := 4000
assert.Equal(t, GetTiDBPort(""), defaultPort)

customPort := 30000
inputs := config.Config{
Port: uint(customPort),
}
var firstBuffer bytes.Buffer
e := toml.NewEncoder(&firstBuffer)
err := e.Encode(inputs)
assert.Nil(t, err)
// create temp config file
tmp, err := os.CreateTemp(os.TempDir(), "tmp-")
assert.Nil(t, err)
defer tmp.Close()
_, err = tmp.Write(firstBuffer.Bytes())
assert.Nil(t, err)

assert.Equal(t, GetTiDBPort(tmp.Name()), customPort)
}
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ replace (
require (
github.com/AstroProfundis/sysinfo v0.0.0-20210610033012-3aad056e509d
github.com/AstroProfundis/tabby v1.1.1-color
github.com/BurntSushi/toml v0.3.1
github.com/BurntSushi/toml v0.4.1
github.com/HdrHistogram/hdrhistogram-go v1.1.0 // indirect
github.com/ScaleFT/sshkeys v0.0.0-20200327173127-6142f742bca5
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 // indirect
github.com/VividCortex/ewma v1.2.0 // indirect
Expand All @@ -35,6 +36,7 @@ require (
github.com/jeremywohl/flatten v1.0.1
github.com/joomcode/errorx v1.0.3
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a
github.com/juju/testing v0.0.0-20210324180055-18c50b0c2098 // indirect
github.com/lunixbochs/vtclean v1.0.0 // indirect
github.com/mattn/go-isatty v0.0.13 // indirect
github.com/mattn/go-runewidth v0.0.13
Expand All @@ -44,6 +46,7 @@ require (
github.com/pingcap/failpoint v0.0.0-20210316064728-7acb0f0a3dfd
github.com/pingcap/fn v0.0.0-20200306044125-d5540d389059
github.com/pingcap/kvproto v0.0.0-20210622031542-706fcaf286c8
github.com/pingcap/tidb v2.0.11+incompatible
github.com/pingcap/tidb-insight v0.3.2
github.com/prometheus/client_model v0.2.0
github.com/prometheus/common v0.29.0
Expand All @@ -58,6 +61,8 @@ require (
github.com/stretchr/testify v1.7.0
github.com/tj/go-termd v0.0.1
github.com/tklauser/go-sysconf v0.3.6 // indirect
github.com/uber/jaeger-client-go v2.29.1+incompatible // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.0
go.etcd.io/etcd/client/v3 v3.5.0
go.uber.org/atomic v1.8.0
Expand Down
Loading