Path: blob/main/components/common-go/baseserver/server_test.go
2498 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package baseserver_test56import (7"context"8"fmt"9"net/http"10"testing"1112"github.com/gitpod-io/gitpod/common-go/baseserver"13"github.com/prometheus/client_golang/prometheus/testutil"14"github.com/stretchr/testify/require"15"google.golang.org/grpc"16"google.golang.org/grpc/credentials/insecure"17"google.golang.org/grpc/health/grpc_health_v1"18)1920func TestServer_StartStop(t *testing.T) {21// We don't use the helper NewForTests, because we want to control stopping ourselves.22srv, err := baseserver.New("server_test",23baseserver.WithUnderTest(),24baseserver.WithHTTP(&baseserver.ServerConfiguration{Address: "localhost:8765"}),25baseserver.WithGRPC(&baseserver.ServerConfiguration{Address: "localhost:8766"}),26)27require.NoError(t, err)28baseserver.StartServerForTests(t, srv)2930require.Equal(t, "http://127.0.0.1:8765", srv.HTTPAddress())31require.Equal(t, "127.0.0.1:8766", srv.GRPCAddress())32require.NoError(t, srv.Close())33}3435func TestServer_ServerCombinations_StartsAndStops(t *testing.T) {36tests := []struct {37StartHTTP bool38StartGRPC bool39}{40{StartHTTP: false, StartGRPC: false},41{StartHTTP: true, StartGRPC: false},42{StartHTTP: true, StartGRPC: true},43{StartHTTP: false, StartGRPC: true},44}4546for _, test := range tests {47t.Run(fmt.Sprintf("with http: %v, grpc: %v", test.StartGRPC, test.StartHTTP), func(t *testing.T) {48var opts []baseserver.Option49opts = append(opts, baseserver.WithUnderTest())50if test.StartHTTP {51opts = append(opts, baseserver.WithHTTP(baseserver.MustUseRandomLocalAddress(t)))52}5354if test.StartGRPC {55opts = append(opts, baseserver.WithGRPC(baseserver.MustUseRandomLocalAddress(t)))56}5758srv, err := baseserver.New("test_server", opts...)59if err != nil {60t.Fatal(err)61}62baseserver.StartServerForTests(t, srv)6364require.NotEmpty(t, srv.DebugAddress(), "must serve debug endpoint")65if test.StartHTTP {66require.NotEmpty(t, srv.HTTPAddress(), "must serve http because startHTTP was set")67} else {68require.Empty(t, srv.HTTPAddress(), "must not serve http")69}7071if test.StartGRPC {72require.NotEmpty(t, srv.GRPCAddress(), "must serve grpc because startGRPC was set")73} else {74require.Empty(t, srv.GRPCAddress(), "must not serve grpc")75}76})77}78}7980func TestServer_Metrics_gRPC(t *testing.T) {81ctx := context.Background()82srv := baseserver.NewForTests(t, baseserver.WithGRPC(baseserver.MustUseRandomLocalAddress(t)))8384// At this point, there must be metrics registry available for use85require.NotNil(t, srv.MetricsRegistry())86// Let's start our server up87baseserver.StartServerForTests(t, srv)8889// We need a client to be able to invoke the RPC, let's construct one90conn, err := grpc.DialContext(ctx, srv.GRPCAddress(), grpc.WithTransportCredentials(insecure.NewCredentials()))91require.NoError(t, err)92client := grpc_health_v1.NewHealthClient(conn)9394// By default the server runs a Health service, we can use it for our purposes95_, err = client.Check(ctx, &grpc_health_v1.HealthCheckRequest{})96require.NoError(t, err)9798// Finally, we can assert that some metrics were produced.99registry := srv.MetricsRegistry()100// We expect at least the following. It's not the full set, but a good baseline to sanity check.101expected := []string{"grpc_server_handled_total", "grpc_server_handling_seconds", "grpc_server_started_total"}102103count, err := testutil.GatherAndCount(registry, expected...)104require.NoError(t, err)105require.Equal(t, len(expected)*1, count, "expected 1 count for each metric")106}107108func TestServer_Metrics_HTTP(t *testing.T) {109srv := baseserver.NewForTests(t, baseserver.WithHTTP(baseserver.MustUseRandomLocalAddress(t)))110111// At this point, there must be metrics registry available for use112require.NotNil(t, srv.MetricsRegistry())113// Let's start our server up114baseserver.StartServerForTests(t, srv)115116_, err := http.Get(fmt.Sprintf("%s/foo/bar", srv.HTTPAddress()))117require.NoError(t, err)118119registry := srv.MetricsRegistry()120121count, err := testutil.GatherAndCount(registry, "gitpod_http_request_duration_seconds", "gitpod_http_response_size_bytes", "gitpod_http_requests_inflight")122require.NoError(t, err)123require.Equal(t, 3, count, "expecting 1 count for each above metric")124}125126127