forked from kata-containers/kata-containers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signals_test.go
205 lines (156 loc) · 3.79 KB
/
signals_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright (c) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package signals
import (
"bytes"
"errors"
"os"
"os/exec"
"reflect"
goruntime "runtime"
"sort"
"strings"
"syscall"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func TestSignalFatalSignal(t *testing.T) {
assert := assert.New(t)
for sig, fatal := range handledSignalsMap {
result := NonFatalSignal(sig)
if fatal {
assert.False(result)
} else {
assert.True(result)
}
}
}
func TestSignalHandledSignalsMap(t *testing.T) {
assert := assert.New(t)
for sig, fatal := range handledSignalsMap {
result := FatalSignal(sig)
if fatal {
assert.True(result)
} else {
assert.False(result)
}
}
}
func TestSignalHandledSignals(t *testing.T) {
assert := assert.New(t)
var expected []syscall.Signal
for sig := range handledSignalsMap {
expected = append(expected, sig)
}
got := HandledSignals()
sort.Slice(expected, func(i, j int) bool {
return int(expected[i]) < int(expected[j])
})
sort.Slice(got, func(i, j int) bool {
return int(got[i]) < int(got[j])
})
assert.True(reflect.DeepEqual(expected, got))
}
func TestSignalNonFatalSignal(t *testing.T) {
assert := assert.New(t)
for sig, fatal := range handledSignalsMap {
result := NonFatalSignal(sig)
if fatal {
assert.False(result)
} else {
assert.True(result)
}
}
}
func TestSignalFatalSignalInvalidSignal(t *testing.T) {
assert := assert.New(t)
sig := syscall.SIGXCPU
result := FatalSignal(sig)
assert.False(result)
}
func TestSignalNonFatalSignalInvalidSignal(t *testing.T) {
assert := assert.New(t)
sig := syscall.SIGXCPU
result := NonFatalSignal(sig)
assert.False(result)
}
func TestSignalBacktrace(t *testing.T) {
assert := assert.New(t)
savedLog := signalLog
defer func() {
signalLog = savedLog
}()
signalLog = logrus.WithFields(logrus.Fields{
"name": "name",
"pid": os.Getpid(),
"source": "throttler",
"test-logger": true})
// create buffer to save logger output
buf := &bytes.Buffer{}
savedOut := signalLog.Logger.Out
defer func() {
signalLog.Logger.Out = savedOut
}()
// capture output to buffer
signalLog.Logger.Out = buf
// determine name of *this* function
pc := make([]uintptr, 1)
goruntime.Callers(1, pc)
Backtrace()
b := buf.String()
// very basic tests to check if a backtrace was produced
assert.True(strings.Contains(b, "contention:"))
assert.True(strings.Contains(b, `level=error`))
}
func TestSignalHandlePanic(t *testing.T) {
assert := assert.New(t)
savedLog := signalLog
defer func() {
signalLog = savedLog
}()
signalLog = logrus.WithFields(logrus.Fields{
"name": "name",
"pid": os.Getpid(),
"source": "throttler",
"test-logger": true})
// Create buffer to save logger output.
buf := &bytes.Buffer{}
savedOut := signalLog.Logger.Out
defer func() {
signalLog.Logger.Out = savedOut
}()
// Capture output to buffer.
signalLog.Logger.Out = buf
HandlePanic(nil)
b := buf.String()
assert.True(len(b) == 0)
}
func TestSignalHandlePanicWithError(t *testing.T) {
assert := assert.New(t)
if os.Getenv("CALL_EXIT") != "1" {
cmd := exec.Command(os.Args[0], "-test.run=TestSignalHandlePanicWithError")
cmd.Env = append(os.Environ(), "CALL_EXIT=1")
err := cmd.Run()
assert.True(err != nil)
exitError, ok := err.(*exec.ExitError)
assert.True(ok)
assert.True(exitError.ExitCode() == 1)
return
}
signalLog = logrus.WithFields(logrus.Fields{
"name": "name",
"pid": os.Getpid(),
"source": "throttler",
"test-logger": true})
// Create buffer to save logger output.
buf := &bytes.Buffer{}
// Capture output to buffer.
signalLog.Logger.Out = buf
dieCallBack := func() {}
defer HandlePanic(dieCallBack)
e := errors.New("test-panic")
panic(e)
}