Skip to content

Commit

Permalink
test: for getTriggerFromRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrMatsko committed Nov 27, 2024
1 parent a4f8d09 commit 95a9798
Showing 1 changed file with 43 additions and 18 deletions.
61 changes: 43 additions & 18 deletions api/handler/triggers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import (
"context"
"encoding/json"
"fmt"
logging "github.com/moira-alert/moira/logging/zerolog_adapter"
"github.com/moira-alert/moira/metric_source/remote"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

logging "github.com/moira-alert/moira/logging/zerolog_adapter"
"github.com/moira-alert/moira/metric_source/remote"

prometheus "github.com/prometheus/client_golang/api/prometheus/v1"

"github.com/moira-alert/moira"
Expand Down Expand Up @@ -196,27 +195,53 @@ func TestGetTriggerFromRequest(t *testing.T) {
}

Convey("for graphite remote", func() {
triggerDTO.TriggerSource = moira.GraphiteRemote
body, _ := json.Marshal(triggerDTO)
Convey("when ErrRemoteTriggerResponse returned", func() {
triggerDTO.TriggerSource = moira.GraphiteRemote
body, _ := json.Marshal(triggerDTO)

request := httptest.NewRequest(http.MethodPut, "/trigger", bytes.NewReader(body))
request.Header.Add("content-type", "application/json")
request = request.WithContext(middleware.SetContextValueForTest(request.Context(), "metricSourceProvider", allSourceProvider))
request = request.WithContext(middleware.SetContextValueForTest(request.Context(), "limits", api.GetTestLimitsConfig()))
request := httptest.NewRequest(http.MethodPut, "/trigger", bytes.NewReader(body))
request.Header.Add("content-type", "application/json")
request = request.WithContext(middleware.SetContextValueForTest(request.Context(), "metricSourceProvider", allSourceProvider))
request = request.WithContext(middleware.SetContextValueForTest(request.Context(), "limits", api.GetTestLimitsConfig()))

testLogger, _ := logging.GetLogger("Test")
testLogger, _ := logging.GetLogger("Test")

request = middleware.WithLogEntry(request, middleware.NewLogEntry(testLogger, request))
request = middleware.WithLogEntry(request, middleware.NewLogEntry(testLogger, request))

var returnedErr error = remote.ErrRemoteTriggerResponse{
InternalError: fmt.Errorf(""),
}
var returnedErr error = remote.ErrRemoteTriggerResponse{
InternalError: fmt.Errorf(""),
}

graphiteRemoteSrc.EXPECT().Fetch(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(nil, returnedErr)

graphiteRemoteSrc.EXPECT().Fetch(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(nil, returnedErr)
_, errRsp := getTriggerFromRequest(request)
So(errRsp, ShouldResemble, api.ErrorInvalidRequest(fmt.Errorf("error from graphite remote: %w", returnedErr)))
})

Convey("when ErrRemoteUnavailable", func() {
triggerDTO.TriggerSource = moira.GraphiteRemote
body, _ := json.Marshal(triggerDTO)

request := httptest.NewRequest(http.MethodPut, "/trigger", bytes.NewReader(body))
request.Header.Add("content-type", "application/json")
request = request.WithContext(middleware.SetContextValueForTest(request.Context(), "metricSourceProvider", allSourceProvider))
request = request.WithContext(middleware.SetContextValueForTest(request.Context(), "limits", api.GetTestLimitsConfig()))

_, errRsp := getTriggerFromRequest(request)
So(errRsp, ShouldResemble, api.ErrorRemoteServerUnavailable(returnedErr))
testLogger, _ := logging.GetLogger("Test")

request = middleware.WithLogEntry(request, middleware.NewLogEntry(testLogger, request))

var returnedErr error = remote.ErrRemoteUnavailable{
InternalError: fmt.Errorf(""),
}

graphiteRemoteSrc.EXPECT().Fetch(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(nil, returnedErr)

_, errRsp := getTriggerFromRequest(request)
So(errRsp, ShouldResemble, api.ErrorRemoteServerUnavailable(returnedErr))
})
})

Convey("for prometheus remote", func() {
Expand Down

0 comments on commit 95a9798

Please sign in to comment.