blob: cdb385de55e59e67620fcaa16980fa4933c7c862 [file] [log] [blame]
amit.ghosh6ab2a982022-09-15 21:04:53 +02001/*
Joey Armstrong9cdee9f2024-01-03 04:56:14 -05002* Copyright 2018-2024 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"
Akash Reddy Kankanala05aff182025-05-06 12:57:32 +053021 "io"
amit.ghosh6ab2a982022-09-15 21:04:53 +020022 "net/http"
23 "testing"
24 "time"
25
Abhay Kumar40252eb2025-10-13 13:25:53 +000026 "github.com/phayes/freeport"
amit.ghosh6ab2a982022-09-15 21:04:53 +020027 "github.com/stretchr/testify/assert"
28 "github.com/stretchr/testify/require"
29)
30
31// TODO: Check how to reset the prom counters and histogram
32func TestPromStatsServer_Start(t *testing.T) {
33 serverCtx, serverCancel := context.WithCancel(context.Background())
34 defer serverCancel()
35
Abhay Kumar40252eb2025-10-13 13:25:53 +000036 // Get a free port to avoid conflicts
37 testPort, err := freeport.GetFreePort()
38 require.NoError(t, err)
amit.ghosh6ab2a982022-09-15 21:04:53 +020039
40 StatsServer.Start(serverCtx, testPort, VCore)
41
42 //give time to the prom server to start
43 time.Sleep(time.Millisecond * 300)
44
45 StatsServer.Count(NumErrorsWritingToBus)
46 StatsServer.Count(NumErrorsWritingToBus)
47
48 StatsServer.CountForDevice("dev1", "serial1", NumOnusActivated)
49 StatsServer.CountForDevice("dev1", "serial1", NumOnusActivated)
50 StatsServer.CountForDevice("dev1", "serial1", NumOnusActivated)
51
52 StatsServer.Add(NumCoreRpcErrors, 4.0)
53
54 StatsServer.AddForDevice("dev2", "serial2", NumDiscoveriesReceived, 56)
55
56 startTime := time.Now()
57
58 time.Sleep(100 * time.Millisecond)
59 StatsServer.CollectDurationForDevice("dev3", "sn3", OnuDiscoveryProcTime, startTime)
60
61 StatsServer.CollectDuration(DBWriteTime, startTime)
62
63 clientCtx, clientCancel := context.WithTimeout(context.Background(), time.Second)
64 defer clientCancel()
65
66 req, err := http.NewRequest("GET", fmt.Sprintf("http://127.0.0.1:%d/metrics", testPort), nil)
67 require.NoError(t, err)
68 req = req.WithContext(clientCtx)
69
70 client := http.DefaultClient
71 res, err := client.Do(req)
72 require.NoError(t, err)
73 defer res.Body.Close()
74
75 assert.Equal(t, 200, res.StatusCode)
76
Akash Reddy Kankanala05aff182025-05-06 12:57:32 +053077 bodyBytes, err := io.ReadAll(res.Body)
amit.ghosh6ab2a982022-09-15 21:04:53 +020078 require.NoError(t, err)
79
80 assert.Contains(t, string(bodyBytes), `voltha_rw_core_counters{counter="bus_write_errors_total"} 2`)
81 assert.Contains(t, string(bodyBytes), `voltha_rw_core_device_counters{counter="activated_onus_total",device_id="dev1",serial_no="serial1"} 3`)
82 assert.Contains(t, string(bodyBytes), `voltha_rw_core_counters{counter="core_rpc_errors_total"} 4`)
83 assert.Contains(t, string(bodyBytes), `voltha_rw_core_device_counters{counter="discoveries_received_total",device_id="dev2",serial_no="serial2"} 56`)
84 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`)
85}