github.com/yandex/pandora@v0.5.32/tests/acceptance/http_scenario_test.go (about)

     1  package acceptance
     2  
     3  import (
     4  	"context"
     5  	"log/slog"
     6  	"os"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/spf13/afero"
    11  	"github.com/stretchr/testify/suite"
    12  	"github.com/yandex/pandora/core/engine"
    13  	"github.com/yandex/pandora/examples/http/server"
    14  	"github.com/yandex/pandora/lib/testutil"
    15  	"go.uber.org/zap"
    16  )
    17  
    18  func TestHTTPScenarioSuite(t *testing.T) {
    19  	suite.Run(t, new(HTTPScenarioSuite))
    20  }
    21  
    22  type HTTPScenarioSuite struct {
    23  	suite.Suite
    24  	fs      afero.Fs
    25  	log     *zap.Logger
    26  	metrics engine.Metrics
    27  	addr    string
    28  	srv     *server.Server
    29  }
    30  
    31  func (s *HTTPScenarioSuite) SetupSuite() {
    32  	s.fs = afero.NewOsFs()
    33  	testOnce.Do(importDependencies(s.fs))
    34  
    35  	s.log = testutil.NewNullLogger()
    36  	s.metrics = engine.NewMetrics("http_scenario_suite")
    37  
    38  	logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
    39  	port := os.Getenv("PORT") // TODO: how to set free port in CI?
    40  	if port == "" {
    41  		port = "8886"
    42  	}
    43  
    44  	s.addr = "localhost:" + port
    45  	s.srv = server.NewServer(s.addr, logger, time.Now().UnixNano())
    46  	s.srv.ServeAsync()
    47  
    48  	go func() {
    49  		err := <-s.srv.Err()
    50  		s.NoError(err)
    51  	}()
    52  }
    53  
    54  func (s *HTTPScenarioSuite) TearDownSuite() {
    55  	err := s.srv.Shutdown(context.Background())
    56  	s.NoError(err)
    57  }
    58  
    59  func (s *HTTPScenarioSuite) SetupTest() {
    60  	s.srv.Stats().Reset()
    61  }
    62  
    63  func (s *HTTPScenarioSuite) Test_Http_Check_Passes() {
    64  	tests := []struct {
    65  		name           string
    66  		filecfg        string
    67  		wantErrContain string
    68  		wantCnt        int
    69  		wantStats      *server.Stats
    70  	}{
    71  		{
    72  			name:    "base",
    73  			filecfg: "testdata/http_scenario/scenario.yaml",
    74  			wantCnt: 4,
    75  			wantStats: &server.Stats{
    76  				Auth200:  map[int64]uint64{1: 2, 2: 1, 3: 1},
    77  				List200:  map[int64]uint64{1: 2, 2: 1, 3: 1},
    78  				Order200: map[int64]uint64{1: 6, 2: 3, 3: 3},
    79  			},
    80  		},
    81  	}
    82  	for _, tt := range tests {
    83  		s.Run(tt.name, func() {
    84  			conf := parseConfigFile(s.T(), tt.filecfg, s.addr)
    85  			s.Require().Equal(1, len(conf.Engine.Pools))
    86  			aggr := &aggregator{}
    87  			conf.Engine.Pools[0].Aggregator = aggr
    88  
    89  			pandora := engine.New(s.log, s.metrics, conf.Engine)
    90  
    91  			err := pandora.Run(context.Background())
    92  			if tt.wantErrContain != "" {
    93  				s.Require().Error(err)
    94  				s.Require().Contains(err.Error(), tt.wantErrContain)
    95  				return
    96  			}
    97  
    98  			s.Require().NoError(err)
    99  			s.Require().Equal(tt.wantStats, s.srv.Stats())
   100  		})
   101  	}
   102  }