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
GuptaManan100 committed Jun 18, 2024
1 parent a9ee4df commit 9f679e6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
18 changes: 9 additions & 9 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 TestCanGetKeyspaces(t *testing.T) {
defer resetConfig(conf)

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

defer func() {
Expand Down Expand Up @@ -222,7 +222,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(t, cluster)
Expand Down Expand Up @@ -258,7 +258,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 @@ -302,7 +302,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"
"net"
"os"
"path"
"strconv"
"strings"

"vitess.io/vitess/go/vt/proto/vttest"
Expand Down Expand Up @@ -230,9 +232,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.Int31n(20000)
return int(v + 10000)
for {
port := int(rand.Int31n(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

Check warning on line 246 in go/vt/vttest/environment.go

View check run for this annotation

Codecov / codecov/patch

go/vt/vttest/environment.go#L245-L246

Added lines #L245 - L246 were not covered by tests
}
ln.Close()
}
if portInUse {
continue

Check warning on line 251 in go/vt/vttest/environment.go

View check run for this annotation

Codecov / codecov/patch

go/vt/vttest/environment.go#L251

Added line #L251 was not covered by tests
}
return port
}
}

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

0 comments on commit 9f679e6

Please sign in to comment.