log_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package framework_test
  14. import (
  15. "errors"
  16. "regexp"
  17. "sort"
  18. "strings"
  19. "testing"
  20. "github.com/onsi/ginkgo"
  21. "github.com/onsi/ginkgo/config"
  22. "github.com/onsi/ginkgo/reporters"
  23. "github.com/onsi/gomega"
  24. "k8s.io/kubernetes/test/e2e/framework"
  25. )
  26. // The line number of the following code is checked in TestFailureOutput below.
  27. // Be careful when moving it around or changing the import statements above.
  28. // Here are some intentionally blank lines that can be removed to compensate
  29. // for future additional import statements.
  30. //
  31. //
  32. //
  33. //
  34. //
  35. //
  36. func runTests(t *testing.T, reporter ginkgo.Reporter) {
  37. // This source code line will be part of the stack dump comparison.
  38. ginkgo.RunSpecsWithDefaultAndCustomReporters(t, "Logging Suite", []ginkgo.Reporter{reporter})
  39. }
  40. var _ = ginkgo.Describe("log", func() {
  41. ginkgo.BeforeEach(func() {
  42. framework.Logf("before")
  43. })
  44. ginkgo.It("fails", func() {
  45. func() {
  46. framework.Failf("I'm failing.")
  47. }()
  48. })
  49. ginkgo.It("asserts", func() {
  50. framework.ExpectEqual(false, true, "false is never true")
  51. })
  52. ginkgo.It("error", func() {
  53. err := errors.New("an error with a long, useless description")
  54. framework.ExpectNoError(err, "hard-coded error")
  55. })
  56. ginkgo.It("equal", func() {
  57. framework.ExpectEqual(0, 1, "of course it's not equal...")
  58. })
  59. ginkgo.AfterEach(func() {
  60. framework.Logf("after")
  61. framework.ExpectEqual(true, false, "true is never false either")
  62. })
  63. })
  64. func TestFailureOutput(t *testing.T) {
  65. // Run the Ginkgo suite with output collected by a custom
  66. // reporter in adddition to the default one. To see what the full
  67. // Ginkgo report looks like, run this test with "go test -v".
  68. config.DefaultReporterConfig.FullTrace = true
  69. gomega.RegisterFailHandler(framework.Fail)
  70. fakeT := &testing.T{}
  71. reporter := reporters.NewFakeReporter()
  72. runTests(fakeT, reporter)
  73. // Now check the output.
  74. actual := normalizeReport(*reporter)
  75. // output from AfterEach
  76. commonOutput := "\n\nINFO: after\nFAIL: true is never false either\nExpected\n <bool>: true\nto equal\n <bool>: false\n\nFull Stack Trace\nk8s.io/kubernetes/test/e2e/framework_test.glob..func1.6()\n\tlog_test.go:71\nk8s.io/kubernetes/test/e2e/framework_test.runTests()\n\tlog_test.go:47\n\n"
  77. // Sorted by name!
  78. expected := suiteResults{
  79. testResult{
  80. name: "[Top Level] log asserts",
  81. output: "INFO: before\nFAIL: false is never true\nExpected\n <bool>: false\nto equal\n <bool>: true\n\nFull Stack Trace\nk8s.io/kubernetes/test/e2e/framework_test.glob..func1.3()\n\tlog_test.go:60\nk8s.io/kubernetes/test/e2e/framework_test.runTests()\n\tlog_test.go:47" + commonOutput,
  82. failure: "false is never true\nExpected\n <bool>: false\nto equal\n <bool>: true",
  83. stack: "k8s.io/kubernetes/test/e2e/framework_test.glob..func1.3()\n\tlog_test.go:60\nk8s.io/kubernetes/test/e2e/framework_test.runTests()\n\tlog_test.go:47\n",
  84. },
  85. testResult{
  86. name: "[Top Level] log equal",
  87. output: "INFO: before\nFAIL: of course it's not equal...\nExpected\n <int>: 0\nto equal\n <int>: 1\n\nFull Stack Trace\nk8s.io/kubernetes/test/e2e/framework_test.glob..func1.5()\n\tlog_test.go:67\nk8s.io/kubernetes/test/e2e/framework_test.runTests()\n\tlog_test.go:47" + commonOutput,
  88. failure: "of course it's not equal...\nExpected\n <int>: 0\nto equal\n <int>: 1",
  89. stack: "k8s.io/kubernetes/test/e2e/framework_test.glob..func1.5()\n\tlog_test.go:67\nk8s.io/kubernetes/test/e2e/framework_test.runTests()\n\tlog_test.go:47\n",
  90. },
  91. testResult{
  92. name: "[Top Level] log error",
  93. output: "INFO: before\nFAIL: hard-coded error\nUnexpected error:\n <*errors.errorString>: {\n s: \"an error with a long, useless description\",\n }\n an error with a long, useless description\noccurred\n\nFull Stack Trace\nk8s.io/kubernetes/test/e2e/framework_test.glob..func1.4()\n\tlog_test.go:64\nk8s.io/kubernetes/test/e2e/framework_test.runTests()\n\tlog_test.go:47" + commonOutput,
  94. failure: "hard-coded error\nUnexpected error:\n <*errors.errorString>: {\n s: \"an error with a long, useless description\",\n }\n an error with a long, useless description\noccurred",
  95. stack: "k8s.io/kubernetes/test/e2e/framework_test.glob..func1.4()\n\tlog_test.go:64\nk8s.io/kubernetes/test/e2e/framework_test.runTests()\n\tlog_test.go:47\n",
  96. },
  97. testResult{
  98. name: "[Top Level] log fails",
  99. output: "INFO: before\nFAIL: I'm failing.\n\nFull Stack Trace\nk8s.io/kubernetes/test/e2e/framework_test.glob..func1.2.1(...)\n\tlog_test.go:56\nk8s.io/kubernetes/test/e2e/framework_test.glob..func1.2()\n\tlog_test.go:57\nk8s.io/kubernetes/test/e2e/framework_test.runTests()\n\tlog_test.go:47" + commonOutput,
  100. failure: "I'm failing.",
  101. stack: "k8s.io/kubernetes/test/e2e/framework_test.glob..func1.2.1(...)\n\tlog_test.go:56\nk8s.io/kubernetes/test/e2e/framework_test.glob..func1.2()\n\tlog_test.go:57\nk8s.io/kubernetes/test/e2e/framework_test.runTests()\n\tlog_test.go:47\n",
  102. },
  103. }
  104. // Compare individual fields. Comparing the slices leads to unreadable error output when there is any mismatch.
  105. framework.ExpectEqual(len(actual), len(expected), "%d entries in %v", len(expected), actual)
  106. for i, a := range actual {
  107. b := expected[i]
  108. framework.ExpectEqual(a.name, b.name, "name in %d", i)
  109. framework.ExpectEqual(a.output, b.output, "output in %d", i)
  110. framework.ExpectEqual(a.failure, b.failure, "failure in %d", i)
  111. // There may be additional stack entries from the "testing" package at the
  112. // end. We ignore those in the comparison because the line number in them
  113. // varies.
  114. framework.ExpectEqual(a.stack, b.stack, "stack in %d: %s", i, a.stack)
  115. }
  116. }
  117. type testResult struct {
  118. name string
  119. // output written to GinkgoWriter during test.
  120. output string
  121. // failure is SpecSummary.Failure.Message with varying parts stripped.
  122. failure string
  123. // stack is a normalized version (just file names, function parametes stripped) of
  124. // Ginkgo's FullStackTrace of a failure. Empty if no failure.
  125. stack string
  126. }
  127. type suiteResults []testResult
  128. func normalizeReport(report reporters.FakeReporter) suiteResults {
  129. var results suiteResults
  130. for _, spec := range report.SpecSummaries {
  131. results = append(results, testResult{
  132. name: strings.Join(spec.ComponentTexts, " "),
  133. output: normalizeLocation(stripAddresses(stripTimes(spec.CapturedOutput))),
  134. failure: stripAddresses(stripTimes(spec.Failure.Message)),
  135. stack: normalizeLocation(spec.Failure.Location.FullStackTrace),
  136. })
  137. }
  138. sort.Slice(results, func(i, j int) bool {
  139. return strings.Compare(results[i].name, results[j].name) < 0
  140. })
  141. return results
  142. }
  143. // timePrefix matches "Jul 17 08:08:25.950: " at the beginning of each line.
  144. var timePrefix = regexp.MustCompile(`(?m)^[[:alpha:]]{3} +[[:digit:]]{1,2} +[[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}.[[:digit:]]{3}: `)
  145. func stripTimes(in string) string {
  146. return timePrefix.ReplaceAllString(in, "")
  147. }
  148. // instanceAddr matches " | 0xc0003dec60>"
  149. var instanceAddr = regexp.MustCompile(` \| 0x[0-9a-fA-F]+>`)
  150. func stripAddresses(in string) string {
  151. return instanceAddr.ReplaceAllString(in, ">")
  152. }
  153. // stackLocation matches "<some path>/<file>.go:75 +0x1f1" after a slash (built
  154. // locally) or one of a few relative paths (built in the Kubernetes CI).
  155. var stackLocation = regexp.MustCompile(`(?:/|vendor/|test/|GOROOT/).*/([[:^space:]]+.go:[[:digit:]]+)( \+0x[0-9a-fA-F]+)?`)
  156. // functionArgs matches "<function name>(...)".
  157. var functionArgs = regexp.MustCompile(`([[:alpha:]]+)\(.*\)`)
  158. // testFailureOutput matches TestFailureOutput() and its source followed by additional stack entries:
  159. //
  160. // k8s.io/kubernetes/test/e2e/framework_test.TestFailureOutput(0xc000558800)
  161. // /nvme/gopath/src/k8s.io/kubernetes/test/e2e/framework/log/log_test.go:73 +0x1c9
  162. // testing.tRunner(0xc000558800, 0x1af2848)
  163. // /nvme/gopath/go/src/testing/testing.go:865 +0xc0
  164. // created by testing.(*T).Run
  165. // /nvme/gopath/go/src/testing/testing.go:916 +0x35a
  166. var testFailureOutput = regexp.MustCompile(`(?m)^k8s.io/kubernetes/test/e2e/framework_test\.TestFailureOutput\(.*\n\t.*(\n.*\n\t.*)*`)
  167. // normalizeLocation removes path prefix and function parameters and certain stack entries
  168. // that we don't care about.
  169. func normalizeLocation(in string) string {
  170. out := in
  171. out = stackLocation.ReplaceAllString(out, "$1")
  172. out = functionArgs.ReplaceAllString(out, "$1()")
  173. out = testFailureOutput.ReplaceAllString(out, "")
  174. return out
  175. }