Skip to content

Commit

Permalink
Vtgateproxy E2E tests (#364)
Browse files Browse the repository at this point in the history
* Add end to end tests for vtgateproxy

* Fix linter warnings and unhandled errors

---------

Signed-off-by: Riley Laine <[email protected]>
  • Loading branch information
rjlaine authored May 31, 2024
1 parent af40bd9 commit d951c5e
Show file tree
Hide file tree
Showing 10 changed files with 1,271 additions and 28 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/evanphx/json-patch v4.9.0+incompatible
github.com/fsnotify/fsnotify v1.4.9
github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab
github.com/go-sql-driver/mysql v1.6.0
github.com/go-sql-driver/mysql v1.8.1
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/golang/mock v1.5.0
github.com/golang/protobuf v1.5.2
Expand Down Expand Up @@ -123,6 +123,7 @@ require (
require github.com/bndr/gotabulate v1.1.2

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/go-chi/chi/v5 v5.0.7 // indirect
github.com/rantav/go-grpc-channelz v0.0.4 // indirect
)
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RX
cloud.google.com/go/storage v1.10.0 h1:STgFzyU5/8miMl0//zKh2aQeTyeaUH3WN9bSUiJ09bA=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/AdaLogics/go-fuzz-headers v0.0.0-20211102141018-f7be0cbad29c h1:9K6I0yCgGSneuHCoIlJl0O09UjqqWduCwd+ZL1nHFWc=
github.com/AdaLogics/go-fuzz-headers v0.0.0-20211102141018-f7be0cbad29c/go.mod h1:WpB7kf89yJUETZxQnP1kgYPNwlT2jjdDYUCoxVggM3g=
github.com/Azure/azure-pipeline-go v0.2.2 h1:6oiIS9yaG6XCCzhgAgKFfIWyo4LLCiDhZot6ltoThhY=
Expand Down Expand Up @@ -282,6 +284,8 @@ github.com/go-openapi/validate v0.19.5/go.mod h1:8DJv2CVJQ6kGNpFW6eV9N3JviE1C85n
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
Expand Down
235 changes: 235 additions & 0 deletions go/test/endtoend/vtgateproxytest/failure_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
/*
Copyright 2024 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This tests select/insert using the unshared keyspace added in main_test
*/
package vtgateproxytest

import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"testing"

_ "github.com/go-sql-driver/mysql"
"github.com/stretchr/testify/assert"

"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/test/endtoend/cluster"
"vitess.io/vitess/go/test/endtoend/utils"
"vitess.io/vitess/go/vt/log"
)

func TestVtgateProxyVtgateFailureRoundRobin(t *testing.T) {
defer cluster.PanicHandler(t)

// insert test value
func() {
conn, err := mysql.Connect(context.Background(), &vtParams)
if err != nil {
t.Fatal(err)
}
defer conn.Close()

utils.Exec(t, conn, "insert into customer(id, email) values(1, 'email1')")
}()

const targetAffinity = "use1-az1"
const vtgateCount = 4
const vtgateproxyConnections = 4

vtgates, err := startAdditionalVtgates(vtgateCount)
if err != nil {
t.Fatal(err)
}
defer teardownVtgates(vtgates)

vtgateHostsFile := filepath.Join(clusterInstance.TmpDirectory, "hosts")
var config []map[string]string

for i, vtgate := range vtgates {
config = append(config, map[string]string{
"host": fmt.Sprintf("vtgate%v", i),
"address": clusterInstance.Hostname,
"grpc": strconv.Itoa(vtgate.GrpcPort),
"az_id": targetAffinity,
"type": "pool1",
})
}

b, err := json.Marshal(config)
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(vtgateHostsFile, b, 0644); err != nil {
t.Fatal(err)
}

vtgateproxyHTTPPort := clusterInstance.GetAndReservePort()
vtgateproxyGrpcPort := clusterInstance.GetAndReservePort()
vtgateproxyMySQLPort := clusterInstance.GetAndReservePort()

vtgateproxyProcInstance := NewVtgateProxyProcess(
clusterInstance.TmpDirectory,
vtgateHostsFile,
targetAffinity,
"round_robin",
vtgateproxyConnections,
vtgateproxyHTTPPort,
vtgateproxyGrpcPort,
vtgateproxyMySQLPort,
)
if err := vtgateproxyProcInstance.Setup(); err != nil {
t.Fatal(err)
}
defer vtgateproxyProcInstance.Teardown()

conn, err := vtgateproxyProcInstance.GetMySQLConn("pool1", targetAffinity)
if err != nil {
t.Fatal(err)
}
defer conn.Close()

if err := conn.Ping(); err != nil {
t.Fatal(err)
}

log.Info("Stopping 1 vtgate")
err = vtgates[0].TearDown()
if err != nil {
t.Fatal(err)
}

log.Info("Reading test value with one stopped vtgate")
for i := 0; i < vtgateproxyConnections; i++ {
result, err := selectHelper[customerEntry](context.Background(), conn, "select id, email from customer")
if err != nil {
t.Fatal(err)
}

assert.Equal(t, []customerEntry{{1, "email1"}}, result)
}
}

func TestVtgateProxyVtgateFailureFirstReady(t *testing.T) {
defer cluster.PanicHandler(t)

// insert test value
func() {
conn, err := mysql.Connect(context.Background(), &vtParams)
if err != nil {
t.Fatal(err)
}
defer conn.Close()

utils.Exec(t, conn, "insert into customer(id, email) values(1, 'email1')")
}()

const targetAffinity = "use1-az1"
const vtgateCount = 4
const vtgateproxyConnections = 4

vtgates, err := startAdditionalVtgates(vtgateCount)
if err != nil {
t.Fatal(err)
}
defer teardownVtgates(vtgates)

vtgateHostsFile := filepath.Join(clusterInstance.TmpDirectory, "hosts")
var config []map[string]string

for i, vtgate := range vtgates {
config = append(config, map[string]string{
"host": fmt.Sprintf("vtgate%v", i),
"address": clusterInstance.Hostname,
"grpc": strconv.Itoa(vtgate.GrpcPort),
"az_id": targetAffinity,
"type": "pool1",
})
}

b, err := json.Marshal(config)
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(vtgateHostsFile, b, 0644); err != nil {
t.Fatal(err)
}

vtgateproxyHTTPPort := clusterInstance.GetAndReservePort()
vtgateproxyGrpcPort := clusterInstance.GetAndReservePort()
vtgateproxyMySQLPort := clusterInstance.GetAndReservePort()

vtgateproxyProcInstance := NewVtgateProxyProcess(
clusterInstance.TmpDirectory,
vtgateHostsFile,
targetAffinity,
"first_ready",
vtgateproxyConnections,
vtgateproxyHTTPPort,
vtgateproxyGrpcPort,
vtgateproxyMySQLPort,
)
if err := vtgateproxyProcInstance.Setup(); err != nil {
t.Fatal(err)
}
defer vtgateproxyProcInstance.Teardown()

conn, err := vtgateproxyProcInstance.GetMySQLConn("pool1", targetAffinity)
if err != nil {
t.Fatal(err)
}
defer conn.Close()

if err := conn.Ping(); err != nil {
t.Fatal(err)
}

// First send some queries to the active vtgate
for i := 0; i < 10; i++ {
result, err := selectHelper[customerEntry](context.Background(), conn, "select id, email from customer")
if err != nil {
t.Fatal(err)
}

assert.Equal(t, []customerEntry{{1, "email1"}}, result)
}

// Now kill the active vtgate
for i := range vtgates {
queryCount, err := getVtgateQueryCount(vtgates[i])
if err != nil {
t.Fatal(err)
}

if queryCount.Sum() > 0 {
err = vtgates[i].TearDown()
if err != nil {
t.Fatal(err)
}
}
}

log.Info("Reading test value after killing the active vtgate")
result, err := selectHelper[customerEntry](context.Background(), conn, "select id, email from customer")
if err != nil {
t.Fatal(err)
}

assert.Equal(t, []customerEntry{{1, "email1"}}, result)
}
Loading

0 comments on commit d951c5e

Please sign in to comment.