github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/cmd/github-post/main_test.go (about) 1 // Copyright 2016 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package main 12 13 import ( 14 "context" 15 "os" 16 "path/filepath" 17 "strings" 18 "testing" 19 ) 20 21 func TestListFailures(t *testing.T) { 22 type issue struct { 23 testName string 24 title string 25 message string 26 author string 27 } 28 // Each test case expects a number of issues. 29 testCases := []struct { 30 pkgEnv string 31 fileName string 32 expPkg string 33 expIssues []issue 34 }{ 35 { 36 pkgEnv: "", 37 fileName: "implicit-pkg.json", 38 expPkg: "github.com/cockroachdb/cockroach/pkg/util/stop", 39 expIssues: []issue{{ 40 testName: "TestStopperWithCancelConcurrent", 41 title: "util/stop: TestStopperWithCancelConcurrent failed", 42 message: "this is just a testing issue", 43 author: "nvanbenschoten@gmail.com", 44 }}, 45 }, 46 { 47 pkgEnv: "github.com/cockroachdb/cockroach/pkg/kv/kvserver", 48 fileName: "stress-failure.json", 49 expPkg: "github.com/cockroachdb/cockroach/pkg/kv/kvserver", 50 expIssues: []issue{{ 51 testName: "TestReplicateQueueRebalance", 52 title: "kv/kvserver: TestReplicateQueueRebalance failed", 53 message: "replicate_queue_test.go:88: condition failed to evaluate within 45s: not balanced: [10 1 10 1 8]", 54 author: "petermattis@gmail.com", 55 }}, 56 }, 57 { 58 pkgEnv: "github.com/cockroachdb/cockroach/pkg/kv/kvserver", 59 fileName: "stress-fatal.json", 60 expPkg: "github.com/cockroachdb/cockroach/pkg/kv/kvserver", 61 expIssues: []issue{{ 62 testName: "TestGossipHandlesReplacedNode", 63 title: "kv/kvserver: TestGossipHandlesReplacedNode failed", 64 message: "F180711 20:13:15.826193 83 storage/replica.go:1877 [n?,s1,r1/1:/M{in-ax}] on-disk and in-memory state diverged:", 65 author: "alexdwanerobinson@gmail.com", 66 }}, 67 }, 68 { 69 pkgEnv: "github.com/cockroachdb/cockroach/pkg/storage", 70 fileName: "stress-unknown.json", 71 expPkg: "github.com/cockroachdb/cockroach/pkg/storage", 72 expIssues: []issue{{ 73 testName: "(unknown)", 74 title: "storage: package failed", 75 message: "make: *** [bin/.submodules-initialized] Error 1", 76 author: "", 77 }}, 78 }, 79 { 80 pkgEnv: "github.com/cockroachdb/cockroach/pkg/util/json", 81 fileName: "stress-subtests.json", 82 expPkg: "github.com/cockroachdb/cockroach/pkg/util/json", 83 expIssues: []issue{{ 84 testName: "TestPretty", 85 title: "util/json: TestPretty failed", 86 message: `=== RUN TestPretty/["hello",_["world"]] 87 --- FAIL: TestPretty/["hello",_["world"]] (0.00s) 88 json_test.go:1656: injected failure`, 89 author: "justin@cockroachlabs.com", 90 }}, 91 }, 92 { 93 // A test run where there's a timeout, and the timed out test was the 94 // longest running test, so the issue assumes it's the culprit. 95 // To spice things up, the test run has another test failure too. 96 pkgEnv: "github.com/cockroachdb/cockroach/pkg/kv/kvclient/kvcoord", 97 fileName: "timeout-culprit-found.json", 98 expPkg: "github.com/cockroachdb/cockroach/pkg/kv/kvclient/kvcoord", 99 expIssues: []issue{ 100 { 101 testName: "TestTxnCoordSenderPipelining", 102 title: "kv/kvclient/kvcoord: TestTxnCoordSenderPipelining failed", 103 message: `injected failure`, 104 author: "nikhil.benesch@gmail.com", 105 }, 106 { 107 testName: "TestAbortReadOnlyTransaction", 108 title: "kv/kvclient/kvcoord: TestAbortReadOnlyTransaction timed out", 109 message: `Slow failing tests: 110 TestAbortReadOnlyTransaction - 3.99s 111 TestTxnCoordSenderPipelining - 1.00s 112 113 Slow passing tests: 114 TestAnchorKey - 1.01s 115 `, 116 author: "andrei@cockroachlabs.com", 117 }, 118 }, 119 }, 120 { 121 // A test run where there's a timeout, but the test that happened to be 122 // running when the timeout hit has not been running for very long, and so 123 // the issue just names the package. 124 pkgEnv: "github.com/cockroachdb/cockroach/pkg/kv", 125 fileName: "timeout-culprit-not-found.json", 126 expPkg: "github.com/cockroachdb/cockroach/pkg/kv", 127 expIssues: []issue{ 128 { 129 testName: "(unknown)", 130 title: "kv: package timed out", 131 message: `Slow failing tests: 132 TestXXX/sub3 - 0.50s 133 134 Slow passing tests: 135 TestXXA - 1.00s 136 `, 137 author: "", 138 }, 139 }, 140 }, 141 { 142 // Like the above, except this time the output comes from a stress run, 143 // not from the test binary directly. 144 pkgEnv: "github.com/cockroachdb/cockroach/pkg/kv", 145 fileName: "stress-timeout-culprit-not-found.json", 146 expPkg: "github.com/cockroachdb/cockroach/pkg/kv", 147 expIssues: []issue{ 148 { 149 testName: "(unknown)", 150 title: "kv: package timed out", 151 message: `Slow failing tests: 152 TestXXX/sub1 - 0.49s 153 154 Slow passing tests: 155 TestXXB - 1.01s 156 TestXXA - 1.00s 157 `, 158 author: "", 159 }, 160 }, 161 }, 162 { 163 // A stress timeout where the test running when the timeout is hit is the 164 // longest. 165 pkgEnv: "github.com/cockroachdb/cockroach/pkg/kv", 166 fileName: "stress-timeout-culprit-found.json", 167 expPkg: "github.com/cockroachdb/cockroach/pkg/kv", 168 expIssues: []issue{ 169 { 170 testName: "TestXXX/sub2", 171 title: "kv: TestXXX/sub2 timed out", 172 message: `Slow failing tests: 173 TestXXX/sub2 - 2.99s 174 175 Slow passing tests: 176 TestXXB - 1.01s 177 TestXXA - 1.00s 178 `, 179 author: "", 180 }, 181 }, 182 }, 183 { 184 // A panic in a test. 185 pkgEnv: "github.com/cockroachdb/cockroach/pkg/kv", 186 fileName: "stress-panic.json", 187 expPkg: "github.com/cockroachdb/cockroach/pkg/kv", 188 expIssues: []issue{ 189 { 190 testName: "TestXXX", 191 title: "kv: TestXXX failed", 192 message: `panic: induced panic`, 193 author: "", 194 }, 195 }, 196 }, 197 { 198 // A panic outside of a test (in this case, in a package init function). 199 pkgEnv: "github.com/cockroachdb/cockroach/pkg/kv", 200 fileName: "stress-init-panic.json", 201 expPkg: "github.com/cockroachdb/cockroach/pkg/kv", 202 expIssues: []issue{ 203 { 204 testName: "(unknown)", 205 title: "kv: package failed", 206 message: `panic: induced panic`, 207 author: "", 208 }, 209 }, 210 }, 211 } 212 for _, c := range testCases { 213 t.Run(c.fileName, func(t *testing.T) { 214 if err := os.Setenv("PKG", c.pkgEnv); err != nil { 215 t.Fatal(err) 216 } 217 218 file, err := os.Open(filepath.Join("testdata", c.fileName)) 219 if err != nil { 220 t.Fatal(err) 221 } 222 defer file.Close() 223 curIssue := 0 224 225 f := func(_ context.Context, title, packageName, testName, testMessage, author string) error { 226 if t.Failed() { 227 return nil 228 } 229 if curIssue >= len(c.expIssues) { 230 t.Errorf("unexpected issue filed. title: %s", title) 231 } 232 if exp := c.expPkg; exp != packageName { 233 t.Errorf("expected package %s, but got %s", exp, packageName) 234 } 235 if exp := c.expIssues[curIssue].testName; exp != testName { 236 t.Errorf("expected test name %s, but got %s", exp, testName) 237 } 238 if exp := c.expIssues[curIssue].author; exp != "" && exp != author { 239 t.Errorf("expected author %s, but got %s", exp, author) 240 } 241 if exp := c.expIssues[curIssue].title; exp != title { 242 t.Errorf("expected title %s, but got %s", exp, title) 243 } 244 if exp := c.expIssues[curIssue].message; !strings.Contains(testMessage, exp) { 245 t.Errorf("expected message containing %s, but got:\n%s", exp, testMessage) 246 } 247 // On next invocation, we'll check the next expected issue. 248 curIssue++ 249 return nil 250 } 251 if err := listFailures(context.Background(), file, f); err != nil { 252 t.Fatal(err) 253 } 254 if curIssue != len(c.expIssues) { 255 t.Fatalf("expected %d issues, got: %d", len(c.expIssues), curIssue) 256 } 257 }) 258 } 259 }