-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend RabbitMQ scaler to support count unacked messages
Signed-off-by: Alex Emelyanov <[email protected]>
- Loading branch information
1 parent
3692eca
commit 6a2e827
Showing
2 changed files
with
195 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
package scalers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
const ( | ||
host = "myHostSecret" | ||
host = "myHostSecret" | ||
apiHost = "myApiHostSecret" | ||
) | ||
|
||
type parseRabbitMQMetadataTestData struct { | ||
|
@@ -15,7 +21,8 @@ type parseRabbitMQMetadataTestData struct { | |
} | ||
|
||
var sampleRabbitMqResolvedEnv = map[string]string{ | ||
host: "none", | ||
host: "amqp://user:[email protected]:5236/vhost", | ||
apiHost: "https://user:[email protected]/vhost", | ||
} | ||
|
||
var testRabbitMQMetadata = []parseRabbitMQMetadataTestData{ | ||
|
@@ -31,6 +38,8 @@ var testRabbitMQMetadata = []parseRabbitMQMetadataTestData{ | |
{map[string]string{"queueLength": "10", "host": host}, true, map[string]string{}}, | ||
// host defined in authParams | ||
{map[string]string{"queueLength": "10"}, true, map[string]string{"host": host}}, | ||
// properly formed metadata with countUnacked | ||
{map[string]string{"queueLength": "10", "queueName": "sample", "apiHost": apiHost, "countUnacked": "true"}, false, map[string]string{}}, | ||
} | ||
|
||
func TestRabbitMQParseMetadata(t *testing.T) { | ||
|
@@ -44,3 +53,67 @@ func TestRabbitMQParseMetadata(t *testing.T) { | |
} | ||
} | ||
} | ||
|
||
type getQueueInfoTestData struct { | ||
response string | ||
responseStatus int | ||
isActive bool | ||
} | ||
|
||
var testQueueInfoTestData = []getQueueInfoTestData{ | ||
{`{"messages": 4, "messages_unacknowledged": 1, "name": "evaluate_trials"}`, http.StatusOK, true}, | ||
{`{"messages": 0, "messages_unacknowledged": 1, "name": "evaluate_trials"}`, http.StatusOK, true}, | ||
{`{"messages": 1, "messages_unacknowledged": 0, "name": "evaluate_trials"}`, http.StatusOK, true}, | ||
{`{"messages": 0, "messages_unacknowledged": 0, "name": "evaluate_trials"}`, http.StatusOK, false}, | ||
{`Password is incorrect`, http.StatusUnauthorized, false}, | ||
} | ||
|
||
func TestGetQueueInfo(t *testing.T) { | ||
for _, testData := range testQueueInfoTestData { | ||
var apiStub = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
expeced_path := "/api/queues/myhost/evaluate_trials" | ||
if r.RequestURI != expeced_path { | ||
t.Error("Expect request path to =", expeced_path, "but it is", r.RequestURI) | ||
} | ||
|
||
w.WriteHeader(testData.responseStatus) | ||
w.Write([]byte(testData.response)) | ||
})) | ||
|
||
resolvedEnv := map[string]string{apiHost: fmt.Sprintf("%s/%s", apiStub.URL, "myhost")} | ||
|
||
metadata := map[string]string{ | ||
"queueLength": "10", | ||
"queueName": "evaluate_trials", | ||
"apiHost": apiHost, | ||
"countUnacked": "true", | ||
} | ||
|
||
s, err := NewRabbitMQScaler(resolvedEnv, metadata, map[string]string{}) | ||
|
||
if err != nil { | ||
t.Error("Expect success", err) | ||
} | ||
|
||
ctx := context.TODO() | ||
active, err := s.IsActive(ctx) | ||
|
||
if testData.responseStatus == http.StatusOK { | ||
if err != nil { | ||
t.Error("Expect success", err) | ||
} | ||
|
||
if active != testData.isActive { | ||
if testData.isActive { | ||
t.Error("Expect to be active") | ||
} else { | ||
t.Error("Expect to not be active") | ||
} | ||
} | ||
} else { | ||
if !strings.Contains(err.Error(), testData.response) { | ||
t.Error("Expect error to be like '", testData.response, "' but it's '", err, "'") | ||
} | ||
} | ||
} | ||
} |