blob: cfe36cbbedf04f7aad28ff53cf0c141a8bd118b4 [file] [log] [blame]
amit.ghosh6ab2a982022-09-15 21:04:53 +02001/*
Joey Armstrong7f8436c2023-07-09 20:23:27 -04002* Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors
amit.ghosh6ab2a982022-09-15 21:04:53 +02003
Joey Armstrong7f8436c2023-07-09 20:23:27 -04004* 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
amit.ghosh6ab2a982022-09-15 21:04:53 +02007
Joey Armstrong7f8436c2023-07-09 20:23:27 -04008* http://www.apache.org/licenses/LICENSE-2.0
amit.ghosh6ab2a982022-09-15 21:04:53 +02009
Joey Armstrong7f8436c2023-07-09 20:23:27 -040010* 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.
amit.ghosh6ab2a982022-09-15 21:04:53 +020015 */
16package stats
17
18import (
19 "context"
20 "fmt"
21 "io/ioutil"
22 "net/http"
23 "testing"
24 "time"
25
26 "github.com/stretchr/testify/assert"
27 "github.com/stretchr/testify/require"
28)
29
30// TODO: Check how to reset the prom counters and histogram
31func TestPromStatsServer_Start(t *testing.T) {
32 serverCtx, serverCancel := context.WithCancel(context.Background())
33 defer serverCancel()
34
35 testPort := 34201
36
37 StatsServer.Start(serverCtx, testPort, VCore)
38
39 //give time to the prom server to start
40 time.Sleep(time.Millisecond * 300)
41
42 StatsServer.Count(NumErrorsWritingToBus)
43 StatsServer.Count(NumErrorsWritingToBus)
44
45 StatsServer.CountForDevice("dev1", "serial1", NumOnusActivated)
46 StatsServer.CountForDevice("dev1", "serial1", NumOnusActivated)
47 StatsServer.CountForDevice("dev1", "serial1", NumOnusActivated)
48
49 StatsServer.Add(NumCoreRpcErrors, 4.0)
50
51 StatsServer.AddForDevice("dev2", "serial2", NumDiscoveriesReceived, 56)
52
53 startTime := time.Now()
54
55 time.Sleep(100 * time.Millisecond)
56 StatsServer.CollectDurationForDevice("dev3", "sn3", OnuDiscoveryProcTime, startTime)
57
58 StatsServer.CollectDuration(DBWriteTime, startTime)
59
60 clientCtx, clientCancel := context.WithTimeout(context.Background(), time.Second)
61 defer clientCancel()
62
63 req, err := http.NewRequest("GET", fmt.Sprintf("http://127.0.0.1:%d/metrics", testPort), nil)
64 require.NoError(t, err)
65 req = req.WithContext(clientCtx)
66
67 client := http.DefaultClient
68 res, err := client.Do(req)
69 require.NoError(t, err)
70 defer res.Body.Close()
71
72 assert.Equal(t, 200, res.StatusCode)
73
74 bodyBytes, err := ioutil.ReadAll(res.Body)
75 require.NoError(t, err)
76
77 assert.Contains(t, string(bodyBytes), `voltha_rw_core_counters{counter="bus_write_errors_total"} 2`)
78 assert.Contains(t, string(bodyBytes), `voltha_rw_core_device_counters{counter="activated_onus_total",device_id="dev1",serial_no="serial1"} 3`)
79 assert.Contains(t, string(bodyBytes), `voltha_rw_core_counters{counter="core_rpc_errors_total"} 4`)
80 assert.Contains(t, string(bodyBytes), `voltha_rw_core_device_counters{counter="discoveries_received_total",device_id="dev2",serial_no="serial2"} 56`)
81 assert.Contains(t, string(bodyBytes), `voltha_rw_core_device_durations_bucket{device_id="dev3",duration="onu_discovery_proc_time",serial_no="sn3",le="300"} 1`)
82}