github.com/yandex/pandora@v0.5.32/tests/http_scenario/main_test.go (about)

     1  package httpscenario
     2  
     3  import (
     4  	"context"
     5  	"log/slog"
     6  	"os"
     7  	"sync"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/spf13/afero"
    12  	"github.com/stretchr/testify/require"
    13  	"github.com/stretchr/testify/suite"
    14  	phttp "github.com/yandex/pandora/components/guns/http"
    15  	httpscenario "github.com/yandex/pandora/components/guns/http_scenario"
    16  	ammo "github.com/yandex/pandora/components/providers/scenario"
    17  	httpammo "github.com/yandex/pandora/components/providers/scenario/http"
    18  	_import "github.com/yandex/pandora/components/providers/scenario/import"
    19  	"github.com/yandex/pandora/core"
    20  	"github.com/yandex/pandora/core/aggregator/netsample"
    21  	"github.com/yandex/pandora/core/plugin/pluginconfig"
    22  	"github.com/yandex/pandora/examples/http/server"
    23  	"go.uber.org/zap"
    24  )
    25  
    26  var testOnce = &sync.Once{}
    27  
    28  func TestGunSuite(t *testing.T) {
    29  	suite.Run(t, new(GunSuite))
    30  }
    31  
    32  type GunSuite struct {
    33  	suite.Suite
    34  	server *server.Server
    35  	addr   string
    36  	fs     afero.Fs
    37  }
    38  
    39  func (s *GunSuite) SetupSuite() {
    40  	s.fs = afero.NewOsFs()
    41  	httpscenario.Import(s.fs)
    42  	_import.Import(s.fs)
    43  	testOnce.Do(func() {
    44  		pluginconfig.AddHooks()
    45  	})
    46  
    47  	logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
    48  	port := os.Getenv("PORT") // TODO: how to set free port in CI?
    49  	if port == "" {
    50  		port = "8886"
    51  	}
    52  
    53  	s.addr = "localhost:" + port
    54  	s.server = server.NewServer(s.addr, logger, time.Now().UnixNano())
    55  	s.server.ServeAsync()
    56  
    57  	go func() {
    58  		err := <-s.server.Err()
    59  		s.NoError(err)
    60  	}()
    61  }
    62  
    63  func (s *GunSuite) TearDownSuite() {
    64  	err := s.server.Shutdown(context.Background())
    65  	s.NoError(err)
    66  }
    67  
    68  func (s *GunSuite) SetupTest() {
    69  	s.server.Stats().Reset()
    70  }
    71  
    72  func (s *GunSuite) Test_SuccessScenario() {
    73  	ctx := context.Background()
    74  	log := zap.NewNop()
    75  	g := httpscenario.NewHTTPGun(phttp.GunConfig{
    76  		Target:         s.addr,
    77  		TargetResolved: s.addr,
    78  		Client:         phttp.ClientConfig{},
    79  	}, log)
    80  
    81  	gunDeps := core.GunDeps{Ctx: ctx, Log: log, PoolID: "pool_id", InstanceID: 1}
    82  	aggr := &Aggregator{}
    83  	err := g.Bind(aggr, gunDeps)
    84  	s.NoError(err)
    85  
    86  	pr, err := httpammo.NewProvider(s.fs, ammo.ProviderConfig{File: "testdata/http_payload.hcl"})
    87  	require.NoError(s.T(), err)
    88  	go func() {
    89  		_ = pr.Run(ctx, core.ProviderDeps{Log: log, PoolID: "pool_id"})
    90  	}()
    91  
    92  	for i := 0; i < 3; i++ {
    93  		am, ok := pr.Acquire()
    94  		s.True(ok)
    95  		scenario, ok := am.(*httpscenario.Scenario)
    96  		s.True(ok)
    97  		g.Shoot(scenario)
    98  	}
    99  
   100  	stats := s.server.Stats()
   101  	s.Equal(map[int64]uint64{1: 1, 2: 1, 3: 1}, stats.Auth200)
   102  	s.Equal(map[int64]uint64{1: 3, 2: 3, 3: 3}, stats.Order200)
   103  }
   104  
   105  type Aggregator struct {
   106  	samples []*netsample.Sample
   107  }
   108  
   109  func (a *Aggregator) Run(ctx context.Context, deps core.AggregatorDeps) error {
   110  	return nil
   111  }
   112  
   113  func (a *Aggregator) Report(s *netsample.Sample) {
   114  	a.samples = append(a.samples, s)
   115  }