Skip to content

Commit

Permalink
Raises an error when RabbitMQ regex matches more than 1 page (#2103)
Browse files Browse the repository at this point in the history
Signed-off-by: jorturfer <jorge_turrado@hotmail.es>
  • Loading branch information
Jorge Turrado Ferrero authored Sep 20, 2021
1 parent a01beaf commit 02aa93a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/scalers/rabbitmq_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ type queueInfo struct {
}

type regexQueueInfo struct {
Queues []queueInfo `json:"items"`
Queues []queueInfo `json:"items"`
TotalPages int `json:"page_count"`
}

type messageStat struct {
Expand Down Expand Up @@ -375,6 +376,9 @@ func getJSON(s *rabbitMQScaler, url string) (queueInfo, error) {
if err != nil {
return queueInfo{}, err
}
if queues.TotalPages > 1 {
return queueInfo{}, fmt.Errorf("regex matches more queues than can be recovered at once")
}
result, err := getComposedQueue(s, queues.Queues)
return result, err
}
Expand Down
59 changes: 59 additions & 0 deletions pkg/scalers/rabbitmq_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,3 +437,62 @@ func TestRabbitMQAnonimizeRabbitMQError(t *testing.T) {
assert.Equal(t, fmt.Sprint(err), testData.message)
}
}

type getQueueInfoNavigationTestData struct {
response string
isError bool
}

var testRegexQueueInfoNavigationTestData = []getQueueInfoNavigationTestData{
// sum queue length
{`{"items":[], "filtered_count": 250, "page": 1, "page_count": 3}`, true},
{`{"items":[], "filtered_count": 250, "page": 1, "page_count": 1}`, false},
}

func TestRegexQueueMissingError(t *testing.T) {
for _, testData := range testRegexQueueInfoNavigationTestData {
var apiStub = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
expectedPath := "/api/queues?page=1&use_regex=true&pagination=false&name=evaluate_trials"
if r.RequestURI != expectedPath {
t.Error("Expect request path to =", expectedPath, "but it is", r.RequestURI)
}

w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(testData.response))
if err != nil {
t.Error("Expect request path to =", testData.response, "but it is", err)
}
}))

resolvedEnv := map[string]string{host: apiStub.URL, "plainHost": apiStub.URL}

metadata := map[string]string{
"queueName": "evaluate_trials",
"hostFromEnv": host,
"protocol": "http",
"useRegex": "true",
}

s, err := NewRabbitMQScaler(
&ScalerConfig{
ResolvedEnv: resolvedEnv,
TriggerMetadata: metadata,
AuthParams: map[string]string{},
GlobalHTTPTimeout: 1000 * time.Millisecond,
},
)

if err != nil {
t.Error("Expect success", err)
}

ctx := context.TODO()
_, err = s.IsActive(ctx)
if err != nil && !testData.isError {
t.Error("Expected success but got error", err)
}
if testData.isError && err == nil {
t.Error("Expected error but got success")
}
}
}

0 comments on commit 02aa93a

Please sign in to comment.