Skip to content

Commit

Permalink
refactoring; connect gun tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oke11o committed Mar 6, 2024
1 parent a34bcde commit 27255c7
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 0 deletions.
5 changes: 5 additions & 0 deletions components/guns/http/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,18 @@ func (g *ConnectGun) WarmUp(opts *warmup.Options) (any, error) {

func (g *ConnectGun) Do(req *http.Request) (*http.Response, error) {
req.URL.Scheme = g.scheme
if req.URL.Host == "" {
req.URL.Host = "127.0.0.1"
}

return g.client.Do(req)
}

func DefaultConnectGunConfig() ConnectGunConfig {
return ConnectGunConfig{
SSL: false,
Client: DefaultClientConfig(),
Base: DefaultBaseGunConfig(),
}
}

Expand Down
115 changes: 115 additions & 0 deletions tests/acceptance/connect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package acceptance

import (
"context"
"net/http"
"net/http/httptest"
"testing"

"github.com/spf13/afero"
"github.com/stretchr/testify/suite"
grpc "github.com/yandex/pandora/components/grpc/import"
phttpimport "github.com/yandex/pandora/components/phttp/import"
"github.com/yandex/pandora/core/engine"
coreimport "github.com/yandex/pandora/core/import"
"github.com/yandex/pandora/lib/testutil"
"go.uber.org/atomic"
"go.uber.org/zap"
)

func TestConnectGunSuite(t *testing.T) {
suite.Run(t, new(ConnectGunSuite))
}

type ConnectGunSuite struct {
suite.Suite
fs afero.Fs
log *zap.Logger
metrics engine.Metrics
}

func (s *ConnectGunSuite) SetupSuite() {
s.fs = afero.NewOsFs()
testOnce.Do(func() {
coreimport.Import(s.fs)
phttpimport.Import(s.fs)
grpc.Import(s.fs)
})

s.log = testutil.NewNullLogger()
s.metrics = newEngineMetrics("connect_suite")
}

func (s *ConnectGunSuite) Test_Connect() {
tests := []struct {
name string
filecfg string
isTLS bool
preStartSrv func(srv *httptest.Server)
wantErrContain string
wantCnt int
}{
{
name: "http",
filecfg: "testdata/connect/connect.yaml",
isTLS: false,
wantCnt: 4,
},
{
name: "http-check-limits",
filecfg: "testdata/connect/connect-check-limit.yaml",
isTLS: false,
wantCnt: 8,
},
{
name: "http-check-passes",
filecfg: "testdata/connect/connect-check-passes.yaml",
isTLS: false,
wantCnt: 15,
},
// TODO: first record does not look like a TLS handshake. Check https://go.dev/src/crypto/tls/conn.go
//{
// name: "connect-ssl",
// filecfg: "testdata/connect/connect-ssl.yaml",
// isTLS: true,
// wantCnt: 4,
//},
}
for _, tt := range tests {
s.Run(tt.name, func() {
var requetsCount atomic.Int64 // Request served by test server.
requetsCount.Store(0)
srv := httptest.NewUnstartedServer(http.HandlerFunc(
func(rw http.ResponseWriter, req *http.Request) {
requetsCount.Inc()
rw.WriteHeader(http.StatusOK)
}))
defer srv.Close()

conf := parseConfigFile(s.T(), tt.filecfg, srv.Listener.Addr().String())
s.Require().Equal(1, len(conf.Engine.Pools))
aggr := &aggregator{}
conf.Engine.Pools[0].Aggregator = aggr
pandora := engine.New(s.log, s.metrics, conf.Engine)

if tt.preStartSrv != nil {
tt.preStartSrv(srv)
}
if tt.isTLS {
srv.StartTLS()
} else {
srv.Start()
}
err := pandora.Run(context.Background())
if tt.wantErrContain != "" {
s.Assert().Equal(int64(0), requetsCount.Load())
s.Require().Error(err)
s.Require().Contains(err.Error(), tt.wantErrContain)
return
}
s.Require().NoError(err)
s.Require().Equal(int64(tt.wantCnt), int64(len(aggr.samples)))
s.Assert().GreaterOrEqual(requetsCount.Load(), int64(len(aggr.samples))) // requetsCount more than shoots
})
}
}
23 changes: 23 additions & 0 deletions tests/acceptance/testdata/connect/connect-check-limit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pools:
- id: ""
ammo:
file: testdata/http/payload5.uri
type: uri
limit: 8
result:
type: discard
gun:
target: {{.target}}
type: connect
answlog:
enabled: false
rps-per-instance: false
rps:
- duration: 5s
ops: 10
type: const
startup:
- times: 2
type: once
log:
level: debug
23 changes: 23 additions & 0 deletions tests/acceptance/testdata/connect/connect-check-passes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pools:
- id: ""
ammo:
file: testdata/http/payload5.uri
type: uri
passes: 3
result:
type: discard
gun:
target: {{.target}}
type: connect
answlog:
enabled: false
rps-per-instance: false
rps:
- duration: 5s
ops: 10
type: const
startup:
- times: 2
type: once
log:
level: debug
25 changes: 25 additions & 0 deletions tests/acceptance/testdata/connect/connect-ssl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pools:
- id: ""
ammo:
file: testdata/http/payload.uri
type: uri
result:
type: discard
gun:
target: {{.target}}
type: connect
ssl: true
answlog:
enabled: false
rps-per-instance: false
rps:
- times: 2
type: once
- duration: 0.5s
ops: 4
type: const
startup:
- times: 2
type: once
log:
level: debug
24 changes: 24 additions & 0 deletions tests/acceptance/testdata/connect/connect.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
pools:
- id: ""
ammo:
file: testdata/http/payload.uri
type: uri
result:
type: discard
gun:
target: {{.target}}
type: connect
answlog:
enabled: false
rps-per-instance: false
rps:
- times: 2
type: once
- duration: 0.5s
ops: 4
type: const
startup:
- times: 2
type: once
log:
level: debug
1 change: 1 addition & 0 deletions tests/acceptance/testdata/connect/payload.uri
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/
5 changes: 5 additions & 0 deletions tests/acceptance/testdata/connect/payload5.uri
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/a
/b
/c
/d
/e

0 comments on commit 27255c7

Please sign in to comment.