Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(168)

Side by Side Diff: go/src/infra/monitoring/analyzer/analyzer_test.go

Issue 1874053002: [alerts-dispatcher] update to build again :) also fixes a concurrent map access (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: typos Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package analyzer 5 package analyzer
6 6
7 import ( 7 import (
8 "fmt" 8 "fmt"
9 "infra/libs/testing/ansidiff" 9 "infra/libs/testing/ansidiff"
10 "infra/monitoring/client" 10 "infra/monitoring/client"
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 name: "Stale master", 100 name: "Stale master",
101 master: "fake.master", 101 master: "fake.master",
102 be: messages.BuildExtract{ 102 be: messages.BuildExtract{
103 CreatedTimestamp: messages.EpochTime(100), 103 CreatedTimestamp: messages.EpochTime(100),
104 }, 104 },
105 t: time.Unix(100, 0).Add(20 * time.Minute), 105 t: time.Unix(100, 0).Add(20 * time.Minute),
106 want: []messages.Alert{ 106 want: []messages.Alert{
107 { 107 {
108 Key: "stale master: fake.master", 108 Key: "stale master: fake.master",
109 Title: "Stale fake.master master dat a", 109 Title: "Stale fake.master master dat a",
110 » » » » » Body: fmt.Sprintf("%s elapsed since last update.", 20*time.Minute), 110 » » » » » Type: "stale-master",
111 » » » » » Severity: staleMasterSev,
112 » » » » » Body: "0h 20m elapsed since last up date.",
111 Time: messages.TimeToEpochTime(time .Unix(100, 0).Add(20 * time.Minute)), 113 Time: messages.TimeToEpochTime(time .Unix(100, 0).Add(20 * time.Minute)),
112 Links: []messages.Link{{"Master", cl ient.MasterURL("fake.master")}}, 114 Links: []messages.Link{{"Master", cl ient.MasterURL("fake.master")}},
113 StartTime: messages.EpochTime(100), 115 StartTime: messages.EpochTime(100),
114 }, 116 },
115 }, 117 },
116 }, 118 },
117 { 119 {
118 name: "Future master", 120 name: "Future master",
119 master: "fake.master", 121 master: "fake.master",
120 be: messages.BuildExtract{ 122 be: messages.BuildExtract{
121 CreatedTimestamp: messages.EpochTime(110), 123 CreatedTimestamp: messages.EpochTime(110),
122 }, 124 },
123 t: time.Unix(100, 0), 125 t: time.Unix(100, 0),
124 want: []messages.Alert{}, 126 want: []messages.Alert{},
125 }, 127 },
126 } 128 }
127 129
128 a := New(&mockReader{}, 0, 10) 130 a := New(&mockReader{}, 0, 10)
129 131
130 for _, test := range tests { 132 for _, test := range tests {
131 a.Now = fakeNow(test.t) 133 a.Now = fakeNow(test.t)
132 got := a.MasterAlerts(test.master, &test.be) 134 got := a.MasterAlerts(test.master, &test.be)
133 if !reflect.DeepEqual(got, test.want) { 135 if !reflect.DeepEqual(got, test.want) {
134 » » » t.Errorf("%s failed. Got %+v, want: %+v", test.name, got , test.want) 136 » » » t.Errorf("%s failed. Got %+v, want: %+v\nDiff: %v", test .name, got, test.want,
137 » » » » ansidiff.Diff(got, test.want))
135 } 138 }
136 } 139 }
137 } 140 }
138 141
139 func TestBuilderAlerts(t *testing.T) { 142 func TestBuilderAlerts(t *testing.T) {
140 tests := []struct { 143 tests := []struct {
141 name string 144 name string
142 url string 145 url string
143 be messages.BuildExtract 146 be messages.BuildExtract
144 filter string 147 filter string
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 b: messages.Builder{ 258 b: messages.Builder{
256 State: messages.StateBuilding, 259 State: messages.StateBuilding,
257 BuilderName: "fake.builder", 260 BuilderName: "fake.builder",
258 CachedBuilds: []int64{0, 1}, 261 CachedBuilds: []int64{0, 1},
259 }, 262 },
260 time: time.Unix(0, 0).Add(4 * time.Hour), 263 time: time.Unix(0, 0).Add(4 * time.Hour),
261 wantAlerts: []messages.Alert{ 264 wantAlerts: []messages.Alert{
262 { 265 {
263 Key: "fake.master.fake.builder.hung ", 266 Key: "fake.master.fake.builder.hung ",
264 Title: "fake.master.fake.builder is h ung in step fake_step.", 267 Title: "fake.master.fake.builder is h ung in step fake_step.",
268 Type: "hung-builder",
265 Time: messages.TimeToEpochTime(time. Unix(0, 0).Add(4 * time.Hour)), 269 Time: messages.TimeToEpochTime(time. Unix(0, 0).Add(4 * time.Hour)),
266 Body: "fake.master.fake.builder has been building for 3h59m50s (last step update 1970-01-01 00:00:10 +0000 UTC), pas t the alerting threshold of 3h0m0s", 270 Body: "fake.master.fake.builder has been building for 3h59m50s (last step update 1970-01-01 00:00:10 +0000 UTC), pas t the alerting threshold of 3h0m0s",
267 » » » » » Severity: 1, 271 » » » » » Severity: hungBuilderSev,
268 Links: []messages.Link{ 272 Links: []messages.Link{
269 {Title: "Builder", Href: "https: //build.chromium.org/p/fake.master/builders/fake.builder"}, 273 {Title: "Builder", Href: "https: //build.chromium.org/p/fake.master/builders/fake.builder"},
270 {Title: "Last build", Href: "htt ps://build.chromium.org/p/fake.master/builders/fake.builder/builds/1"}, 274 {Title: "Last build", Href: "htt ps://build.chromium.org/p/fake.master/builders/fake.builder/builds/1"},
271 {Title: "Last build step", Href: "https://build.chromium.org/p/fake.master/builders/fake.builder/builds/1/steps/ fake_step"}, 275 {Title: "Last build step", Href: "https://build.chromium.org/p/fake.master/builders/fake.builder/builds/1/steps/ fake_step"},
272 }, 276 },
273 }, 277 },
274 }, 278 },
275 wantErrs: []error{}, 279 wantErrs: []error{},
276 }, 280 },
277 } 281 }
278 282
279 a := New(nil, 0, 10) 283 a := New(nil, 0, 10)
280 284
281 for _, test := range tests { 285 for _, test := range tests {
282 a.Now = fakeNow(test.time) 286 a.Now = fakeNow(test.time)
283 a.Reader = mockReader{ 287 a.Reader = mockReader{
284 builds: test.builds, 288 builds: test.builds,
285 } 289 }
286 gotAlerts, gotErrs := a.builderAlerts(test.master, test.builder, &test.b) 290 gotAlerts, gotErrs := a.builderAlerts(test.master, test.builder, &test.b)
287 if !reflect.DeepEqual(gotAlerts, test.wantAlerts) { 291 if !reflect.DeepEqual(gotAlerts, test.wantAlerts) {
288 » » » t.Errorf("%s failed. Got:\n%+v, want:\n%+v", test.name, gotAlerts, test.wantAlerts) 292 » » » t.Errorf("%s failed. Got:\n%+v, want:\n%+v\nDiff: %v", t est.name, gotAlerts, test.wantAlerts,
293 » » » » ansidiff.Diff(gotAlerts, test.wantAlerts))
289 } 294 }
290 if !reflect.DeepEqual(gotErrs, test.wantErrs) { 295 if !reflect.DeepEqual(gotErrs, test.wantErrs) {
291 t.Errorf("%s failed. Got %+v, want: %+v", test.name, got Errs, test.wantErrs) 296 t.Errorf("%s failed. Got %+v, want: %+v", test.name, got Errs, test.wantErrs)
292 } 297 }
293 } 298 }
294 } 299 }
295 300
296 func TestBuilderStepAlerts(t *testing.T) { 301 func TestBuilderStepAlerts(t *testing.T) {
297 tests := []struct { 302 tests := []struct {
298 name string 303 name string
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 { 349 {
345 Repository: "tes ting.git", 350 Repository: "tes ting.git",
346 Revision: "424 2", 351 Revision: "424 2",
347 }, 352 },
348 }, 353 },
349 }, 354 },
350 }, 355 },
351 }, 356 },
352 wantAlerts: []messages.Alert{ 357 wantAlerts: []messages.Alert{
353 { 358 {
354 » » » » » Key: "fake.master.fake.builder.fake_st ep", 359 » » » » » Key: "fake.master.fake.builder.fake _step",
355 » » » » » Title: "Builder step failure: fake.maste r.fake.builder", 360 » » » » » Title: "fake.builder step failure",
356 » » » » » Type: "buildfailure", 361 » » » » » Type: "buildfailure",
362 » » » » » Body: "fake_step failing on fake.mas ter/fake.builder",
363 » » » » » Severity: newFailureSev,
357 Extension: messages.BuildFailure{ 364 Extension: messages.BuildFailure{
358 Builders: []messages.AlertedBuil der{ 365 Builders: []messages.AlertedBuil der{
359 { 366 {
360 Name: "fake.buil der", 367 Name: "fake.buil der",
361 URL: "https://b uild.chromium.org/p/fake.master/builders/fake.builder", 368 URL: "https://b uild.chromium.org/p/fake.master/builders/fake.builder",
362 }, 369 },
363 }, 370 },
364 Reasons: []messages.Reason{ 371 Reasons: []messages.Reason{
365 { 372 {
366 Step: "fake_step ", 373 Step: "fake_step ",
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 { 468 {
462 Repository: "tes ting2.git", 469 Repository: "tes ting2.git",
463 Revision: "444 4", 470 Revision: "444 4",
464 }, 471 },
465 }, 472 },
466 }, 473 },
467 }, 474 },
468 }, 475 },
469 wantAlerts: []messages.Alert{ 476 wantAlerts: []messages.Alert{
470 { 477 {
471 » » » » » Key: "fake.master.fake.builder.fake_st ep", 478 » » » » » Key: "fake.master.fake.builder.fake _step",
472 » » » » » Title: "Builder step failure: fake.maste r.fake.builder", 479 » » » » » Title: "fake.builder step failure",
473 » » » » » Type: "buildfailure", 480 » » » » » Type: "buildfailure",
481 » » » » » Body: "fake_step failing on fake.mas ter/fake.builder",
482 » » » » » Severity: reliableFailureSev,
474 Extension: messages.BuildFailure{ 483 Extension: messages.BuildFailure{
475 Builders: []messages.AlertedBuil der{ 484 Builders: []messages.AlertedBuil der{
476 { 485 {
477 Name: " fake.builder", 486 Name: " fake.builder",
478 URL: " https://build.chromium.org/p/fake.master/builders/fake.builder", 487 URL: " https://build.chromium.org/p/fake.master/builders/fake.builder",
479 FirstFailure: 0 , 488 FirstFailure: 0 ,
480 LatestFailure: 3 , 489 LatestFailure: 3 ,
481 }, 490 },
482 }, 491 },
483 Reasons: []messages.Reason{ 492 Reasons: []messages.Reason{
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
670 RegressionRanges: []messages.Reg ressionRange{ 679 RegressionRanges: []messages.Reg ressionRange{
671 { 680 {
672 Repo: "repo.c", 681 Repo: "repo.c",
673 }, 682 },
674 }, 683 },
675 }, 684 },
676 }, 685 },
677 }, 686 },
678 want: []messages.Alert{ 687 want: []messages.Alert{
679 { 688 {
680 » » » » » Title: "step_a (failing on 3 builders)", 689 » » » » » Title: "step_a failing on 3 builders",
681 Type: "buildfailure", 690 Type: "buildfailure",
691 Body: "builder A, builder B, builder C" ,
682 Extension: messages.BuildFailure{ 692 Extension: messages.BuildFailure{
683 Builders: []messages.AlertedBuil der{ 693 Builders: []messages.AlertedBuil der{
684 {Name: "builder A"}, 694 {Name: "builder A"},
685 {Name: "builder B"}, 695 {Name: "builder B"},
686 {Name: "builder C"}, 696 {Name: "builder C"},
687 }, 697 },
688 Reasons: []messages.Reason{ 698 Reasons: []messages.Reason{
689 { 699 {
690 Step: "step_a", 700 Step: "step_a",
691 }, 701 },
(...skipping 18 matching lines...) Expand all
710 }, 720 },
711 }, 721 },
712 }, 722 },
713 }, 723 },
714 } 724 }
715 725
716 a := New(&mockReader{}, 0, 10) 726 a := New(&mockReader{}, 0, 10)
717 for _, test := range tests { 727 for _, test := range tests {
718 got := a.mergeAlertsByStep(test.in) 728 got := a.mergeAlertsByStep(test.in)
719 if !reflect.DeepEqual(got, test.want) { 729 if !reflect.DeepEqual(got, test.want) {
720 » » » t.Errorf("%s failed. Got:\n\t%+v, want:\n\t%+v", test.na me, got, test.want) 730 » » » t.Errorf("%s failed. Got:\n\t%+v, want:\n\t%+v\nDiff: %v ", test.name, got, test.want, ansidiff.Diff(got, test.want))
721 } 731 }
722 } 732 }
723 } 733 }
724 734
725 func TestUnexpected(t *testing.T) { 735 func TestUnexpected(t *testing.T) {
726 tests := []struct { 736 tests := []struct {
727 name string 737 name string
728 expected, actual, want []string 738 expected, actual, want []string
729 }{ 739 }{
730 { 740 {
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
914 Number: 42, 924 Number: 42,
915 }, 925 },
916 step: messages.Step{ 926 step: messages.Step{
917 Name: "fake_tests", 927 Name: "fake_tests",
918 }, 928 },
919 }, 929 },
920 }, 930 },
921 testResults: messages.TestResults{}, 931 testResults: messages.TestResults{},
922 alerts: []messages.Alert{ 932 alerts: []messages.Alert{
923 { 933 {
924 » » » » » Key: "fake.master.fake.builder.fake_te sts", 934 » » » » » Key: "fake.master.fake.builder.fake _tests",
925 » » » » » Title: "Builder step failure: fake.maste r.fake.builder", 935 » » » » » Title: "fake.builder step failure",
926 » » » » » Type: "buildfailure", 936 » » » » » Body: "fake_tests failing on fake.ma ster/fake.builder",
937 » » » » » Severity: newFailureSev,
938 » » » » » Type: "buildfailure",
927 Extension: messages.BuildFailure{ 939 Extension: messages.BuildFailure{
928 Builders: []messages.AlertedBuil der{ 940 Builders: []messages.AlertedBuil der{
929 { 941 {
930 Name: " fake.builder", 942 Name: " fake.builder",
931 URL: " https://build.chromium.org/p/fake.master/builders/fake.builder", 943 URL: " https://build.chromium.org/p/fake.master/builders/fake.builder",
932 FirstFailure: 4 2, 944 FirstFailure: 4 2,
933 LatestFailure: 4 2, 945 LatestFailure: 4 2,
934 }, 946 },
935 }, 947 },
936 Reasons: []messages.Reason{ 948 Reasons: []messages.Reason{
(...skipping 10 matching lines...) Expand all
947 } 959 }
948 960
949 mc := &mockReader{} 961 mc := &mockReader{}
950 a := New(mc, 0, 10) 962 a := New(mc, 0, 10)
951 a.Now = fakeNow(time.Unix(0, 0)) 963 a.Now = fakeNow(time.Unix(0, 0))
952 964
953 for _, test := range tests { 965 for _, test := range tests {
954 mc.testResults = &test.testResults 966 mc.testResults = &test.testResults
955 alerts, err := a.stepFailureAlerts(test.failures) 967 alerts, err := a.stepFailureAlerts(test.failures)
956 if !reflect.DeepEqual(alerts, test.alerts) { 968 if !reflect.DeepEqual(alerts, test.alerts) {
957 » » » t.Errorf("%s failed. Got:\n\t%+v, want:\n\t%+v", test.na me, alerts, test.alerts) 969 » » » t.Errorf("%s failed. Got:\n\t%+v, want:\n\t%+v\nDiff: %s ", test.name, alerts, test.alerts,
970 » » » » ansidiff.Diff(alerts, test.alerts))
958 } 971 }
959 if !reflect.DeepEqual(err, test.err) { 972 if !reflect.DeepEqual(err, test.err) {
960 t.Errorf("%s failed. Got %+v, want %+v", test.name, err, test.err) 973 t.Errorf("%s failed. Got %+v, want %+v", test.name, err, test.err)
961 } 974 }
962 } 975 }
963 } 976 }
964 977
965 func TestLatestBuildStep(t *testing.T) { 978 func TestLatestBuildStep(t *testing.T) {
966 tests := []struct { 979 tests := []struct {
967 name string 980 name string
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
1093 } 1106 }
1094 if gotErr != test.wantErr { 1107 if gotErr != test.wantErr {
1095 t.Errorf("%s failed. Got %s, want %s.", test.name, gotEr r, test.wantErr) 1108 t.Errorf("%s failed. Got %s, want %s.", test.name, gotEr r, test.wantErr)
1096 } 1109 }
1097 } 1110 }
1098 } 1111 }
1099 1112
1100 func TestExcludeFailure(t *testing.T) { 1113 func TestExcludeFailure(t *testing.T) {
1101 tests := []struct { 1114 tests := []struct {
1102 name string 1115 name string
1103 » » cfgs map[string]messages.MasterConfig 1116 » » gk messages.GatekeeperConfig
1104 master, builder, step string 1117 master, builder, step string
1105 want bool 1118 want bool
1106 }{ 1119 }{
1107 { 1120 {
1108 name: "empty config", 1121 name: "empty config",
1109 master: "fake.master", 1122 master: "fake.master",
1110 builder: "fake.builder", 1123 builder: "fake.builder",
1111 step: "fake_step", 1124 step: "fake_step",
1112 want: false, 1125 want: false,
1113 }, 1126 },
1114 { 1127 {
1115 name: "specifically excluded builder", 1128 name: "specifically excluded builder",
1116 master: "fake.master", 1129 master: "fake.master",
1117 builder: "fake.builder", 1130 builder: "fake.builder",
1118 step: "fake_step", 1131 step: "fake_step",
1119 » » » cfgs: map[string]messages.MasterConfig{ 1132 » » » gk: messages.GatekeeperConfig{Masters: map[string][]mess ages.MasterConfig{
1120 » » » » "fake.master": { 1133 » » » » "fake.master": {{
1121 ExcludedBuilders: []string{"fake.builder "}, 1134 ExcludedBuilders: []string{"fake.builder "},
1122 » » » » }, 1135 » » » » }},
1123 » » » }, 1136 » » » }},
1124 want: true, 1137 want: true,
1125 }, 1138 },
1126 { 1139 {
1127 name: "specifically excluded master step", 1140 name: "specifically excluded master step",
1128 master: "fake.master", 1141 master: "fake.master",
1129 builder: "fake.builder", 1142 builder: "fake.builder",
1130 step: "fake_step", 1143 step: "fake_step",
1131 » » » cfgs: map[string]messages.MasterConfig{ 1144 » » » gk: messages.GatekeeperConfig{Masters: map[string][]mess ages.MasterConfig{
1132 » » » » "fake.master": { 1145 » » » » "fake.master": {{
1133 ExcludedSteps: []string{"fake_step"}, 1146 ExcludedSteps: []string{"fake_step"},
1134 » » » » }, 1147 » » » » }},
1135 » » » }, 1148 » » » }},
1136 want: true, 1149 want: true,
1137 }, 1150 },
1138 { 1151 {
1139 name: "specifically excluded builder step", 1152 name: "specifically excluded builder step",
1140 master: "fake.master", 1153 master: "fake.master",
1141 builder: "fake.builder", 1154 builder: "fake.builder",
1142 step: "fake_step", 1155 step: "fake_step",
1143 » » » cfgs: map[string]messages.MasterConfig{ 1156 » » » gk: messages.GatekeeperConfig{Masters: map[string][]mess ages.MasterConfig{
1144 » » » » "fake.master": { 1157 » » » » "fake.master": {{
1145 Builders: map[string]messages.BuilderCon fig{ 1158 Builders: map[string]messages.BuilderCon fig{
1146 "fake.builder": { 1159 "fake.builder": {
1147 ExcludedSteps: []string{ "fake_step"}, 1160 ExcludedSteps: []string{ "fake_step"},
1148 }, 1161 },
1149 » » » » » }, 1162 » » » » » }},
1150 » » » » }, 1163 » » » » }},
1151 }, 1164 },
1152 want: true, 1165 want: true,
1153 }, 1166 },
1154 { 1167 {
1155 name: "wildcard builder excluded", 1168 name: "wildcard builder excluded",
1156 master: "fake.master", 1169 master: "fake.master",
1157 builder: "fake.builder", 1170 builder: "fake.builder",
1158 step: "fake_step", 1171 step: "fake_step",
1159 » » » cfgs: map[string]messages.MasterConfig{ 1172 » » » gk: messages.GatekeeperConfig{Masters: map[string][]mess ages.MasterConfig{
1160 » » » » "fake.master": { 1173 » » » » "fake.master": {{
1161 ExcludedBuilders: []string{"*"}, 1174 ExcludedBuilders: []string{"*"},
1162 » » » » }, 1175 » » » » }},
1163 » » » }, 1176 » » » }},
1164 want: true, 1177 want: true,
1165 }, 1178 },
1166 } 1179 }
1167 1180
1168 a := New(&mockReader{}, 0, 10) 1181 a := New(&mockReader{}, 0, 10)
1169 for _, test := range tests { 1182 for _, test := range tests {
1170 » » a.MasterCfgs = test.cfgs 1183 » » a.Gatekeeper = NewGatekeeperRules(test.gk)
1171 » » got := a.excludeFailure(test.master, test.builder, test.step) 1184 » » got := a.Gatekeeper.ExcludeFailure(test.master, test.builder, te st.step)
1172 if got != test.want { 1185 if got != test.want {
1173 t.Errorf("%s failed. Got: %+v, want: %+v", test.name, go t, test.want) 1186 t.Errorf("%s failed. Got: %+v, want: %+v", test.name, go t, test.want)
1174 } 1187 }
1175 } 1188 }
1176 } 1189 }
OLDNEW
« no previous file with comments | « go/src/infra/monitoring/analyzer/analyzer.go ('k') | go/src/infra/monitoring/analyzer/gatekeeper.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698