-
Notifications
You must be signed in to change notification settings - Fork 0
/
testmetrics.go
114 lines (97 loc) · 2.97 KB
/
testmetrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Copyright 2019 Insolar Technologies
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package testmetrics
import (
"context"
"errors"
"io/ioutil"
"net/http"
"strconv"
"strings"
"testing"
"time"
"github.com/insolar/insolar/configuration"
"github.com/insolar/insolar/core"
"github.com/insolar/insolar/instrumentation/inslogger"
"github.com/insolar/insolar/metrics"
"github.com/insolar/insolar/testutils"
)
// TestMetrics provides testing helpers for metrics.
type TestMetrics struct {
ctx context.Context
Metrics *metrics.Metrics
}
// Start configures, creates and starts metrics server,
// returns initialized TestMetrics object.
func Start(ctx context.Context, t *testing.T) TestMetrics {
inslog := inslogger.FromContext(ctx)
cfg := configuration.NewMetrics()
host, _ := parseAddr(cfg.ListenAddress)
// just use any available port
cfg.ListenAddress = host + ":0"
// don't wait too long in tests
cfg.ReportingPeriod = time.Millisecond
m, err := metrics.NewMetrics(ctx, cfg, metrics.GetInsolarRegistry("test"), "test")
if err != nil {
panic(err)
}
cert := testutils.NewCertificateMock(t)
cert.GetRoleMock.Return(core.StaticRoleVirtual)
cm := testutils.NewCertificateManagerMock(t)
cm.GetCertificateMock.Return(cert)
m.CertificateManager = cm
err = m.Start(ctx)
if err != nil {
inslog.Fatal("metrics server failed to start:", err)
}
return TestMetrics{
ctx: ctx,
Metrics: m,
}
}
func parseAddr(address string) (string, int32) {
pair := strings.SplitN(address, ":", 2)
currentPort, err := strconv.Atoi(pair[1])
if err != nil {
panic(err)
}
return pair[0], int32(currentPort)
}
// FetchContent fetches content from /metrics.
func (tm TestMetrics) FetchContent() (string, error) {
code, content, err := tm.FetchURL("/metrics")
if err != nil && code != http.StatusOK {
return "", errors.New("got non 200 code")
}
return content, err
}
// FetchURL fetches content from provided relative url.
func (tm TestMetrics) FetchURL(relurl string) (int, string, error) {
// to be sure metrics are available
time.Sleep(time.Millisecond * 5)
fetchurl := "http://" + tm.Metrics.AddrString() + relurl
response, err := http.Get(fetchurl) //nolint: gosec
if err != nil {
return 0, "", err
}
defer response.Body.Close()
content, err := ioutil.ReadAll(response.Body)
return response.StatusCode, string(content), err
}
// Stop wraps metrics Stop method.
func (tm TestMetrics) Stop() error {
return tm.Metrics.Stop(tm.ctx)
}