Skip to content

Commit 50e632b

Browse files
tests: give vtctl/workflow tests their own port range (#20150)
Signed-off-by: Arthur Schreiber <arthur@planetscale.com>
1 parent fe26c0d commit 50e632b

5 files changed

Lines changed: 54 additions & 36 deletions

File tree

go/testfiles/ports.go

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,41 @@ import (
2929

3030
// Port definitions. Unit tests may run at the same time,
3131
// so they should not use the same ports.
32+
//
33+
// Each port has its own constant; do not introduce ad-hoc `port + N`
34+
// arithmetic at call sites or in this file. New entries must extend
35+
// the range without overlapping any existing constant.
3236
var (
3337
// vtPortStart is the starting port for all tests.
3438
vtPortStart = getPortStart()
3539

36-
// GoVtTopoEtcd2topoPort is used by the go/vt/topo/etcd2topo package.
37-
// Takes two ports.
38-
GoVtTopoEtcd2topoPort = vtPortStart
40+
// Ports used by the go/vt/topo/etcd2topo package tests.
41+
GoVtTopoEtcd2topoPort = vtPortStart // etcd client URL (plaintext)
42+
GoVtTopoEtcd2topoPeerPort = vtPortStart + 1 // etcd peer URL (plaintext)
43+
GoVtTopoEtcd2topoTLSPort = vtPortStart + 2 // etcd client URL (TLS)
44+
GoVtTopoEtcd2topoTLSPeerPort = vtPortStart + 3 // etcd peer URL (TLS)
45+
// Per-cell etcd pairs for TestEtcd2TopoGetTabletsPartialResults, which
46+
// starts a global etcd plus one etcd per cell.
47+
GoVtTopoEtcd2topoCell1Port = vtPortStart + 4 // cell1 etcd client URL
48+
GoVtTopoEtcd2topoCell1PeerPort = vtPortStart + 5 // cell1 etcd peer URL
49+
GoVtTopoEtcd2topoCell2Port = vtPortStart + 6 // cell2 etcd client URL
50+
GoVtTopoEtcd2topoCell2PeerPort = vtPortStart + 7 // cell2 etcd peer URL
51+
52+
// Base port used by the go/vt/topo/zk2topo package tests.
53+
// zkctl.StartLocalZk consumes three consecutive ports (leader, election,
54+
// client) starting at this base, so vtPortStart+8..10 are reserved.
55+
GoVtTopoZk2topoPort = vtPortStart + 8
3956

40-
// GoVtTopoZk2topoPort is used by the go/vt/topo/zk2topo package.
41-
// Takes three ports.
42-
GoVtTopoZk2topoPort = GoVtTopoEtcd2topoPort + 2
57+
// Ports used by the go/vt/topo/consultopo package tests.
58+
GoVtTopoConsultopoDNSPort = vtPortStart + 11
59+
GoVtTopoConsultopoHTTPPort = vtPortStart + 12
60+
GoVtTopoConsultopoSerfLANPort = vtPortStart + 13
61+
GoVtTopoConsultopoSerfWANPort = vtPortStart + 14
4362

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

4969
// Zookeeper server ID definitions. Unit tests may run at the

go/vt/topo/consultopo/server_flaky_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,12 @@ func startConsul(t *testing.T, authToken string) (*exec.Cmd, string, string) {
5454
require.NoError(t, err)
5555

5656
// Create the JSON config, save it.
57-
port := testfiles.GoVtTopoConsultopoPort
5857
config := map[string]any{
5958
"ports": map[string]int{
60-
"dns": port,
61-
"http": port + 1,
62-
"serf_lan": port + 2,
63-
"serf_wan": port + 3,
59+
"dns": testfiles.GoVtTopoConsultopoDNSPort,
60+
"http": testfiles.GoVtTopoConsultopoHTTPPort,
61+
"serf_lan": testfiles.GoVtTopoConsultopoSerfLANPort,
62+
"serf_wan": testfiles.GoVtTopoConsultopoSerfWANPort,
6463
},
6564
}
6665

@@ -92,7 +91,7 @@ func startConsul(t *testing.T, authToken string) (*exec.Cmd, string, string) {
9291
require.NoError(t, err)
9392

9493
// Create a client to connect to the created consul.
95-
serverAddr := fmt.Sprintf("localhost:%v", port+1)
94+
serverAddr := fmt.Sprintf("localhost:%v", testfiles.GoVtTopoConsultopoHTTPPort)
9695
cfg := api.DefaultConfig()
9796
cfg.Address = serverAddr
9897
if authToken != "" {

go/vt/topo/etcd2topo/server_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,13 @@ import (
4040
)
4141

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

47-
// Get our two ports to listen to.
48-
if port == 0 {
49-
port = testfiles.GoVtTopoEtcd2topoPort
50-
}
5147
name := "vitess_unit_test"
52-
clientAddr := fmt.Sprintf("http://localhost:%v", port)
53-
peerAddr := fmt.Sprintf("http://localhost:%v", port+1)
48+
clientAddr := fmt.Sprintf("http://localhost:%v", clientPort)
49+
peerAddr := fmt.Sprintf("http://localhost:%v", peerPort)
5450
initialCluster := fmt.Sprintf("%v=%v", name, peerAddr)
5551

5652
cmd := exec.Command("etcd",
@@ -104,11 +100,9 @@ func startEtcdWithTLS(t *testing.T) (string, *tlstest.ClientServerKeyPairs) {
104100
// Create a temporary directory.
105101
dataDir := t.TempDir()
106102

107-
// Get our two ports to listen to.
108-
port := testfiles.GoVtTopoEtcd2topoPort
109103
name := "vitess_unit_test"
110-
clientAddr := fmt.Sprintf("https://localhost:%v", port+2)
111-
peerAddr := fmt.Sprintf("https://localhost:%v", port+3)
104+
clientAddr := fmt.Sprintf("https://localhost:%v", testfiles.GoVtTopoEtcd2topoTLSPort)
105+
peerAddr := fmt.Sprintf("https://localhost:%v", testfiles.GoVtTopoEtcd2topoTLSPeerPort)
112106
initialCluster := fmt.Sprintf("%v=%v", name, peerAddr)
113107

114108
certs := tlstest.CreateClientServerCertPairs(dataDir)
@@ -209,7 +203,7 @@ func TestEtcd2TLS(t *testing.T) {
209203

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

214208
testIndex := 0
215209
newServer := func() *topo.Server {
@@ -253,16 +247,22 @@ func TestEtcd2TopoGetTabletsPartialResults(t *testing.T) {
253247
root := "/vitess"
254248
// Start three etcd instances in the background. One will serve the global topo data
255249
// while the other two will serve the cell topo data.
256-
globalClientAddr, _ := startEtcd(t, 0)
250+
globalClientAddr, _ := startEtcd(t, testfiles.GoVtTopoEtcd2topoPort, testfiles.GoVtTopoEtcd2topoPeerPort)
251+
cellPorts := [...]struct {
252+
client, peer int
253+
}{
254+
{testfiles.GoVtTopoEtcd2topoCell1Port, testfiles.GoVtTopoEtcd2topoCell1PeerPort},
255+
{testfiles.GoVtTopoEtcd2topoCell2Port, testfiles.GoVtTopoEtcd2topoCell2PeerPort},
256+
}
257+
require.Equal(t, len(cells), len(cellPorts))
257258
cellClientAddrs := make([]string, len(cells))
258259
cellClientCmds := make([]*exec.Cmd, len(cells))
259260
cellTSs := make([]*topo.Server, len(cells))
260261
for i := range cells {
261-
addr, cmd := startEtcd(t, testfiles.GoVtTopoEtcd2topoPort+(i+100*i))
262+
addr, cmd := startEtcd(t, cellPorts[i].client, cellPorts[i].peer)
262263
cellClientAddrs[i] = addr
263264
cellClientCmds[i] = cmd
264265
}
265-
require.Equal(t, len(cells), len(cellTSs))
266266

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

358358
testRoot := "/test-closed"
359359

go/vt/topo/etcd2topo/watch_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
clientv3 "go.etcd.io/etcd/client/v3"
3030

3131
"vitess.io/vitess/go/test/utils"
32+
"vitess.io/vitess/go/testfiles"
3233
"vitess.io/vitess/go/vt/topo"
3334
)
3435

@@ -41,7 +42,7 @@ import (
4142
// behavior accidentally/uinintentionally.
4243
func TestWatchTopoVersion(t *testing.T) {
4344
ctx := utils.LeakCheckContext(t)
44-
etcdServerAddr, _ := startEtcd(t, 0)
45+
etcdServerAddr, _ := startEtcd(t, testfiles.GoVtTopoEtcd2topoPort, testfiles.GoVtTopoEtcd2topoPeerPort)
4546
root := "/vitess/test"
4647
name := "testkey"
4748
path := path.Join(root, name)

go/vt/vtctl/workflow/utils_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,9 @@ func startEtcd(t *testing.T) string {
237237
// Create a temporary directory.
238238
dataDir := t.TempDir()
239239

240-
// Get our two ports to listen to.
241-
port := testfiles.GoVtTopoEtcd2topoPort
242240
name := "vitess_unit_test"
243-
clientAddr := fmt.Sprintf("http://localhost:%v", port)
244-
peerAddr := fmt.Sprintf("http://localhost:%v", port+1)
241+
clientAddr := fmt.Sprintf("http://localhost:%v", testfiles.GoVtVtctlWorkflowPort)
242+
peerAddr := fmt.Sprintf("http://localhost:%v", testfiles.GoVtVtctlWorkflowPeerPort)
245243
initialCluster := fmt.Sprintf("%v=%v", name, peerAddr)
246244
cmd := exec.Command("etcd",
247245
"-name", name,

0 commit comments

Comments
 (0)