forked from kata-containers/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signals_test.go
140 lines (108 loc) · 2.53 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
// Copyright (c) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package signals
import (
"bytes"
"os"
"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)
fn := goruntime.FuncForPC(pc[0])
name := fn.Name()
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`))
assert.True(strings.Contains(b, name))
}