forked from kata-containers/kata-containers
-
Notifications
You must be signed in to change notification settings - Fork 1
/
display_test.go
149 lines (108 loc) · 2.63 KB
/
display_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//
// Copyright (c) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestNewDisplayHandlers(t *testing.T) {
assert := assert.New(t)
h := NewDisplayHandlers()
assert.NotEmpty(h.handlers)
}
type mockDisplayHandler struct {
}
var handlerCalled = false
func (m *mockDisplayHandler) Display(entries *LogEntries, fieldNames []string, file *os.File) error {
handlerCalled = true
return nil
}
func TestDisplayHandlersFind(t *testing.T) {
assert := assert.New(t)
origHandlers := handlers
handlers = map[string]displayHandler{}
defer func() {
handlers = origHandlers
}()
h := NewDisplayHandlers()
assert.Empty(h.handlers)
assert.Nil(h.find("foo"))
handlers = map[string]displayHandler{
"foo": &mockDisplayHandler{},
}
h = NewDisplayHandlers()
assert.NotEmpty(h.handlers)
assert.NotNil(h.find("foo"))
assert.Equal(h.find("foo"), &mockDisplayHandler{})
}
func TestDisplayHandlersGet(t *testing.T) {
assert := assert.New(t)
origHandlers := handlers
handlers = map[string]displayHandler{
"foo": &mockDisplayHandler{},
"bar": &mockDisplayHandler{},
"baz": &mockDisplayHandler{},
}
defer func() {
handlers = origHandlers
}()
h := NewDisplayHandlers()
assert.NotEmpty(h.handlers)
// list should be sorted
expected := []string{"bar", "baz", "foo"}
assert.Equal(expected, h.Get())
}
func TestDisplayHandlersSupplementEntries(t *testing.T) {
assert := assert.New(t)
now := time.Now().UTC()
later := now.Add(time.Second * 1)
diff := later.Sub(now)
entries := []LogEntry{
{Time: now},
{Time: later},
}
le := LogEntries{
Entries: entries,
}
d := NewDisplayHandlers()
assert.NotEmpty(d.handlers)
d.supplementEntries(&le)
assert.Equal(entries[0].Count, uint64(1))
assert.Equal(entries[0].Time, now)
assert.Equal(entries[0].TimeDelta, NewTimeDelta(0))
assert.Equal(entries[1].Count, uint64(2))
assert.Equal(entries[1].Time, later)
assert.Equal(entries[1].TimeDelta, NewTimeDelta(diff))
}
func TestDisplayHandlersHandle(t *testing.T) {
assert := assert.New(t)
now := time.Now().UTC()
later := now.Add(time.Second * 1)
entries := []LogEntry{
{Time: now},
{Time: later},
}
le := LogEntries{
Entries: entries,
}
origHandlers := handlers
handlers = map[string]displayHandler{
"foo": &mockDisplayHandler{},
}
defer func() {
handlers = origHandlers
}()
d := NewDisplayHandlers()
assert.NotEmpty(d.handlers)
assert.False(handlerCalled)
err := d.Handle(&le, "invalid", os.Stdout)
assert.Error(err)
err = d.Handle(&le, "foo", os.Stdout)
assert.NoError(err)
assert.True(handlerCalled)
}