2020-10-27 22:27:15 +00:00
|
|
|
package connection
|
|
|
|
|
|
|
|
import (
|
2022-03-04 11:35:57 +00:00
|
|
|
"bytes"
|
2020-10-27 22:27:15 +00:00
|
|
|
"context"
|
2021-10-19 19:01:17 +00:00
|
|
|
"errors"
|
2020-10-27 22:27:15 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gobwas/ws/wsutil"
|
2022-04-27 10:51:06 +00:00
|
|
|
"github.com/google/uuid"
|
2020-11-25 06:55:13 +00:00
|
|
|
"github.com/rs/zerolog"
|
2021-03-23 14:30:43 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-10-27 22:27:15 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"golang.org/x/net/http2"
|
2021-03-23 14:30:43 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/tunnelrpc/pogs"
|
|
|
|
tunnelpogs "github.com/cloudflare/cloudflared/tunnelrpc/pogs"
|
2020-10-27 22:27:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testTransport = http2.Transport{}
|
|
|
|
)
|
|
|
|
|
2021-07-16 15:14:37 +00:00
|
|
|
func newTestHTTP2Connection() (*HTTP2Connection, net.Conn) {
|
2021-10-19 19:01:17 +00:00
|
|
|
edgeConn, cfdConn := net.Pipe()
|
2020-10-27 22:27:15 +00:00
|
|
|
var connIndex = uint8(0)
|
2021-07-16 15:14:37 +00:00
|
|
|
log := zerolog.Nop()
|
2022-07-20 23:17:29 +00:00
|
|
|
obs := NewObserver(&log, &log)
|
2021-08-17 14:30:02 +00:00
|
|
|
controlStream := NewControlStream(
|
|
|
|
obs,
|
|
|
|
mockConnectedFuse{},
|
2022-02-07 09:42:07 +00:00
|
|
|
&NamedTunnelProperties{},
|
2021-08-17 14:30:02 +00:00
|
|
|
connIndex,
|
|
|
|
nil,
|
|
|
|
nil,
|
2022-06-18 00:24:37 +00:00
|
|
|
nil,
|
2021-08-17 14:30:02 +00:00
|
|
|
1*time.Second,
|
2022-08-11 20:31:36 +00:00
|
|
|
HTTP2,
|
2021-08-17 14:30:02 +00:00
|
|
|
)
|
2020-10-27 22:27:15 +00:00
|
|
|
return NewHTTP2Connection(
|
2021-10-19 19:01:17 +00:00
|
|
|
cfdConn,
|
2022-02-07 09:42:07 +00:00
|
|
|
// OriginProxy is set in testConfigManager
|
2022-02-11 10:49:06 +00:00
|
|
|
testOrchestrator,
|
2020-10-27 22:27:15 +00:00
|
|
|
&pogs.ConnectionOptions{},
|
2021-08-17 14:30:02 +00:00
|
|
|
obs,
|
2020-10-27 22:27:15 +00:00
|
|
|
connIndex,
|
2021-08-17 14:30:02 +00:00
|
|
|
controlStream,
|
2021-07-16 15:14:37 +00:00
|
|
|
&log,
|
2020-10-27 22:27:15 +00:00
|
|
|
), edgeConn
|
|
|
|
}
|
|
|
|
|
2022-03-04 11:35:57 +00:00
|
|
|
func TestHTTP2ConfigurationSet(t *testing.T) {
|
|
|
|
http2Conn, edgeConn := newTestHTTP2Connection()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
http2Conn.Serve(ctx)
|
|
|
|
}()
|
|
|
|
|
|
|
|
edgeHTTP2Conn, err := testTransport.NewClientConn(edgeConn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
endpoint := fmt.Sprintf("http://localhost:8080/ok")
|
|
|
|
reqBody := []byte(`{
|
|
|
|
"version": 2,
|
|
|
|
"config": {"warp-routing": {"enabled": true}, "originRequest" : {"connectTimeout": 10}, "ingress" : [ {"hostname": "test", "service": "https://localhost:8000" } , {"service": "http_status:404"} ]}}
|
|
|
|
`)
|
|
|
|
reader := bytes.NewReader(reqBody)
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPut, endpoint, reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
req.Header.Set(InternalUpgradeHeader, ConfigurationUpdate)
|
|
|
|
|
|
|
|
resp, err := edgeHTTP2Conn.RoundTrip(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
bdy, err := ioutil.ReadAll(resp.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, `{"lastAppliedVersion":2,"err":null}`, string(bdy))
|
|
|
|
cancel()
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-10-27 22:27:15 +00:00
|
|
|
func TestServeHTTP(t *testing.T) {
|
|
|
|
tests := []testRequest{
|
|
|
|
{
|
|
|
|
name: "ok",
|
|
|
|
endpoint: "ok",
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: []byte(http.StatusText(http.StatusOK)),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "large_file",
|
|
|
|
endpoint: "large_file",
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: testLargeResp,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Bad request",
|
|
|
|
endpoint: "400",
|
|
|
|
expectedStatus: http.StatusBadRequest,
|
|
|
|
expectedBody: []byte(http.StatusText(http.StatusBadRequest)),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Internal server error",
|
|
|
|
endpoint: "500",
|
|
|
|
expectedStatus: http.StatusInternalServerError,
|
|
|
|
expectedBody: []byte(http.StatusText(http.StatusInternalServerError)),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Proxy error",
|
|
|
|
endpoint: "error",
|
|
|
|
expectedStatus: http.StatusBadGateway,
|
|
|
|
expectedBody: nil,
|
|
|
|
isProxyError: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
http2Conn, edgeConn := newTestHTTP2Connection()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
http2Conn.Serve(ctx)
|
|
|
|
}()
|
|
|
|
|
|
|
|
edgeHTTP2Conn, err := testTransport.NewClientConn(edgeConn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
endpoint := fmt.Sprintf("http://localhost:8080/%s", test.endpoint)
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp, err := edgeHTTP2Conn.RoundTrip(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, test.expectedStatus, resp.StatusCode)
|
|
|
|
if test.expectedBody != nil {
|
|
|
|
respBody, err := ioutil.ReadAll(resp.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, test.expectedBody, respBody)
|
|
|
|
}
|
|
|
|
if test.isProxyError {
|
2021-03-26 04:04:56 +00:00
|
|
|
require.Equal(t, responseMetaHeaderCfd, resp.Header.Get(ResponseMetaHeader))
|
2020-10-27 22:27:15 +00:00
|
|
|
} else {
|
2021-03-26 04:04:56 +00:00
|
|
|
require.Equal(t, responseMetaHeaderOrigin, resp.Header.Get(ResponseMetaHeader))
|
2020-10-27 22:27:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
cancel()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockNamedTunnelRPCClient struct {
|
2021-03-04 18:45:39 +00:00
|
|
|
shouldFail error
|
2020-10-27 22:27:15 +00:00
|
|
|
registered chan struct{}
|
|
|
|
unregistered chan struct{}
|
|
|
|
}
|
|
|
|
|
2022-04-27 10:51:06 +00:00
|
|
|
func (mc mockNamedTunnelRPCClient) SendLocalConfiguration(c context.Context, config []byte, observer *Observer) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-27 22:27:15 +00:00
|
|
|
func (mc mockNamedTunnelRPCClient) RegisterConnection(
|
|
|
|
c context.Context,
|
2022-02-07 09:42:07 +00:00
|
|
|
properties *NamedTunnelProperties,
|
2020-10-27 22:27:15 +00:00
|
|
|
options *tunnelpogs.ConnectionOptions,
|
|
|
|
connIndex uint8,
|
2022-06-18 00:24:37 +00:00
|
|
|
edgeAddress net.IP,
|
2020-10-27 22:27:15 +00:00
|
|
|
observer *Observer,
|
2022-04-27 10:51:06 +00:00
|
|
|
) (*tunnelpogs.ConnectionDetails, error) {
|
2021-03-04 18:45:39 +00:00
|
|
|
if mc.shouldFail != nil {
|
2022-04-27 10:51:06 +00:00
|
|
|
return nil, mc.shouldFail
|
2021-03-04 18:45:39 +00:00
|
|
|
}
|
2020-10-27 22:27:15 +00:00
|
|
|
close(mc.registered)
|
2022-04-27 10:51:06 +00:00
|
|
|
return &tunnelpogs.ConnectionDetails{
|
|
|
|
Location: "LIS",
|
|
|
|
UUID: uuid.New(),
|
|
|
|
TunnelIsRemotelyManaged: false,
|
|
|
|
}, nil
|
2020-10-27 22:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mc mockNamedTunnelRPCClient) GracefulShutdown(ctx context.Context, gracePeriod time.Duration) {
|
|
|
|
close(mc.unregistered)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mockNamedTunnelRPCClient) Close() {}
|
|
|
|
|
|
|
|
type mockRPCClientFactory struct {
|
2021-03-04 18:45:39 +00:00
|
|
|
shouldFail error
|
2020-10-27 22:27:15 +00:00
|
|
|
registered chan struct{}
|
|
|
|
unregistered chan struct{}
|
|
|
|
}
|
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
func (mf *mockRPCClientFactory) newMockRPCClient(context.Context, io.ReadWriteCloser, *zerolog.Logger) NamedTunnelRPCClient {
|
2020-10-27 22:27:15 +00:00
|
|
|
return mockNamedTunnelRPCClient{
|
2021-03-04 18:45:39 +00:00
|
|
|
shouldFail: mf.shouldFail,
|
2020-10-27 22:27:15 +00:00
|
|
|
registered: mf.registered,
|
|
|
|
unregistered: mf.unregistered,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type wsRespWriter struct {
|
|
|
|
*httptest.ResponseRecorder
|
|
|
|
readPipe *io.PipeReader
|
|
|
|
writePipe *io.PipeWriter
|
2021-10-19 19:01:17 +00:00
|
|
|
closed bool
|
|
|
|
panicked bool
|
2020-10-27 22:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newWSRespWriter() *wsRespWriter {
|
|
|
|
readPipe, writePipe := io.Pipe()
|
|
|
|
return &wsRespWriter{
|
|
|
|
httptest.NewRecorder(),
|
|
|
|
readPipe,
|
|
|
|
writePipe,
|
2021-10-19 19:01:17 +00:00
|
|
|
false,
|
|
|
|
false,
|
2020-10-27 22:27:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 19:01:17 +00:00
|
|
|
type nowriter struct {
|
|
|
|
io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nowriter) Write(_ []byte) (int, error) {
|
|
|
|
return 0, fmt.Errorf("writer not implemented")
|
|
|
|
}
|
|
|
|
|
2020-10-27 22:27:15 +00:00
|
|
|
func (w *wsRespWriter) RespBody() io.ReadWriter {
|
|
|
|
return nowriter{w.readPipe}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wsRespWriter) Write(data []byte) (n int, err error) {
|
2021-10-19 19:01:17 +00:00
|
|
|
if w.closed {
|
|
|
|
w.panicked = true
|
|
|
|
return 0, errors.New("wsRespWriter panicked")
|
|
|
|
}
|
2020-10-27 22:27:15 +00:00
|
|
|
return w.writePipe.Write(data)
|
|
|
|
}
|
|
|
|
|
2021-10-19 19:01:17 +00:00
|
|
|
func (w *wsRespWriter) close() {
|
|
|
|
w.closed = true
|
|
|
|
}
|
|
|
|
|
2020-10-27 22:27:15 +00:00
|
|
|
func TestServeWS(t *testing.T) {
|
|
|
|
http2Conn, _ := newTestHTTP2Connection()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
respWriter := newWSRespWriter()
|
|
|
|
readPipe, writePipe := io.Pipe()
|
|
|
|
|
2021-10-19 19:01:17 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8080/ws/echo", readPipe)
|
2020-10-27 22:27:15 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-26 04:04:56 +00:00
|
|
|
req.Header.Set(InternalUpgradeHeader, WebsocketUpgrade)
|
2020-10-27 22:27:15 +00:00
|
|
|
|
2021-10-19 19:01:17 +00:00
|
|
|
serveDone := make(chan struct{})
|
2020-10-27 22:27:15 +00:00
|
|
|
go func() {
|
2021-10-19 19:01:17 +00:00
|
|
|
defer close(serveDone)
|
2020-10-27 22:27:15 +00:00
|
|
|
http2Conn.ServeHTTP(respWriter, req)
|
2021-10-19 19:01:17 +00:00
|
|
|
respWriter.close()
|
2020-10-27 22:27:15 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
data := []byte("test websocket")
|
2021-10-19 19:01:17 +00:00
|
|
|
err = wsutil.WriteClientBinary(writePipe, data)
|
2020-10-27 22:27:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-10-19 19:01:17 +00:00
|
|
|
respBody, err := wsutil.ReadServerBinary(respWriter.RespBody())
|
2020-10-27 22:27:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, data, respBody, fmt.Sprintf("Expect %s, got %s", string(data), string(respBody)))
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
resp := respWriter.Result()
|
|
|
|
// http2RespWriter should rewrite status 101 to 200
|
|
|
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
2021-03-26 04:04:56 +00:00
|
|
|
require.Equal(t, responseMetaHeaderOrigin, resp.Header.Get(ResponseMetaHeader))
|
2020-10-27 22:27:15 +00:00
|
|
|
|
2021-10-19 19:01:17 +00:00
|
|
|
<-serveDone
|
|
|
|
require.False(t, respWriter.panicked)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestNoWriteAfterServeHTTPReturns is a regression test of https://jira.cfops.it/browse/TUN-5184
|
|
|
|
// to make sure we don't write to the ResponseWriter after the ServeHTTP method returns
|
|
|
|
func TestNoWriteAfterServeHTTPReturns(t *testing.T) {
|
|
|
|
cfdHTTP2Conn, edgeTCPConn := newTestHTTP2Connection()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
serverDone := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(serverDone)
|
|
|
|
cfdHTTP2Conn.Serve(ctx)
|
|
|
|
}()
|
|
|
|
|
|
|
|
edgeTransport := http2.Transport{}
|
|
|
|
edgeHTTP2Conn, err := edgeTransport.NewClientConn(edgeTCPConn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
message := []byte(t.Name())
|
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
readPipe, writePipe := io.Pipe()
|
|
|
|
reqCtx, reqCancel := context.WithCancel(ctx)
|
|
|
|
req, err := http.NewRequestWithContext(reqCtx, http.MethodGet, "http://localhost:8080/ws/flaky", readPipe)
|
|
|
|
require.NoError(t, err)
|
|
|
|
req.Header.Set(InternalUpgradeHeader, WebsocketUpgrade)
|
|
|
|
|
|
|
|
resp, err := edgeHTTP2Conn.RoundTrip(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
// http2RespWriter should rewrite status 101 to 200
|
|
|
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-reqCtx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
_ = wsutil.WriteClientBinary(writePipe, message)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
reqCancel()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-10-27 22:27:15 +00:00
|
|
|
wg.Wait()
|
2021-10-19 19:01:17 +00:00
|
|
|
cancel()
|
|
|
|
<-serverDone
|
2020-10-27 22:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestServeControlStream(t *testing.T) {
|
|
|
|
http2Conn, edgeConn := newTestHTTP2Connection()
|
|
|
|
|
|
|
|
rpcClientFactory := mockRPCClientFactory{
|
|
|
|
registered: make(chan struct{}),
|
|
|
|
unregistered: make(chan struct{}),
|
|
|
|
}
|
2021-08-17 14:30:02 +00:00
|
|
|
|
2022-07-20 23:17:29 +00:00
|
|
|
obs := NewObserver(&log, &log)
|
2021-08-17 14:30:02 +00:00
|
|
|
controlStream := NewControlStream(
|
|
|
|
obs,
|
|
|
|
mockConnectedFuse{},
|
2022-02-07 09:42:07 +00:00
|
|
|
&NamedTunnelProperties{},
|
2021-08-17 14:30:02 +00:00
|
|
|
1,
|
2022-06-18 00:24:37 +00:00
|
|
|
nil,
|
2021-08-17 14:30:02 +00:00
|
|
|
rpcClientFactory.newMockRPCClient,
|
|
|
|
nil,
|
|
|
|
1*time.Second,
|
2022-08-11 20:31:36 +00:00
|
|
|
HTTP2,
|
2021-08-17 14:30:02 +00:00
|
|
|
)
|
|
|
|
http2Conn.controlStreamHandler = controlStream
|
2020-10-27 22:27:15 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
http2Conn.Serve(ctx)
|
|
|
|
}()
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8080/", nil)
|
|
|
|
require.NoError(t, err)
|
2021-03-26 04:04:56 +00:00
|
|
|
req.Header.Set(InternalUpgradeHeader, ControlStreamUpgrade)
|
2020-10-27 22:27:15 +00:00
|
|
|
|
|
|
|
edgeHTTP2Conn, err := testTransport.NewClientConn(edgeConn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
edgeHTTP2Conn.RoundTrip(req)
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-rpcClientFactory.registered
|
|
|
|
cancel()
|
|
|
|
<-rpcClientFactory.unregistered
|
2021-01-20 19:41:09 +00:00
|
|
|
assert.False(t, http2Conn.stoppedGracefully)
|
2020-10-27 22:27:15 +00:00
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2021-03-04 18:45:39 +00:00
|
|
|
func TestFailRegistration(t *testing.T) {
|
|
|
|
http2Conn, edgeConn := newTestHTTP2Connection()
|
|
|
|
|
|
|
|
rpcClientFactory := mockRPCClientFactory{
|
|
|
|
shouldFail: errDuplicationConnection,
|
|
|
|
registered: make(chan struct{}),
|
|
|
|
unregistered: make(chan struct{}),
|
|
|
|
}
|
2021-08-17 14:30:02 +00:00
|
|
|
|
2022-07-20 23:17:29 +00:00
|
|
|
obs := NewObserver(&log, &log)
|
2021-08-17 14:30:02 +00:00
|
|
|
controlStream := NewControlStream(
|
|
|
|
obs,
|
|
|
|
mockConnectedFuse{},
|
2022-02-07 09:42:07 +00:00
|
|
|
&NamedTunnelProperties{},
|
2021-08-17 14:30:02 +00:00
|
|
|
http2Conn.connIndex,
|
2022-06-18 00:24:37 +00:00
|
|
|
nil,
|
2021-08-17 14:30:02 +00:00
|
|
|
rpcClientFactory.newMockRPCClient,
|
|
|
|
nil,
|
|
|
|
1*time.Second,
|
2022-08-11 20:31:36 +00:00
|
|
|
HTTP2,
|
2021-08-17 14:30:02 +00:00
|
|
|
)
|
|
|
|
http2Conn.controlStreamHandler = controlStream
|
2021-03-04 18:45:39 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
http2Conn.Serve(ctx)
|
|
|
|
}()
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8080/", nil)
|
|
|
|
require.NoError(t, err)
|
2021-03-26 04:04:56 +00:00
|
|
|
req.Header.Set(InternalUpgradeHeader, ControlStreamUpgrade)
|
2021-03-04 18:45:39 +00:00
|
|
|
|
|
|
|
edgeHTTP2Conn, err := testTransport.NewClientConn(edgeConn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
resp, err := edgeHTTP2Conn.RoundTrip(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusBadGateway, resp.StatusCode)
|
|
|
|
|
|
|
|
assert.NotNil(t, http2Conn.controlStreamErr)
|
|
|
|
cancel()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2021-01-20 19:41:09 +00:00
|
|
|
func TestGracefulShutdownHTTP2(t *testing.T) {
|
|
|
|
http2Conn, edgeConn := newTestHTTP2Connection()
|
|
|
|
|
|
|
|
rpcClientFactory := mockRPCClientFactory{
|
|
|
|
registered: make(chan struct{}),
|
|
|
|
unregistered: make(chan struct{}),
|
|
|
|
}
|
2021-02-04 21:09:17 +00:00
|
|
|
events := &eventCollectorSink{}
|
2021-08-17 14:30:02 +00:00
|
|
|
|
2021-02-05 00:07:49 +00:00
|
|
|
shutdownC := make(chan struct{})
|
2022-07-20 23:17:29 +00:00
|
|
|
obs := NewObserver(&log, &log)
|
2021-08-17 14:30:02 +00:00
|
|
|
obs.RegisterSink(events)
|
|
|
|
controlStream := NewControlStream(
|
|
|
|
obs,
|
|
|
|
mockConnectedFuse{},
|
2022-02-07 09:42:07 +00:00
|
|
|
&NamedTunnelProperties{},
|
2021-08-17 14:30:02 +00:00
|
|
|
http2Conn.connIndex,
|
2022-06-18 00:24:37 +00:00
|
|
|
nil,
|
2021-08-17 14:30:02 +00:00
|
|
|
rpcClientFactory.newMockRPCClient,
|
|
|
|
shutdownC,
|
|
|
|
1*time.Second,
|
2022-08-11 20:31:36 +00:00
|
|
|
HTTP2,
|
2021-08-17 14:30:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
http2Conn.controlStreamHandler = controlStream
|
2021-01-20 19:41:09 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
http2Conn.Serve(ctx)
|
|
|
|
}()
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8080/", nil)
|
|
|
|
require.NoError(t, err)
|
2021-03-26 04:04:56 +00:00
|
|
|
req.Header.Set(InternalUpgradeHeader, ControlStreamUpgrade)
|
2021-01-20 19:41:09 +00:00
|
|
|
|
|
|
|
edgeHTTP2Conn, err := testTransport.NewClientConn(edgeConn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
_, _ = edgeHTTP2Conn.RoundTrip(req)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-rpcClientFactory.registered:
|
2022-04-27 10:51:06 +00:00
|
|
|
break // ok
|
2021-01-20 19:41:09 +00:00
|
|
|
case <-time.Tick(time.Second):
|
|
|
|
t.Fatal("timeout out waiting for registration")
|
|
|
|
}
|
|
|
|
|
|
|
|
// signal graceful shutdown
|
2021-02-05 00:07:49 +00:00
|
|
|
close(shutdownC)
|
2021-01-20 19:41:09 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-rpcClientFactory.unregistered:
|
2022-04-27 10:51:06 +00:00
|
|
|
break // ok
|
2021-01-20 19:41:09 +00:00
|
|
|
case <-time.Tick(time.Second):
|
|
|
|
t.Fatal("timeout out waiting for unregistered signal")
|
|
|
|
}
|
2021-08-17 14:30:02 +00:00
|
|
|
assert.True(t, controlStream.IsStopped())
|
2021-01-20 19:41:09 +00:00
|
|
|
|
|
|
|
cancel()
|
|
|
|
wg.Wait()
|
2021-02-04 21:09:17 +00:00
|
|
|
|
|
|
|
events.assertSawEvent(t, Event{
|
|
|
|
Index: http2Conn.connIndex,
|
|
|
|
EventType: Unregistering,
|
|
|
|
})
|
2021-01-20 19:41:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 22:27:15 +00:00
|
|
|
func benchmarkServeHTTP(b *testing.B, test testRequest) {
|
|
|
|
http2Conn, edgeConn := newTestHTTP2Connection()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
http2Conn.Serve(ctx)
|
|
|
|
}()
|
|
|
|
|
|
|
|
endpoint := fmt.Sprintf("http://localhost:8080/%s", test.endpoint)
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
|
|
|
|
require.NoError(b, err)
|
|
|
|
|
|
|
|
edgeHTTP2Conn, err := testTransport.NewClientConn(edgeConn)
|
|
|
|
require.NoError(b, err)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StartTimer()
|
|
|
|
resp, err := edgeHTTP2Conn.RoundTrip(req)
|
|
|
|
b.StopTimer()
|
|
|
|
require.NoError(b, err)
|
|
|
|
require.Equal(b, test.expectedStatus, resp.StatusCode)
|
|
|
|
if test.expectedBody != nil {
|
|
|
|
respBody, err := ioutil.ReadAll(resp.Body)
|
|
|
|
require.NoError(b, err)
|
|
|
|
require.Equal(b, test.expectedBody, respBody)
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2021-01-20 19:41:09 +00:00
|
|
|
|
2020-10-27 22:27:15 +00:00
|
|
|
func BenchmarkServeHTTPSimple(b *testing.B) {
|
|
|
|
test := testRequest{
|
|
|
|
name: "ok",
|
|
|
|
endpoint: "ok",
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: []byte(http.StatusText(http.StatusOK)),
|
|
|
|
}
|
|
|
|
|
|
|
|
benchmarkServeHTTP(b, test)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkServeHTTPLargeFile(b *testing.B) {
|
|
|
|
test := testRequest{
|
|
|
|
name: "large_file",
|
|
|
|
endpoint: "large_file",
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedBody: testLargeResp,
|
|
|
|
}
|
|
|
|
|
|
|
|
benchmarkServeHTTP(b, test)
|
|
|
|
}
|