Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/common-go/baseserver/server_test.go
2498 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package baseserver_test
6
7
import (
8
"context"
9
"fmt"
10
"net/http"
11
"testing"
12
13
"github.com/gitpod-io/gitpod/common-go/baseserver"
14
"github.com/prometheus/client_golang/prometheus/testutil"
15
"github.com/stretchr/testify/require"
16
"google.golang.org/grpc"
17
"google.golang.org/grpc/credentials/insecure"
18
"google.golang.org/grpc/health/grpc_health_v1"
19
)
20
21
func TestServer_StartStop(t *testing.T) {
22
// We don't use the helper NewForTests, because we want to control stopping ourselves.
23
srv, err := baseserver.New("server_test",
24
baseserver.WithUnderTest(),
25
baseserver.WithHTTP(&baseserver.ServerConfiguration{Address: "localhost:8765"}),
26
baseserver.WithGRPC(&baseserver.ServerConfiguration{Address: "localhost:8766"}),
27
)
28
require.NoError(t, err)
29
baseserver.StartServerForTests(t, srv)
30
31
require.Equal(t, "http://127.0.0.1:8765", srv.HTTPAddress())
32
require.Equal(t, "127.0.0.1:8766", srv.GRPCAddress())
33
require.NoError(t, srv.Close())
34
}
35
36
func TestServer_ServerCombinations_StartsAndStops(t *testing.T) {
37
tests := []struct {
38
StartHTTP bool
39
StartGRPC bool
40
}{
41
{StartHTTP: false, StartGRPC: false},
42
{StartHTTP: true, StartGRPC: false},
43
{StartHTTP: true, StartGRPC: true},
44
{StartHTTP: false, StartGRPC: true},
45
}
46
47
for _, test := range tests {
48
t.Run(fmt.Sprintf("with http: %v, grpc: %v", test.StartGRPC, test.StartHTTP), func(t *testing.T) {
49
var opts []baseserver.Option
50
opts = append(opts, baseserver.WithUnderTest())
51
if test.StartHTTP {
52
opts = append(opts, baseserver.WithHTTP(baseserver.MustUseRandomLocalAddress(t)))
53
}
54
55
if test.StartGRPC {
56
opts = append(opts, baseserver.WithGRPC(baseserver.MustUseRandomLocalAddress(t)))
57
}
58
59
srv, err := baseserver.New("test_server", opts...)
60
if err != nil {
61
t.Fatal(err)
62
}
63
baseserver.StartServerForTests(t, srv)
64
65
require.NotEmpty(t, srv.DebugAddress(), "must serve debug endpoint")
66
if test.StartHTTP {
67
require.NotEmpty(t, srv.HTTPAddress(), "must serve http because startHTTP was set")
68
} else {
69
require.Empty(t, srv.HTTPAddress(), "must not serve http")
70
}
71
72
if test.StartGRPC {
73
require.NotEmpty(t, srv.GRPCAddress(), "must serve grpc because startGRPC was set")
74
} else {
75
require.Empty(t, srv.GRPCAddress(), "must not serve grpc")
76
}
77
})
78
}
79
}
80
81
func TestServer_Metrics_gRPC(t *testing.T) {
82
ctx := context.Background()
83
srv := baseserver.NewForTests(t, baseserver.WithGRPC(baseserver.MustUseRandomLocalAddress(t)))
84
85
// At this point, there must be metrics registry available for use
86
require.NotNil(t, srv.MetricsRegistry())
87
// Let's start our server up
88
baseserver.StartServerForTests(t, srv)
89
90
// We need a client to be able to invoke the RPC, let's construct one
91
conn, err := grpc.DialContext(ctx, srv.GRPCAddress(), grpc.WithTransportCredentials(insecure.NewCredentials()))
92
require.NoError(t, err)
93
client := grpc_health_v1.NewHealthClient(conn)
94
95
// By default the server runs a Health service, we can use it for our purposes
96
_, err = client.Check(ctx, &grpc_health_v1.HealthCheckRequest{})
97
require.NoError(t, err)
98
99
// Finally, we can assert that some metrics were produced.
100
registry := srv.MetricsRegistry()
101
// We expect at least the following. It's not the full set, but a good baseline to sanity check.
102
expected := []string{"grpc_server_handled_total", "grpc_server_handling_seconds", "grpc_server_started_total"}
103
104
count, err := testutil.GatherAndCount(registry, expected...)
105
require.NoError(t, err)
106
require.Equal(t, len(expected)*1, count, "expected 1 count for each metric")
107
}
108
109
func TestServer_Metrics_HTTP(t *testing.T) {
110
srv := baseserver.NewForTests(t, baseserver.WithHTTP(baseserver.MustUseRandomLocalAddress(t)))
111
112
// At this point, there must be metrics registry available for use
113
require.NotNil(t, srv.MetricsRegistry())
114
// Let's start our server up
115
baseserver.StartServerForTests(t, srv)
116
117
_, err := http.Get(fmt.Sprintf("%s/foo/bar", srv.HTTPAddress()))
118
require.NoError(t, err)
119
120
registry := srv.MetricsRegistry()
121
122
count, err := testutil.GatherAndCount(registry, "gitpod_http_request_duration_seconds", "gitpod_http_response_size_bytes", "gitpod_http_requests_inflight")
123
require.NoError(t, err)
124
require.Equal(t, 3, count, "expecting 1 count for each above metric")
125
}
126
127