forked from kata-containers/shim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework the signal handling code so that if debug is enabled and a `SIGUSR1` signal is received, backtrace to the system log but continue to run. Added some basic tests for the signal handling code. Fixes kata-containers#76. Signed-off-by: James O. D. Hunt <[email protected]>
- Loading branch information
1 parent
435f325
commit 42bd402
Showing
4 changed files
with
178 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// Copyright (c) 2018 Intel Corporation | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package main | ||
|
||
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 := shimLog | ||
defer func() { | ||
shimLog = savedLog | ||
}() | ||
|
||
shimLog = logrus.WithFields(logrus.Fields{ | ||
"name": shimName, | ||
"pid": os.Getpid(), | ||
"source": "shim", | ||
"test-logger": true}) | ||
|
||
// create buffer to save logger output | ||
buf := &bytes.Buffer{} | ||
|
||
savedOut := shimLog.Logger.Out | ||
defer func() { | ||
shimLog.Logger.Out = savedOut | ||
}() | ||
|
||
// capture output to buffer | ||
shimLog.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)) | ||
} |