forked from kata-containers/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signals_test.go
134 lines (102 loc) · 2.42 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
// Copyright (c) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"bytes"
"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)
// create buffer to save logger output
buf := &bytes.Buffer{}
savedLog := agentLog
defer func() {
agentLog = savedLog
}()
agentLog = logrus.WithField("test-agent-logger", true)
agentLog.Logger.Formatter = &logrus.TextFormatter{
DisableColors: true,
}
// capture output to buffer
agentLog.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))
}