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

     1  //go:build !windows
     2  // +build !windows
     3  
     4  package admin_test
     5  
     6  import (
     7  	"net/http"
     8  	"os"
     9  	"time"
    10  
    11  	. "github.com/onsi/ginkgo/v2"
    12  	. "github.com/onsi/gomega"
    13  	"github.com/sirupsen/logrus/hooks/test"
    14  
    15  	"github.com/pyroscope-io/pyroscope/pkg/admin"
    16  )
    17  
    18  // The idea for these tests is to have client and server
    19  // communicating over the unix socket
    20  var _ = Describe("integration", func() {
    21  	var server *admin.Server
    22  	var httpC *http.Client
    23  	var socketAddr string
    24  	var cleanup func()
    25  
    26  	BeforeEach(func() {
    27  		logger, _ := test.NewNullLogger()
    28  
    29  		// the bind syscall will create the socket file
    30  		// so we first create a temporary directory
    31  		// and pass a well-known file name
    32  		// that way tests can be run concurrently
    33  		clean, dir := genRandomDir()
    34  		cleanup = clean
    35  		socketAddr = dir + "/pyroscope.tmp.sock"
    36  
    37  		// create the server
    38  		http, err := admin.NewHTTPOverUDSClient(socketAddr, admin.WithTimeout(time.Millisecond*10))
    39  		Expect(err).ToNot(HaveOccurred())
    40  
    41  		httpServer, err := admin.NewUdsHTTPServer(socketAddr, http)
    42  		Expect(err).ToNot(HaveOccurred())
    43  		ctrl := admin.NewController(logger, mockStorage{}, mockUserService{}, mockStorageService{})
    44  		s, err := admin.NewServer(logger, ctrl, httpServer)
    45  		Expect(err).ToNot(HaveOccurred())
    46  		server = s
    47  
    48  		// create the client
    49  		httpClient, err := admin.NewHTTPOverUDSClient(socketAddr)
    50  		Expect(err).ToNot(HaveOccurred())
    51  		httpC = httpClient
    52  
    53  		go (func() {
    54  			defer GinkgoRecover()
    55  			// we don't care if the server is closed
    56  			_ = server.Start()
    57  		})()
    58  		waitUntilServerIsReady(socketAddr)
    59  	})
    60  
    61  	AfterEach(func() {
    62  		server.Stop()
    63  		cleanup()
    64  	})
    65  
    66  	It("works", func() {
    67  		//		go func() {
    68  		//			defer GinkgoRecover()
    69  		//
    70  		//			err := server.Start()
    71  		//			Expect(err).ToNot(HaveOccurred())
    72  		//		}()
    73  
    74  		resp, err := httpC.Get("http://dummy/health")
    75  		Expect(err).ToNot(HaveOccurred())
    76  		Expect(resp.StatusCode).To(Equal(http.StatusOK))
    77  	})
    78  })
    79  
    80  func genRandomDir() (func(), string) {
    81  	// the bind syscall will create the socket file
    82  	// so we first create a temporary directory
    83  	// and pass a well-known file name
    84  	// that way tests can be run concurrently
    85  	dir, err := os.MkdirTemp("", "")
    86  	Expect(err).ToNot(HaveOccurred())
    87  
    88  	return func() {
    89  		os.RemoveAll(dir)
    90  	}, dir
    91  }