Merge pull request #84 from Random-Liu/fix-transition-timestamp

Only change transition timestamp when condition is changed.
This commit is contained in:
Lantao Liu
2017-02-07 10:41:51 -08:00
committed by GitHub
2 changed files with 33 additions and 5 deletions
+7 -2
View File
@@ -154,10 +154,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
}
}
+26 -3
View File
@@ -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) {