Skip to content

Commit

Permalink
add KILL client tests (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
Denchick authored Jan 12, 2024
1 parent c7716d2 commit 539dd24
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 38 deletions.
105 changes: 67 additions & 38 deletions test/feature/features/kill.feature
Original file line number Diff line number Diff line change
@@ -1,52 +1,81 @@
Feature: Coordinator should kill client
Background:
#
# Make host "coordinator" take control
#
Feature: Kill client test

Scenario: kill client in coordinator works
Given cluster is up and running
And host "coordinator2" is stopped
And host "coordinator2" is started

When I execute SQL on host "coordinator"
"""
REGISTER ROUTER r1 ADDRESS regress_router:7000;
"""
Then command return code should be "0"
When I run SQL on host "coordinator"
"""
SHOW routers
"""
"""
REGISTER ROUTER r1 ADDRESS regress_router::7000
"""
Then command return code should be "0"
And SQL result should match regexp
"""
r1-regress_router:7000
"""
Given I execute SQL on host "router"
"""
SELECT pg_sleep(1)
"""
And I execute SQL on host "router2"
"""
SELECT pg_sleep(1)
"""

Scenario: kill client
When I run SQL on host "coordinator"
"""
show clients;
SHOW clients
"""
Then command return code should be "0"
And SQL result should not match regexp
Then we save response row "0" column "client_id"
And hide "client_id" field
Then SQL result should match json
"""
[
{
"client_id":"**IGNORE**",
"dbname":"regress",
"router_address":"regress_router:7000",
"server_id":"no backend connection",
"user":"regress"
}
]
"""
# TODO KILL client in coordinator is failing with
# ERROR: grpcConnectionIterator pop not implemented (SQLSTATE )
# When I execute SQL on host "coordinator"
# """
# KILL client {{ .client_id }}
# """
# Then command return code should be "0"
# When I run SQL on host "coordinator"
# """
# SHOW clients
# """
# Then SQL result should match json
# """
# []
# """

Scenario: kill client in router works
Given cluster is up and running
When I run SQL on host "router-admin"
"""
SHOW clients
"""
Then we save response row "0" column "client_id"
And hide "client_id" field
Then SQL result should match json
"""
clientID=$(psql "host=spqr_router_1_1 sslmode=disable user=user1 dbname=db1 port=7432" -c 'show clients;' --csv | head -2 | tail -1 | awk -F ',' '{print $1 }')
[
{
"client_id":"**IGNORE**",
"dbname":"regress",
"router_address":"local",
"router_time_0.75":"0.00ms",
"server_id":"no backend connection",
"shard_time_0.75":"0.00ms",
"user":"regress"
}
]
"""
When I run command on host "coordinator"
When I execute SQL on host "router-admin"
"""
clientID=$(psql "host=spqr_router_1_1 sslmode=disable user=user1 dbname=db1 port=7432" -c 'show clients;' --csv | head -2 | tail -1 | awk -F ',' '{print $1 }')
out=$(psql "host=spqr_router_1_1 sslmode=disable user=user1 dbname=db1 port=7432" -c "kill client $clientID;" | clearID)
test "$out" = " kill client
------------------------------------
the client ************ was killed
(1 row)"
KILL client {{ .client_id }}
"""
Then command return code should be "0"
When I run SQL on host "router-admin"
"""
SHOW clients
"""
Then SQL result should match json
"""
[]
"""
39 changes: 39 additions & 0 deletions test/feature/spqr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ func (tctx *testContext) stepSQLResultShouldMatch(matcher string, body *godog.Do

func (tctx *testContext) stepIExecuteSql(host string, body *godog.DocString) error {
query := strings.TrimSpace(body.Content)
query = strings.Replace(query, "\"", "", 2)

err := tctx.executePostgresql(host, query, struct{}{})
return err
Expand Down Expand Up @@ -670,6 +671,39 @@ func (tctx *testContext) stepFileOnHostShouldMatch(path string, node string, mat
return m(string(actualContent), expectedContent)
}

func (tctx *testContext) stepSaveResponseBodyAtPathAsJSON(rowIndex string, column string) error {
i, err := strconv.Atoi(rowIndex)
if err != nil {
return fmt.Errorf("Failed to get row index: %q not a number", rowIndex)
}
if i >= len(tctx.sqlQueryResult) {
return fmt.Errorf("Failed to get row at index %q: index is out of range", i)
}

a, b := tctx.sqlQueryResult[i][column]
if !b {
return fmt.Errorf("column does not exist")
}

actualPartByte, err := json.Marshal(a)
if err != nil {
return fmt.Errorf("Can't marshal to json: %w", err)
}

tctx.variables[column] = string(actualPartByte)
return nil
}

func (tctx *testContext) stepHideField(fieldName string) error {
for _, row := range tctx.sqlQueryResult {
_, ok := row[fieldName]
if ok {
row[fieldName] = "**IGNORE**"
}
}
return nil
}

func (tctx *testContext) stepRecordQDBTx(key string, body *godog.DocString) error {
query := strings.TrimSpace(body.Content)
var st qdb.DataTransferTransaction
Expand Down Expand Up @@ -732,6 +766,7 @@ func InitializeScenario(s *godog.ScenarioContext, t *testing.T) {
"ROUTER_CONFIG=/spqr/test/feature/conf/router.yaml",
"COORDINATOR_CONFIG=/spqr/test/feature/conf/coordinator.yaml",
}
tctx.variables = make(map[string]interface{})
return ctx, nil
})
s.StepContext().Before(func(ctx context.Context, step *godog.Step) (context.Context, error) {
Expand Down Expand Up @@ -793,6 +828,10 @@ func InitializeScenario(s *godog.ScenarioContext, t *testing.T) {
s.Step(`^SQL error on host "([^"]*)" should match (\w+)$`, tctx.stepErrorShouldMatch)
s.Step(`^file "([^"]*)" on host "([^"]*)" should match (\w+)$`, tctx.stepFileOnHostShouldMatch)

// variable manipulation
s.Step(`^we save response row "([^"]*)" column "([^"]*)"$`, tctx.stepSaveResponseBodyAtPathAsJSON)
s.Step(`^hide "([^"]*)" field$`, tctx.stepHideField)

}

func TestSpqr(t *testing.T) {
Expand Down

0 comments on commit 539dd24

Please sign in to comment.