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

     1  package acceptance
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"testing"
     8  
     9  	"github.com/spf13/afero"
    10  	"github.com/stretchr/testify/suite"
    11  	"github.com/yandex/pandora/core/engine"
    12  	"github.com/yandex/pandora/lib/testutil"
    13  	"go.uber.org/atomic"
    14  	"go.uber.org/zap"
    15  )
    16  
    17  func TestConnectGunSuite(t *testing.T) {
    18  	suite.Run(t, new(ConnectGunSuite))
    19  }
    20  
    21  type ConnectGunSuite struct {
    22  	suite.Suite
    23  	fs      afero.Fs
    24  	log     *zap.Logger
    25  	metrics engine.Metrics
    26  }
    27  
    28  func (s *ConnectGunSuite) SetupSuite() {
    29  	s.fs = afero.NewOsFs()
    30  	testOnce.Do(importDependencies(s.fs))
    31  
    32  	s.log = testutil.NewNullLogger()
    33  	s.metrics = engine.NewMetrics("connect_suite")
    34  }
    35  
    36  func (s *ConnectGunSuite) Test_Connect() {
    37  	tests := []struct {
    38  		name           string
    39  		filecfg        string
    40  		isTLS          bool
    41  		preStartSrv    func(srv *httptest.Server)
    42  		wantErrContain string
    43  		wantCnt        int
    44  	}{
    45  		{
    46  			name:    "http",
    47  			filecfg: "testdata/connect/connect.yaml",
    48  			isTLS:   false,
    49  			wantCnt: 6,
    50  		},
    51  		{
    52  			name:    "http-check-limits",
    53  			filecfg: "testdata/connect/connect-check-limit.yaml",
    54  			isTLS:   false,
    55  			wantCnt: 8,
    56  		},
    57  		{
    58  			name:    "http-check-passes",
    59  			filecfg: "testdata/connect/connect-check-passes.yaml",
    60  			isTLS:   false,
    61  			wantCnt: 15,
    62  		},
    63  		{
    64  			name:    "connect-ssl",
    65  			filecfg: "testdata/connect/connect-ssl.yaml",
    66  			isTLS:   true,
    67  			wantCnt: 4,
    68  		},
    69  		{
    70  			name:    "connect-shared-client",
    71  			filecfg: "testdata/connect/connect-shared-client.yaml",
    72  			isTLS:   false,
    73  			wantCnt: 4,
    74  		},
    75  	}
    76  	for _, tt := range tests {
    77  		s.Run(tt.name, func() {
    78  			var requetsCount atomic.Int64 // Request served by test server.
    79  			requetsCount.Store(0)
    80  			srv := httptest.NewUnstartedServer(http.HandlerFunc(
    81  				func(rw http.ResponseWriter, req *http.Request) {
    82  					requetsCount.Inc()
    83  					rw.WriteHeader(http.StatusOK)
    84  				}))
    85  			defer srv.Close()
    86  
    87  			conf := parseConfigFile(s.T(), tt.filecfg, srv.Listener.Addr().String())
    88  			s.Require().Equal(1, len(conf.Engine.Pools))
    89  			aggr := &aggregator{}
    90  			conf.Engine.Pools[0].Aggregator = aggr
    91  			pandora := engine.New(s.log, s.metrics, conf.Engine)
    92  
    93  			if tt.preStartSrv != nil {
    94  				tt.preStartSrv(srv)
    95  			}
    96  			if tt.isTLS {
    97  				srv.StartTLS()
    98  			} else {
    99  				srv.Start()
   100  			}
   101  			err := pandora.Run(context.Background())
   102  			if tt.wantErrContain != "" {
   103  				s.Assert().Equal(int64(0), requetsCount.Load())
   104  				s.Require().Error(err)
   105  				s.Require().Contains(err.Error(), tt.wantErrContain)
   106  				return
   107  			}
   108  			s.Require().NoError(err)
   109  			s.Require().Equal(int64(tt.wantCnt), int64(len(aggr.samples)))
   110  			s.Assert().GreaterOrEqual(requetsCount.Load(), int64(len(aggr.samples))) // requetsCount more than shoots
   111  		})
   112  	}
   113  }