Skip to content

Commit

Permalink
Fix flaky tests that use vtcombo (#16178)
Browse files Browse the repository at this point in the history
Signed-off-by: Manan Gupta <manan@planetscale.com>
  • Loading branch information
vitess-bot[bot] committed Jun 18, 2024
1 parent 6c591bd commit 5edebcc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
22 changes: 11 additions & 11 deletions go/cmd/vttestserver/cli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestRunsVschemaMigrations(t *testing.T) {
cluster, err := startCluster()
defer cluster.TearDown()

assert.NoError(t, err)
require.NoError(t, err)
assertColumnVindex(t, cluster, columnVindex{keyspace: "test_keyspace", table: "test_table", vindex: "my_vdx", vindexType: "hash", column: "id"})
assertColumnVindex(t, cluster, columnVindex{keyspace: "app_customer", table: "customers", vindex: "hash", vindexType: "hash", column: "id"})

Expand All @@ -77,7 +77,7 @@ func TestPersistentMode(t *testing.T) {
dir := t.TempDir()

cluster, err := startPersistentCluster(dir)
assert.NoError(t, err)
require.NoError(t, err)

// Add a new "ad-hoc" vindex via vtgate once the cluster is up, to later make sure it is persisted across teardowns
err = addColumnVindex(cluster, "test_keyspace", "alter vschema on persistence_test add vindex my_vdx(id)")
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestPersistentMode(t *testing.T) {
cluster.PersistentMode = false // Cleanup the tmpdir as we're done
cluster.TearDown()
}()
assert.NoError(t, err)
require.NoError(t, err)

// rerun our sanity checks to make sure vschema is persisted correctly
assertColumnVindex(t, cluster, columnVindex{keyspace: "test_keyspace", table: "test_table", vindex: "my_vdx", vindexType: "hash", column: "id"})
Expand All @@ -137,7 +137,7 @@ func TestForeignKeysAndDDLModes(t *testing.T) {
defer resetConfig(conf)

cluster, err := startCluster("--foreign_key_mode=allow", "--enable_online_ddl=true", "--enable_direct_ddl=true")
assert.NoError(t, err)
require.NoError(t, err)
defer cluster.TearDown()

err = execOnCluster(cluster, "test_keyspace", func(conn *mysql.Conn) error {
Expand All @@ -163,7 +163,7 @@ func TestForeignKeysAndDDLModes(t *testing.T) {

cluster.TearDown()
cluster, err = startCluster("--foreign_key_mode=disallow", "--enable_online_ddl=false", "--enable_direct_ddl=false")
assert.NoError(t, err)
require.NoError(t, err)
defer cluster.TearDown()

err = execOnCluster(cluster, "test_keyspace", func(conn *mysql.Conn) error {
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestNoScatter(t *testing.T) {
defer resetConfig(conf)

cluster, err := startCluster("--no_scatter")
assert.NoError(t, err)
require.NoError(t, err)
defer cluster.TearDown()

_ = execOnCluster(cluster, "app_customer", func(conn *mysql.Conn) error {
Expand All @@ -208,7 +208,7 @@ func TestCreateDbaTCPUser(t *testing.T) {
defer resetConfig(conf)

clusterInstance, err := startCluster("--initialize-with-vt-dba-tcp=true")
assert.NoError(t, err)
require.NoError(t, err)
defer clusterInstance.TearDown()

defer func() {
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestCanGetKeyspaces(t *testing.T) {
defer cancel()

clusterInstance, err := startCluster()
assert.NoError(t, err)
require.NoError(t, err)
defer clusterInstance.TearDown()

defer func() {
Expand Down Expand Up @@ -276,7 +276,7 @@ func TestExternalTopoServerConsul(t *testing.T) {

cluster, err := startCluster("--external_topo_implementation=consul",
fmt.Sprintf("--external_topo_global_server_address=%s", serverAddr), "--external_topo_global_root=consul_test/global")
assert.NoError(t, err)
require.NoError(t, err)
defer cluster.TearDown()

assertGetKeyspaces(ctx, t, cluster)
Expand Down Expand Up @@ -312,7 +312,7 @@ func TestMtlsAuth(t *testing.T) {
fmt.Sprintf("--vtctld_grpc_cert=%s", clientCert),
fmt.Sprintf("--vtctld_grpc_ca=%s", caCert),
fmt.Sprintf("--grpc_auth_mtls_allowed_substrings=%s", "CN=ClientApp"))
assert.NoError(t, err)
require.NoError(t, err)
defer func() {
cluster.PersistentMode = false // Cleanup the tmpdir as we're done
cluster.TearDown()
Expand Down Expand Up @@ -356,7 +356,7 @@ func TestMtlsAuthUnauthorizedFails(t *testing.T) {
fmt.Sprintf("--grpc_auth_mtls_allowed_substrings=%s", "CN=ClientApp"))
defer cluster.TearDown()

assert.Error(t, err)
require.Error(t, err)
assert.Contains(t, err.Error(), "code = Unauthenticated desc = client certificate not authorized")
}

Expand Down
23 changes: 21 additions & 2 deletions go/vt/vttest/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package vttest
import (
"fmt"
"math/rand/v2"
"net"
"os"
"path"
"strconv"
"strings"

"vitess.io/vitess/go/vt/proto/vttest"
Expand Down Expand Up @@ -231,9 +233,26 @@ func tmpdir(dataroot string) (dir string, err error) {
return
}

// randomPort gets a random port that is available for a TCP connection.
// After we generate a random port, we try to establish tcp connections on it and the next 5 values.
// If any of them fail, then we try a different port.
func randomPort() int {
v := rand.Int32N(20000)
return int(v + 10000)
for {
port := int(rand.Int32N(20000) + 10000)
portInUse := false
for i := 0; i < 6; i++ {
ln, err := net.Listen("tcp", net.JoinHostPort("127.0.0.1", strconv.Itoa(port+i)))
if err != nil {
portInUse = true
break
}
ln.Close()
}
if portInUse {
continue
}
return port
}
}

// NewLocalTestEnv returns an instance of the default test environment used
Expand Down

0 comments on commit 5edebcc

Please sign in to comment.