forked from dolthub/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testsuite.go
89 lines (80 loc) · 3.4 KB
/
testsuite.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
package testsuite
import (
"reflect"
"testing"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/vt/callerid"
querypb "github.com/youtube/vitess/go/vt/proto/query"
vtrpcpb "github.com/youtube/vitess/go/vt/proto/vtrpc"
)
const (
// FakePrincipal is the principal of testing effective CallerID
FakePrincipal = "TestPrincipal"
// FakeComponent is the component of testing effective CallerID
FakeComponent = "TestComponent"
// FakeSubcomponent is the subcomponent of testing effective CallerID
FakeSubcomponent = "TestSubcomponent"
// FakeUsername is the username of testing immediate CallerID
FakeUsername = "TestUsername"
)
// RunTests performs the necessary testsuite for CallerID operations
func RunTests(t *testing.T, im *querypb.VTGateCallerID, ef *vtrpcpb.CallerID) {
ctx := context.TODO()
ctxim := callerid.ImmediateCallerIDFromContext(ctx)
// For Contexts without immediate CallerID, ImmediateCallerIDFromContext should fail
if ctxim != nil {
t.Errorf("Expect nil from ImmediateCallerIDFromContext, but got %v", ctxim)
}
// For Contexts without effective CallerID, EffectiveCallerIDFromContext should fail
ctxef := callerid.EffectiveCallerIDFromContext(ctx)
if ctxef != nil {
t.Errorf("Expect nil from EffectiveCallerIDFromContext, but got %v", ctxef)
}
ctx = callerid.NewContext(ctx, nil, nil)
ctxim = callerid.ImmediateCallerIDFromContext(ctx)
// For Contexts with nil immediate CallerID, ImmediateCallerIDFromContext should fail
if ctxim != nil {
t.Errorf("Expect nil from ImmediateCallerIDFromContext, but got %v", ctxim)
}
// For Contexts with nil effective CallerID, EffectiveCallerIDFromContext should fail
ctxef = callerid.EffectiveCallerIDFromContext(ctx)
if ctxef != nil {
t.Errorf("Expect nil from EffectiveCallerIDFromContext, but got %v", ctxef)
}
// Test GetXxx on nil receivers, should get all empty strings
if u := callerid.GetUsername(ctxim); u != "" {
t.Errorf("Expect empty string from GetUsername(nil), but got %v", u)
}
if p := callerid.GetPrincipal(ctxef); p != "" {
t.Errorf("Expect empty string from GetPrincipal(nil), but got %v", p)
}
if c := callerid.GetComponent(ctxef); c != "" {
t.Errorf("Expect empty string from GetComponent(nil), but got %v", c)
}
if s := callerid.GetSubcomponent(ctxef); s != "" {
t.Errorf("Expect empty string from GetSubcomponent(nil), but got %v", s)
}
ctx = callerid.NewContext(ctx, ef, im)
ctxim = callerid.ImmediateCallerIDFromContext(ctx)
// retrieved immediate CallerID should be equal to the one we put into Context
if !reflect.DeepEqual(ctxim, im) {
t.Errorf("Expect %v from ImmediateCallerIDFromContext, but got %v", im, ctxim)
}
if u := callerid.GetUsername(im); u != FakeUsername {
t.Errorf("Expect %v from GetUsername(im), but got %v", FakeUsername, u)
}
ctxef = callerid.EffectiveCallerIDFromContext(ctx)
// retrieved effective CallerID should be equal to the one we put into Context
if !reflect.DeepEqual(ctxef, ef) {
t.Errorf("Expect %v from EffectiveCallerIDFromContext, but got %v", ef, ctxef)
}
if p := callerid.GetPrincipal(ef); p != FakePrincipal {
t.Errorf("Expect %v from GetPrincipal(ef), but got %v", FakePrincipal, p)
}
if c := callerid.GetComponent(ef); c != FakeComponent {
t.Errorf("Expect %v from GetComponent(ef), but got %v", FakeComponent, c)
}
if s := callerid.GetSubcomponent(ef); s != FakeSubcomponent {
t.Errorf("Expect %v from GetSubcomponent(ef), but got %v", FakeSubcomponent, s)
}
}