forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 8
/
callerid.go
109 lines (87 loc) · 3.7 KB
/
callerid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Copyright 2017 Google Inc.
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.
*/
package goclienttest
import (
"encoding/json"
"strings"
"testing"
"golang.org/x/net/context"
"vitess.io/vitess/go/cmd/vtgateclienttest/services"
"vitess.io/vitess/go/vt/callerid"
"vitess.io/vitess/go/vt/vtgate/vtgateconn"
querypb "vitess.io/vitess/go/vt/proto/query"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
vtgatepb "vitess.io/vitess/go/vt/proto/vtgate"
)
// testCallerID adds a caller ID to a context, and makes sure the server
// gets it.
func testCallerID(t *testing.T, conn *vtgateconn.VTGateConn, session *vtgateconn.VTGateSession) {
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 = session.Execute(ctx, query, 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(session.StreamExecute(ctx, query, 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)
}
func checkCallerIDError(t *testing.T, name string, err error) {
if err == nil {
t.Errorf("callerid: got no error for %v", name)
return
}
if !strings.Contains(err.Error(), "SUCCESS: ") {
t.Errorf("failed to pass callerid for %v: %v", name, err)
}
}