// MasterAlerts returns alerts generated from the master. func (a *Analyzer) MasterAlerts(master string, be *messages.BuildExtract) []messages.Alert { ret := []messages.Alert{} // Copied logic from builder_messages. // No created_timestamp should be a warning sign, no? if be.CreatedTimestamp == messages.EpochTime(0) { return ret } elapsed := a.Now().Sub(be.CreatedTimestamp.Time()) if elapsed > a.StaleMasterThreshold { ret = append(ret, messages.Alert{ Key: fmt.Sprintf("stale master: %v", master), Title: fmt.Sprintf("Stale %s master data", master), Body: fmt.Sprintf("%s elapsed since last update.", elapsed), StartTime: messages.TimeToEpochTime(be.CreatedTimestamp.Time()), Severity: staleMasterSev, Time: messages.TimeToEpochTime(a.Now()), Links: []messages.Link{{"Master", client.MasterURL(master)}}, // No type or extension for now. }) } if elapsed < 0 { // Add this to the alerts returned, rather than just log it? log.Errorf("Master %s timestamp is newer than current time (%s): %s old.", master, a.Now(), elapsed) } return ret }
func TestMasterAlerts(t *testing.T) { tests := []struct { name string master string be messages.BuildExtract t time.Time want []messages.Alert }{ { name: "empty", master: "fake-empty", want: []messages.Alert{}, }, { name: "Not stale master", master: "fake-not-stale", be: messages.BuildExtract{ CreatedTimestamp: messages.EpochTime(100), }, t: time.Unix(100, 0), want: []messages.Alert{}, }, { name: "Stale master", master: "fake.master", be: messages.BuildExtract{ CreatedTimestamp: messages.EpochTime(100), }, t: time.Unix(100, 0).Add(20 * time.Minute), want: []messages.Alert{ { Key: "stale master: fake.master", Title: "Stale fake.master master data", Body: fmt.Sprintf("%s elapsed since last update.", 20*time.Minute), Time: messages.TimeToEpochTime(time.Unix(100, 0).Add(20 * time.Minute)), Links: []messages.Link{{"Master", client.MasterURL("fake.master")}}, StartTime: messages.EpochTime(100), }, }, }, { name: "Future master", master: "fake.master", be: messages.BuildExtract{ CreatedTimestamp: messages.EpochTime(110), }, t: time.Unix(100, 0), want: []messages.Alert{}, }, } a := New(&mockReader{}, 0, 10) for _, test := range tests { a.now = fakeNow(test.t) got := a.MasterAlerts(test.master, &test.be) if !reflect.DeepEqual(got, test.want) { t.Errorf("%s failed. Got %+v, want: %+v", test.name, got, test.want) } } }