-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
289 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,3 @@ | ||
// Copyright (c) 2017 Yandex LLC. All rights reserved. | ||
// Use of this source code is governed by a MPL 2.0 | ||
// license that can be found in the LICENSE file. | ||
// Author: Vladimir Skipor <[email protected]> | ||
|
||
package phttp | ||
|
||
import ( | ||
|
@@ -27,13 +22,13 @@ func Import(fs afero.Fs) { | |
|
||
register.Gun("http", func(conf phttp.HTTPGunConfig) func() core.Gun { | ||
targetResolved, _ := PreResolveTargetAddr(&conf.Client, conf.Gun.Target) | ||
answLog := answlog.Init(conf.Gun.Base.AnswLog.Path) | ||
answLog := answlog.Init(conf.Gun.Base.AnswLog.Path, conf.Gun.Base.AnswLog.Enabled) | ||
return func() core.Gun { return phttp.WrapGun(phttp.NewHTTPGun(conf, answLog, targetResolved)) } | ||
}, phttp.DefaultHTTPGunConfig) | ||
|
||
register.Gun("http2", func(conf phttp.HTTP2GunConfig) func() (core.Gun, error) { | ||
targetResolved, _ := PreResolveTargetAddr(&conf.Client, conf.Gun.Target) | ||
answLog := answlog.Init(conf.Gun.Base.AnswLog.Path) | ||
answLog := answlog.Init(conf.Gun.Base.AnswLog.Path, conf.Gun.Base.AnswLog.Enabled) | ||
return func() (core.Gun, error) { | ||
gun, err := phttp.NewHTTP2Gun(conf, answLog, targetResolved) | ||
return phttp.WrapGun(gun), err | ||
|
@@ -42,7 +37,7 @@ func Import(fs afero.Fs) { | |
|
||
register.Gun("connect", func(conf phttp.ConnectGunConfig) func() core.Gun { | ||
conf.Target, _ = PreResolveTargetAddr(&conf.Client, conf.Target) | ||
answLog := answlog.Init(conf.BaseGunConfig.AnswLog.Path) | ||
answLog := answlog.Init(conf.BaseGunConfig.AnswLog.Path, conf.BaseGunConfig.AnswLog.Enabled) | ||
return func() core.Gun { | ||
return phttp.WrapGun(phttp.NewConnectGun(conf, answLog)) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
package httphttp2 | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"sync" | ||
"testing" | ||
"text/template" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/suite" | ||
"github.com/yandex/pandora/cli" | ||
grpc "github.com/yandex/pandora/components/grpc/import" | ||
phttpimport "github.com/yandex/pandora/components/phttp/import" | ||
"github.com/yandex/pandora/core" | ||
"github.com/yandex/pandora/core/config" | ||
"github.com/yandex/pandora/core/engine" | ||
coreimport "github.com/yandex/pandora/core/import" | ||
"github.com/yandex/pandora/lib/monitoring" | ||
"go.uber.org/atomic" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zaptest/observer" | ||
"golang.org/x/net/http2" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
var testOnce = &sync.Once{} | ||
|
||
func TestGunSuite(t *testing.T) { | ||
suite.Run(t, new(PandoraSuite)) | ||
} | ||
|
||
type PandoraSuite struct { | ||
suite.Suite | ||
fs afero.Fs | ||
log *zap.Logger | ||
metrics engine.Metrics | ||
} | ||
|
||
func (s *PandoraSuite) SetupSuite() { | ||
s.fs = afero.NewOsFs() | ||
coreimport.Import(s.fs) | ||
phttpimport.Import(s.fs) | ||
grpc.Import(s.fs) | ||
|
||
s.log = setupLogsCapture() | ||
s.metrics = newEngineMetrics() | ||
} | ||
|
||
func (s *PandoraSuite) Test_Http() { | ||
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 := s.parseConfigFile("testdata/http/http.yaml", 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) | ||
|
||
srv.Start() | ||
err := pandora.Run(context.Background()) | ||
s.Assert().Equal(int64(4), requetsCount.Load()) | ||
s.Require().NoError(err) | ||
s.Require().Equal(4, len(aggr.samples)) | ||
} | ||
|
||
func (s *PandoraSuite) Test_Https() { | ||
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 := s.parseConfigFile("testdata/http/https.yaml", 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) | ||
|
||
srv.StartTLS() | ||
err := pandora.Run(context.Background()) | ||
s.Assert().Equal(int64(4), requetsCount.Load()) | ||
s.Require().NoError(err) | ||
s.Require().Equal(4, len(aggr.samples)) | ||
} | ||
|
||
func (s *PandoraSuite) Test_Http2() { | ||
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 := s.parseConfigFile("testdata/http/http2.yaml", 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) | ||
|
||
_ = http2.ConfigureServer(srv.Config, nil) | ||
srv.TLS = srv.Config.TLSConfig | ||
srv.StartTLS() | ||
|
||
err := pandora.Run(context.Background()) | ||
s.Assert().Equal(int64(4), requetsCount.Load()) | ||
s.Require().NoError(err) | ||
s.Require().Equal(4, len(aggr.samples)) | ||
} | ||
|
||
func (s *PandoraSuite) Test_Http2_UnsupportTarget() { | ||
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 := s.parseConfigFile("testdata/http/http2.yaml", 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) | ||
|
||
//_ = http2.ConfigureServer(srv.Config, nil) | ||
//srv.TLS = srv.Config.TLSConfig | ||
srv.StartTLS() | ||
|
||
err := pandora.Run(context.Background()) | ||
s.Assert().Equal(int64(0), requetsCount.Load()) | ||
s.Require().Error(err) | ||
s.Require().Contains(err.Error(), "shoot panic: Non HTTP/2 connection established. Seems that target doesn't support HTTP/2.") | ||
} | ||
|
||
func (s *PandoraSuite) parseConfigFile(filename string, serverAddr string) *cli.CliConfig { | ||
f, err := os.ReadFile(filename) | ||
s.Require().NoError(err) | ||
tmpl, err := template.New("x").Parse(string(f)) | ||
s.Require().NoError(err) | ||
b := &bytes.Buffer{} | ||
err = tmpl.Execute(b, map[string]string{"target": serverAddr}) | ||
s.Require().NoError(err) | ||
mapCfg := map[string]any{} | ||
err = yaml.Unmarshal(b.Bytes(), &mapCfg) | ||
s.Require().NoError(err) | ||
|
||
conf := cli.DefaultConfig() | ||
err = config.DecodeAndValidate(mapCfg, conf) | ||
s.Require().NoError(err) | ||
|
||
return conf | ||
} | ||
|
||
func setupLogsCapture() *zap.Logger { | ||
c, _ := observer.New(zap.InfoLevel) | ||
return zap.New(c) | ||
} | ||
|
||
func newEngineMetrics() engine.Metrics { | ||
return engine.Metrics{ | ||
Request: monitoring.NewCounter("engine_Requests"), | ||
Response: monitoring.NewCounter("engine_Responses"), | ||
InstanceStart: monitoring.NewCounter("engine_UsersStarted"), | ||
InstanceFinish: monitoring.NewCounter("engine_UsersFinished"), | ||
} | ||
} | ||
|
||
type aggregator struct { | ||
samples []core.Sample | ||
} | ||
|
||
func (a *aggregator) Run(ctx context.Context, deps core.AggregatorDeps) error { | ||
return nil | ||
} | ||
|
||
func (a *aggregator) Report(s core.Sample) { | ||
a.samples = append(a.samples, s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: http | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: http2 | ||
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 |
Oops, something went wrong.