-
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
5 changed files
with
247 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package httpscenario | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"os" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/suite" | ||
phttp "github.com/yandex/pandora/components/guns/http" | ||
httpscenario "github.com/yandex/pandora/components/guns/http_scenario" | ||
ammo "github.com/yandex/pandora/components/providers/http_scenario" | ||
"github.com/yandex/pandora/core" | ||
"github.com/yandex/pandora/core/aggregator/netsample" | ||
"github.com/yandex/pandora/core/plugin/pluginconfig" | ||
"github.com/yandex/pandora/examples/http/server" | ||
"go.uber.org/zap" | ||
) | ||
|
||
var testOnce = &sync.Once{} | ||
|
||
func TestGunSuite(t *testing.T) { | ||
suite.Run(t, new(GunSuite)) | ||
} | ||
|
||
type GunSuite struct { | ||
suite.Suite | ||
server *server.Server | ||
addr string | ||
fs afero.Fs | ||
} | ||
|
||
func (s *GunSuite) SetupSuite() { | ||
s.fs = afero.NewOsFs() | ||
httpscenario.Import(s.fs) | ||
ammo.Import(s.fs) | ||
testOnce.Do(func() { | ||
pluginconfig.AddHooks() | ||
}) | ||
|
||
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})) | ||
port := os.Getenv("PORT") | ||
if port == "" { | ||
port = "8886" | ||
} | ||
|
||
s.addr = "localhost:" + port | ||
s.server = server.NewServer(s.addr, logger, time.Now().UnixNano()) | ||
s.server.ServeAsync() | ||
|
||
go func() { | ||
err := <-s.server.Err() | ||
s.NoError(err) | ||
}() | ||
} | ||
|
||
func (s *GunSuite) TearDownSuite() { | ||
err := s.server.Shutdown(context.Background()) | ||
s.NoError(err) | ||
} | ||
|
||
func (s *GunSuite) SetupTest() { | ||
s.server.Stats().Reset() | ||
} | ||
|
||
func (s *GunSuite) Test_SuccessScenario() { | ||
ctx := context.Background() | ||
log := zap.NewNop() | ||
g := httpscenario.NewHTTPGun(phttp.HTTPGunConfig{ | ||
Gun: phttp.ClientGunConfig{ | ||
Target: s.addr, | ||
}, | ||
Client: phttp.ClientConfig{}, | ||
}, log, s.addr) | ||
|
||
gunDeps := core.GunDeps{Ctx: ctx, Log: log, PoolID: "pool_id", InstanceID: 1} | ||
aggr := &Aggregator{} | ||
err := g.Bind(aggr, gunDeps) | ||
s.NoError(err) | ||
|
||
pr, err := ammo.NewProvider(s.fs, ammo.Config{File: "testdata/test_payload.hcl"}) | ||
s.NoError(err) | ||
go func() { | ||
_ = pr.Run(ctx, core.ProviderDeps{Log: log, PoolID: "pool_id"}) | ||
}() | ||
|
||
for i := 0; i < 3; i++ { | ||
am, ok := pr.Acquire() | ||
s.True(ok) | ||
g.Shoot(am.(httpscenario.Ammo)) | ||
} | ||
s.Equal(15, len(aggr.samples)) | ||
|
||
stats := s.server.Stats() | ||
s.Equal(map[int64]uint64{1: 1, 2: 1, 3: 1}, stats.Auth200) | ||
s.Equal(map[int64]uint64{1: 3, 2: 3, 3: 3}, stats.Order200) | ||
} | ||
|
||
type Aggregator struct { | ||
samples []*netsample.Sample | ||
} | ||
|
||
func (a *Aggregator) Run(ctx context.Context, deps core.AggregatorDeps) error { | ||
return nil | ||
} | ||
|
||
func (a *Aggregator) Report(s *netsample.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,3 @@ | ||
{ | ||
"name": "Spiral 4v4 NS" | ||
} |
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,123 @@ | ||
|
||
variable_source "users" "file/csv" { | ||
file = "testdata/users.csv" | ||
fields = ["user_id", "name", "pass"] | ||
ignore_first_line = true | ||
delimiter = "," | ||
} | ||
variable_source "filter_src" "file/json" { | ||
file = "testdata/filter.json" | ||
} | ||
request "auth_req" { | ||
method = "POST" | ||
uri = "/auth" | ||
headers = { | ||
Content-Type = "application/json" | ||
Useragent = "Yandex" | ||
} | ||
tag = "auth" | ||
body = <<EOF | ||
{"user_id": {{.request.auth_req.preprocessor.user_id}}} | ||
EOF | ||
templater { | ||
type = "html" | ||
} | ||
|
||
preprocessor { | ||
mapping = { | ||
user_id = "source.users[next].user_id" | ||
} | ||
} | ||
postprocessor "var/header" { | ||
mapping = { | ||
Content-Type = "Content-Type|upper" | ||
httpAuthorization = "Http-Authorization" | ||
} | ||
} | ||
postprocessor "var/jsonpath" { | ||
mapping = { | ||
token = "$.auth_key" | ||
} | ||
} | ||
postprocessor "assert/response" { | ||
headers = { | ||
Content-Type = "json" | ||
} | ||
body = ["key"] | ||
size { | ||
val = 40 | ||
op = ">" | ||
} | ||
} | ||
postprocessor "assert/response" { | ||
body = ["auth"] | ||
} | ||
} | ||
request "list_req" { | ||
method = "GET" | ||
headers = { | ||
Authorization = "Bearer {{.request.auth_req.postprocessor.token}}" | ||
Content-Type = "application/json" | ||
Useragent = "Yandex" | ||
} | ||
tag = "list" | ||
uri = "/list" | ||
|
||
postprocessor "var/jsonpath" { | ||
mapping = { | ||
item_id = "$.items[0]" | ||
items = "$.items" | ||
} | ||
} | ||
} | ||
request "order_req" { | ||
method = "POST" | ||
uri = "/order" | ||
headers = { | ||
Authorization = "Bearer {{.request.auth_req.postprocessor.token}}" | ||
Content-Type = "application/json" | ||
Useragent = "Yandex" | ||
} | ||
tag = "order_req" | ||
body = <<EOF | ||
{"item_id": {{.request.order_req.preprocessor.item}}} | ||
EOF | ||
|
||
preprocessor { | ||
mapping = { | ||
item = "request.list_req.postprocessor.items[next]" | ||
} | ||
} | ||
} | ||
|
||
request "order_req2" { | ||
method = "POST" | ||
uri = "/order" | ||
headers = { | ||
Authorization = "Bearer {{.request.auth_req.postprocessor.token}}" | ||
Content-Type = "application/json" | ||
Useragent = "Yandex" | ||
} | ||
tag = "order_req" | ||
body = <<EOF | ||
{"item_id": {{.request.order_req2.preprocessor.item}} } | ||
EOF | ||
|
||
preprocessor { | ||
mapping = { | ||
item = "request.list_req.postprocessor.items[next]" | ||
} | ||
} | ||
} | ||
|
||
scenario "scenario_name" { | ||
weight = 50 | ||
min_waiting_time = 10 | ||
requests = [ | ||
"auth_req(1)", | ||
"sleep(100)", | ||
"list_req(1)", | ||
"sleep(100)", | ||
"order_req(3)" | ||
] | ||
} |
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,4 @@ | ||
user_id,login,pass | ||
1,1,1 | ||
2,2,2 | ||
3,3,3 |