Skip to content

Commit

Permalink
tests(alerts): add percent alerts tests (#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsonroy-fastly authored May 7, 2024
1 parent d375ad8 commit 869906d
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
69 changes: 69 additions & 0 deletions fastly/alerts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,75 @@ func TestClient_FastlyAlerts(t *testing.T) {
}
}

func TestClient_FastlyPercentAlerts(t *testing.T) {
t.Parallel()

testDimensions := map[string][]string{}
testEvaluationStrategy := map[string]any{
"period": "2m",
"threshold": 0.1, // Increase of 10 percent
"type": "percent_increase",
"ignore_below": float64(5),
}
cadi := &CreateAlertDefinitionInput{
Description: ToPointer("test description"),
Dimensions: testDimensions,
EvaluationStrategy: testEvaluationStrategy,
IntegrationIDs: []string{},
Metric: ToPointer("status_5xx"),
Name: ToPointer("test name"),
ServiceID: ToPointer(testServiceID),
Source: ToPointer("stats"),
}

// Create
var ad *AlertDefinition
var err error
record(t, "alerts/create_alert_definition_stats_percent", func(c *Client) {
ad, err = c.CreateAlertDefinition(cadi)

})
if err != nil {
t.Fatal(err)
}
// Ensure deleted
defer func() {
record(t, "alerts/cleanup_alert_definition_stats_percent", func(c *Client) {
err = c.DeleteAlertDefinition(&DeleteAlertDefinitionInput{
ID: &ad.ID,
})
})
}()

if ad.Description != "test description" {
t.Errorf("bad description: %v", ad.Description)
}

if ad.Metric != "status_5xx" {
t.Errorf("bad metric: %v", ad.Metric)
}

if ad.Name != "test name" {
t.Errorf("bad name: %v", ad.Name)
}

if ad.ServiceID != testServiceID {
t.Errorf("bad service_id: %v", ad.ServiceID)
}

if ad.Source != "stats" {
t.Errorf("bad source: %v", ad.Source)
}

if diff := cmp.Diff(testDimensions, ad.Dimensions); diff != "" {
t.Errorf("bad dimensions: diff -want +got\n%v", diff)
}

if diff := cmp.Diff(testEvaluationStrategy, ad.EvaluationStrategy); diff != "" {
t.Errorf("bad evaluation_strategy: diff -want +got\n%v", diff)
}
}

func TestClient_GetAlertDefinition_validation(t *testing.T) {
var err error
_, err = testClient.GetAlertDefinition(&GetAlertDefinitionInput{
Expand Down
43 changes: 43 additions & 0 deletions fastly/fixtures/alerts/cleanup_alert_definition_stats_percent.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
version: 1
interactions:
- request:
body: ""
form: {}
headers:
User-Agent:
- FastlyGo/9.2.2 (+github.com/fastly/go-fastly; go1.22.1)
url: https://api.fastly.com/alerts/definitions/6v7dRqITNUwcbMoIg3UHiT
method: DELETE
response:
body: ""
headers:
Accept-Ranges:
- bytes
Cache-Control:
- no-store
Date:
- Tue, 16 Apr 2024 13:11:28 GMT
Pragma:
- no-cache
Server:
- control-gateway
Status:
- 204 No Content
Strict-Transport-Security:
- max-age=31536000
Vary:
- Accept-Encoding
Via:
- 1.1 varnish
X-Cache:
- MISS
X-Cache-Hits:
- "0"
X-Served-By:
- cache-yul1970048-YUL
X-Timer:
- S1713273088.055584,VS0,VE167
status: 204 No Content
code: 204
duration: ""
53 changes: 53 additions & 0 deletions fastly/fixtures/alerts/create_alert_definition_stats_percent.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
version: 1
interactions:
- request:
body: '{"description":"test description","dimensions":{},"evaluation_strategy":{"ignore_below":5,"period":"2m","threshold":0.1,"type":"percent_increase"},"integration_ids":[],"metric":"status_5xx","name":"test
name","service_id":"7i6HN3TK9wS159v2gPAZ8A","source":"stats"}'
form: {}
headers:
Accept:
- application/json
Content-Type:
- application/json
User-Agent:
- FastlyGo/9.2.2 (+github.com/fastly/go-fastly; go1.22.1)
url: https://api.fastly.com/alerts/definitions
method: POST
response:
body: '{"id":"6v7dRqITNUwcbMoIg3UHiT","object":"definition","service_id":"7i6HN3TK9wS159v2gPAZ8A","name":"test
name","description":"test description","dimensions":{},"metric":"status_5xx","source":"stats","evaluation_strategy":{"ignore_below":5,"period":"2m","threshold":0.1,"type":"percent_increase"},"integration_ids":[],"updated_at":"2024-04-16T13:11:27Z","created_at":"2024-04-16T13:11:27Z"}'
headers:
Accept-Ranges:
- bytes
Cache-Control:
- no-store
Content-Length:
- "388"
Content-Type:
- application/json
Date:
- Tue, 16 Apr 2024 13:11:28 GMT
Pragma:
- no-cache
Server:
- control-gateway
Status:
- 201 Created
Strict-Transport-Security:
- max-age=31536000
Vary:
- Accept-Encoding
Via:
- 1.1 varnish
X-Cache:
- MISS
X-Cache-Hits:
- "0"
X-Served-By:
- cache-yul1970048-YUL
X-Timer:
- S1713273088.516499,VS0,VE519
status: 201 Created
code: 201
duration: ""

0 comments on commit 869906d

Please sign in to comment.