github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/server/build_test.go (about)

     1  package server
     2  
     3  import (
     4  	"encoding/json"
     5  	"io"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"runtime"
     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  var _ = Describe("server", func() {
    24  	testing.WithConfig(func(cfg **config.Config) {
    25  		Describe("/build", func() {
    26  			It("works properly", func() {
    27  				done := make(chan interface{})
    28  				go func() {
    29  					defer GinkgoRecover()
    30  					defer close(done)
    31  
    32  					(*cfg).Server.APIBindAddr = ":10044"
    33  					s, err := storage.New(
    34  						storage.NewConfig(&(*cfg).Server),
    35  						logrus.StandardLogger(),
    36  						prometheus.NewRegistry(),
    37  						new(health.Controller),
    38  						storage.NoopApplicationMetadataService{},
    39  					)
    40  
    41  					Expect(err).ToNot(HaveOccurred())
    42  					defer s.Close()
    43  					e, _ := exporter.NewExporter(nil, nil)
    44  					c, _ := New(Config{
    45  						Configuration:           &(*cfg).Server,
    46  						Storage:                 s,
    47  						Ingester:                parser.New(logrus.StandardLogger(), s, e),
    48  						Logger:                  logrus.New(),
    49  						MetricsRegisterer:       prometheus.NewRegistry(),
    50  						ExportedMetricsRegistry: prometheus.NewRegistry(),
    51  						Notifier:                mockNotifier{},
    52  					})
    53  					h, _ := c.serverMux()
    54  					httpServer := httptest.NewServer(h)
    55  					defer httpServer.Close()
    56  
    57  					res, err := http.Get(httpServer.URL + "/build")
    58  					Expect(err).ToNot(HaveOccurred())
    59  					Expect(res.StatusCode).To(Equal(200))
    60  
    61  					b, err := io.ReadAll(res.Body)
    62  					Expect(err).ToNot(HaveOccurred())
    63  
    64  					actual := make(map[string]interface{})
    65  					err = json.Unmarshal(b, &actual)
    66  
    67  					Expect(err).ToNot(HaveOccurred())
    68  					Expect(actual["goos"]).To(Equal(runtime.GOOS))
    69  				}()
    70  				Eventually(done, 2).Should(BeClosed())
    71  			})
    72  		})
    73  	})
    74  })