github.com/yandex/pandora@v0.5.32/tests/acceptance/http_test.go (about) 1 package acceptance 2 3 import ( 4 "context" 5 "net/http" 6 "net/http/httptest" 7 "sync" 8 "testing" 9 10 "github.com/spf13/afero" 11 "github.com/stretchr/testify/suite" 12 "github.com/yandex/pandora/core/engine" 13 "github.com/yandex/pandora/lib/testutil" 14 "go.uber.org/atomic" 15 "go.uber.org/zap" 16 "golang.org/x/net/http2" 17 ) 18 19 var testOnce = &sync.Once{} 20 21 func TestGunSuite(t *testing.T) { 22 suite.Run(t, new(PandoraSuite)) 23 } 24 25 type PandoraSuite struct { 26 suite.Suite 27 fs afero.Fs 28 log *zap.Logger 29 metrics engine.Metrics 30 } 31 32 func (s *PandoraSuite) SetupSuite() { 33 s.fs = afero.NewOsFs() 34 testOnce.Do(importDependencies(s.fs)) 35 36 s.log = testutil.NewNullLogger() 37 s.metrics = engine.NewMetrics("http_suite") 38 } 39 40 func (s *PandoraSuite) Test_Http_Check_Passes() { 41 tests := []struct { 42 name string 43 filecfg string 44 isTLS bool 45 preStartSrv func(srv *httptest.Server) 46 wantErrContain string 47 wantCnt int 48 }{ 49 { 50 name: "http", 51 filecfg: "testdata/http/http.yaml", 52 isTLS: false, 53 wantCnt: 4, 54 }, 55 { 56 name: "https", 57 filecfg: "testdata/http/https.yaml", 58 isTLS: true, 59 wantCnt: 4, 60 }, 61 { 62 name: "http2", 63 filecfg: "testdata/http/http2.yaml", 64 isTLS: true, 65 preStartSrv: func(srv *httptest.Server) { 66 _ = http2.ConfigureServer(srv.Config, nil) 67 srv.TLS = srv.Config.TLSConfig 68 }, 69 wantCnt: 4, 70 }, 71 { 72 name: "http2 unsapported", 73 filecfg: "testdata/http/http2.yaml", 74 isTLS: true, 75 preStartSrv: func(srv *httptest.Server) { 76 //_ = http2.ConfigureServer(srv.Config, nil) 77 //srv.TLS = srv.Config.TLSConfig 78 }, 79 wantErrContain: "shoot panic: Non HTTP/2 connection established. Seems that target doesn't support HTTP/2.", 80 }, 81 { 82 name: "http-check-limits", 83 filecfg: "testdata/http/http-check-limit.yaml", 84 isTLS: false, 85 wantCnt: 8, 86 }, 87 { 88 name: "http-check-passes", 89 filecfg: "testdata/http/http-check-passes.yaml", 90 isTLS: false, 91 wantCnt: 15, 92 }, 93 { 94 name: "http2-shared-client", 95 filecfg: "testdata/http/http2-shared-client.yaml", 96 isTLS: true, 97 preStartSrv: func(srv *httptest.Server) { 98 _ = http2.ConfigureServer(srv.Config, nil) 99 srv.TLS = srv.Config.TLSConfig 100 }, 101 wantCnt: 8, 102 }, 103 } 104 for _, tt := range tests { 105 s.Run(tt.name, func() { 106 var requetsCount atomic.Int64 // Request served by test server. 107 requetsCount.Store(0) 108 srv := httptest.NewUnstartedServer(http.HandlerFunc( 109 func(rw http.ResponseWriter, req *http.Request) { 110 requetsCount.Inc() 111 rw.WriteHeader(http.StatusOK) 112 })) 113 defer srv.Close() 114 115 conf := parseConfigFile(s.T(), tt.filecfg, srv.Listener.Addr().String()) 116 s.Require().Equal(1, len(conf.Engine.Pools)) 117 aggr := &aggregator{} 118 conf.Engine.Pools[0].Aggregator = aggr 119 pandora := engine.New(s.log, s.metrics, conf.Engine) 120 121 if tt.preStartSrv != nil { 122 tt.preStartSrv(srv) 123 } 124 if tt.isTLS { 125 srv.StartTLS() 126 } else { 127 srv.Start() 128 } 129 err := pandora.Run(context.Background()) 130 if tt.wantErrContain != "" { 131 s.Assert().Equal(int64(0), requetsCount.Load()) 132 s.Require().Error(err) 133 s.Require().Contains(err.Error(), tt.wantErrContain) 134 return 135 } 136 s.Require().NoError(err) 137 s.Assert().Equal(int64(tt.wantCnt), requetsCount.Load()) 138 s.Require().Equal(tt.wantCnt, len(aggr.samples)) 139 }) 140 } 141 }