-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
This fixes #11737 , where setting mode: all now spawns multiple sub-tasks, but the parent task still runs as a job, even though it tests very little, and will always succeed so long as the DNS query does. It is essentially a parent job testing that the DNS resolver works. From a certain strict POV the behavior before this patch is correct. We executed a discrete job (checking DNS) and it worked successfully. A second part of the job, actually hitting the endpoints, failed. The total job (as noted in the summary) did fail, but a sub-part did succeed. That said, this is too complex and a bad UX, so this patch makes sense. Maybe in the future we will be under different constraints. The fix here involved marking that job's emitted event as cancelled using job metadata. I'd discussed using event.private for that with @ruflin, but looking at the code metadata seemed more appropriate. I'd love to hear your $0.02 @ruflin if you think private is more appropriate. Metricbeat wraps all events in its own metadata struct, but I don't think we're there yet in heartbeat in terms of a major refactor being justified. Testing this manually is easy, just point at a domain with multiple A records, like elastic.co. Truly integration testing it programmatically is hard without setting up a DNS resolver. The compromise I reached here is unit tests for the individual bits of logic. Fixes #11737 (cherry picked from commit 078612e)
- Loading branch information
Showing
6 changed files
with
267 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package monitors | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/elastic/beats/heartbeat/eventext" | ||
"github.com/elastic/beats/heartbeat/monitors/jobs" | ||
"github.com/elastic/beats/libbeat/beat" | ||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/libbeat/common/mapval" | ||
) | ||
|
||
func Test_runPublishJob(t *testing.T) { | ||
simpleJob := func(event *beat.Event) (j []jobs.Job, e error) { | ||
eventext.MergeEventFields(event, common.MapStr{"foo": "bar"}) | ||
return nil, nil | ||
} | ||
|
||
testCases := []struct { | ||
name string | ||
job jobs.Job | ||
validators []mapval.Validator | ||
}{ | ||
{ | ||
"simple", | ||
simpleJob, | ||
[]mapval.Validator{ | ||
mapval.MustCompile(mapval.Map{"foo": "bar"}), | ||
}, | ||
}, | ||
{ | ||
"one cont", | ||
func(event *beat.Event) (j []jobs.Job, e error) { | ||
simpleJob(event) | ||
return []jobs.Job{simpleJob}, nil | ||
}, | ||
[]mapval.Validator{ | ||
mapval.MustCompile(mapval.Map{"foo": "bar"}), | ||
mapval.MustCompile(mapval.Map{"foo": "bar"}), | ||
}, | ||
}, | ||
{ | ||
"cancelled cont", | ||
func(event *beat.Event) (j []jobs.Job, e error) { | ||
eventext.CancelEvent(event) | ||
return []jobs.Job{simpleJob}, nil | ||
}, | ||
[]mapval.Validator{ | ||
mapval.MustCompile(mapval.Map{"foo": "bar"}), | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
client := &MockBeatClient{} | ||
queue := runPublishJob(tc.job, client) | ||
for { | ||
if len(queue) == 0 { | ||
break | ||
} | ||
tf := queue[0] | ||
queue = queue[1:] | ||
conts := tf() | ||
for _, cont := range conts { | ||
queue = append(queue, cont) | ||
} | ||
} | ||
client.Close() | ||
|
||
require.Len(t, client.publishes, len(tc.validators)) | ||
for idx, event := range client.publishes { | ||
mapval.Test(t, tc.validators[idx], event.Fields) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.