Beispiel #1
0
func testGetSrvKeyspace(t *testing.T, conn *vtgateconn.VTGateConn) {
	want := &topodatapb.SrvKeyspace{
		Partitions: []*topodatapb.SrvKeyspace_KeyspacePartition{
			&topodatapb.SrvKeyspace_KeyspacePartition{
				ServedType: topodatapb.TabletType_REPLICA,
				ShardReferences: []*topodatapb.ShardReference{
					&topodatapb.ShardReference{
						Name: "shard0",
						KeyRange: &topodatapb.KeyRange{
							Start: []byte{0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
							End:   []byte{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
						},
					},
				},
			},
		},
		ShardingColumnName: "sharding_column_name",
		ShardingColumnType: topodatapb.KeyspaceIdType_UINT64,
		ServedFrom: []*topodatapb.SrvKeyspace_ServedFrom{
			&topodatapb.SrvKeyspace_ServedFrom{
				TabletType: topodatapb.TabletType_MASTER,
				Keyspace:   "other_keyspace",
			},
		},
		SplitShardCount: 128,
	}
	got, err := conn.GetSrvKeyspace(context.Background(), "big")
	if err != nil {
		t.Fatalf("GetSrvKeyspace error: %v", err)
	}
	if !proto.Equal(got, want) {
		t.Errorf("GetSrvKeyspace() = %v, want %v", proto.MarshalTextString(got), proto.MarshalTextString(want))
	}
}
Beispiel #2
0
func testUpdateStreamErrors(t *testing.T, conn *vtgateconn.VTGateConn) {
	ctx := context.Background()

	checkStreamExecuteErrors(t, func(query string) error {
		return getUpdateStreamError(conn.UpdateStream(ctx, query, nil, tabletType, 0, nil))
	})
}
Beispiel #3
0
func testStreamExecuteKeyspaceIdsPanic(t *testing.T, conn *vtgateconn.VTGateConn) {
	ctx := context.Background()
	execCase := execMap["request1"]
	packets, errFunc := conn.StreamExecuteKeyspaceIds(ctx, execCase.keyspaceIdQuery.Sql, execCase.keyspaceIdQuery.Keyspace, execCase.keyspaceIdQuery.KeyspaceIds, execCase.keyspaceIdQuery.BindVariables, execCase.keyspaceIdQuery.TabletType)
	if _, ok := <-packets; ok {
		t.Fatalf("Received packets instead of panic?")
	}
	err := errFunc()
	expectPanic(t, err)
}
Beispiel #4
0
func testSplitQuery(t *testing.T, conn *vtgateconn.VTGateConn) {
	ctx := context.Background()
	qsl, err := conn.SplitQuery(ctx, splitQueryRequest.Keyspace, splitQueryRequest.Query, splitQueryRequest.SplitCount)
	if err != nil {
		t.Fatalf("SplitQuery failed: %v", err)
	}
	if !reflect.DeepEqual(qsl, splitQueryResult.Splits) {
		t.Errorf("SplitQuery returned worng result: got %v wanted %v", qsl, splitQueryResult.Splits)
	}
}
Beispiel #5
0
func testEchoSplitQuery(t *testing.T, conn *vtgateconn.VTGateConn) {
	want := &pbg.SplitQueryResponse_Part{
		Query:        tproto.BoundQueryToProto3(echoPrefix+query+":split_column:123", bindVars),
		KeyRangePart: &pbg.SplitQueryResponse_KeyRangePart{Keyspace: keyspace},
	}
	got, err := conn.SplitQuery(context.Background(), keyspace, echoPrefix+query, bindVars, "split_column", 123)
	if err != nil {
		t.Fatalf("SplitQuery error: %v", err)
	}
	// For some reason, proto.Equal() is calling them unequal even though no diffs
	// are found.
	gotstr, wantstr := got[0].String(), want.String()
	if gotstr != wantstr {
		t.Errorf("SplitQuery() = %v, want %v", gotstr, wantstr)
	}
}
Beispiel #6
0
func testEchoUpdateStream(t *testing.T, conn *vtgateconn.VTGateConn) {
	var stream vtgateconn.UpdateStreamReader
	var err error

	ctx := callerid.NewContext(context.Background(), callerID, nil)

	stream, err = conn.UpdateStream(ctx, echoPrefix+query, keyRanges[0], tabletType, 0, eventToken)
	if err != nil {
		t.Fatal(err)
	}
	se, _, err := stream.Recv()
	if err != nil {
		t.Fatal(err)
	}
	if se.EventToken.Position != updateStreamEcho {
		t.Errorf("UpdateStream(0) = %v, want %v", se.EventToken.Position, updateStreamEcho)
	}
}
Beispiel #7
0
// testCallerID adds a caller ID to a context, and makes sure the server
// gets it.
func testCallerID(t *testing.T, conn *vtgateconn.VTGateConn) {
	t.Log("testCallerID")
	ctx := context.Background()
	callerID := callerid.NewEffectiveCallerID("test_principal", "test_component", "test_subcomponent")
	ctx = callerid.NewContext(ctx, callerID, nil)

	data, err := json.Marshal(callerID)
	if err != nil {
		t.Errorf("failed to marshal callerid: %v", err)
		return
	}
	query := services.CallerIDPrefix + string(data)

	// test Execute forwards the callerID
	if _, err := conn.Execute(ctx, query, nil, pb.TabletType_MASTER); err != nil {
		t.Errorf("failed to pass callerid: %v", err)
	}

	// FIXME(alainjobart) add all function calls
}
Beispiel #8
0
func testTxPassNotInTransaction(t *testing.T, conn *vtgateconn.VTGateConn) {
	ctx := context.Background()
	execCase := execMap["txRequestNIT"]

	tx, err := conn.Begin(ctx)
	if err != nil {
		t.Error(err)
	}
	_, err = tx.Execute(ctx, execCase.execQuery.Sql, execCase.execQuery.BindVariables, execCase.execQuery.TabletType, true)
	if err != nil {
		t.Error(err)
	}
	_, err = tx.ExecuteShard(ctx, execCase.shardQuery.Sql, execCase.shardQuery.Keyspace, execCase.shardQuery.Shards, execCase.shardQuery.BindVariables, execCase.shardQuery.TabletType, true)
	if err != nil {
		t.Error(err)
	}
	_, err = tx.ExecuteKeyspaceIds(ctx, execCase.keyspaceIdQuery.Sql, execCase.keyspaceIdQuery.Keyspace, execCase.keyspaceIdQuery.KeyspaceIds, execCase.keyspaceIdQuery.BindVariables, execCase.keyspaceIdQuery.TabletType, true)
	if err != nil {
		t.Error(err)
	}
	_, err = tx.ExecuteKeyRanges(ctx, execCase.keyRangeQuery.Sql, execCase.keyRangeQuery.Keyspace, execCase.keyRangeQuery.KeyRanges, execCase.keyRangeQuery.BindVariables, execCase.keyRangeQuery.TabletType, true)
	if err != nil {
		t.Error(err)
	}
	_, err = tx.ExecuteEntityIds(ctx, execCase.entityIdsQuery.Sql, execCase.entityIdsQuery.Keyspace, execCase.entityIdsQuery.EntityColumnName, execCase.entityIdsQuery.EntityKeyspaceIDs, execCase.entityIdsQuery.BindVariables, execCase.entityIdsQuery.TabletType, true)
	if err != nil {
		t.Error(err)
	}
	_, err = tx.ExecuteBatchShard(ctx, execCase.batchQueryShard.Queries, execCase.batchQueryShard.Keyspace, execCase.batchQueryShard.Shards, execCase.batchQueryShard.TabletType, true)
	if err != nil {
		t.Error(err)
	}
	_, err = tx.ExecuteBatchKeyspaceIds(ctx, execCase.keyspaceIdBatchQuery.Queries, execCase.keyspaceIdBatchQuery.Keyspace, execCase.keyspaceIdBatchQuery.KeyspaceIds, execCase.keyspaceIdBatchQuery.TabletType, true)
	if err != nil {
		t.Error(err)
	}
	// no rollback necessary
}
Beispiel #9
0
func testStreamExecuteErrors(t *testing.T, conn *vtgateconn.VTGateConn) {
	ctx := context.Background()

	checkStreamExecuteErrors(t, func(query string) error {
		return getStreamError(conn.StreamExecute(ctx, query, bindVars, tabletType))
	})
	checkStreamExecuteErrors(t, func(query string) error {
		return getStreamError(conn.StreamExecuteShards(ctx, query, keyspace, shards, bindVars, tabletType))
	})
	checkStreamExecuteErrors(t, func(query string) error {
		return getStreamError(conn.StreamExecuteKeyspaceIds(ctx, query, keyspace, keyspaceIDs, bindVars, tabletType))
	})
	checkStreamExecuteErrors(t, func(query string) error {
		return getStreamError(conn.StreamExecuteKeyRanges(ctx, query, keyspace, keyRanges, bindVars, tabletType))
	})
}
Beispiel #10
0
func testEchoStreamExecute(t *testing.T, conn *vtgateconn.VTGateConn) {
	var qrc <-chan *mproto.QueryResult
	var err error

	ctx := callerid.NewContext(context.Background(), callerID, nil)

	qrc, _, err = conn.StreamExecute(ctx, echoPrefix+query, bindVars, tabletType)
	checkEcho(t, "StreamExecute", <-qrc, err, map[string]string{
		"callerId":   callerIDEcho,
		"query":      echoPrefix + query,
		"bindVars":   bindVarsEcho,
		"tabletType": tabletTypeEcho,
	})

	qrc, _, err = conn.StreamExecuteShards(ctx, echoPrefix+query, keyspace, shards, bindVars, tabletType)
	checkEcho(t, "StreamExecuteShards", <-qrc, err, map[string]string{
		"callerId":   callerIDEcho,
		"query":      echoPrefix + query,
		"keyspace":   keyspace,
		"shards":     shardsEcho,
		"bindVars":   bindVarsEcho,
		"tabletType": tabletTypeEcho,
	})

	qrc, _, err = conn.StreamExecuteKeyspaceIds(ctx, echoPrefix+query, keyspace, keyspaceIDs, bindVars, tabletType)
	checkEcho(t, "StreamExecuteKeyspaceIds", <-qrc, err, map[string]string{
		"callerId":    callerIDEcho,
		"query":       echoPrefix + query,
		"keyspace":    keyspace,
		"keyspaceIds": keyspaceIDsEcho,
		"bindVars":    bindVarsEcho,
		"tabletType":  tabletTypeEcho,
	})

	qrc, _, err = conn.StreamExecuteKeyRanges(ctx, echoPrefix+query, keyspace, keyRanges, bindVars, tabletType)
	checkEcho(t, "StreamExecuteKeyRanges", <-qrc, err, map[string]string{
		"callerId":   callerIDEcho,
		"query":      echoPrefix + query,
		"keyspace":   keyspace,
		"keyRanges":  keyRangesEcho,
		"bindVars":   bindVarsEcho,
		"tabletType": tabletTypeEcho,
	})
}
Beispiel #11
0
func testStreamExecuteKeyspaceIds(t *testing.T, conn *vtgateconn.VTGateConn) {
	ctx := context.Background()
	execCase := execMap["request1"]
	packets, errFunc := conn.StreamExecuteKeyspaceIds(ctx, execCase.keyspaceIdQuery.Sql, execCase.keyspaceIdQuery.Keyspace, execCase.keyspaceIdQuery.KeyspaceIds, execCase.keyspaceIdQuery.BindVariables, execCase.keyspaceIdQuery.TabletType)
	var qr mproto.QueryResult
	for packet := range packets {
		if len(packet.Fields) != 0 {
			qr.Fields = packet.Fields
		}
		if len(packet.Rows) != 0 {
			qr.Rows = append(qr.Rows, packet.Rows...)
		}
	}
	wantResult := *execCase.reply.Result
	wantResult.RowsAffected = 0
	wantResult.InsertId = 0
	if !reflect.DeepEqual(qr, wantResult) {
		t.Errorf("Unexpected result from Execute: got %+v want %+v", qr, wantResult)
	}
	err := errFunc()
	if err != nil {
		t.Error(err)
	}

	packets, errFunc = conn.StreamExecuteKeyspaceIds(ctx, "none", "", []key.KeyspaceId{}, nil, "")
	for packet := range packets {
		t.Errorf("packet: %+v, want none", packet)
	}
	err = errFunc()
	want := "no match for: none"
	if err == nil || !strings.Contains(err.Error(), want) {
		t.Errorf("none request: %v, want %v", err, want)
	}

	execCase = execMap["errorRequst"]
	packets, errFunc = conn.StreamExecuteKeyspaceIds(ctx, execCase.keyspaceIdQuery.Sql, execCase.keyspaceIdQuery.Keyspace, execCase.keyspaceIdQuery.KeyspaceIds, execCase.keyspaceIdQuery.BindVariables, execCase.keyspaceIdQuery.TabletType)
	for packet := range packets {
		t.Errorf("packet: %+v, want none", packet)
	}
	err = errFunc()
	want = "app error"
	if err == nil || !strings.Contains(err.Error(), want) {
		t.Errorf("errorRequst: %v, want %v", err, want)
	}
}
Beispiel #12
0
func testExecuteShard(t *testing.T, conn *vtgateconn.VTGateConn) {
	ctx := context.Background()
	execCase := execMap["request1"]
	qr, err := conn.ExecuteShard(ctx, execCase.shardQuery.Sql, execCase.shardQuery.Keyspace, execCase.shardQuery.Shards, execCase.shardQuery.BindVariables, execCase.shardQuery.TabletType)
	if err != nil {
		t.Error(err)
	}
	if !reflect.DeepEqual(qr, execCase.reply.Result) {
		t.Errorf("Unexpected result from Execute: got %+v want %+v", qr, execCase.reply.Result)
	}

	_, err = conn.ExecuteShard(ctx, "none", "", []string{}, nil, "")
	want := "no match for: none"
	if err == nil || !strings.Contains(err.Error(), want) {
		t.Errorf("none request: %v, want %v", err, want)
	}

	_, err = conn.ExecuteShard(ctx, "errorRequst", "", []string{}, nil, "")
	want = "app error"
	if err == nil || err.Error() != want {
		t.Errorf("errorRequst: %v, want %v", err, want)
	}
}
Beispiel #13
0
func checkTransactionExecuteErrors(t *testing.T, conn *vtgateconn.VTGateConn, execute func(tx *vtgateconn.VTGateTx, query string) error) {
	ctx := context.Background()

	for errStr, errCode := range executeErrors {
		query := errorPrefix + errStr
		tx, err := conn.Begin(ctx)
		if err != nil {
			t.Errorf("[%v] Begin error: %v", query, err)
		}
		checkError(t, execute(tx, query), query, errStr, errCode)

		// Partial error where server doesn't close the session.
		query = partialErrorPrefix + errStr
		tx, err = conn.Begin(ctx)
		if err != nil {
			t.Errorf("[%v] Begin error: %v", query, err)
		}
		checkError(t, execute(tx, query), query, errStr, errCode)
		// The transaction should still be usable now.
		if err := tx.Rollback(ctx); err != nil {
			t.Errorf("[%v] Rollback error: %v", query, err)
		}

		// Partial error where server closes the session.
		tx, err = conn.Begin(ctx)
		if err != nil {
			t.Errorf("[%v] Begin error: %v", query, err)
		}
		query = partialErrorPrefix + errStr + "/close transaction"
		checkError(t, execute(tx, query), query, errStr, errCode)
		// The transaction should be unusable now.
		if tx.Rollback(ctx) == nil {
			t.Errorf("[%v] expected Rollback error, got nil", query)
		}
	}
}
Beispiel #14
0
func testExecuteBatchKeyspaceIds(t *testing.T, conn *vtgateconn.VTGateConn) {
	ctx := context.Background()
	execCase := execMap["request1"]
	ql, err := conn.ExecuteBatchKeyspaceIds(ctx, execCase.keyspaceIdBatchQuery.Queries, execCase.keyspaceIdBatchQuery.Keyspace, execCase.keyspaceIdBatchQuery.KeyspaceIds, execCase.keyspaceIdBatchQuery.TabletType)
	if err != nil {
		t.Error(err)
	}
	if !reflect.DeepEqual(&ql[0], execCase.reply.Result) {
		t.Errorf("Unexpected result from Execute: got %+v want %+v", ql, execCase.reply.Result)
	}

	_, err = conn.ExecuteBatchKeyspaceIds(ctx, []tproto.BoundQuery{tproto.BoundQuery{Sql: "none"}}, "", []key.KeyspaceId{}, "")
	want := "no match for: none"
	if err == nil || !strings.Contains(err.Error(), want) {
		t.Errorf("none request: %v, want %v", err, want)
	}

	execCase = execMap["errorRequst"]
	_, err = conn.ExecuteBatchKeyspaceIds(ctx, execCase.keyspaceIdBatchQuery.Queries, execCase.keyspaceIdBatchQuery.Keyspace, execCase.keyspaceIdBatchQuery.KeyspaceIds, execCase.keyspaceIdBatchQuery.TabletType)
	want = "app error"
	if err == nil || err.Error() != want {
		t.Errorf("errorRequst: %v, want %v", err, want)
	}
}
Beispiel #15
0
// testCallerID adds a caller ID to a context, and makes sure the server
// gets it.
func testCallerID(t *testing.T, conn *vtgateconn.VTGateConn) {
	t.Log("testCallerID")
	ctx := context.Background()
	callerID := callerid.NewEffectiveCallerID("test_principal", "test_component", "test_subcomponent")
	ctx = callerid.NewContext(ctx, callerID, nil)

	data, err := json.Marshal(callerID)
	if err != nil {
		t.Errorf("failed to marshal callerid: %v", err)
		return
	}
	query := services.CallerIDPrefix + string(data)

	// test Execute calls forward the callerID
	_, err = conn.Execute(ctx, query, nil, topodatapb.TabletType_MASTER, nil)
	checkCallerIDError(t, "Execute", err)

	_, err = conn.ExecuteShards(ctx, query, "", nil, nil, topodatapb.TabletType_MASTER, nil)
	checkCallerIDError(t, "ExecuteShards", err)

	_, err = conn.ExecuteKeyspaceIds(ctx, query, "", nil, nil, topodatapb.TabletType_MASTER, nil)
	checkCallerIDError(t, "ExecuteKeyspaceIds", err)

	_, err = conn.ExecuteKeyRanges(ctx, query, "", nil, nil, topodatapb.TabletType_MASTER, nil)
	checkCallerIDError(t, "ExecuteKeyRanges", err)

	_, err = conn.ExecuteEntityIds(ctx, query, "", "", nil, nil, topodatapb.TabletType_MASTER, nil)
	checkCallerIDError(t, "ExecuteEntityIds", err)

	// test ExecuteBatch calls forward the callerID
	_, err = conn.ExecuteBatchShards(ctx, []*vtgatepb.BoundShardQuery{
		{
			Query: &querypb.BoundQuery{
				Sql: query,
			},
		},
	}, topodatapb.TabletType_MASTER, false, nil)
	checkCallerIDError(t, "ExecuteBatchShards", err)

	_, err = conn.ExecuteBatchKeyspaceIds(ctx, []*vtgatepb.BoundKeyspaceIdQuery{
		{
			Query: &querypb.BoundQuery{
				Sql: query,
			},
		},
	}, topodatapb.TabletType_MASTER, false, nil)
	checkCallerIDError(t, "ExecuteBatchKeyspaceIds", err)

	// test StreamExecute calls forward the callerID
	err = getStreamError(conn.StreamExecute(ctx, query, nil, topodatapb.TabletType_MASTER, nil))
	checkCallerIDError(t, "StreamExecute", err)

	err = getStreamError(conn.StreamExecuteShards(ctx, query, "", nil, nil, topodatapb.TabletType_MASTER, nil))
	checkCallerIDError(t, "StreamExecuteShards", err)

	err = getStreamError(conn.StreamExecuteKeyspaceIds(ctx, query, "", nil, nil, topodatapb.TabletType_MASTER, nil))
	checkCallerIDError(t, "StreamExecuteKeyspaceIds", err)

	err = getStreamError(conn.StreamExecuteKeyRanges(ctx, query, "", nil, nil, topodatapb.TabletType_MASTER, nil))
	checkCallerIDError(t, "StreamExecuteKeyRanges", err)

	// test UpdateStream forwards the callerID
	err = getUpdateStreamError(conn.UpdateStream(ctx, query, nil, topodatapb.TabletType_MASTER, 0, nil))
	checkCallerIDError(t, "UpdateStream", err)
}
Beispiel #16
0
func testEchoExecute(t *testing.T, conn *vtgateconn.VTGateConn) {
	var qr *mproto.QueryResult
	var err error

	ctx := callerid.NewContext(context.Background(), callerID, nil)

	qr, err = conn.Execute(ctx, echoPrefix+query, bindVars, tabletType)
	checkEcho(t, "Execute", qr, err, map[string]string{
		"callerId":   callerIDEcho,
		"query":      echoPrefix + query,
		"bindVars":   bindVarsEcho,
		"tabletType": tabletTypeEcho,
	})

	qr, err = conn.ExecuteShards(ctx, echoPrefix+query, keyspace, shards, bindVars, tabletType)
	checkEcho(t, "ExecuteShards", qr, err, map[string]string{
		"callerId":   callerIDEcho,
		"query":      echoPrefix + query,
		"keyspace":   keyspace,
		"shards":     shardsEcho,
		"bindVars":   bindVarsEcho,
		"tabletType": tabletTypeEcho,
	})

	qr, err = conn.ExecuteKeyspaceIds(ctx, echoPrefix+query, keyspace, keyspaceIDs, bindVars, tabletType)
	checkEcho(t, "ExecuteKeyspaceIds", qr, err, map[string]string{
		"callerId":    callerIDEcho,
		"query":       echoPrefix + query,
		"keyspace":    keyspace,
		"keyspaceIds": keyspaceIDsEcho,
		"bindVars":    bindVarsEcho,
		"tabletType":  tabletTypeEcho,
	})

	qr, err = conn.ExecuteKeyRanges(ctx, echoPrefix+query, keyspace, keyRanges, bindVars, tabletType)
	checkEcho(t, "ExecuteKeyRanges", qr, err, map[string]string{
		"callerId":   callerIDEcho,
		"query":      echoPrefix + query,
		"keyspace":   keyspace,
		"keyRanges":  keyRangesEcho,
		"bindVars":   bindVarsEcho,
		"tabletType": tabletTypeEcho,
	})

	qr, err = conn.ExecuteEntityIds(ctx, echoPrefix+query, keyspace, "column1", entityKeyspaceIDs, bindVars, tabletType)
	checkEcho(t, "ExecuteEntityIds", qr, err, map[string]string{
		"callerId":         callerIDEcho,
		"query":            echoPrefix + query,
		"keyspace":         keyspace,
		"entityColumnName": "column1",
		"entityIds":        entityKeyspaceIDsEcho,
		"bindVars":         bindVarsEcho,
		"tabletType":       tabletTypeEcho,
	})

	var qrs []mproto.QueryResult

	qrs, err = conn.ExecuteBatchShards(ctx, []gproto.BoundShardQuery{
		gproto.BoundShardQuery{
			Sql:           echoPrefix + query,
			Keyspace:      keyspace,
			Shards:        shards,
			BindVariables: bindVars,
		},
	}, tabletType, true)
	checkEcho(t, "ExecuteBatchShards", &qrs[0], err, map[string]string{
		"callerId":      callerIDEcho,
		"query":         echoPrefix + query,
		"keyspace":      keyspace,
		"shards":        shardsEcho,
		"bindVars":      bindVarsEcho,
		"tabletType":    tabletTypeEcho,
		"asTransaction": "true",
	})

	qrs, err = conn.ExecuteBatchKeyspaceIds(ctx, []gproto.BoundKeyspaceIdQuery{
		gproto.BoundKeyspaceIdQuery{
			Sql:           echoPrefix + query,
			Keyspace:      keyspace,
			KeyspaceIds:   keyspaceIDs,
			BindVariables: bindVars,
		},
	}, tabletType, true)
	checkEcho(t, "ExecuteBatchKeyspaceIds", &qrs[0], err, map[string]string{
		"callerId":      callerIDEcho,
		"query":         echoPrefix + query,
		"keyspace":      keyspace,
		"keyspaceIds":   keyspaceIDsEcho,
		"bindVars":      bindVarsEcho,
		"tabletType":    tabletTypeEcho,
		"asTransaction": "true",
	})
}
Beispiel #17
0
func testEchoTransactionExecute(t *testing.T, conn *vtgateconn.VTGateConn) {
	var qr *sqltypes.Result
	var err error

	ctx := callerid.NewContext(context.Background(), callerID, nil)

	tx, err := conn.Begin(ctx)
	if err != nil {
		t.Fatalf("Begin error: %v", err)
	}

	qr, err = tx.Execute(ctx, echoPrefix+query, bindVars, tabletType, true)
	checkEcho(t, "Execute", qr, err, map[string]string{
		"callerId":         callerIDEcho,
		"query":            echoPrefix + query,
		"bindVars":         bindVarsEcho,
		"tabletType":       tabletTypeEcho,
		"session":          sessionEcho,
		"notInTransaction": "true",
	})

	qr, err = tx.ExecuteShards(ctx, echoPrefix+query, keyspace, shards, bindVars, tabletType, true)
	checkEcho(t, "ExecuteShards", qr, err, map[string]string{
		"callerId":         callerIDEcho,
		"query":            echoPrefix + query,
		"keyspace":         keyspace,
		"shards":           shardsEcho,
		"bindVars":         bindVarsEcho,
		"tabletType":       tabletTypeEcho,
		"session":          sessionEcho,
		"notInTransaction": "true",
	})

	qr, err = tx.ExecuteKeyspaceIds(ctx, echoPrefix+query, keyspace, keyspaceIDs, bindVars, tabletType, true)
	checkEcho(t, "ExecuteKeyspaceIds", qr, err, map[string]string{
		"callerId":         callerIDEcho,
		"query":            echoPrefix + query,
		"keyspace":         keyspace,
		"keyspaceIds":      keyspaceIDsEcho,
		"bindVars":         bindVarsEcho,
		"tabletType":       tabletTypeEcho,
		"session":          sessionEcho,
		"notInTransaction": "true",
	})

	qr, err = tx.ExecuteKeyRanges(ctx, echoPrefix+query, keyspace, keyRanges, bindVars, tabletType, true)
	checkEcho(t, "ExecuteKeyRanges", qr, err, map[string]string{
		"callerId":         callerIDEcho,
		"query":            echoPrefix + query,
		"keyspace":         keyspace,
		"keyRanges":        keyRangesEcho,
		"bindVars":         bindVarsEcho,
		"tabletType":       tabletTypeEcho,
		"session":          sessionEcho,
		"notInTransaction": "true",
	})

	qr, err = tx.ExecuteEntityIds(ctx, echoPrefix+query, keyspace, "column1", entityKeyspaceIDs, bindVars, tabletType, true)
	checkEcho(t, "ExecuteEntityIds", qr, err, map[string]string{
		"callerId":         callerIDEcho,
		"query":            echoPrefix + query,
		"keyspace":         keyspace,
		"entityColumnName": "column1",
		"entityIds":        entityKeyspaceIDsEcho,
		"bindVars":         bindVarsEcho,
		"tabletType":       tabletTypeEcho,
		"session":          sessionEcho,
		"notInTransaction": "true",
	})

	if err := tx.Rollback(ctx); err != nil {
		t.Fatalf("Rollback error: %v", err)
	}
	tx, err = conn.Begin(ctx)
	if err != nil {
		t.Fatalf("Begin (again) error: %v", err)
	}

	var qrs []sqltypes.Result

	qrs, err = tx.ExecuteBatchShards(ctx, []gproto.BoundShardQuery{
		gproto.BoundShardQuery{
			Sql:           echoPrefix + query,
			Keyspace:      keyspace,
			Shards:        shards,
			BindVariables: bindVars,
		},
	}, tabletType, true)
	checkEcho(t, "ExecuteBatchShards", &qrs[0], err, map[string]string{
		"callerId":      callerIDEcho,
		"query":         echoPrefix + query,
		"keyspace":      keyspace,
		"shards":        shardsEcho,
		"bindVars":      bindVarsEcho,
		"tabletType":    tabletTypeEcho,
		"session":       sessionEcho,
		"asTransaction": "true",
	})

	qrs, err = tx.ExecuteBatchKeyspaceIds(ctx, []gproto.BoundKeyspaceIdQuery{
		gproto.BoundKeyspaceIdQuery{
			Sql:           echoPrefix + query,
			Keyspace:      keyspace,
			KeyspaceIds:   keyspaceIDs,
			BindVariables: bindVars,
		},
	}, tabletType, true)
	checkEcho(t, "ExecuteBatchKeyspaceIds", &qrs[0], err, map[string]string{
		"callerId":      callerIDEcho,
		"query":         echoPrefix + query,
		"keyspace":      keyspace,
		"keyspaceIds":   keyspaceIDsEcho,
		"bindVars":      bindVarsEcho,
		"tabletType":    tabletTypeEcho,
		"session":       sessionEcho,
		"asTransaction": "true",
	})
}
Beispiel #18
0
func testTxFail(t *testing.T, conn *vtgateconn.VTGateConn) {
	ctx := context.Background()
	tx, err := conn.Begin(ctx)
	if err != nil {
		t.Error(err)
	}
	err = tx.Commit(ctx)
	want := "commit: session mismatch"
	if err == nil || !strings.Contains(err.Error(), want) {
		t.Errorf("Commit: %v, want %v", err, want)
	}

	_, err = tx.Execute(ctx, "", nil, "", false)
	want = "execute: not in transaction"
	if err == nil || err.Error() != want {
		t.Errorf("Execute: %v, want %v", err, want)
	}

	_, err = tx.ExecuteShard(ctx, "", "", nil, nil, "", false)
	want = "executeShard: not in transaction"
	if err == nil || err.Error() != want {
		t.Errorf("ExecuteShard: %v, want %v", err, want)
	}

	_, err = tx.ExecuteKeyspaceIds(ctx, "", "", nil, nil, "", false)
	want = "executeKeyspaceIds: not in transaction"
	if err == nil || err.Error() != want {
		t.Errorf("ExecuteShard: %v, want %v", err, want)
	}

	_, err = tx.ExecuteKeyRanges(ctx, "", "", nil, nil, "", false)
	want = "executeKeyRanges: not in transaction"
	if err == nil || err.Error() != want {
		t.Errorf("ExecuteShard: %v, want %v", err, want)
	}

	_, err = tx.ExecuteEntityIds(ctx, "", "", "", nil, nil, "", false)
	want = "executeEntityIds: not in transaction"
	if err == nil || err.Error() != want {
		t.Errorf("ExecuteShard: %v, want %v", err, want)
	}

	_, err = tx.ExecuteBatchShard(ctx, nil, "", nil, "", false)
	want = "executeBatchShard: not in transaction"
	if err == nil || err.Error() != want {
		t.Errorf("ExecuteShard: %v, want %v", err, want)
	}

	_, err = tx.ExecuteBatchKeyspaceIds(ctx, nil, "", nil, "", false)
	want = "executeBatchKeyspaceIds: not in transaction"
	if err == nil || err.Error() != want {
		t.Errorf("ExecuteShard: %v, want %v", err, want)
	}

	err = tx.Commit(ctx)
	want = "commit: not in transaction"
	if err == nil || err.Error() != want {
		t.Errorf("Commit: %v, want %v", err, want)
	}

	err = tx.Rollback(ctx)
	if err != nil {
		t.Error(err)
	}

	tx, err = conn.Begin(ctx)
	if err != nil {
		t.Error(err)
	}
	err = tx.Rollback(ctx)
	want = "rollback: session mismatch"
	if err == nil || !strings.Contains(err.Error(), want) {
		t.Errorf("Rollback: %v, want %v", err, want)
	}
}
Beispiel #19
0
func testExecuteKeyRangesPanic(t *testing.T, conn *vtgateconn.VTGateConn) {
	ctx := context.Background()
	execCase := execMap["request1"]
	_, err := conn.ExecuteKeyRanges(ctx, execCase.keyRangeQuery.Sql, execCase.keyRangeQuery.Keyspace, execCase.keyRangeQuery.KeyRanges, execCase.keyRangeQuery.BindVariables, execCase.keyRangeQuery.TabletType)
	expectPanic(t, err)
}
Beispiel #20
0
func testEchoStreamExecute(t *testing.T, conn *vtgateconn.VTGateConn) {
	var stream sqltypes.ResultStream
	var err error
	var qr *sqltypes.Result

	ctx := callerid.NewContext(context.Background(), callerID, nil)

	stream, err = conn.StreamExecute(ctx, echoPrefix+query, bindVars, tabletType)
	if err != nil {
		t.Fatal(err)
	}
	qr, err = stream.Recv()
	checkEcho(t, "StreamExecute", qr, err, map[string]string{
		"callerId":   callerIDEcho,
		"query":      echoPrefix + query,
		"bindVars":   bindVarsEcho,
		"tabletType": tabletTypeEcho,
	})

	stream, err = conn.StreamExecuteShards(ctx, echoPrefix+query, keyspace, shards, bindVars, tabletType)
	if err != nil {
		t.Fatal(err)
	}
	qr, err = stream.Recv()
	checkEcho(t, "StreamExecuteShards", qr, err, map[string]string{
		"callerId":   callerIDEcho,
		"query":      echoPrefix + query,
		"keyspace":   keyspace,
		"shards":     shardsEcho,
		"bindVars":   bindVarsEcho,
		"tabletType": tabletTypeEcho,
	})

	stream, err = conn.StreamExecuteKeyspaceIds(ctx, echoPrefix+query, keyspace, keyspaceIDs, bindVars, tabletType)
	if err != nil {
		t.Fatal(err)
	}
	qr, err = stream.Recv()
	checkEcho(t, "StreamExecuteKeyspaceIds", qr, err, map[string]string{
		"callerId":    callerIDEcho,
		"query":       echoPrefix + query,
		"keyspace":    keyspace,
		"keyspaceIds": keyspaceIDsEcho,
		"bindVars":    bindVarsEcho,
		"tabletType":  tabletTypeEcho,
	})

	stream, err = conn.StreamExecuteKeyRanges(ctx, echoPrefix+query, keyspace, keyRanges, bindVars, tabletType)
	if err != nil {
		t.Fatal(err)
	}
	qr, err = stream.Recv()
	checkEcho(t, "StreamExecuteKeyRanges", qr, err, map[string]string{
		"callerId":   callerIDEcho,
		"query":      echoPrefix + query,
		"keyspace":   keyspace,
		"keyRanges":  keyRangesEcho,
		"bindVars":   bindVarsEcho,
		"tabletType": tabletTypeEcho,
	})
}
Beispiel #21
0
func testTxPass(t *testing.T, conn *vtgateconn.VTGateConn) {
	ctx := context.Background()
	execCase := execMap["txRequest"]

	// Execute
	tx, err := conn.Begin(ctx)
	if err != nil {
		t.Error(err)
	}
	_, err = tx.Execute(ctx, execCase.execQuery.Sql, execCase.execQuery.BindVariables, execCase.execQuery.TabletType, false)
	if err != nil {
		t.Error(err)
	}
	err = tx.Commit(ctx)
	if err != nil {
		t.Error(err)
	}

	// ExecuteShard
	tx, err = conn.Begin(ctx)
	if err != nil {
		t.Error(err)
	}
	_, err = tx.ExecuteShard(ctx, execCase.shardQuery.Sql, execCase.shardQuery.Keyspace, execCase.shardQuery.Shards, execCase.shardQuery.BindVariables, execCase.shardQuery.TabletType, false)
	if err != nil {
		t.Error(err)
	}
	err = tx.Rollback(ctx)
	if err != nil {
		t.Error(err)
	}

	// ExecuteKeyspaceIds
	tx, err = conn.Begin(ctx)
	if err != nil {
		t.Error(err)
	}
	_, err = tx.ExecuteKeyspaceIds(ctx, execCase.keyspaceIdQuery.Sql, execCase.keyspaceIdQuery.Keyspace, execCase.keyspaceIdQuery.KeyspaceIds, execCase.keyspaceIdQuery.BindVariables, execCase.keyspaceIdQuery.TabletType, false)
	if err != nil {
		t.Error(err)
	}
	err = tx.Rollback(ctx)
	if err != nil {
		t.Error(err)
	}

	// ExecuteKeyRanges
	tx, err = conn.Begin(ctx)
	if err != nil {
		t.Error(err)
	}
	_, err = tx.ExecuteKeyRanges(ctx, execCase.keyRangeQuery.Sql, execCase.keyRangeQuery.Keyspace, execCase.keyRangeQuery.KeyRanges, execCase.keyRangeQuery.BindVariables, execCase.keyRangeQuery.TabletType, false)
	if err != nil {
		t.Error(err)
	}
	err = tx.Rollback(ctx)
	if err != nil {
		t.Error(err)
	}

	// ExecuteEntityIds
	tx, err = conn.Begin(ctx)
	if err != nil {
		t.Error(err)
	}
	_, err = tx.ExecuteEntityIds(ctx, execCase.entityIdsQuery.Sql, execCase.entityIdsQuery.Keyspace, execCase.entityIdsQuery.EntityColumnName, execCase.entityIdsQuery.EntityKeyspaceIDs, execCase.entityIdsQuery.BindVariables, execCase.entityIdsQuery.TabletType, false)
	if err != nil {
		t.Error(err)
	}
	err = tx.Rollback(ctx)
	if err != nil {
		t.Error(err)
	}

	// ExecuteBatchShard
	tx, err = conn.Begin(ctx)
	if err != nil {
		t.Error(err)
	}
	_, err = tx.ExecuteBatchShard(ctx, execCase.batchQueryShard.Queries, execCase.batchQueryShard.Keyspace, execCase.batchQueryShard.Shards, execCase.batchQueryShard.TabletType, false)
	if err != nil {
		t.Error(err)
	}
	err = tx.Rollback(ctx)
	if err != nil {
		t.Error(err)
	}

	// ExecuteBatchKeyspaceIds
	tx, err = conn.Begin(ctx)
	if err != nil {
		t.Error(err)
	}
	_, err = tx.ExecuteBatchKeyspaceIds(ctx, execCase.keyspaceIdBatchQuery.Queries, execCase.keyspaceIdBatchQuery.Keyspace, execCase.keyspaceIdBatchQuery.KeyspaceIds, execCase.keyspaceIdBatchQuery.TabletType, false)
	if err != nil {
		t.Error(err)
	}
	err = tx.Rollback(ctx)
	if err != nil {
		t.Error(err)
	}
}
Beispiel #22
0
func testSplitQueryPanic(t *testing.T, conn *vtgateconn.VTGateConn) {
	ctx := context.Background()
	_, err := conn.SplitQuery(ctx, splitQueryRequest.Keyspace, splitQueryRequest.Query, splitQueryRequest.SplitCount)
	expectPanic(t, err)
}
Beispiel #23
0
func testExecuteEntityIdsPanic(t *testing.T, conn *vtgateconn.VTGateConn) {
	ctx := context.Background()
	execCase := execMap["request1"]
	_, err := conn.ExecuteEntityIds(ctx, execCase.entityIdsQuery.Sql, execCase.entityIdsQuery.Keyspace, execCase.entityIdsQuery.EntityColumnName, execCase.entityIdsQuery.EntityKeyspaceIDs, execCase.entityIdsQuery.BindVariables, execCase.entityIdsQuery.TabletType)
	expectPanic(t, err)
}
Beispiel #24
0
func testExecuteErrors(t *testing.T, conn *vtgateconn.VTGateConn) {
	ctx := context.Background()

	checkExecuteErrors(t, func(query string) error {
		_, err := conn.Execute(ctx, query, bindVars, tabletType)
		return err
	})
	checkExecuteErrors(t, func(query string) error {
		_, err := conn.ExecuteShards(ctx, query, keyspace, shards, bindVars, tabletType)
		return err
	})
	checkExecuteErrors(t, func(query string) error {
		_, err := conn.ExecuteKeyspaceIds(ctx, query, keyspace, keyspaceIDs, bindVars, tabletType)
		return err
	})
	checkExecuteErrors(t, func(query string) error {
		_, err := conn.ExecuteKeyRanges(ctx, query, keyspace, keyRanges, bindVars, tabletType)
		return err
	})
	checkExecuteErrors(t, func(query string) error {
		_, err := conn.ExecuteEntityIds(ctx, query, keyspace, "column1", entityKeyspaceIDs, bindVars, tabletType)
		return err
	})
	checkExecuteErrors(t, func(query string) error {
		_, err := conn.ExecuteBatchShards(ctx, []*vtgatepb.BoundShardQuery{
			{
				Query: &querypb.BoundQuery{
					Sql:           query,
					BindVariables: bindVarsP3,
				},
				Keyspace: keyspace,
				Shards:   shards,
			},
		}, tabletType, true)
		return err
	})
	checkExecuteErrors(t, func(query string) error {
		_, err := conn.ExecuteBatchKeyspaceIds(ctx, []*vtgatepb.BoundKeyspaceIdQuery{
			{
				Query: &querypb.BoundQuery{
					Sql:           query,
					BindVariables: bindVarsP3,
				},
				Keyspace:    keyspace,
				KeyspaceIds: keyspaceIDs,
			},
		}, tabletType, true)
		return err
	})
}
Beispiel #25
0
func testBeginPanic(t *testing.T, conn *vtgateconn.VTGateConn) {
	ctx := context.Background()
	_, err := conn.Begin(ctx)
	expectPanic(t, err)
}
Beispiel #26
0
func testExecuteBatchKeyspaceIdsPanic(t *testing.T, conn *vtgateconn.VTGateConn) {
	ctx := context.Background()
	execCase := execMap["request1"]
	_, err := conn.ExecuteBatchKeyspaceIds(ctx, execCase.keyspaceIdBatchQuery.Queries, execCase.keyspaceIdBatchQuery.Keyspace, execCase.keyspaceIdBatchQuery.KeyspaceIds, execCase.keyspaceIdBatchQuery.TabletType)
	expectPanic(t, err)
}
Beispiel #27
0
func testExecuteShardPanic(t *testing.T, conn *vtgateconn.VTGateConn) {
	ctx := context.Background()
	execCase := execMap["request1"]
	_, err := conn.ExecuteShard(ctx, execCase.execQuery.Sql, "ks", []string{"1", "2"}, execCase.execQuery.BindVariables, execCase.execQuery.TabletType)
	expectPanic(t, err)
}