Skip to content

Commit 8e50d43

Browse files
committed
testcluster: assign unique ClusterName
This commit makes TestCluster by default assign unique cluster name in the TestServerArgs. The cluster name is shared by all participating or added nodes, unless overridden. Epic: none Release note: none
1 parent a59328e commit 8e50d43

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

pkg/base/test_server_args.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,14 @@ type TestServerArgs struct {
122122
UseDatabase string
123123

124124
// If set, this will be configured in the test server to check connections
125-
// from other test servers and to report in the SQL introspection.
125+
// from other test servers and to report in the SQL introspection. It is
126+
// advised to make the name sufficiently unique, in order to prevent a
127+
// TestCluster from accidentally getting messages from unrelated clusters in
128+
// the same environment that used the same TCP ports recently (e.g. see
129+
// https://github.com/cockroachdb/cockroach/issues/157838).
130+
//
131+
// If empty (most cases), a unique ClusterName is generated automatically, or
132+
// a higher-level default is used (e.g. taken from TestClusterArgs).
126133
ClusterName string
127134

128135
// Stopper can be used to stop the server. If not set, a stopper will be

pkg/cli/zip_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import (
4545
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
4646
"github.com/cockroachdb/cockroach/pkg/util/log"
4747
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
48+
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
4849
"github.com/cockroachdb/datadriven"
4950
"github.com/cockroachdb/errors"
5051
"github.com/cockroachdb/errors/oserror"
@@ -403,10 +404,12 @@ func TestConcurrentZip(t *testing.T) {
403404

404405
ctx := context.Background()
405406

407+
clusterName := fmt.Sprintf("TestConcurrentZip-%d", timeutil.Now().UnixNano())
406408
// Three nodes. We want to see what `zip` thinks when one of the nodes is down.
407409
tc := testcluster.StartTestCluster(t, 3, base.TestClusterArgs{
408410
ServerArgs: base.TestServerArgs{
409411
DefaultTestTenant: base.TestIsSpecificToStorageLayerAndNeedsASystemTenant,
412+
ClusterName: clusterName,
410413
Insecure: true,
411414
},
412415
})
@@ -421,10 +424,11 @@ func TestConcurrentZip(t *testing.T) {
421424
defer func(prevStderr *os.File) { stderr = prevStderr }(stderr)
422425
stderr = os.Stdout
423426

424-
out, err := c.RunWithCapture("debug zip --timeout=30s --cpu-profile-duration=0s --validate-zip-file=false " + os.DevNull)
425-
if err != nil {
426-
t.Fatal(err)
427-
}
427+
out, err := c.RunWithCapture(fmt.Sprintf(
428+
"debug zip --timeout=30s --cpu-profile-duration=0s --validate-zip-file=false --cluster-name=%s %s",
429+
clusterName, os.DevNull,
430+
))
431+
require.NoError(t, err)
428432

429433
// Strip any non-deterministic messages.
430434
out = eraseNonDeterministicZipOutput(out)

pkg/testutils/testcluster/testcluster.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@ func NewTestCluster(
317317
if clusterArgs.StartSingleNode && nodes > 1 {
318318
t.Fatal("StartSingleNode implies 1 node only, but asked to create", nodes)
319319
}
320+
if clusterArgs.ServerArgs.ClusterName == "" {
321+
clusterArgs.ServerArgs.ClusterName = fmt.Sprintf("TestCluster-%d", timeutil.Now().UnixNano())
322+
}
320323

321324
if err := checkServerArgsForCluster(
322325
clusterArgs.ServerArgs, clusterArgs.ReplicationMode, disallowJoinAddr,
@@ -634,6 +637,9 @@ func (tc *TestCluster) AddServer(
634637
if serverArgs.JoinAddr != "" {
635638
serverArgs.NoAutoInitializeCluster = true
636639
}
640+
if serverArgs.ClusterName == "" {
641+
serverArgs.ClusterName = tc.clusterArgs.ServerArgs.ClusterName
642+
}
637643

638644
// Check args even though we have called checkServerArgsForCluster()
639645
// already in NewTestCluster(). AddServer might be called for servers

0 commit comments

Comments
 (0)