api: Implement OpenAPI generated Alertmanager API V2
The current Alertmanager API v1 is undocumented and written by hand.
This patch introduces a new Alertmanager API - v2. The API is fully
generated via an OpenAPI 2.0 [1] specification (see
`api/v2/openapi.yaml`) with the exception of the http handlers itself.
Pros:
- Generated server code
- Ability to generate clients in all major languages
(Go, Java, JS, Python, Ruby, Haskell, *elm* [3] ...)
- Strict contract (OpenAPI spec) between server and clients.
- Instant feedback on frontend-breaking changes, due to strictly
typed frontend language elm.
- Generated documentation (See Alertmanager online Swagger UI [4])
Cons:
- Dependency on open api ecosystem including go-swagger [2]
In addition this patch includes the following changes.
- README.md: Add API section
- test: Duplicate acceptance test to API v1 & API v2 version
The Alertmanager acceptance test framework has a decent test coverage
on the Alertmanager API. Introducing the Alertmanager API v2 does not go
hand in hand with deprecating API v1. They should live alongside each
other for a couple of minor Alertmanager versions.
Instead of porting the acceptance test framework to use the new API v2,
this patch duplicates the acceptance tests, one using the API v1, the
other API v2.
Once API v1 is removed we can simply remove `test/with_api_v1` and bring
`test/with_api_v2` to `test/`.
[1]
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md
[2] https://github.com/go-swagger/go-swagger/
[3] https://github.com/ahultgren/swagger-elm
[4]
http://petstore.swagger.io/?url=https://raw.githubusercontent.com/mxinden/alertmanager/apiv2/api/v2/openapi.yaml
Signed-off-by: Max Leonard Inden <IndenML@gmail.com>
2018-04-26 06:12:49 +00:00
|
|
|
// Copyright 2018 Prometheus Team
|
2015-10-11 15:24:49 +00:00
|
|
|
// Licensed 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.
|
|
|
|
|
2015-09-30 14:13:00 +00:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
2018-08-04 21:04:00 +00:00
|
|
|
"encoding/json"
|
2015-09-30 14:13:00 +00:00
|
|
|
"fmt"
|
2018-02-27 17:18:53 +00:00
|
|
|
"sync"
|
2015-09-30 14:13:00 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
api: Implement OpenAPI generated Alertmanager API V2
The current Alertmanager API v1 is undocumented and written by hand.
This patch introduces a new Alertmanager API - v2. The API is fully
generated via an OpenAPI 2.0 [1] specification (see
`api/v2/openapi.yaml`) with the exception of the http handlers itself.
Pros:
- Generated server code
- Ability to generate clients in all major languages
(Go, Java, JS, Python, Ruby, Haskell, *elm* [3] ...)
- Strict contract (OpenAPI spec) between server and clients.
- Instant feedback on frontend-breaking changes, due to strictly
typed frontend language elm.
- Generated documentation (See Alertmanager online Swagger UI [4])
Cons:
- Dependency on open api ecosystem including go-swagger [2]
In addition this patch includes the following changes.
- README.md: Add API section
- test: Duplicate acceptance test to API v1 & API v2 version
The Alertmanager acceptance test framework has a decent test coverage
on the Alertmanager API. Introducing the Alertmanager API v2 does not go
hand in hand with deprecating API v1. They should live alongside each
other for a couple of minor Alertmanager versions.
Instead of porting the acceptance test framework to use the new API v2,
this patch duplicates the acceptance tests, one using the API v1, the
other API v2.
Once API v1 is removed we can simply remove `test/with_api_v1` and bring
`test/with_api_v2` to `test/`.
[1]
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md
[2] https://github.com/go-swagger/go-swagger/
[3] https://github.com/ahultgren/swagger-elm
[4]
http://petstore.swagger.io/?url=https://raw.githubusercontent.com/mxinden/alertmanager/apiv2/api/v2/openapi.yaml
Signed-off-by: Max Leonard Inden <IndenML@gmail.com>
2018-04-26 06:12:49 +00:00
|
|
|
"github.com/prometheus/alertmanager/test/with_api_v2/api_v2_client/models"
|
2015-09-30 14:13:00 +00:00
|
|
|
)
|
|
|
|
|
2015-11-10 12:47:04 +00:00
|
|
|
// Collector gathers alerts received by a notification receiver
|
2015-09-30 14:13:00 +00:00
|
|
|
// and verifies whether all arrived and within the correct time boundaries.
|
|
|
|
type Collector struct {
|
|
|
|
t *testing.T
|
|
|
|
name string
|
|
|
|
opts *AcceptanceOpts
|
|
|
|
|
api: Implement OpenAPI generated Alertmanager API V2
The current Alertmanager API v1 is undocumented and written by hand.
This patch introduces a new Alertmanager API - v2. The API is fully
generated via an OpenAPI 2.0 [1] specification (see
`api/v2/openapi.yaml`) with the exception of the http handlers itself.
Pros:
- Generated server code
- Ability to generate clients in all major languages
(Go, Java, JS, Python, Ruby, Haskell, *elm* [3] ...)
- Strict contract (OpenAPI spec) between server and clients.
- Instant feedback on frontend-breaking changes, due to strictly
typed frontend language elm.
- Generated documentation (See Alertmanager online Swagger UI [4])
Cons:
- Dependency on open api ecosystem including go-swagger [2]
In addition this patch includes the following changes.
- README.md: Add API section
- test: Duplicate acceptance test to API v1 & API v2 version
The Alertmanager acceptance test framework has a decent test coverage
on the Alertmanager API. Introducing the Alertmanager API v2 does not go
hand in hand with deprecating API v1. They should live alongside each
other for a couple of minor Alertmanager versions.
Instead of porting the acceptance test framework to use the new API v2,
this patch duplicates the acceptance tests, one using the API v1, the
other API v2.
Once API v1 is removed we can simply remove `test/with_api_v1` and bring
`test/with_api_v2` to `test/`.
[1]
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md
[2] https://github.com/go-swagger/go-swagger/
[3] https://github.com/ahultgren/swagger-elm
[4]
http://petstore.swagger.io/?url=https://raw.githubusercontent.com/mxinden/alertmanager/apiv2/api/v2/openapi.yaml
Signed-off-by: Max Leonard Inden <IndenML@gmail.com>
2018-04-26 06:12:49 +00:00
|
|
|
collected map[float64][]models.Alerts
|
|
|
|
expected map[Interval][]models.Alerts
|
2018-02-27 17:18:53 +00:00
|
|
|
|
|
|
|
mtx sync.RWMutex
|
2015-09-30 14:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Collector) String() string {
|
|
|
|
return c.name
|
|
|
|
}
|
|
|
|
|
2018-08-04 21:04:00 +00:00
|
|
|
// Collected returns a map of alerts collected by the collector indexed with the
|
|
|
|
// receive timestamp.
|
|
|
|
func (c *Collector) Collected() map[float64][]models.Alerts {
|
|
|
|
c.mtx.RLock()
|
|
|
|
defer c.mtx.RUnlock()
|
|
|
|
return c.collected
|
|
|
|
}
|
|
|
|
|
api: Implement OpenAPI generated Alertmanager API V2
The current Alertmanager API v1 is undocumented and written by hand.
This patch introduces a new Alertmanager API - v2. The API is fully
generated via an OpenAPI 2.0 [1] specification (see
`api/v2/openapi.yaml`) with the exception of the http handlers itself.
Pros:
- Generated server code
- Ability to generate clients in all major languages
(Go, Java, JS, Python, Ruby, Haskell, *elm* [3] ...)
- Strict contract (OpenAPI spec) between server and clients.
- Instant feedback on frontend-breaking changes, due to strictly
typed frontend language elm.
- Generated documentation (See Alertmanager online Swagger UI [4])
Cons:
- Dependency on open api ecosystem including go-swagger [2]
In addition this patch includes the following changes.
- README.md: Add API section
- test: Duplicate acceptance test to API v1 & API v2 version
The Alertmanager acceptance test framework has a decent test coverage
on the Alertmanager API. Introducing the Alertmanager API v2 does not go
hand in hand with deprecating API v1. They should live alongside each
other for a couple of minor Alertmanager versions.
Instead of porting the acceptance test framework to use the new API v2,
this patch duplicates the acceptance tests, one using the API v1, the
other API v2.
Once API v1 is removed we can simply remove `test/with_api_v1` and bring
`test/with_api_v2` to `test/`.
[1]
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md
[2] https://github.com/go-swagger/go-swagger/
[3] https://github.com/ahultgren/swagger-elm
[4]
http://petstore.swagger.io/?url=https://raw.githubusercontent.com/mxinden/alertmanager/apiv2/api/v2/openapi.yaml
Signed-off-by: Max Leonard Inden <IndenML@gmail.com>
2018-04-26 06:12:49 +00:00
|
|
|
func batchesEqual(as, bs models.Alerts, opts *AcceptanceOpts) bool {
|
2015-09-30 16:45:49 +00:00
|
|
|
if len(as) != len(bs) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-10-05 14:51:34 +00:00
|
|
|
for _, a := range as {
|
|
|
|
found := false
|
|
|
|
for _, b := range bs {
|
|
|
|
if equalAlerts(a, b, opts) {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2015-09-30 16:45:49 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-09-30 14:13:00 +00:00
|
|
|
// latest returns the latest relative point in time where a notification is
|
|
|
|
// expected.
|
|
|
|
func (c *Collector) latest() float64 {
|
2018-02-27 17:18:53 +00:00
|
|
|
c.mtx.RLock()
|
|
|
|
defer c.mtx.RUnlock()
|
2015-09-30 14:13:00 +00:00
|
|
|
var latest float64
|
2015-10-05 11:22:23 +00:00
|
|
|
for iv := range c.expected {
|
2015-09-30 14:13:00 +00:00
|
|
|
if iv.end > latest {
|
|
|
|
latest = iv.end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return latest
|
|
|
|
}
|
|
|
|
|
2015-11-20 14:10:38 +00:00
|
|
|
// Want declares that the Collector expects to receive the given alerts
|
2015-09-30 14:13:00 +00:00
|
|
|
// within the given time boundaries.
|
|
|
|
func (c *Collector) Want(iv Interval, alerts ...*TestAlert) {
|
2018-02-27 17:18:53 +00:00
|
|
|
c.mtx.Lock()
|
|
|
|
defer c.mtx.Unlock()
|
api: Implement OpenAPI generated Alertmanager API V2
The current Alertmanager API v1 is undocumented and written by hand.
This patch introduces a new Alertmanager API - v2. The API is fully
generated via an OpenAPI 2.0 [1] specification (see
`api/v2/openapi.yaml`) with the exception of the http handlers itself.
Pros:
- Generated server code
- Ability to generate clients in all major languages
(Go, Java, JS, Python, Ruby, Haskell, *elm* [3] ...)
- Strict contract (OpenAPI spec) between server and clients.
- Instant feedback on frontend-breaking changes, due to strictly
typed frontend language elm.
- Generated documentation (See Alertmanager online Swagger UI [4])
Cons:
- Dependency on open api ecosystem including go-swagger [2]
In addition this patch includes the following changes.
- README.md: Add API section
- test: Duplicate acceptance test to API v1 & API v2 version
The Alertmanager acceptance test framework has a decent test coverage
on the Alertmanager API. Introducing the Alertmanager API v2 does not go
hand in hand with deprecating API v1. They should live alongside each
other for a couple of minor Alertmanager versions.
Instead of porting the acceptance test framework to use the new API v2,
this patch duplicates the acceptance tests, one using the API v1, the
other API v2.
Once API v1 is removed we can simply remove `test/with_api_v1` and bring
`test/with_api_v2` to `test/`.
[1]
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md
[2] https://github.com/go-swagger/go-swagger/
[3] https://github.com/ahultgren/swagger-elm
[4]
http://petstore.swagger.io/?url=https://raw.githubusercontent.com/mxinden/alertmanager/apiv2/api/v2/openapi.yaml
Signed-off-by: Max Leonard Inden <IndenML@gmail.com>
2018-04-26 06:12:49 +00:00
|
|
|
var nas models.Alerts
|
2015-09-30 14:13:00 +00:00
|
|
|
for _, a := range alerts {
|
|
|
|
nas = append(nas, a.nativeAlert(c.opts))
|
|
|
|
}
|
|
|
|
|
2015-10-05 11:22:23 +00:00
|
|
|
c.expected[iv] = append(c.expected[iv], nas)
|
2015-09-30 14:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add the given alerts to the collected alerts.
|
api: Implement OpenAPI generated Alertmanager API V2
The current Alertmanager API v1 is undocumented and written by hand.
This patch introduces a new Alertmanager API - v2. The API is fully
generated via an OpenAPI 2.0 [1] specification (see
`api/v2/openapi.yaml`) with the exception of the http handlers itself.
Pros:
- Generated server code
- Ability to generate clients in all major languages
(Go, Java, JS, Python, Ruby, Haskell, *elm* [3] ...)
- Strict contract (OpenAPI spec) between server and clients.
- Instant feedback on frontend-breaking changes, due to strictly
typed frontend language elm.
- Generated documentation (See Alertmanager online Swagger UI [4])
Cons:
- Dependency on open api ecosystem including go-swagger [2]
In addition this patch includes the following changes.
- README.md: Add API section
- test: Duplicate acceptance test to API v1 & API v2 version
The Alertmanager acceptance test framework has a decent test coverage
on the Alertmanager API. Introducing the Alertmanager API v2 does not go
hand in hand with deprecating API v1. They should live alongside each
other for a couple of minor Alertmanager versions.
Instead of porting the acceptance test framework to use the new API v2,
this patch duplicates the acceptance tests, one using the API v1, the
other API v2.
Once API v1 is removed we can simply remove `test/with_api_v1` and bring
`test/with_api_v2` to `test/`.
[1]
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md
[2] https://github.com/go-swagger/go-swagger/
[3] https://github.com/ahultgren/swagger-elm
[4]
http://petstore.swagger.io/?url=https://raw.githubusercontent.com/mxinden/alertmanager/apiv2/api/v2/openapi.yaml
Signed-off-by: Max Leonard Inden <IndenML@gmail.com>
2018-04-26 06:12:49 +00:00
|
|
|
func (c *Collector) add(alerts ...*models.Alert) {
|
2018-02-27 17:18:53 +00:00
|
|
|
c.mtx.Lock()
|
|
|
|
defer c.mtx.Unlock()
|
2015-09-30 14:13:00 +00:00
|
|
|
arrival := c.opts.relativeTime(time.Now())
|
|
|
|
|
api: Implement OpenAPI generated Alertmanager API V2
The current Alertmanager API v1 is undocumented and written by hand.
This patch introduces a new Alertmanager API - v2. The API is fully
generated via an OpenAPI 2.0 [1] specification (see
`api/v2/openapi.yaml`) with the exception of the http handlers itself.
Pros:
- Generated server code
- Ability to generate clients in all major languages
(Go, Java, JS, Python, Ruby, Haskell, *elm* [3] ...)
- Strict contract (OpenAPI spec) between server and clients.
- Instant feedback on frontend-breaking changes, due to strictly
typed frontend language elm.
- Generated documentation (See Alertmanager online Swagger UI [4])
Cons:
- Dependency on open api ecosystem including go-swagger [2]
In addition this patch includes the following changes.
- README.md: Add API section
- test: Duplicate acceptance test to API v1 & API v2 version
The Alertmanager acceptance test framework has a decent test coverage
on the Alertmanager API. Introducing the Alertmanager API v2 does not go
hand in hand with deprecating API v1. They should live alongside each
other for a couple of minor Alertmanager versions.
Instead of porting the acceptance test framework to use the new API v2,
this patch duplicates the acceptance tests, one using the API v1, the
other API v2.
Once API v1 is removed we can simply remove `test/with_api_v1` and bring
`test/with_api_v2` to `test/`.
[1]
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md
[2] https://github.com/go-swagger/go-swagger/
[3] https://github.com/ahultgren/swagger-elm
[4]
http://petstore.swagger.io/?url=https://raw.githubusercontent.com/mxinden/alertmanager/apiv2/api/v2/openapi.yaml
Signed-off-by: Max Leonard Inden <IndenML@gmail.com>
2018-04-26 06:12:49 +00:00
|
|
|
c.collected[arrival] = append(c.collected[arrival], models.Alerts(alerts))
|
2015-09-30 14:13:00 +00:00
|
|
|
}
|
|
|
|
|
2018-08-04 21:04:00 +00:00
|
|
|
func (c *Collector) Check() string {
|
2015-09-30 16:45:49 +00:00
|
|
|
report := fmt.Sprintf("\ncollector %q:\n\n", c)
|
2015-09-30 14:13:00 +00:00
|
|
|
|
2018-02-27 17:18:53 +00:00
|
|
|
c.mtx.RLock()
|
|
|
|
defer c.mtx.RUnlock()
|
2015-10-05 11:22:23 +00:00
|
|
|
for iv, expected := range c.expected {
|
2015-09-30 14:13:00 +00:00
|
|
|
report += fmt.Sprintf("interval %v\n", iv)
|
|
|
|
|
api: Implement OpenAPI generated Alertmanager API V2
The current Alertmanager API v1 is undocumented and written by hand.
This patch introduces a new Alertmanager API - v2. The API is fully
generated via an OpenAPI 2.0 [1] specification (see
`api/v2/openapi.yaml`) with the exception of the http handlers itself.
Pros:
- Generated server code
- Ability to generate clients in all major languages
(Go, Java, JS, Python, Ruby, Haskell, *elm* [3] ...)
- Strict contract (OpenAPI spec) between server and clients.
- Instant feedback on frontend-breaking changes, due to strictly
typed frontend language elm.
- Generated documentation (See Alertmanager online Swagger UI [4])
Cons:
- Dependency on open api ecosystem including go-swagger [2]
In addition this patch includes the following changes.
- README.md: Add API section
- test: Duplicate acceptance test to API v1 & API v2 version
The Alertmanager acceptance test framework has a decent test coverage
on the Alertmanager API. Introducing the Alertmanager API v2 does not go
hand in hand with deprecating API v1. They should live alongside each
other for a couple of minor Alertmanager versions.
Instead of porting the acceptance test framework to use the new API v2,
this patch duplicates the acceptance tests, one using the API v1, the
other API v2.
Once API v1 is removed we can simply remove `test/with_api_v1` and bring
`test/with_api_v2` to `test/`.
[1]
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md
[2] https://github.com/go-swagger/go-swagger/
[3] https://github.com/ahultgren/swagger-elm
[4]
http://petstore.swagger.io/?url=https://raw.githubusercontent.com/mxinden/alertmanager/apiv2/api/v2/openapi.yaml
Signed-off-by: Max Leonard Inden <IndenML@gmail.com>
2018-04-26 06:12:49 +00:00
|
|
|
var alerts []models.Alerts
|
2018-01-23 15:52:03 +00:00
|
|
|
for at, got := range c.collected {
|
|
|
|
if iv.contains(at) {
|
|
|
|
alerts = append(alerts, got...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-30 14:13:00 +00:00
|
|
|
for _, exp := range expected {
|
2018-01-23 15:52:03 +00:00
|
|
|
found := len(exp) == 0 && len(alerts) == 0
|
2015-10-01 13:46:39 +00:00
|
|
|
|
2015-09-30 16:45:49 +00:00
|
|
|
report += fmt.Sprintf("---\n")
|
|
|
|
|
|
|
|
for _, e := range exp {
|
2015-10-05 14:51:34 +00:00
|
|
|
report += fmt.Sprintf("- %v\n", c.opts.alertString(e))
|
2015-09-30 16:45:49 +00:00
|
|
|
}
|
2015-09-30 14:13:00 +00:00
|
|
|
|
2018-01-23 15:52:03 +00:00
|
|
|
for _, a := range alerts {
|
|
|
|
if batchesEqual(exp, a, c.opts) {
|
|
|
|
found = true
|
2015-09-30 14:13:00 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 15:52:03 +00:00
|
|
|
if found {
|
2015-09-30 16:45:49 +00:00
|
|
|
report += fmt.Sprintf(" [ ✓ ]\n")
|
2015-09-30 14:13:00 +00:00
|
|
|
} else {
|
|
|
|
c.t.Fail()
|
2015-09-30 16:45:49 +00:00
|
|
|
report += fmt.Sprintf(" [ ✗ ]\n")
|
2015-09-30 14:13:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect unexpected notifications.
|
|
|
|
var totalExp, totalAct int
|
2015-10-05 11:22:23 +00:00
|
|
|
for _, exp := range c.expected {
|
2015-09-30 16:45:49 +00:00
|
|
|
for _, e := range exp {
|
|
|
|
totalExp += len(e)
|
|
|
|
}
|
2015-09-30 14:13:00 +00:00
|
|
|
}
|
|
|
|
for _, act := range c.collected {
|
2015-09-30 16:45:49 +00:00
|
|
|
for _, a := range act {
|
|
|
|
if len(a) == 0 {
|
|
|
|
c.t.Error("received empty notifications")
|
|
|
|
}
|
|
|
|
totalAct += len(a)
|
|
|
|
}
|
2015-09-30 14:13:00 +00:00
|
|
|
}
|
|
|
|
if totalExp != totalAct {
|
|
|
|
c.t.Fail()
|
|
|
|
report += fmt.Sprintf("\nExpected total of %d alerts, got %d", totalExp, totalAct)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.t.Failed() {
|
|
|
|
report += "\nreceived:\n"
|
|
|
|
|
|
|
|
for at, col := range c.collected {
|
2015-09-30 16:45:49 +00:00
|
|
|
for _, alerts := range col {
|
|
|
|
report += fmt.Sprintf("@ %v\n", at)
|
|
|
|
for _, a := range alerts {
|
2015-10-05 14:51:34 +00:00
|
|
|
report += fmt.Sprintf("- %v\n", c.opts.alertString(a))
|
2015-09-30 16:45:49 +00:00
|
|
|
}
|
2015-09-30 14:13:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return report
|
|
|
|
}
|
2018-08-04 21:04:00 +00:00
|
|
|
|
|
|
|
// alertsToString returns a string representation of the given Alerts. Use for
|
|
|
|
// debugging.
|
|
|
|
func alertsToString(as []*models.Alert) (string, error) {
|
|
|
|
b, err := json.Marshal(as)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CompareCollectors compares two collectors based on their collected alerts
|
|
|
|
func CompareCollectors(a, b *Collector, opts *AcceptanceOpts) (bool, error) {
|
|
|
|
f := func(collected map[float64][]models.Alerts) []*models.Alert {
|
|
|
|
result := []*models.Alert{}
|
|
|
|
for _, batches := range collected {
|
|
|
|
for _, batch := range batches {
|
|
|
|
for _, alert := range batch {
|
|
|
|
result = append(result, alert)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
aAlerts := f(a.Collected())
|
|
|
|
bAlerts := f(b.Collected())
|
|
|
|
|
|
|
|
if len(aAlerts) != len(bAlerts) {
|
|
|
|
aAsString, err := alertsToString(aAlerts)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
bAsString, err := alertsToString(bAlerts)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = fmt.Errorf(
|
|
|
|
"first collector has %v alerts, second collector has %v alerts\n%v\n%v",
|
|
|
|
len(aAlerts), len(bAlerts),
|
|
|
|
aAsString, bAsString,
|
|
|
|
)
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, aAlert := range aAlerts {
|
|
|
|
found := false
|
|
|
|
for _, bAlert := range bAlerts {
|
|
|
|
if equalAlerts(aAlert, bAlert, opts) {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
aAsString, err := alertsToString([]*models.Alert{aAlert})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
bAsString, err := alertsToString(bAlerts)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = fmt.Errorf(
|
|
|
|
"could not find matching alert for alert from first collector\n%v\nin alerts of second collector\n%v",
|
|
|
|
aAsString, bAsString,
|
|
|
|
)
|
|
|
|
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|