github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/server/controller_gzip_test.go (about) 1 package server 2 3 import ( 4 "fmt" 5 "net/http" 6 "net/http/httptest" 7 "os" 8 "path/filepath" 9 10 . "github.com/onsi/ginkgo/v2" 11 . "github.com/onsi/gomega" 12 "github.com/prometheus/client_golang/prometheus" 13 "github.com/sirupsen/logrus" 14 15 "github.com/pyroscope-io/pyroscope/pkg/config" 16 "github.com/pyroscope-io/pyroscope/pkg/exporter" 17 "github.com/pyroscope-io/pyroscope/pkg/health" 18 "github.com/pyroscope-io/pyroscope/pkg/parser" 19 "github.com/pyroscope-io/pyroscope/pkg/storage" 20 "github.com/pyroscope-io/pyroscope/pkg/testing" 21 ) 22 23 const assetAtCompressionThreshold, assetLtCompressionThreshold = "AssetAtCompressionThreshold", "AssetLTCompressionThreshold" 24 25 var tempAssetDir *testing.TmpDirectory 26 27 var _ = BeforeSuite(func() { 28 tempAssetDir = testing.TmpDirSync() 29 os.Mkdir(filepath.Join(tempAssetDir.Path, "assets"), 0755) 30 os.WriteFile(filepath.Join(tempAssetDir.Path, "assets", assetLtCompressionThreshold), make([]byte, gzHTTPCompressionThreshold-1), 0644) 31 os.WriteFile(filepath.Join(tempAssetDir.Path, "assets", assetAtCompressionThreshold), make([]byte, gzHTTPCompressionThreshold), 0644) 32 }) 33 34 var _ = AfterSuite(func() { 35 tempAssetDir.Close() 36 }) 37 38 var _ = Describe("server", func() { 39 testing.WithConfig(func(cfg **config.Config) { 40 DescribeTable("compress assets", 41 func(filename string, uncompressed bool) { 42 done := make(chan interface{}) 43 go func(filename string, uncompressed bool) { 44 defer GinkgoRecover() 45 defer close(done) 46 47 (*cfg).Server.APIBindAddr = ":10045" 48 s, err := storage.New( 49 storage.NewConfig(&(*cfg).Server), 50 logrus.StandardLogger(), 51 prometheus.NewRegistry(), 52 new(health.Controller), 53 storage.NoopApplicationMetadataService{}, 54 ) 55 Expect(err).ToNot(HaveOccurred()) 56 defer s.Close() 57 e, _ := exporter.NewExporter(nil, nil) 58 c, _ := New(Config{ 59 Configuration: &(*cfg).Server, 60 Storage: s, 61 Ingester: parser.New(logrus.StandardLogger(), s, e), 62 Logger: logrus.New(), 63 MetricsRegisterer: prometheus.NewRegistry(), 64 ExportedMetricsRegistry: prometheus.NewRegistry(), 65 Notifier: mockNotifier{}, 66 }) 67 c.dir = http.Dir(tempAssetDir.Path) 68 h, _ := c.getHandler() 69 httpServer := httptest.NewServer(h) 70 defer httpServer.Close() 71 72 res, err := http.Get(fmt.Sprintf("%s/assets/%s", httpServer.URL, filename)) 73 Expect(err).ToNot(HaveOccurred()) 74 Expect(res.StatusCode).To(Equal(http.StatusOK)) 75 Expect(res.Uncompressed).To(Equal(uncompressed)) 76 }(filename, uncompressed) 77 Eventually(done, 4).Should(BeClosed()) 78 }, 79 Entry("Should compress assets greater than or equal to threshold", assetAtCompressionThreshold, true), 80 Entry("Should not compress assets less than threshold", assetLtCompressionThreshold, false), 81 ) 82 }) 83 })