From a986976a1d8716ba36ba301c3e0043e051b06b43 Mon Sep 17 00:00:00 2001 From: Random-Liu Date: Fri, 27 Jan 2017 13:30:10 -0800 Subject: [PATCH] Only change transition timestamp when condition is changed. --- pkg/kernelmonitor/kernel_monitor.go | 9 ++++++-- pkg/kernelmonitor/kernel_monitor_test.go | 29 +++++++++++++++++++++--- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/pkg/kernelmonitor/kernel_monitor.go b/pkg/kernelmonitor/kernel_monitor.go index 3092c476..60ec9e6b 100644 --- a/pkg/kernelmonitor/kernel_monitor.go +++ b/pkg/kernelmonitor/kernel_monitor.go @@ -161,10 +161,15 @@ func (k *kernelMonitor) generateStatus(logs []*kerntypes.KernelLog, rule kerntyp condition := &k.conditions[i] if condition.Type == rule.Condition { condition.Type = rule.Condition + // Update transition timestamp and message when the condition + // changes. Condition is considered to be changed only when + // status or reason changes. + if !condition.Status || condition.Reason != rule.Reason { + condition.Transition = timestamp + condition.Message = message + } condition.Status = true - condition.Transition = timestamp condition.Reason = rule.Reason - condition.Message = message break } } diff --git a/pkg/kernelmonitor/kernel_monitor_test.go b/pkg/kernelmonitor/kernel_monitor_test.go index 26dc1856..8d84f297 100644 --- a/pkg/kernelmonitor/kernel_monitor_test.go +++ b/pkg/kernelmonitor/kernel_monitor_test.go @@ -36,12 +36,13 @@ func TestGenerateStatus(t *testing.T) { { Type: testConditionA, Status: true, - Transition: time.Now(), + Transition: time.Unix(500, 500), + Reason: "initial reason", }, { Type: testConditionB, Status: false, - Transition: time.Now(), + Transition: time.Unix(500, 500), }, } logs := []*kerntypes.KernelLog{ @@ -79,6 +80,26 @@ func TestGenerateStatus(t *testing.T) { }, }, }, + // Should not update transition time when status and reason are not changed. + { + rule: kerntypes.Rule{ + Type: kerntypes.Perm, + Condition: testConditionA, + Reason: "initial reason", + }, + expected: types.Status{ + Source: testSource, + Conditions: []types.Condition{ + { + Type: testConditionA, + Status: true, + Transition: time.Unix(500, 500), + Reason: "initial reason", + }, + initConditions[1], + }, + }, + }, { rule: kerntypes.Rule{ Type: kerntypes.Temp, @@ -100,7 +121,9 @@ func TestGenerateStatus(t *testing.T) { config: MonitorConfig{ Source: testSource, }, - conditions: initConditions, + // Copy the init conditions to make sure it's not changed + // during the test. + conditions: append([]types.Condition{}, initConditions...), } got := k.generateStatus(logs, test.rule) if !reflect.DeepEqual(&test.expected, got) {