Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
999a02ce
Commit
999a02ce
authored
Apr 01, 2019
by
Haowei Cai
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test panic log text
parent
0e61b778
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
68 additions
and
0 deletions
+68
-0
runtime_test.go
.../src/k8s.io/apimachinery/pkg/util/runtime/runtime_test.go
+68
-0
No files found.
staging/src/k8s.io/apimachinery/pkg/util/runtime/runtime_test.go
View file @
999a02ce
...
@@ -17,7 +17,12 @@ limitations under the License.
...
@@ -17,7 +17,12 @@ limitations under the License.
package
runtime
package
runtime
import
(
import
(
"bytes"
"fmt"
"fmt"
"io"
"os"
"regexp"
"strings"
"testing"
"testing"
)
)
...
@@ -69,3 +74,66 @@ func TestCustomHandleError(t *testing.T) {
...
@@ -69,3 +74,66 @@ func TestCustomHandleError(t *testing.T) {
t
.
Errorf
(
"did not receive custom handler"
)
t
.
Errorf
(
"did not receive custom handler"
)
}
}
}
}
func
TestHandleCrashLog
(
t
*
testing
.
T
)
{
log
,
err
:=
captureStderr
(
func
()
{
defer
func
()
{
if
r
:=
recover
();
r
==
nil
{
t
.
Fatalf
(
"expected a panic to recover from"
)
}
}()
defer
HandleCrash
()
panic
(
"test panic"
)
})
if
err
!=
nil
{
t
.
Fatalf
(
"%v"
,
err
)
}
// Example log:
//
// ...] Observed a panic: test panic
// goroutine 6 [running]:
// command-line-arguments.logPanic(0x..., 0x...)
// .../src/k8s.io/kubernetes/staging/src/k8s.io/apimachinery/pkg/util/runtime/runtime.go:69 +0x...
lines
:=
strings
.
Split
(
log
,
"
\n
"
)
if
len
(
lines
)
<
4
{
t
.
Fatalf
(
"panic log should have 1 line of message, 1 line per goroutine and 2 lines per function call"
)
}
if
match
,
_
:=
regexp
.
MatchString
(
"Observed a panic: test panic"
,
lines
[
0
]);
!
match
{
t
.
Errorf
(
"mismatch panic message: %s"
,
lines
[
0
])
}
// The following regexp's verify that Kubernetes panic log matches Golang stdlib
// stacktrace pattern. We need to update these regexp's if stdlib changes its pattern.
if
match
,
_
:=
regexp
.
MatchString
(
`goroutine [0-9]+ \[.+\]:`
,
lines
[
1
]);
!
match
{
t
.
Errorf
(
"mismatch goroutine: %s"
,
lines
[
1
])
}
if
match
,
_
:=
regexp
.
MatchString
(
`logPanic(.*)`
,
lines
[
2
]);
!
match
{
t
.
Errorf
(
"mismatch symbolized function name: %s"
,
lines
[
2
])
}
if
match
,
_
:=
regexp
.
MatchString
(
`runtime\.go:[0-9]+ \+0x`
,
lines
[
3
]);
!
match
{
t
.
Errorf
(
"mismatch file/line/offset information: %s"
,
lines
[
3
])
}
}
// captureStderr redirects stderr to result string, and then restore stderr from backup
func
captureStderr
(
f
func
())
(
string
,
error
)
{
r
,
w
,
err
:=
os
.
Pipe
()
if
err
!=
nil
{
return
""
,
err
}
bak
:=
os
.
Stderr
os
.
Stderr
=
w
defer
func
()
{
os
.
Stderr
=
bak
}()
resultCh
:=
make
(
chan
string
)
// copy the output in a separate goroutine so printing can't block indefinitely
go
func
()
{
var
buf
bytes
.
Buffer
io
.
Copy
(
&
buf
,
r
)
resultCh
<-
buf
.
String
()
}()
f
()
w
.
Close
()
return
<-
resultCh
,
nil
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment