Skip to content

Commit

Permalink
Testing harness and test
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisJasso committed Apr 18, 2024
1 parent eca061e commit ee59405
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 0 deletions.
25 changes: 25 additions & 0 deletions pager/opsgenie.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,31 @@ func NewOpsgenie(apiKey string) *Opsgenie {
}
}

func NewOpsgenieWithURL(apiKey, url string) *Opsgenie {

// Create a new userClient
var userClient, _ = user.NewClient(&client.Config{
ApiKey: apiKey,
OpsGenieAPIURL: client.ApiUrl(url),
})

var teamClient, _ = team.NewClient(&client.Config{
ApiKey: apiKey,
OpsGenieAPIURL: client.ApiUrl(url),
})

var scheduleClient, _ = schedule.NewClient(&client.Config{
ApiKey: apiKey,
OpsGenieAPIURL: client.ApiUrl(url),
})

return &Opsgenie{
userClient: userClient,
teamClient: teamClient,
scheduleClient: scheduleClient,
}
}

func (p *Opsgenie) Kind() string {
return "opsgenie"
}
Expand Down
89 changes: 89 additions & 0 deletions pager/opsgenie_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package pager_test

import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strings"
"testing"

"github.com/firehydrant/signals-migrator/pager"
"github.com/firehydrant/signals-migrator/store"
"github.com/gosimple/slug"
)

func opsgenieHttpServer(t *testing.T) *httptest.Server {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
filename, err := url.JoinPath("testdata", t.Name(), slug.Make(r.URL.Path)+".json")
if err != nil {
t.Fatalf("error joining path for expected response: %s", err)
}
if _, err := os.Stat(filename); err != nil {
t.Logf("file not found: %s", filename)
http.NotFound(w, r)
return
}
http.ServeFile(w, r, filename)
})
return httptest.NewServer(h)
}

func opsgenieTestClient(t *testing.T, apiURL string) (context.Context, *pager.Opsgenie) {
ctx := withTestDB(t)
og := pager.NewOpsgenieWithURL("api-key-very-secret", apiURL)
return ctx, og
}

func TestOpsgenie(t *testing.T) {
ts := opsgenieHttpServer(t)
ctx, og := opsgenieTestClient(t, strings.TrimPrefix(ts.URL, "http://"))

t.Run("ListUsers", func(t *testing.T) {
u, err := og.ListUsers(ctx)
if err != nil {
t.Fatalf("error loading users: %s", err)
}
t.Logf("found %d users", len(u))

// Verify that the first user is as expected.
mika := u[0]
expected := &pager.User{
Email: "[email protected]",
Resource: pager.Resource{
ID: "b5b92115-bfe7-43eb-8c2a-e467f2e5ddc4",
Name: "john doe",
},
}
assertDeepEqual(t, mika, expected)
})

t.Run("LoadSchedules", func(t *testing.T) {
if err := og.LoadSchedules(ctx); err != nil {
t.Fatalf("error loading schedules: %s", err)
}

s, err := store.UseQueries(ctx).ListExtSchedules(ctx)
if err != nil {
t.Fatalf("error loading schedules: %s", err)
}
t.Logf("found %d schedules", len(s))

// Verify that the first schedule is as expected.
first := s[0]
expected := store.ExtSchedule{
ID: "3fee43f2-02da-49be-ab50-c88ed13aecc3-b1b5f7f6-728b-47bd-af02-c3e1c33bf219",
Name: "Customer Success_schedule - Rot1",
Description: "(Rot1)",
Timezone: "America/Los_Angeles",
Strategy: "weekly",
ShiftDuration: "",
StartTime: "",
HandoffTime: "04:45:32",
HandoffDay: "tuesday",
}
assertDeepEqual(t, first, expected)
})

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"data": {
"id": "3fee43f2-02da-49be-ab50-c88ed13aecc3",
"name": "Customer Success_schedule",
"description": "",
"timezone": "America/Los_Angeles",
"enabled": true,
"ownerTeam": {
"id": "b7acbc33-9853-4150-8a4b-10156d9408c8",
"name": "Customer Success"
},
"rotations": [
{
"id": "b1b5f7f6-728b-47bd-af02-c3e1c33bf219",
"name": "Rot1",
"startDate": "1970-01-20T04:45:32.4Z",
"endDate": null,
"type": "weekly",
"length": 1,
"participants": [
{
"type": "user",
"id": "b5b92115-bfe7-43eb-8c2a-e467f2e5ddc4",
"username": "[email protected]"
}
],
"timeRestriction": null
}
]
},
"took": 0.039,
"requestId": "b5cf1da9-faba-4fc4-b6d9-d3b0a264b9b7"
}
21 changes: 21 additions & 0 deletions pager/testdata/TestOpsgenie/v2-schedules.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"data": [
{
"id": "3fee43f2-02da-49be-ab50-c88ed13aecc3",
"name": "Customer Success_schedule",
"description": "",
"timezone": "America/Los_Angeles",
"enabled": true,
"ownerTeam": {
"id": "b7acbc33-9853-4150-8a4b-10156d9408c8",
"name": "Customer Success"
},
"rotations": []
}
],
"expandable": [
"rotation"
],
"took": 0.087,
"requestId": "7fc91399-2baf-4c26-a156-3e77c29507ef"
}
53 changes: 53 additions & 0 deletions pager/testdata/TestOpsgenie/v2-users.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"totalCount": 2,
"data": [
{
"blocked": false,
"verified": true,
"id": "b5b92115-bfe7-43eb-8c2a-e467f2e5ddc4",
"username": "[email protected]",
"fullName": "john doe",
"role": {
"id": "Admin",
"name": "Admin"
},
"timeZone": "America/New_York",
"locale": "en_US",
"userAddress": {
"country": "",
"state": "",
"city": "",
"line": "",
"zipCode": ""
},
"createdAt": "2022-07-07T20:42:29.853Z"
},
{
"blocked": false,
"verified": true,
"id": "b5b92115-bfe7-43eb-8c2a-e467f2e5ddc5",
"username": "[email protected]",
"fullName": "jane doe",
"role": {
"id": "Admin",
"name": "Admin"
},
"timeZone": "America/New_York",
"locale": "en_US",
"userAddress": {
"country": "",
"state": "",
"city": "",
"line": "",
"zipCode": ""
},
"createdAt": "2022-07-07T20:42:29.853Z"
}
],
"paging": {
"first": "https://api.opsgenie.com/v2/users?limit=100&offset=0&order=ASC&sort=username",
"last": "https://api.opsgenie.com/v2/users?limit=100&offset=0&order=ASC&sort=username"
},
"took": 0.054,
"requestId": "1a80d272-80f1-4b38-b319-f617ff8ee136"
}

0 comments on commit ee59405

Please sign in to comment.