From 27255c73a389afe49013bd986f806b6270043126 Mon Sep 17 00:00:00 2001 From: Sergey Bevzenko Date: Wed, 6 Mar 2024 18:49:41 +0100 Subject: [PATCH] refactoring; connect gun tests --- components/guns/http/connect.go | 5 + tests/acceptance/connect_test.go | 115 ++++++++++++++++++ .../testdata/connect/connect-check-limit.yaml | 23 ++++ .../connect/connect-check-passes.yaml | 23 ++++ .../testdata/connect/connect-ssl.yaml | 25 ++++ .../acceptance/testdata/connect/connect.yaml | 24 ++++ tests/acceptance/testdata/connect/payload.uri | 1 + .../acceptance/testdata/connect/payload5.uri | 5 + 8 files changed, 221 insertions(+) create mode 100644 tests/acceptance/connect_test.go create mode 100644 tests/acceptance/testdata/connect/connect-check-limit.yaml create mode 100644 tests/acceptance/testdata/connect/connect-check-passes.yaml create mode 100644 tests/acceptance/testdata/connect/connect-ssl.yaml create mode 100644 tests/acceptance/testdata/connect/connect.yaml create mode 100644 tests/acceptance/testdata/connect/payload.uri create mode 100644 tests/acceptance/testdata/connect/payload5.uri diff --git a/components/guns/http/connect.go b/components/guns/http/connect.go index 3e9860c83..427da7c34 100644 --- a/components/guns/http/connect.go +++ b/components/guns/http/connect.go @@ -58,6 +58,10 @@ 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) } @@ -65,6 +69,7 @@ func DefaultConnectGunConfig() ConnectGunConfig { return ConnectGunConfig{ SSL: false, Client: DefaultClientConfig(), + Base: DefaultBaseGunConfig(), } } diff --git a/tests/acceptance/connect_test.go b/tests/acceptance/connect_test.go new file mode 100644 index 000000000..f0483c8ee --- /dev/null +++ b/tests/acceptance/connect_test.go @@ -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 + }) + } +} diff --git a/tests/acceptance/testdata/connect/connect-check-limit.yaml b/tests/acceptance/testdata/connect/connect-check-limit.yaml new file mode 100644 index 000000000..cbc77ecac --- /dev/null +++ b/tests/acceptance/testdata/connect/connect-check-limit.yaml @@ -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 diff --git a/tests/acceptance/testdata/connect/connect-check-passes.yaml b/tests/acceptance/testdata/connect/connect-check-passes.yaml new file mode 100644 index 000000000..42aa9f66b --- /dev/null +++ b/tests/acceptance/testdata/connect/connect-check-passes.yaml @@ -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 diff --git a/tests/acceptance/testdata/connect/connect-ssl.yaml b/tests/acceptance/testdata/connect/connect-ssl.yaml new file mode 100644 index 000000000..9ac9156a2 --- /dev/null +++ b/tests/acceptance/testdata/connect/connect-ssl.yaml @@ -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 diff --git a/tests/acceptance/testdata/connect/connect.yaml b/tests/acceptance/testdata/connect/connect.yaml new file mode 100644 index 000000000..323a9cfd2 --- /dev/null +++ b/tests/acceptance/testdata/connect/connect.yaml @@ -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 diff --git a/tests/acceptance/testdata/connect/payload.uri b/tests/acceptance/testdata/connect/payload.uri new file mode 100644 index 000000000..35ec3b9d7 --- /dev/null +++ b/tests/acceptance/testdata/connect/payload.uri @@ -0,0 +1 @@ +/ \ No newline at end of file diff --git a/tests/acceptance/testdata/connect/payload5.uri b/tests/acceptance/testdata/connect/payload5.uri new file mode 100644 index 000000000..760465f78 --- /dev/null +++ b/tests/acceptance/testdata/connect/payload5.uri @@ -0,0 +1,5 @@ +/a +/b +/c +/d +/e