Skip to content

Commit

Permalink
http scenario tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oke11o committed Dec 22, 2023
1 parent 979ced4 commit 3a4889b
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 1 deletion.
6 changes: 5 additions & 1 deletion .mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -314,5 +314,9 @@
"lib/zaputil/zaputil_suite_test.go":"load/projects/pandora/lib/zaputil/zaputil_suite_test.go",
"main.go":"load/projects/pandora/main.go",
"script/checkfmt.sh":"load/projects/pandora/script/checkfmt.sh",
"script/coverage.sh":"load/projects/pandora/script/coverage.sh"
"script/coverage.sh":"load/projects/pandora/script/coverage.sh",
"tests/http_scenario/main_test.go":"load/projects/pandora/tests/http_scenario/main_test.go",
"tests/http_scenario/testdata/filter.json":"load/projects/pandora/tests/http_scenario/testdata/filter.json",
"tests/http_scenario/testdata/test_payload.hcl":"load/projects/pandora/tests/http_scenario/testdata/test_payload.hcl",
"tests/http_scenario/testdata/users.csv":"load/projects/pandora/tests/http_scenario/testdata/users.csv"
}
112 changes: 112 additions & 0 deletions tests/http_scenario/main_test.go
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)
}
3 changes: 3 additions & 0 deletions tests/http_scenario/testdata/filter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "Spiral 4v4 NS"
}
123 changes: 123 additions & 0 deletions tests/http_scenario/testdata/test_payload.hcl
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)"
]
}
4 changes: 4 additions & 0 deletions tests/http_scenario/testdata/users.csv
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

0 comments on commit 3a4889b

Please sign in to comment.