Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Commit

Permalink
[epociask/issue-160-slack-fix] Pass lint and test
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethen Pociask committed Oct 16, 2023
1 parent bd3d2b8 commit 996d7ca
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
16 changes: 15 additions & 1 deletion e2e/mock_servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package e2e
import (
"encoding/json"
"fmt"
"math/rand"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -61,6 +62,11 @@ func (svr *TestPagerDutyServer) Close() {
svr.Server.Close()
}

// RandBool ... Returns a random boolean based on the current time
func RandBool() bool {
return rand.Intn(2) == 1 //nolint:gosec,gomnd //This is not a security issue as this is only used for testing
}

// mockPagerDutyPost ... Mocks a pagerduty post request
func (svr *TestPagerDutyServer) mockPagerDutyPost(w http.ResponseWriter, r *http.Request) {
var alert *client.PagerDutyRequest
Expand All @@ -73,8 +79,16 @@ func (svr *TestPagerDutyServer) mockPagerDutyPost(w http.ResponseWriter, r *http

svr.Payloads = append(svr.Payloads, alert)

// Randomly return different API payload responses
// This ensures that the client implementation can handle different
// slack workspace types
publicAPI := RandBool()
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"status":"success", "message":""}`))
if publicAPI {
_, _ = w.Write([]byte(`ok`))
} else {
_, _ = w.Write([]byte(`{"status":"success", "message":""}`))
}
}

// PagerDutyAlerts ... Returns the pagerduty alerts
Expand Down
1 change: 1 addition & 0 deletions e2e/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func CreateSysTestSuite(t *testing.T) *SysTestSuite {
t.Fatal(err)
}

t.Parallel()
go pess.ListenForShutdown(kill)

return &SysTestSuite{
Expand Down
3 changes: 1 addition & 2 deletions internal/client/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (sc slackClient) PostEvent(ctx context.Context, event *AlertEventTrigger) (

// 3.b. validate status code
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Slack API returned bad status code %d", resp.StatusCode)
return nil, fmt.Errorf("slack API returned bad status code %d", resp.StatusCode)
}

// 3.c. validate response body
Expand All @@ -137,7 +137,6 @@ func (sc slackClient) PostEvent(ctx context.Context, event *AlertEventTrigger) (
Status: core.SuccessStatus,
Message: "ok",
}, nil

}

// 4 convert response to alert response
Expand Down
2 changes: 1 addition & 1 deletion internal/core/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"time"
)

// PagerDutySeverity represents the severity of an event
// PagerDutySeverity ... represents the severity of an event
type PagerDutySeverity string

const (
Expand Down

0 comments on commit 996d7ca

Please sign in to comment.