Skip to content
Open
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
38 changes: 29 additions & 9 deletions go/testfiles/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,41 @@ import (

// Port definitions. Unit tests may run at the same time,
// so they should not use the same ports.
//
// Each port has its own constant; do not introduce ad-hoc `port + N`
// arithmetic at call sites or in this file. New entries must extend
// the range without overlapping any existing constant.
Comment on lines +33 to +35
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
// Ports used by the go/vt/topo/etcd2topo package tests.
GoVtTopoEtcd2topoPort = vtPortStart // etcd client URL (plaintext)
GoVtTopoEtcd2topoPeerPort = vtPortStart + 1 // etcd peer URL (plaintext)
GoVtTopoEtcd2topoTLSPort = vtPortStart + 2 // etcd client URL (TLS)
GoVtTopoEtcd2topoTLSPeerPort = vtPortStart + 3 // etcd peer URL (TLS)
// Per-cell etcd pairs for TestEtcd2TopoGetTabletsPartialResults, which
// starts a global etcd plus one etcd per cell.
GoVtTopoEtcd2topoCell1Port = vtPortStart + 4 // cell1 etcd client URL
GoVtTopoEtcd2topoCell1PeerPort = vtPortStart + 5 // cell1 etcd peer URL
GoVtTopoEtcd2topoCell2Port = vtPortStart + 6 // cell2 etcd client URL
GoVtTopoEtcd2topoCell2PeerPort = vtPortStart + 7 // cell2 etcd peer URL

// Base port used by the go/vt/topo/zk2topo package tests.
// zkctl.StartLocalZk consumes three consecutive ports (leader, election,
// client) starting at this base, so vtPortStart+8..10 are reserved.
GoVtTopoZk2topoPort = vtPortStart + 8

// GoVtTopoZk2topoPort is used by the go/vt/topo/zk2topo package.
// Takes three ports.
GoVtTopoZk2topoPort = GoVtTopoEtcd2topoPort + 2
// Ports used by the go/vt/topo/consultopo package tests.
GoVtTopoConsultopoDNSPort = vtPortStart + 11
GoVtTopoConsultopoHTTPPort = vtPortStart + 12
GoVtTopoConsultopoSerfLANPort = vtPortStart + 13
GoVtTopoConsultopoSerfWANPort = vtPortStart + 14

// GoVtTopoConsultopoPort is used by the go/vt/topo/consultopo package.
// Takes four ports.
GoVtTopoConsultopoPort = GoVtTopoZk2topoPort + 3
// Ports used by the go/vt/vtctl/workflow package tests for the
// etcd-backed keyspace routing rules tests.
GoVtVtctlWorkflowPort = vtPortStart + 15 // etcd client URL
GoVtVtctlWorkflowPeerPort = vtPortStart + 16 // etcd peer URL
)

// Zookeeper server ID definitions. Unit tests may run at the
Expand Down
11 changes: 5 additions & 6 deletions go/vt/topo/consultopo/server_flaky_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,12 @@ func startConsul(t *testing.T, authToken string) (*exec.Cmd, string, string) {
}

// Create the JSON config, save it.
port := testfiles.GoVtTopoConsultopoPort
config := map[string]any{
"ports": map[string]int{
"dns": port,
"http": port + 1,
"serf_lan": port + 2,
"serf_wan": port + 3,
"dns": testfiles.GoVtTopoConsultopoDNSPort,
"http": testfiles.GoVtTopoConsultopoHTTPPort,
"serf_lan": testfiles.GoVtTopoConsultopoSerfLANPort,
"serf_wan": testfiles.GoVtTopoConsultopoSerfWANPort,
},
}

Expand Down Expand Up @@ -99,7 +98,7 @@ func startConsul(t *testing.T, authToken string) (*exec.Cmd, string, string) {
}

// Create a client to connect to the created consul.
serverAddr := fmt.Sprintf("localhost:%v", port+1)
serverAddr := fmt.Sprintf("localhost:%v", testfiles.GoVtTopoConsultopoHTTPPort)
cfg := api.DefaultConfig()
cfg.Address = serverAddr
if authToken != "" {
Expand Down
38 changes: 23 additions & 15 deletions go/vt/topo/etcd2topo/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,13 @@ import (
)

// startEtcd starts an etcd subprocess, and waits for it to be ready.
func startEtcd(t *testing.T, port int) (string, *exec.Cmd) {
func startEtcd(t *testing.T, clientPort, peerPort int) (string, *exec.Cmd) {
// Create a temporary directory.
dataDir := t.TempDir()

// Get our two ports to listen to.
if port == 0 {
port = testfiles.GoVtTopoEtcd2topoPort
}
name := "vitess_unit_test"
clientAddr := fmt.Sprintf("http://localhost:%v", port)
peerAddr := fmt.Sprintf("http://localhost:%v", port+1)
clientAddr := fmt.Sprintf("http://localhost:%v", clientPort)
peerAddr := fmt.Sprintf("http://localhost:%v", peerPort)
initialCluster := fmt.Sprintf("%v=%v", name, peerAddr)

cmd := exec.Command("etcd",
Expand Down Expand Up @@ -107,11 +103,9 @@ func startEtcdWithTLS(t *testing.T) (string, *tlstest.ClientServerKeyPairs) {
// Create a temporary directory.
dataDir := t.TempDir()

// Get our two ports to listen to.
port := testfiles.GoVtTopoEtcd2topoPort
name := "vitess_unit_test"
clientAddr := fmt.Sprintf("https://localhost:%v", port+2)
peerAddr := fmt.Sprintf("https://localhost:%v", port+3)
clientAddr := fmt.Sprintf("https://localhost:%v", testfiles.GoVtTopoEtcd2topoTLSPort)
peerAddr := fmt.Sprintf("https://localhost:%v", testfiles.GoVtTopoEtcd2topoTLSPeerPort)
initialCluster := fmt.Sprintf("%v=%v", name, peerAddr)

certs := tlstest.CreateClientServerCertPairs(dataDir)
Expand Down Expand Up @@ -224,7 +218,7 @@ func TestEtcd2TLS(t *testing.T) {

func TestEtcd2Topo(t *testing.T) {
// Start a single etcd in the background.
clientAddr, _ := startEtcd(t, 0)
clientAddr, _ := startEtcd(t, testfiles.GoVtTopoEtcd2topoPort, testfiles.GoVtTopoEtcd2topoPeerPort)

testIndex := 0
newServer := func() *topo.Server {
Expand Down Expand Up @@ -271,16 +265,30 @@ func TestEtcd2TopoGetTabletsPartialResults(t *testing.T) {
root := "/vitess"
// Start three etcd instances in the background. One will serve the global topo data
// while the other two will serve the cell topo data.
globalClientAddr, _ := startEtcd(t, 0)
globalClientAddr, _ := startEtcd(t, testfiles.GoVtTopoEtcd2topoPort, testfiles.GoVtTopoEtcd2topoPeerPort)
cellPorts := [...]struct {
client, peer int
}{
{testfiles.GoVtTopoEtcd2topoCell1Port, testfiles.GoVtTopoEtcd2topoCell1PeerPort},
{testfiles.GoVtTopoEtcd2topoCell2Port, testfiles.GoVtTopoEtcd2topoCell2PeerPort},
}
require.Equal(t, len(cells), len(cellPorts))
cellClientAddrs := make([]string, len(cells))
cellClientCmds := make([]*exec.Cmd, len(cells))
cellTSs := make([]*topo.Server, len(cells))
<<<<<<< HEAD
for i := 0; i < len(cells); i++ {
addr, cmd := startEtcd(t, testfiles.GoVtTopoEtcd2topoPort+(i+100*i))
||||||| parent of 50e632b1fc (tests: give vtctl/workflow tests their own port range (#20150))
for i := range cells {
addr, cmd := startEtcd(t, testfiles.GoVtTopoEtcd2topoPort+(i+100*i))
=======
for i := range cells {
addr, cmd := startEtcd(t, cellPorts[i].client, cellPorts[i].peer)
>>>>>>> 50e632b1fc (tests: give vtctl/workflow tests their own port range (#20150))
cellClientAddrs[i] = addr
cellClientCmds[i] = cmd
}
require.Equal(t, len(cells), len(cellTSs))

// Setup the global topo server.
globalTS, err := topo.OpenServer("etcd2", globalClientAddr, path.Join(root, topo.GlobalCell))
Expand Down Expand Up @@ -365,7 +373,7 @@ func TestEtcd2TopoGetTabletsPartialResults(t *testing.T) {
// appropriate errors instead of panicking due to nil pointer dereference.
func TestEtcd2TopoServerClosed(t *testing.T) {
// Start a single etcd in the background.
clientAddr, _ := startEtcd(t, 0)
clientAddr, _ := startEtcd(t, testfiles.GoVtTopoEtcd2topoPort, testfiles.GoVtTopoEtcd2topoPeerPort)

testRoot := "/test-closed"

Expand Down
3 changes: 2 additions & 1 deletion go/vt/topo/etcd2topo/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
clientv3 "go.etcd.io/etcd/client/v3"

"vitess.io/vitess/go/test/utils"
"vitess.io/vitess/go/testfiles"
"vitess.io/vitess/go/vt/topo"
)

Expand All @@ -41,7 +42,7 @@ import (
// behavior accidentally/uinintentionally.
func TestWatchTopoVersion(t *testing.T) {
ctx := utils.LeakCheckContext(t)
etcdServerAddr, _ := startEtcd(t, 0)
etcdServerAddr, _ := startEtcd(t, testfiles.GoVtTopoEtcd2topoPort, testfiles.GoVtTopoEtcd2topoPeerPort)
root := "/vitess/test"
name := "testkey"
path := path.Join(root, name)
Expand Down
6 changes: 2 additions & 4 deletions go/vt/vtctl/workflow/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,9 @@ func startEtcd(t *testing.T) string {
// Create a temporary directory.
dataDir := t.TempDir()

// 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)
clientAddr := fmt.Sprintf("http://localhost:%v", testfiles.GoVtVtctlWorkflowPort)
peerAddr := fmt.Sprintf("http://localhost:%v", testfiles.GoVtVtctlWorkflowPeerPort)
initialCluster := fmt.Sprintf("%v=%v", name, peerAddr)
cmd := exec.Command("etcd",
"-name", name,
Expand Down
Loading