Skip to content

Commit

Permalink
Add integration tests for uptime alerts.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsomething committed Dec 5, 2023
1 parent d7e4a2e commit 20fee99
Show file tree
Hide file tree
Showing 3 changed files with 350 additions and 3 deletions.
7 changes: 4 additions & 3 deletions commands/uptime_alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ import (
func UptimeAlert() *Command {
cmd := &Command{
Command: &cobra.Command{
Use: "alert",
Short: "Display commands to manage uptime alerts",
Long: `The sub-commands of ` + "`" + `doctl uptime alert` + "`" + ` manage your uptime alerts.
Use: "alert",
Aliases: []string{"alerts"},
Short: "Display commands to manage uptime alerts",
Long: `The sub-commands of ` + "`" + `doctl monitoring uptime alert` + "`" + ` manage your uptime alerts.
DigitalOcean Uptime Alerts provide the ability to monitor your endpoints from around the world,
and alert you when they're slow, unavailable, or SSL certificates are expiring.`,
Expand Down
1 change: 1 addition & 0 deletions do/uptime_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func (ucs *uptimeChecksService) CreateAlert(id string, req *godo.CreateUptimeAle
if err != nil {
return nil, err
}

return &UptimeAlert{uptimeAlert}, nil
}

Expand Down
345 changes: 345 additions & 0 deletions integration/uptime_alert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,345 @@
package integration

import (
"net/http"
"net/http/httptest"
"net/http/httputil"
"os/exec"
"strings"
"testing"

"github.com/sclevine/spec"
"github.com/stretchr/testify/require"
)

var _ = suite("monitoring/uptime/alerts/get", func(t *testing.T, when spec.G, it spec.S) {
var (
expect *require.Assertions
server *httptest.Server
)

it.Before(func() {
expect = require.New(t)

server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Add("content-type", "application/json")

switch req.URL.Path {
case "/v2/uptime/checks/valid-check-uuid/alerts/valid-alert-uuid":
auth := req.Header.Get("Authorization")
if auth != "Bearer some-magic-token" {
w.WriteHeader(http.StatusUnauthorized)
return
}

if req.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

w.Write([]byte(uptimeAlertGetResponse))
default:
dump, err := httputil.DumpRequest(req, true)
if err != nil {
t.Fatal("failed to dump request")
}

t.Fatalf("received unknown request: %s", dump)
}
}))
})

it("returns the details of the uptime alert", func() {
cmd := exec.Command(builtBinaryPath,
"-t", "some-magic-token",
"-u", server.URL,
"monitoring",
"uptime",
"alert",
"get",
"valid-check-uuid",
"valid-alert-uuid",
)

output, err := cmd.CombinedOutput()
expect.NoError(err)

expect.Equal(strings.TrimSpace(uptimeAlertGetOutput), strings.TrimSpace(string(output)))
})
})

const (
uptimeAlertGetResponse = `
{
"alert": {
"id": "valid-alert-uuid",
"name": "example.com is down",
"type": "down",
"threshold": 1,
"comparison": "less_than",
"notifications": {
"email": [
"[email protected]"
],
"slack": [
{
"channel": "#alerts",
"url": "https://hooks.slack.com/services/ID"
}
]
},
"period": "2m"
}
}`
uptimeAlertGetOutput = `
ID Name Type Threshold Comparison Period Emails Slack Channels
valid-alert-uuid example.com is down down 1 less_than 2m [email protected] #alerts
`
)

var _ = suite("monitoring/uptime/alerts/list", func(t *testing.T, when spec.G, it spec.S) {
var (
expect *require.Assertions
server *httptest.Server
)

it.Before(func() {
expect = require.New(t)

server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Add("content-type", "application/json")

switch req.URL.Path {
case "/v2/uptime/checks/valid-check-uuid/alerts":
auth := req.Header.Get("Authorization")
if auth != "Bearer some-magic-token" {
w.WriteHeader(http.StatusUnauthorized)
return
}

if req.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

w.Write([]byte(uptimeAlertListResponse))
default:
dump, err := httputil.DumpRequest(req, true)
if err != nil {
t.Fatal("failed to dump request")
}

t.Fatalf("received unknown request: %s", dump)
}
}))
})

it("lists all uptime alerts", func() {
cmd := exec.Command(builtBinaryPath,
"-t", "some-magic-token",
"-u", server.URL,
"monitoring",
"uptime",
"alert",
"list",
"valid-check-uuid",
)

output, err := cmd.CombinedOutput()
expect.NoError(err, string(output))

expect.Equal(strings.TrimSpace(uptimeAlertListOutput), strings.TrimSpace(string(output)))
})
})

const (
uptimeAlertListResponse = `
{
"alerts":[
{
"id": "valid-alert-uuid",
"name": "example.com is down",
"type": "down",
"threshold": 1,
"comparison": "less_than",
"notifications": {
"email": [
"[email protected]"
],
"slack": [
{
"channel": "#alerts",
"url": "https://hooks.slack.com/services/ID"
}
]
},
"period": "2m"
},
{
"id": "fee528f9-73a2-46a9-a248-84056c9a4488",
"name": "example.com increased latency",
"type": "latency",
"threshold": 1000,
"comparison": "greater_than",
"notifications": {
"email": [
"[email protected]"
]
},
"period": "2m"
}
]
}`
uptimeAlertListOutput = `
ID Name Type Threshold Comparison Period Emails Slack Channels
valid-alert-uuid example.com is down down 1 less_than 2m [email protected] #alerts
fee528f9-73a2-46a9-a248-84056c9a4488 example.com increased latency latency 1000 greater_than 2m [email protected]
`
)

var _ = suite("monitoring/uptime/alerts/create", func(t *testing.T, when spec.G, it spec.S) {
var (
expect *require.Assertions
server *httptest.Server
)

it.Before(func() {
expect = require.New(t)

server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Add("content-type", "application/json")

switch req.URL.Path {
case "/v2/uptime/checks/valid-check-uuid/alerts":
auth := req.Header.Get("Authorization")
if auth != "Bearer some-magic-token" {
w.WriteHeader(http.StatusUnauthorized)
return
}

if req.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

w.Write([]byte(uptimeAlertPolicyCreateResponse))
default:
dump, err := httputil.DumpRequest(req, true)
if err != nil {
t.Fatal("failed to dump request")
}

t.Fatalf("received unknown request: %s", dump)
}
}))
})

it("creates a new uptime alert", func() {
cmd := exec.Command(builtBinaryPath,
"-t", "some-magic-token",
"-u", server.URL,
"monitoring",
"uptime",
"alerts",
"create",
"--comparison", "less_than",
"--type", "down",
"--period", "2m",
"--name", "example.com is down",
"--emails", "[email protected]",
"--threshold", "1",
"--slack-channels", "#alerts",
"--slack-urls", "https://hooks.slack.com/services/ID",
"valid-check-uuid",
)

output, err := cmd.CombinedOutput()
expect.NoError(err, string(output))

expect.Equal(strings.TrimSpace(createUptimeAlertPolicyOutput), strings.TrimSpace(string(output)))
})
})

const (
uptimeAlertPolicyCreateResponse = `{
"alert": {
"id": "valid-alert-uuid",
"name": "example.com is down",
"type": "down",
"threshold": 1,
"comparison": "less_than",
"notifications": {
"email": [
"[email protected]"
],
"slack": [
{
"channel": "#alerts",
"url": "https://hooks.slack.com/services/ID"
}
]
},
"period": "2m"
}
}`

createUptimeAlertPolicyOutput = `
ID Name Type Threshold Comparison Period Emails Slack Channels
valid-alert-uuid example.com is down down 1 less_than 2m [email protected] #alerts`
)

var _ = suite("monitoring/uptime/alerts/delete", func(t *testing.T, when spec.G, it spec.S) {
var (
expect *require.Assertions
cmd *exec.Cmd
server *httptest.Server
)

it.Before(func() {
expect = require.New(t)

server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
switch req.URL.Path {
case "/v2/uptime/checks/valid-check-uuid/alerts/valid-alert-uuid":
auth := req.Header.Get("Authorization")
if auth != "Bearer some-magic-token" {
w.WriteHeader(http.StatusUnauthorized)
return
}

if req.Method != http.MethodDelete {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

w.WriteHeader(http.StatusNoContent)
default:
dump, err := httputil.DumpRequest(req, true)
if err != nil {
t.Fatal("failed to dump request")
}

t.Fatalf("received unknown request: %s", dump)
}
}))

})

when("required flags are passed", func() {
it("deletes the uptime alert", func() {
cmd = exec.Command(builtBinaryPath,
"-t", "some-magic-token",
"-u", server.URL,
"monitoring",
"uptime",
"alert",
"delete",
"valid-check-uuid",
"valid-alert-uuid",
)
output, err := cmd.CombinedOutput()
expect.NoError(err)
expect.Empty(output)
})
})
})

0 comments on commit 20fee99

Please sign in to comment.