12
"mosn.io/mosn/pkg/protocol/xprotocol/bolt"
13
"mosn.io/mosn/pkg/protocol/xprotocol/dubbo"
14
"mosn.io/mosn/pkg/protocol/xprotocol/dubbothrift"
15
"mosn.io/mosn/pkg/protocol/xprotocol/tars"
17
"golang.org/x/net/http2"
18
_ "mosn.io/mosn/pkg/filter/network/proxy"
19
_ "mosn.io/mosn/pkg/filter/network/streamproxy"
20
"mosn.io/mosn/pkg/protocol"
21
"mosn.io/mosn/pkg/types"
22
"mosn.io/mosn/test/util"
23
"mosn.io/mosn/test/util/mosn"
26
// TODO: base interface
27
//type Case interface {
30
// RunCase(n int, interval int)
34
AppProtocol types.ProtocolName
35
MeshProtocol types.ProtocolName
38
AppServer util.UpstreamServer
44
func NewTestCase(t *testing.T, app, mesh types.ProtocolName, server util.UpstreamServer) *TestCase {
51
Finish: make(chan bool),
55
// client - mesh - server
57
// ignore parameter : mesh protocol
58
func (c *TestCase) StartProxy() {
60
appAddr := c.AppServer.Addr()
61
clientMeshAddr := util.CurrentMeshAddr()
62
c.ClientMeshAddr = clientMeshAddr
63
cfg := util.CreateProxyMesh(clientMeshAddr, []string{appAddr}, c.AppProtocol)
64
mesh := mosn.NewMosn(cfg)
72
time.Sleep(5 * time.Second) //wait server and mesh start
75
// client - mesh - mesh - server
76
func (c *TestCase) Start(tls bool) {
78
appAddr := c.AppServer.Addr()
79
clientMeshAddr := util.CurrentMeshAddr()
80
c.ClientMeshAddr = clientMeshAddr
81
serverMeshAddr := util.CurrentMeshAddr()
82
cfg := util.CreateMeshToMeshConfig(clientMeshAddr, serverMeshAddr, c.AppProtocol, c.MeshProtocol, []string{appAddr}, tls)
83
mesh := mosn.NewMosn(cfg)
91
time.Sleep(5 * time.Second) //wait server and mesh start
94
// Finish case and wait close returns
95
func (c *TestCase) FinishCase() {
100
const HTTPTestPath = "test/path"
102
// mesh to mesh use tls if "istls" is true
103
// client do "n" times request, interval time (ms)
104
func (c *TestCase) RunCase(n int, interval int) {
106
var call func() error
107
switch c.AppProtocol {
109
call = func() error {
110
resp, err := http.Get(fmt.Sprintf("http://%s/%s", c.ClientMeshAddr, HTTPTestPath))
114
defer resp.Body.Close()
115
if resp.StatusCode != http.StatusOK {
116
return fmt.Errorf("response status: %d", resp.StatusCode)
118
b, err := ioutil.ReadAll(resp.Body)
122
c.T.Logf("HTTP client receive data: %s\n", string(b))
126
tr := &http2.Transport{
128
DialTLS: func(netw, addr string, cfg *tls.Config) (net.Conn, error) {
129
return net.Dial(netw, addr)
132
httpClient := http.Client{Transport: tr}
133
call = func() error {
134
resp, err := httpClient.Get(fmt.Sprintf("http://%s/%s", c.ClientMeshAddr, HTTPTestPath))
138
defer resp.Body.Close()
139
if resp.StatusCode != http.StatusOK {
140
return fmt.Errorf("response status: %d", resp.StatusCode)
143
b, err := ioutil.ReadAll(resp.Body)
147
c.T.Logf("HTTP2 client receive data: %s\n", string(b))
151
c.C <- fmt.Errorf("unsupported protocol: %v", c.AppProtocol)
154
for i := 0; i < n; i++ {
155
if err := call(); err != nil {
159
time.Sleep(time.Duration(interval) * time.Millisecond)
164
// XProtocol case, should use sub-protocol
165
type XTestCase struct {
167
SubProtocol types.ProtocolName
170
func NewXTestCase(t *testing.T, subProtocol types.ProtocolName, server util.UpstreamServer) *XTestCase {
173
AppProtocol: types.ProtocolName("X"),
174
MeshProtocol: types.ProtocolName("X"),
178
Finish: make(chan bool),
180
SubProtocol: subProtocol,
184
// client - mesh - server
186
// ignore parameter : mesh protocol
187
func (c *XTestCase) StartProxy() {
188
c.AppServer.GoServe()
189
appAddr := c.AppServer.Addr()
190
clientMeshAddr := util.CurrentMeshAddr()
191
c.ClientMeshAddr = clientMeshAddr
192
cfg := util.CreateXProtocolProxyMesh(clientMeshAddr, []string{appAddr}, c.SubProtocol)
193
mesh := mosn.NewMosn(cfg)
201
time.Sleep(5 * time.Second) //wait server and mesh start
204
func (c *XTestCase) Start(tls bool) {
205
c.AppServer.GoServe()
206
appAddr := c.AppServer.Addr()
207
clientMeshAddr := util.CurrentMeshAddr()
208
c.ClientMeshAddr = clientMeshAddr
209
serverMeshAddr := util.CurrentMeshAddr()
210
cfg := util.CreateXProtocolMesh(clientMeshAddr, serverMeshAddr, c.SubProtocol, []string{appAddr}, tls)
211
mesh := mosn.NewMosn(cfg)
219
time.Sleep(5 * time.Second) //wait server and mesh start
222
// mesh to mesh use tls if "istls" is true
223
// client do "n" times request, interval time (ms)
224
func (c *XTestCase) RunCase(n int, interval int) {
226
var call func() error
227
switch c.SubProtocol {
228
case bolt.ProtocolName, dubbo.ProtocolName, tars.ProtocolName, dubbothrift.ProtocolName:
229
server, ok := c.AppServer.(*util.RPCServer)
231
c.C <- fmt.Errorf("need a xprotocol rpc server")
234
client := server.Client
235
if err := client.Connect(c.ClientMeshAddr); err != nil {
240
call = func() error {
242
if !util.WaitMapEmpty(&client.Waits, 2*time.Second) {
243
return fmt.Errorf("request get no response")
248
c.C <- fmt.Errorf("unsupported protocol: %v", c.AppProtocol)
251
for i := 0; i < n; i++ {
252
if err := call(); err != nil {
256
time.Sleep(time.Duration(interval) * time.Millisecond)