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