-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make vttestserver compatible with persistent data directories #7718
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,12 +21,14 @@ import ( | |
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"math/rand" | ||
"os" | ||
"path" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"vitess.io/vitess/go/sqltypes" | ||
"vitess.io/vitess/go/vt/tlstest" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
@@ -52,10 +54,12 @@ type columnVindex struct { | |
} | ||
|
||
func TestRunsVschemaMigrations(t *testing.T) { | ||
args := os.Args | ||
conf := config | ||
defer resetFlags(args, conf) | ||
|
||
cluster, err := startCluster() | ||
defer cluster.TearDown() | ||
args := os.Args | ||
defer resetFlags(args) | ||
|
||
assert.NoError(t, err) | ||
assertColumnVindex(t, cluster, columnVindex{keyspace: "test_keyspace", table: "test_table", vindex: "my_vdx", vindexType: "hash", column: "id"}) | ||
|
@@ -67,12 +71,69 @@ func TestRunsVschemaMigrations(t *testing.T) { | |
assertColumnVindex(t, cluster, columnVindex{keyspace: "test_keyspace", table: "test_table1", vindex: "my_vdx", vindexType: "hash", column: "id"}) | ||
} | ||
|
||
func TestPersistentMode(t *testing.T) { | ||
args := os.Args | ||
conf := config | ||
defer resetFlags(args, conf) | ||
|
||
dir, err := ioutil.TempDir("/tmp", "vttestserver_persistent_mode_") | ||
assert.NoError(t, err) | ||
defer os.RemoveAll(dir) | ||
|
||
cluster, err := startPersistentCluster(dir) | ||
assert.NoError(t, err) | ||
|
||
// basic sanity checks similar to TestRunsVschemaMigrations | ||
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"}) | ||
|
||
// insert some data to ensure persistence across teardowns | ||
err = execOnCluster(cluster, "app_customer", func(conn *mysql.Conn) error { | ||
_, err := conn.ExecuteFetch("insert into customers (id, name) values (1, 'gopherson')", 1, false) | ||
return err | ||
}) | ||
assert.NoError(t, err) | ||
|
||
expectedRows := [][]sqltypes.Value{ | ||
{sqltypes.NewInt64(1), sqltypes.NewVarChar("gopherson"), sqltypes.NULL}, | ||
} | ||
|
||
// ensure data was actually inserted | ||
var res *sqltypes.Result | ||
err = execOnCluster(cluster, "app_customer", func(conn *mysql.Conn) (err error) { | ||
res, err = conn.ExecuteFetch("SELECT * FROM customers", 1, false) | ||
return err | ||
}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedRows, res.Rows) | ||
|
||
// reboot the persistent cluster | ||
cluster.TearDown() | ||
cluster, err = startPersistentCluster(dir) | ||
defer cluster.TearDown() | ||
assert.NoError(t, err) | ||
|
||
// rerun our sanity checks to make sure vschema migrations are run during every startup | ||
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"}) | ||
|
||
// ensure previous data was successfully persisted | ||
err = execOnCluster(cluster, "app_customer", func(conn *mysql.Conn) (err error) { | ||
res, err = conn.ExecuteFetch("SELECT * FROM customers", 1, false) | ||
return err | ||
}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedRows, res.Rows) | ||
} | ||
|
||
func TestCanVtGateExecute(t *testing.T) { | ||
args := os.Args | ||
conf := config | ||
defer resetFlags(args, conf) | ||
|
||
cluster, err := startCluster() | ||
assert.NoError(t, err) | ||
defer cluster.TearDown() | ||
args := os.Args | ||
defer resetFlags(args) | ||
|
||
client, err := vtctlclient.New(fmt.Sprintf("localhost:%v", cluster.GrpcPort())) | ||
assert.NoError(t, err) | ||
|
@@ -109,6 +170,10 @@ Out: | |
} | ||
|
||
func TestMtlsAuth(t *testing.T) { | ||
args := os.Args | ||
conf := config | ||
defer resetFlags(args, conf) | ||
|
||
// Our test root. | ||
root, err := ioutil.TempDir("", "tlstest") | ||
if err != nil { | ||
|
@@ -141,15 +206,17 @@ func TestMtlsAuth(t *testing.T) { | |
fmt.Sprintf("-grpc_auth_mtls_allowed_substrings=%s", "CN=ClientApp")) | ||
assert.NoError(t, err) | ||
defer cluster.TearDown() | ||
args := os.Args | ||
defer resetFlags(args) | ||
|
||
// startCluster will apply vschema migrations using vtctl grpc and the clientCert. | ||
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"}) | ||
} | ||
|
||
func TestMtlsAuthUnauthorizedFails(t *testing.T) { | ||
args := os.Args | ||
conf := config | ||
defer resetFlags(args, conf) | ||
|
||
// Our test root. | ||
root, err := ioutil.TempDir("", "tlstest") | ||
if err != nil { | ||
|
@@ -182,13 +249,21 @@ func TestMtlsAuthUnauthorizedFails(t *testing.T) { | |
fmt.Sprintf("-vtctld_grpc_ca=%s", caCert), | ||
fmt.Sprintf("-grpc_auth_mtls_allowed_substrings=%s", "CN=ClientApp")) | ||
defer cluster.TearDown() | ||
args := os.Args | ||
defer resetFlags(args) | ||
|
||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "code = Unauthenticated desc = client certificate not authorized") | ||
} | ||
|
||
func startPersistentCluster(dir string, flags ...string) (vttest.LocalCluster, error) { | ||
flags = append(flags, []string{ | ||
"-persistent_mode", | ||
// FIXME: if port is not provided, data_dir is not respected | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is still a bug unfortunately. It's not really something that concerns us day-to-day but I may give it an hour or two today to make a PR for it anyway, if it's a simple fix There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I propose we move forward without this fix in this PR and tackle this work separately. I can open an issue. Edit: opened an issue |
||
fmt.Sprintf("-port=%d", randomPort()), | ||
fmt.Sprintf("-data_dir=%s", dir), | ||
}...) | ||
return startCluster(flags...) | ||
} | ||
|
||
func startCluster(flags ...string) (vttest.LocalCluster, error) { | ||
schemaDirArg := "-schema_dir=data/schema" | ||
tabletHostname := "-tablet_hostname=localhost" | ||
|
@@ -201,6 +276,13 @@ func startCluster(flags ...string) (vttest.LocalCluster, error) { | |
} | ||
|
||
func addColumnVindex(cluster vttest.LocalCluster, keyspace string, vschemaMigration string) error { | ||
return execOnCluster(cluster, keyspace, func(conn *mysql.Conn) error { | ||
_, err := conn.ExecuteFetch(vschemaMigration, 1, false) | ||
return err | ||
}) | ||
} | ||
|
||
func execOnCluster(cluster vttest.LocalCluster, keyspace string, f func(*mysql.Conn) error) error { | ||
ctx := context.Background() | ||
vtParams := mysql.ConnParams{ | ||
Host: "localhost", | ||
|
@@ -213,8 +295,7 @@ func addColumnVindex(cluster vttest.LocalCluster, keyspace string, vschemaMigrat | |
return err | ||
} | ||
defer conn.Close() | ||
_, err = conn.ExecuteFetch(vschemaMigration, 1, false) | ||
return err | ||
return f(conn) | ||
} | ||
|
||
func assertColumnVindex(t *testing.T, cluster vttest.LocalCluster, expected columnVindex) { | ||
|
@@ -243,6 +324,12 @@ func assertEqual(t *testing.T, actual string, expected string, message string) { | |
} | ||
} | ||
|
||
func resetFlags(args []string) { | ||
func resetFlags(args []string, conf vttest.Config) { | ||
os.Args = args | ||
config = conf | ||
} | ||
|
||
func randomPort() int { | ||
v := rand.Int31n(20000) | ||
return int(v + 10000) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a neat trick