|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package log_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "regexp" |
| 22 | + "sort" |
| 23 | + "strings" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/onsi/ginkgo" |
| 27 | + "github.com/onsi/ginkgo/config" |
| 28 | + "github.com/onsi/ginkgo/reporters" |
| 29 | + "github.com/onsi/gomega" |
| 30 | + |
| 31 | + "k8s.io/kubernetes/test/e2e/framework" |
| 32 | + "k8s.io/kubernetes/test/e2e/framework/ginkgowrapper" |
| 33 | + "k8s.io/kubernetes/test/e2e/framework/log" |
| 34 | +) |
| 35 | + |
| 36 | +var _ = ginkgo.Describe("log", func() { |
| 37 | + ginkgo.BeforeEach(func() { |
| 38 | + log.Logf("before") |
| 39 | + }) |
| 40 | + ginkgo.It("fails", func() { |
| 41 | + func() { |
| 42 | + log.Failf("I'm failing.") |
| 43 | + }() |
| 44 | + }) |
| 45 | + ginkgo.It("asserts", func() { |
| 46 | + gomega.Expect(false).To(gomega.Equal(true), "false is never true") |
| 47 | + }) |
| 48 | + ginkgo.It("error", func() { |
| 49 | + err := errors.New("I'm an error, nice to meet to.") |
| 50 | + framework.ExpectNoError(err, "hard-coded error") |
| 51 | + }) |
| 52 | + ginkgo.AfterEach(func() { |
| 53 | + log.Logf("after") |
| 54 | + gomega.Expect(true).To(gomega.Equal(false), "true is never false either") |
| 55 | + }) |
| 56 | +}) |
| 57 | + |
| 58 | +func TestFailureOutput(t *testing.T) { |
| 59 | + // Run the Ginkgo suite with output collected by a custom |
| 60 | + // reporter in adddition to the default one. To see what the full |
| 61 | + // Ginkgo report looks like, run this test with "go test -v". |
| 62 | + config.DefaultReporterConfig.FullTrace = true |
| 63 | + gomega.RegisterFailHandler(ginkgowrapper.Fail) |
| 64 | + fakeT := &testing.T{} |
| 65 | + reporter := reporters.NewFakeReporter() |
| 66 | + ginkgo.RunSpecsWithDefaultAndCustomReporters(fakeT, "Logging Suite", []ginkgo.Reporter{reporter}) |
| 67 | + |
| 68 | + // Now check the output. |
| 69 | + g := gomega.NewGomegaWithT(t) |
| 70 | + g.Expect(normalizeReport(*reporter)).To(gomega.Equal(suiteResults{ |
| 71 | + testResult{ |
| 72 | + name: "[Top Level] log asserts", |
| 73 | + // TODO: also log the failed assertion as it happens |
| 74 | + output: "INFO: before\nINFO: after\n", |
| 75 | + failure: "false is never true\nExpected\n <bool>: false\nto equal\n <bool>: true", |
| 76 | + // TODO: ginkowrapper.Fail should also prune this stack. |
| 77 | + stack: "\tassertion.go:75\nk8s.io/kubernetes/vendor/github.com/onsi/gomega/internal/assertion.(*Assertion).To()\n\tassertion.go:38\nk8s.io/kubernetes/test/e2e/framework/log_test.glob..func1.3()\n\tlogger_test.go:46\nk8s.io/kubernetes/vendor/github.com/onsi/ginkgo/internal/leafnodes.(*runner).runSync()\n\tlogger_test.go:66\n", |
| 78 | + }, |
| 79 | + testResult{ |
| 80 | + name: "[Top Level] log error", |
| 81 | + // TODO: the additional information about the error should be logged |
| 82 | + output: "INFO: before\nINFO: Unexpected error occurred: I'm an error, nice to meet to.\nINFO: after\n", |
| 83 | + failure: "hard-coded error\nUnexpected error:\n <*errors.errorString>: {\n s: \"I'm an error, nice to meet to.\",\n }\n I'm an error, nice to meet to.\noccurred", |
| 84 | + // TODO: ginkowrapper.Fail should also prune this stack. |
| 85 | + stack: "\tutil.go:1362\nk8s.io/kubernetes/test/e2e/framework.ExpectNoError()\n\tutil.go:1356\nk8s.io/kubernetes/test/e2e/framework/log_test.glob..func1.4()\n\tlogger_test.go:49\nk8s.io/kubernetes/vendor/github.com/onsi/ginkgo/internal/leafnodes.(*runner).runSync()\n\tlogger_test.go:65\n", |
| 86 | + }, |
| 87 | + testResult{ |
| 88 | + name: "[Top Level] log fails", |
| 89 | + // TODO: why is the failure log as "INFO"? |
| 90 | + output: "INFO: before\nINFO: I'm failing.\nINFO: after\n", |
| 91 | + failure: "I'm failing.", |
| 92 | + // TODO: ginkowrapper.Fail should also prune this stack. |
| 93 | + stack: "\tlogger.go:52\nk8s.io/kubernetes/test/e2e/framework/log.Failf()\n\tlogger.go:44\nk8s.io/kubernetes/test/e2e/framework/log_test.glob..func1.2.1(...)\n\tlogger_test.go:41\nk8s.io/kubernetes/test/e2e/framework/log_test.glob..func1.2()\n\tlogger_test.go:42\nk8s.io/kubernetes/vendor/github.com/onsi/ginkgo/internal/leafnodes.(*runner).runSync()\n\tlogger_test.go:65\n", |
| 94 | + }, |
| 95 | + })) |
| 96 | +} |
| 97 | + |
| 98 | +type testResult struct { |
| 99 | + name string |
| 100 | + // output written to GinkgoWriter during test. |
| 101 | + output string |
| 102 | + // failure is SpecSummary.Failure.Message with varying parts stripped. |
| 103 | + failure string |
| 104 | + // stack is a normalized version (just file names, function parametes stripped) of |
| 105 | + // Ginkgo's FullStackTrace of a failure. Empty if no failure. |
| 106 | + stack string |
| 107 | +} |
| 108 | + |
| 109 | +type suiteResults []testResult |
| 110 | + |
| 111 | +func normalizeReport(report reporters.FakeReporter) suiteResults { |
| 112 | + var results suiteResults |
| 113 | + for _, spec := range report.SpecSummaries { |
| 114 | + results = append(results, testResult{ |
| 115 | + name: strings.Join(spec.ComponentTexts, " "), |
| 116 | + output: stripAddresses(stripTimes(spec.CapturedOutput)), |
| 117 | + failure: stripAddresses(stripTimes(spec.Failure.Message)), |
| 118 | + stack: normalizeLocation(spec.Failure.Location.FullStackTrace), |
| 119 | + }) |
| 120 | + } |
| 121 | + sort.Slice(results, func(i, j int) bool { |
| 122 | + return strings.Compare(results[i].name, results[j].name) < 0 |
| 123 | + }) |
| 124 | + return results |
| 125 | +} |
| 126 | + |
| 127 | +// timePrefix matches "Jul 17 08:08:25.950: " at the beginning of each line. |
| 128 | +var timePrefix = regexp.MustCompile(`(?m)^[[:alpha:]]{3} [[:digit:]]{1,2} [[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}.[[:digit:]]{3}: `) |
| 129 | + |
| 130 | +func stripTimes(in string) string { |
| 131 | + return timePrefix.ReplaceAllString(in, "") |
| 132 | +} |
| 133 | + |
| 134 | +// instanceAddr matches " | 0xc0003dec60>" |
| 135 | +var instanceAddr = regexp.MustCompile(` \| 0x[0-9a-fA-F]+>`) |
| 136 | + |
| 137 | +func stripAddresses(in string) string { |
| 138 | + return instanceAddr.ReplaceAllString(in, ">") |
| 139 | +} |
| 140 | + |
| 141 | +// stackLocation matches "\t/<some path>/<file>.go:75 +0x1f1". |
| 142 | +var stackLocation = regexp.MustCompile(`/.*/([[:^space:]]+.go:[[:digit:]]+)( \+0x[0-9a-fA-F]+)?`) |
| 143 | + |
| 144 | +// functionArgs matches "<function name>(...)". |
| 145 | +var functionArgs = regexp.MustCompile(`([[:alpha:]]+)\(.*\)`) |
| 146 | + |
| 147 | +// testingStackEntries matches "testing.tRunner" and "created by" entries. |
| 148 | +var testingStackEntries = regexp.MustCompile(`(?m)(?:testing\.|created by).*\n\t.*\n`) |
| 149 | + |
| 150 | +// normalizeLocation removes path prefix and function parameters and certain stack entries |
| 151 | +// that we don't care about. |
| 152 | +func normalizeLocation(in string) string { |
| 153 | + out := in |
| 154 | + out = stackLocation.ReplaceAllString(out, "$1") |
| 155 | + out = functionArgs.ReplaceAllString(out, "$1()") |
| 156 | + out = testingStackEntries.ReplaceAllString(out, "") |
| 157 | + return out |
| 158 | +} |
0 commit comments