зеркало из https://github.com/github/vitess-gh.git
Adding a testfiles/ports.go library.
It handles port and zookeeper IDs allocation for tests, so they don't step on eachothers toes.
This commit is contained in:
Родитель
3527af20ae
Коммит
a1ef409126
|
@ -3,6 +3,7 @@
|
|||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package testfiles locates test files within the Vitess directory tree.
|
||||
// It also handles test port allocation.
|
||||
package testfiles
|
||||
|
||||
import (
|
||||
|
@ -12,6 +13,7 @@ import (
|
|||
"path/filepath"
|
||||
)
|
||||
|
||||
// Locate returns a file path that came from $VTROOT/data/test.
|
||||
func Locate(filename string) string {
|
||||
vtroot := os.Getenv("VTROOT")
|
||||
if vtroot == "" {
|
||||
|
@ -20,6 +22,7 @@ func Locate(filename string) string {
|
|||
return path.Join(vtroot, "data", "test", filename)
|
||||
}
|
||||
|
||||
// Glob returns all files matching a pattern in $VTROOT/data/test.
|
||||
func Glob(pattern string) []string {
|
||||
vtroot := os.Getenv("VTROOT")
|
||||
if vtroot == "" {
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
package testfiles
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// This file contains helper methods and declarations so all unit
|
||||
// tests use different ports.
|
||||
//
|
||||
// We also use it to allocate Zookeeper server IDs.
|
||||
|
||||
//
|
||||
// Port definitions. Unit tests may run at the same time,
|
||||
// so they should not use the same ports.
|
||||
//
|
||||
var (
|
||||
// vtPortStart is the starting port for all tests.
|
||||
vtPortStart = getPortStart()
|
||||
|
||||
// GoVtTopoEtcd2topoPort is used by the go/vt/topo/etcd2topo package.
|
||||
// Takes two ports.
|
||||
GoVtTopoEtcd2topoPort = vtPortStart
|
||||
|
||||
// GoVtTopoZk2topoPort is used by the go/vt/topo/zk2topo package.
|
||||
// Takes three ports.
|
||||
GoVtTopoZk2topoPort = GoVtTopoEtcd2topoPort + 2
|
||||
|
||||
// GoVtTabletserverCustomruleZkcustomrulePort is used by the go/vt/tabletserver/customrule/zkcustomrule package.
|
||||
// Takes three ports.
|
||||
GoVtTabletserverCustomruleZkcustomrulePort = GoVtTopoZk2topoPort + 3
|
||||
|
||||
// GoVtZktopoPort is used by the go/vt/zktopo package.
|
||||
// Takes three ports.
|
||||
GoVtZktopoPort = GoVtTabletserverCustomruleZkcustomrulePort + 3
|
||||
)
|
||||
|
||||
//
|
||||
// Zookeeper server ID definitions. Unit tests may run at the
|
||||
// same time, so they can't use the same Zookeeper server IDs.
|
||||
//
|
||||
var (
|
||||
// GoVtTopoZk2topoZk is used by the go/vt/topo/zk2topo package.
|
||||
GoVtTopoZk2topoZk = 1
|
||||
|
||||
// GoVtTabletserverCustomruleZkcustomruleZk is used by the
|
||||
// go/vt/tabletserver/customrule/zkcustomrule package.
|
||||
GoVtTabletserverCustomruleZkcustomruleZk = 2
|
||||
|
||||
// GoVtZktopoZk is used by the go/vt/zktopo package.
|
||||
GoVtZktopoZk = 3
|
||||
)
|
||||
|
||||
func getPortStart() int {
|
||||
env := os.Getenv("VTPORTSTART")
|
||||
if env == "" {
|
||||
env = "6700"
|
||||
}
|
||||
portStart, err := strconv.Atoi(env)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("cannot parse VTPORTSTART: %v", err))
|
||||
}
|
||||
return portStart
|
||||
}
|
|
@ -12,6 +12,7 @@ import (
|
|||
|
||||
"github.com/samuel/go-zookeeper/zk"
|
||||
|
||||
"github.com/youtube/vitess/go/testfiles"
|
||||
"github.com/youtube/vitess/go/vt/tabletserver"
|
||||
"github.com/youtube/vitess/go/vt/tabletserver/tabletservermock"
|
||||
"github.com/youtube/vitess/go/vt/topo/zk2topo"
|
||||
|
@ -43,7 +44,7 @@ var customRule2 = `
|
|||
|
||||
func TestZkCustomRule(t *testing.T) {
|
||||
// Start a real single ZK daemon, and close it after all tests are done.
|
||||
zkd, serverAddr := zkctl.StartLocalZk(2)
|
||||
zkd, serverAddr := zkctl.StartLocalZk(testfiles.GoVtTabletserverCustomruleZkcustomruleZk, testfiles.GoVtTabletserverCustomruleZkcustomrulePort)
|
||||
defer zkd.Teardown()
|
||||
|
||||
// Create fake file.
|
||||
|
|
|
@ -10,13 +10,13 @@ import (
|
|||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/youtube/vitess/go/testfiles"
|
||||
"github.com/youtube/vitess/go/vt/topo"
|
||||
"github.com/youtube/vitess/go/vt/topo/test"
|
||||
|
||||
|
@ -31,15 +31,8 @@ func startEtcd(t *testing.T) (*exec.Cmd, string, string) {
|
|||
t.Fatalf("cannot create tempdir: %v", err)
|
||||
}
|
||||
|
||||
// Find two ports to listen to. Same logic as the end-to-end tests.
|
||||
env := os.Getenv("VTPORTSTART")
|
||||
if env == "" {
|
||||
env = "6700"
|
||||
}
|
||||
port, err := strconv.Atoi(env)
|
||||
if err != nil {
|
||||
t.Fatalf("cannot parse VTPORTSTART: %v", err)
|
||||
}
|
||||
// Get our two ports to listen to.
|
||||
port := testfiles.GoVtTopoEtcd2topoPort
|
||||
name := "vitess_unit_test"
|
||||
clientAddr := fmt.Sprintf("http://localhost:%v", port)
|
||||
peerAddr := fmt.Sprintf("http://localhost:%v", port+1)
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"github.com/samuel/go-zookeeper/zk"
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/youtube/vitess/go/testfiles"
|
||||
"github.com/youtube/vitess/go/vt/topo"
|
||||
"github.com/youtube/vitess/go/vt/topo/test"
|
||||
"github.com/youtube/vitess/go/zk/zkctl"
|
||||
|
@ -18,7 +19,7 @@ import (
|
|||
|
||||
func TestZk2Topo(t *testing.T) {
|
||||
// Start a real single ZK daemon, and close it after all tests are done.
|
||||
zkd, serverAddr := zkctl.StartLocalZk(1)
|
||||
zkd, serverAddr := zkctl.StartLocalZk(testfiles.GoVtTopoZk2topoZk, testfiles.GoVtTopoZk2topoPort)
|
||||
defer zkd.Teardown()
|
||||
|
||||
// This function will create a toplevel directory for a new test.
|
||||
|
|
|
@ -11,6 +11,7 @@ import (
|
|||
zookeeper "github.com/samuel/go-zookeeper/zk"
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/youtube/vitess/go/testfiles"
|
||||
"github.com/youtube/vitess/go/vt/topo"
|
||||
"github.com/youtube/vitess/go/vt/topo/test"
|
||||
"github.com/youtube/vitess/go/zk"
|
||||
|
@ -22,7 +23,7 @@ import (
|
|||
// Run the topology test suite on zktopo.
|
||||
func TestZkTopo(t *testing.T) {
|
||||
// Start a real single ZK daemon, and close it after all tests are done.
|
||||
zkd, serverAddr := zkctl.StartLocalZk(3)
|
||||
zkd, serverAddr := zkctl.StartLocalZk(testfiles.GoVtZktopoZk, testfiles.GoVtZktopoPort)
|
||||
defer zkd.Teardown()
|
||||
|
||||
// Create a ZK_CLIENT_CONFIG file to use.
|
||||
|
|
|
@ -2,8 +2,6 @@ package zkctl
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
log "github.com/golang/glog"
|
||||
|
||||
|
@ -12,20 +10,9 @@ import (
|
|||
|
||||
// StartLocalZk is a helper method to create a local ZK process. Used
|
||||
// in tests, mostly. It will log.Fatal out if there is an error. Each
|
||||
// call should use different serverID, so tests don't interfere with
|
||||
// eachother.
|
||||
func StartLocalZk(id int) (*Zkd, string) {
|
||||
// Get the starting port.
|
||||
env := os.Getenv("VTPORTSTART")
|
||||
if env == "" {
|
||||
env = "6700"
|
||||
}
|
||||
portStart, err := strconv.Atoi(env)
|
||||
if err != nil {
|
||||
log.Fatalf("cannot parse VTPORTSTART: %v", err)
|
||||
}
|
||||
port := portStart + (id-1)*3
|
||||
|
||||
// call should use different serverID / ports, so tests don't
|
||||
// interfere with eachother. Use the testfiles package to achieve this.
|
||||
func StartLocalZk(id, port int) (*Zkd, string) {
|
||||
// Build the config parameters.
|
||||
hostname := netutil.FullyQualifiedHostnameOrPanic()
|
||||
zkCfg := fmt.Sprintf("%v@%v:%v:%v:%v", id, hostname, port, port+1, port+2)
|
||||
|
|
Загрузка…
Ссылка в новой задаче