Skip to content

Commit

Permalink
improving metrics unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Ding <jackding@gmail.com>
  • Loading branch information
jzding committed Nov 27, 2023
1 parent 91c6d4b commit bcc1359
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 17 deletions.
35 changes: 29 additions & 6 deletions plugins/ptp_operator/metrics/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type PTPEventManager struct {
lock sync.RWMutex
Stats map[types.ConfigName]map[types.IFace]*stats.Stats
mock bool
mockEvent ptp.EventType
// PtpConfigMapUpdates holds ptp-configmap updated details
PtpConfigMapUpdates *ptpConfig.LinuxPTPConfigMapUpdate
// Ptp4lConfigInterfaces holds interfaces and its roles , after reading from ptp4l config files
Expand Down Expand Up @@ -120,15 +121,15 @@ func (p *PTPEventManager) GetPTPConfig(configName types.ConfigName) *ptp4lconf.P
}

// GetStatsForInterface ... get stats for interface
func (p *PTPEventManager) GetStatsForInterface(name types.ConfigName, iface types.IFace) map[types.IFace]*stats.Stats {
func (p *PTPEventManager) GetStatsForInterface(name types.ConfigName, iface types.IFace) *stats.Stats {
var found bool
if _, found = p.Stats[name]; !found {
p.Stats[name] = make(map[types.IFace]*stats.Stats)
p.Stats[name][iface] = stats.NewStats(string(name))
} else if _, found = p.Stats[name][iface]; found {
} else if _, found = p.Stats[name][iface]; !found {
p.Stats[name][iface] = stats.NewStats(string(name))
}
return p.Stats[name]
return p.Stats[name][iface]
}

// GetStats ... get stats
Expand All @@ -148,13 +149,23 @@ func (p *PTPEventManager) DeletePTPConfig(key types.ConfigName) {

// PublishClockClassEvent ...publish events
func (p *PTPEventManager) PublishClockClassEvent(clockClass float64, source string, eventType ptp.EventType) {
if p.mock {
p.mockEvent = eventType
log.Infof("PublishClockClassEvent clockClass=%f, source=%s, eventType=%s", clockClass, source, eventType)
return
}
data := p.GetPTPEventsData(ptp.LOCKED, int64(clockClass), source, eventType)
resourceAddress := fmt.Sprintf(p.resourcePrefix, p.nodeName, string(p.publisherTypes[eventType].Resource))
p.publish(*data, resourceAddress, eventType)
}

// PublishClockClassEvent ...publish events
func (p *PTPEventManager) publishGNSSEvent(state int64, offset float64, source string, eventType ptp.EventType) {
if p.mock {
p.mockEvent = eventType
log.Infof("publishGNSSEvent state=%d, offset=%f, source=%s, eventType=%s", state, offset, source, eventType)
return
}
var data *ceevent.Data
if state < 3 {
data = p.GetPTPEventsData(ptp.LOCKED, int64(offset), source, eventType)
Expand Down Expand Up @@ -219,16 +230,18 @@ func (p *PTPEventManager) PublishEvent(state ptp.SyncState, ptpOffset int64, sou
if state == "" {
return
}
if p.mock {
p.mockEvent = eventType
log.Infof("PublishEvent state=%s, ptpOffset=%d, source=%s, eventType=%s", state, ptpOffset, source, eventType)
return
}
// /cluster/xyz/ptp/CLOCK_REALTIME this is not address the event is published to
data := p.GetPTPEventsData(state, ptpOffset, source, eventType)
resourceAddress := fmt.Sprintf(p.resourcePrefix, p.nodeName, string(p.publisherTypes[eventType].Resource))
p.publish(*data, resourceAddress, eventType)
}

func (p *PTPEventManager) publish(data ceevent.Data, eventSource string, eventType ptp.EventType) {
if p.mock {
return
}
if pubs, ok := p.publisherTypes[eventType]; ok {
e, err := common.CreateEvent(pubs.PubID, string(eventType), eventSource, data)
if err != nil {
Expand Down Expand Up @@ -320,3 +333,13 @@ func (p *PTPEventManager) GenPTPEvent(ptpProfileName string, oStats *stats.Stats
func (p *PTPEventManager) NodeName() string {
return p.nodeName
}

// GetMockEvent ...
func (p *PTPEventManager) GetMockEvent() ptp.EventType {
return p.mockEvent
}

// ResetMockEvent ...
func (p *PTPEventManager) ResetMockEvent() {
p.mockEvent = ""
}
58 changes: 47 additions & 11 deletions plugins/ptp_operator/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const (
s2 = 1.0
MYNODE = "mynode"
// SKIP skip the verification of the metric
SKIP = 12345678
SKIP = 987654321
CLEANUP = -1
)

Expand All @@ -67,6 +67,7 @@ var ptpEventManager *metrics.PTPEventManager
var scConfig *common.SCConfiguration
var resourcePrefix = ""
var registry *prometheus.Registry
var ptpStats *stats.Stats

// InitPubSubTypes ... initialize types of publishers for ptp operator
func InitPubSubTypes() map[ptp.EventType]*types.EventPublisherType {
Expand Down Expand Up @@ -96,13 +97,15 @@ type TestCase struct {
process string
node string
iface string
lastSyncState ptp.SyncState
expectedPtpOffset float64 // offset_ns
expectedPtpMaxOffset float64 // max_offset_ns
expectedPtpFrequencyAdjustment float64 // frequency_adjustment_ns
expectedPtpDelay float64 // delay_ns
expectedSyncState float64 // clock_state
expectedNmeaStatus float64 // nmea_status
expectedPpsStatus float64 // pps_status
expectedEvent ptp.EventType
}

func (tc *TestCase) init() {
Expand All @@ -113,6 +116,7 @@ func (tc *TestCase) init() {
tc.expectedSyncState = SKIP
tc.expectedNmeaStatus = SKIP
tc.expectedPpsStatus = SKIP
tc.expectedEvent = ""
}

func (tc *TestCase) String() string {
Expand All @@ -122,6 +126,7 @@ func (tc *TestCase) String() string {
b.WriteString("process: " + tc.process + "\n")
b.WriteString("node: " + tc.node + "\n")
b.WriteString("iface: " + tc.iface + "\n")
b.WriteString("lastSyncState: " + string(tc.lastSyncState) + "\n")
return b.String()
}

Expand All @@ -130,13 +135,29 @@ func (tc *TestCase) cleanupMetrics() {
metrics.SyncState.With(map[string]string{"process": tc.process, "node": tc.node, "iface": tc.iface}).Set(CLEANUP)
metrics.PpsStatus.With(map[string]string{"process": tc.process, "node": tc.node, "iface": tc.iface}).Set(CLEANUP)
metrics.NmeaStatus.With(map[string]string{"process": tc.process, "node": tc.node, "iface": tc.iface}).Set(CLEANUP)
ptpEventManager.ResetMockEvent()
}

func setLastSyncState(process string, iface string, state ptp.SyncState) {
var configName string
switch process {
case "gnss":
case "ts2phc":
configName = ptp4lConfig.Name
default:
configName = ptp4lConfig.Name
}
s := ptpEventManager.GetStatsForInterface(types.ConfigName(configName), types.IFace(iface))
s.SetLastSyncState(state)
}

var testCases = []TestCase{
{
log: "dpll[1700614893]:[ts2phc.0.config] ens7f0 frequency_status 3 offset 5 phase_status 3 pps_status 1 s2",
log: "dpll[1000000100]:[ts2phc.0.config] ens7f0 frequency_status 3 offset 5 phase_status 3 pps_status 1 s2",
from: "master",
process: "dpll",
iface: "ens7fx",
lastSyncState: ptp.FREERUN,
expectedPtpOffset: SKIP,
expectedPtpMaxOffset: SKIP,
expectedPtpFrequencyAdjustment: SKIP,
Expand All @@ -146,44 +167,53 @@ var testCases = []TestCase{
expectedPpsStatus: 1,
},
{
log: "dpll[1700614893]:[ts2phc.0.config] ens7f0 frequency_status 3 offset 5 phase_status 3 pps_status 0 s0",
log: "dpll[1000000110]:[ts2phc.0.config] ens7f0 frequency_status 3 offset 5 phase_status 3 pps_status 0 s0",
from: "master",
process: "dpll",
iface: "ens7fx",
lastSyncState: ptp.LOCKED,
expectedPtpOffset: SKIP,
expectedPtpMaxOffset: SKIP,
expectedPtpFrequencyAdjustment: SKIP,
expectedPtpDelay: SKIP,
expectedSyncState: s0,
expectedNmeaStatus: SKIP,
expectedPpsStatus: 0,
expectedEvent: ptp.PtpStateChange,
},
{
log: "dpll[1700614893]:[ts2phc.0.config] ens7f0 frequency_status 3 offset 7 phase_status 3 pps_status 0 s1",
log: "dpll[1000000120]:[ts2phc.0.config] ens7f0 frequency_status 3 offset 7 phase_status 3 pps_status 0 s1",
from: "master",
process: "dpll",
iface: "ens7fx",
lastSyncState: ptp.FREERUN,
expectedPtpOffset: SKIP,
expectedPtpMaxOffset: SKIP,
expectedPtpFrequencyAdjustment: SKIP,
expectedPtpDelay: SKIP,
expectedSyncState: s1,
expectedNmeaStatus: SKIP,
expectedPpsStatus: 0,
expectedEvent: "",
},
{
log: "ts2phc[1699929121]:[ts2phc.0.config] ens2f0 nmea_status 0 offset 999999 s0",
from: "ts2phc",
log: "ts2phc[1000000200]:[ts2phc.0.config] ens2f0 nmea_status 0 offset 999999 s0",
from: "master",
process: "ts2phc",
iface: "ens2fx",
lastSyncState: ptp.LOCKED,
expectedPtpOffset: SKIP,
expectedPtpMaxOffset: SKIP,
expectedPtpFrequencyAdjustment: SKIP,
expectedPtpDelay: SKIP,
expectedSyncState: s0,
expectedNmeaStatus: 0,
expectedPpsStatus: SKIP,
expectedEvent: ptp.PtpStateChange,
},
{
log: "ts2phc[1699929171]:[ts2phc.0.config] ens2fx nmea_status 1 offset 0 s2",
log: "ts2phc[1000000210]:[ts2phc.0.config] ens2fx nmea_status 1 offset 0 s2",
from: "master",
process: "ts2phc",
iface: "ens2fx",
expectedPtpOffset: SKIP,
Expand All @@ -195,7 +225,7 @@ var testCases = []TestCase{
expectedPpsStatus: SKIP,
},
{
log: "ts2phc[441664.291]: [ts2phc.0.config] ens2f0 master offset 0 s2 freq -0",
log: "ts2phc[1000000300]: [ts2phc.0.config] ens2f0 master offset 0 s2 freq -0",
from: "master",
process: "ts2phc",
iface: "ens2fx",
Expand All @@ -206,9 +236,10 @@ var testCases = []TestCase{
expectedSyncState: s2,
expectedNmeaStatus: SKIP,
expectedPpsStatus: SKIP,
expectedEvent: ptp.PtpStateChange,
},
{
log: "ts2phc[441664.291]: [ts2phc.0.config] ens7f0 master offset 999 s0 freq -0",
log: "ts2phc[1000000310]: [ts2phc.0.config] ens7f0 master offset 999 s0 freq -0",
from: "master",
process: "ts2phc",
iface: "ens7fx",
Expand All @@ -219,9 +250,10 @@ var testCases = []TestCase{
expectedSyncState: s0,
expectedNmeaStatus: SKIP,
expectedPpsStatus: SKIP,
expectedEvent: ptp.PtpStateChange,
},
{
log: "GM[1699929086]:[ts2phc.0.config] ens2f0 T-GM-STATUS s0",
log: "GM[1000000400]:[ts2phc.0.config] ens2f0 T-GM-STATUS s0",
from: "master",
process: "GM",
iface: "ens2fx",
Expand All @@ -234,17 +266,19 @@ var testCases = []TestCase{
expectedPpsStatus: SKIP,
},
{
log: "gnss[1689014431]:[ts2phc.0.config] ens2f1 gnss_status 3 offset 5 s2",
log: "gnss[1000000500]:[ts2phc.0.config] ens2f1 gnss_status 3 offset 5 s2",
from: "gnss",
process: "gnss",
iface: "ens2fx",
lastSyncState: ptp.FREERUN,
expectedPtpOffset: 5,
expectedPtpMaxOffset: SKIP,
expectedPtpFrequencyAdjustment: SKIP,
expectedPtpDelay: SKIP,
expectedSyncState: s2,
expectedNmeaStatus: SKIP,
expectedPpsStatus: SKIP,
expectedEvent: ptp.GnssStateChange,
},
}

Expand Down Expand Up @@ -288,6 +322,7 @@ func Test_ExtractMetrics(t *testing.T) {
for _, tc := range testCases {
tc.node = MYNODE
tc.cleanupMetrics()
setLastSyncState(tc.process, tc.from, tc.lastSyncState)
ptpEventManager.ExtractMetrics(tc.log)
if tc.expectedPtpOffset != SKIP {
ptpOffset := metrics.PtpOffset.With(map[string]string{"from": tc.from, "process": tc.process, "node": tc.node, "iface": tc.iface})
Expand Down Expand Up @@ -317,5 +352,6 @@ func Test_ExtractMetrics(t *testing.T) {
nmeaStatus := metrics.NmeaStatus.With(map[string]string{"process": tc.process, "node": tc.node, "iface": tc.iface})
assert.Equal(tc.expectedNmeaStatus, testutil.ToFloat64(nmeaStatus), "NmeaStatus does not match\n%s", tc.String())
}
assert.Equal(tc.expectedEvent, ptpEventManager.GetMockEvent(), "Expected Event does not match\n%s", tc.String())
}
}

0 comments on commit bcc1359

Please sign in to comment.