mirror of
https://github.com/kyverno/kyverno.git
synced 2024-12-14 11:57:48 +00:00
chore: add engine api stats unit tests (#7451)
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
parent
a345e15511
commit
1d2b50bc03
1 changed files with 158 additions and 0 deletions
158
pkg/engine/api/stats_test.go
Normal file
158
pkg/engine/api/stats_test.go
Normal file
|
@ -0,0 +1,158 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNewExecutionStats(t *testing.T) {
|
||||
now := time.Now()
|
||||
tests := []struct {
|
||||
name string
|
||||
startTime time.Time
|
||||
endTime time.Time
|
||||
want ExecutionStats
|
||||
}{{
|
||||
startTime: now,
|
||||
endTime: now,
|
||||
want: ExecutionStats{0, now},
|
||||
}, {
|
||||
startTime: now,
|
||||
endTime: now.Add(time.Hour),
|
||||
want: ExecutionStats{time.Hour, now},
|
||||
}}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := NewExecutionStats(tt.startTime, tt.endTime); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("NewExecutionStats() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecutionStats_Time(t *testing.T) {
|
||||
now := time.Now()
|
||||
tests := []struct {
|
||||
name string
|
||||
startTime time.Time
|
||||
endTime time.Time
|
||||
want time.Time
|
||||
}{{
|
||||
startTime: now,
|
||||
endTime: now,
|
||||
want: now,
|
||||
}, {
|
||||
startTime: now,
|
||||
endTime: now.Add(time.Hour),
|
||||
want: now,
|
||||
}}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
s := NewExecutionStats(tt.startTime, tt.endTime)
|
||||
if got := s.Time(); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("ExecutionStats.Time() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecutionStats_Timestamp(t *testing.T) {
|
||||
now := time.Now()
|
||||
tests := []struct {
|
||||
name string
|
||||
startTime time.Time
|
||||
endTime time.Time
|
||||
want int64
|
||||
}{{
|
||||
startTime: now,
|
||||
endTime: now,
|
||||
want: now.Unix(),
|
||||
}, {
|
||||
startTime: now,
|
||||
endTime: now.Add(time.Hour),
|
||||
want: now.Unix(),
|
||||
}}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
s := NewExecutionStats(tt.startTime, tt.endTime)
|
||||
if got := s.Timestamp(); got != tt.want {
|
||||
t.Errorf("ExecutionStats.Timestamp() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecutionStats_ProcessingTime(t *testing.T) {
|
||||
now := time.Now()
|
||||
tests := []struct {
|
||||
name string
|
||||
startTime time.Time
|
||||
endTime time.Time
|
||||
want time.Duration
|
||||
}{{
|
||||
startTime: now,
|
||||
endTime: now,
|
||||
want: 0,
|
||||
}, {
|
||||
startTime: now,
|
||||
endTime: now.Add(time.Hour),
|
||||
want: time.Hour,
|
||||
}}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
s := NewExecutionStats(tt.startTime, tt.endTime)
|
||||
if got := s.ProcessingTime(); got != tt.want {
|
||||
t.Errorf("ExecutionStats.ProcessingTime() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPolicyStats_RulesAppliedCount(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
rulesAppliedCount int
|
||||
rulesErrorCount int
|
||||
want int
|
||||
}{{}, {
|
||||
rulesAppliedCount: 10,
|
||||
rulesErrorCount: 20,
|
||||
want: 10,
|
||||
}}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ps := &PolicyStats{
|
||||
rulesAppliedCount: tt.rulesAppliedCount,
|
||||
rulesErrorCount: tt.rulesErrorCount,
|
||||
}
|
||||
if got := ps.RulesAppliedCount(); got != tt.want {
|
||||
t.Errorf("PolicyStats.RulesAppliedCount() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPolicyStats_RulesErrorCount(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
rulesAppliedCount int
|
||||
rulesErrorCount int
|
||||
want int
|
||||
}{{}, {
|
||||
rulesAppliedCount: 10,
|
||||
rulesErrorCount: 20,
|
||||
want: 20,
|
||||
}}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ps := &PolicyStats{
|
||||
rulesAppliedCount: tt.rulesAppliedCount,
|
||||
rulesErrorCount: tt.rulesErrorCount,
|
||||
}
|
||||
if got := ps.RulesErrorCount(); got != tt.want {
|
||||
t.Errorf("PolicyStats.RulesErrorCount() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue