forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestenv.go
100 lines (85 loc) · 2.58 KB
/
testenv.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
package testenv
import (
"io"
"net"
"google.golang.org/grpc"
mixerpb "istio.io/api/mixer/v1"
"istio.io/istio/mixer/cmd/server/cmd"
"istio.io/istio/mixer/cmd/shared"
"istio.io/istio/mixer/pkg/adapter"
"istio.io/istio/mixer/pkg/attribute"
"istio.io/istio/mixer/pkg/template"
)
// TestEnv interface
type TestEnv interface {
io.Closer
CreateMixerClient() (mixerpb.MixerClient, *grpc.ClientConn, error)
}
type testEnv struct {
addr string
mixerContext *cmd.ServerContext
shutdown chan struct{}
}
// Args includes the required args to initialize Mixer server.
type Args struct {
MixerServerAddr string
ConfigStoreURL string
ConfigStore2URL string
ConfigDefaultNamespace string
ConfigIdentityAttribute string
ConfigIdentityAttributeDomain string
UseAstEvaluator bool
}
// NewEnv creates a TestEnv instance.
func NewEnv(args *Args, info map[string]template.Info, adapters []adapter.InfoFn) (TestEnv, error) {
lis, err := net.Listen("tcp", args.MixerServerAddr)
if err != nil {
return nil, err
}
context := cmd.SetupTestServer(info, adapters, []adapter.RegisterFn{}, args.ConfigStoreURL, args.ConfigStore2URL,
args.ConfigDefaultNamespace, args.ConfigIdentityAttribute, args.ConfigIdentityAttributeDomain, args.UseAstEvaluator)
shutdown := make(chan struct{})
go func() {
shared.Printf("Start test Mixer on:%v\n", lis.Addr())
{
defer context.GP.Close()
defer context.AdapterGP.Close()
if err := context.Server.Serve(lis); err != nil {
shared.Printf("Mixer Shutdown: %v\n", err)
}
}
shutdown <- struct{}{}
}()
return &testEnv{
addr: lis.Addr().String(),
mixerContext: context,
shutdown: shutdown,
}, nil
}
// CreateMixerClient returns a Mixer Grpc client.
func (env *testEnv) CreateMixerClient() (mixerpb.MixerClient, *grpc.ClientConn, error) {
conn, err := grpc.Dial(env.addr, grpc.WithInsecure())
if err != nil {
return nil, nil, err
}
return mixerpb.NewMixerClient(conn), conn, nil
}
// Close cleans up TestEnv.
func (env *testEnv) Close() error {
env.mixerContext.Server.GracefulStop()
<-env.shutdown
close(env.shutdown)
env.mixerContext = nil
return nil
}
// GetAttrBag creates Attributes proto.
func GetAttrBag(attrs map[string]interface{}, identityAttr, identityAttrDomain string) mixerpb.CompressedAttributes {
requestBag := attribute.GetMutableBag(nil)
requestBag.Set(identityAttr, identityAttrDomain)
for k, v := range attrs {
requestBag.Set(k, v)
}
var attrProto mixerpb.CompressedAttributes
requestBag.ToProto(&attrProto, nil, 0)
return attrProto
}