Skip to content

Commit

Permalink
Fix bug when overriding with empty string; Fix TestGetQueueInfo
Browse files Browse the repository at this point in the history
Signed-off-by: Valentin Deaconu <valentin.deaconu@sectorlabs.ro>
  • Loading branch information
valentindeaconu committed Mar 3, 2022
1 parent 47c081e commit 5f7e741
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
7 changes: 6 additions & 1 deletion pkg/scalers/rabbitmq_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,12 @@ func (s *rabbitMQScaler) getQueueInfoViaHTTP() (*queueInfo, error) {

// Override vhost if requested.
if s.metadata.vhostName != nil {
vhost = "/" + url.QueryEscape(*s.metadata.vhostName)
// If the desired vhost is "All" vhosts, no path is necessary
if *s.metadata.vhostName == "" {
vhost = ""
} else {
vhost = "/" + url.QueryEscape(*s.metadata.vhostName)
}
}

// Encode the '/' vhost if necessary.
Expand Down
22 changes: 14 additions & 8 deletions pkg/scalers/rabbitmq_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,13 @@ var testQueueInfoTestData = []getQueueInfoTestData{
}

var vhostPathes = []string{"/myhost", "", "/", "//", "/%2F"}
var vhostPathesForRegex = []string{"", "/test-vh", "/%2F"}

var testQueueInfoTestDataSingleVhost = []getQueueInfoTestData{
{`{"messages": 4, "messages_unacknowledged": 1, "message_stats": {"publish_details": {"rate": 1.4}}, "name": "evaluate_trials"}`, http.StatusOK, true, map[string]string{"hostFromEnv": "plainHost", "vhostName": "myhost"}, "/myhost"},
{`{"messages": 4, "messages_unacknowledged": 1, "message_stats": {"publish_details": {"rate": 1.4}}, "name": "evaluate_trials"}`, http.StatusOK, true, map[string]string{"hostFromEnv": "plainHost", "vhostName": "/"}, "/"},
{`{"messages": 4, "messages_unacknowledged": 1, "message_stats": {"publish_details": {"rate": 1.4}}, "name": "evaluate_trials"}`, http.StatusOK, true, map[string]string{"hostFromEnv": "plainHost", "vhostName": ""}, "/"},
{`{"messages": 4, "messages_unacknowledged": 1, "message_stats": {"publish_details": {"rate": 1.4}}, "name": "evaluate_trials"}`, http.StatusOK, true, map[string]string{"hostFromEnv": "plainHost", "vhostName": "/"}, "//"},
{`{"messages": 4, "messages_unacknowledged": 1, "message_stats": {"publish_details": {"rate": 1.4}}, "name": "evaluate_trials"}`, http.StatusOK, true, map[string]string{"hostFromEnv": "plainHost", "vhostName": ""}, ""},
{`{"messages": 4, "messages_unacknowledged": 1, "message_stats": {"publish_details": {"rate": 0}}, "name": "evaluate_trials"}`, http.StatusOK, true, map[string]string{"hostFromEnv": "plainHost", "vhostName": "myhost"}, "/myhost"},
{`{"messages": 4, "messages_unacknowledged": 1, "message_stats": {"publish_details": {"rate": 0}}, "name": "evaluate_trials"}`, http.StatusOK, true, map[string]string{"hostFromEnv": "plainHost", "vhostName": "/"}, "/"},
{`{"messages": 4, "messages_unacknowledged": 1, "message_stats": {"publish_details": {"rate": 0}}, "name": "evaluate_trials"}`, http.StatusOK, true, map[string]string{"hostFromEnv": "plainHost", "vhostName": "/"}, "/%2F"},
{`{"messages": 4, "messages_unacknowledged": 1, "message_stats": {"publish_details": {"rate": 0}}, "name": "evaluate_trials"}`, http.StatusOK, true, map[string]string{"hostFromEnv": "plainHost", "vhostName": ""}, "/"},
}

Expand All @@ -217,14 +216,19 @@ func TestGetQueueInfo(t *testing.T) {

for _, testData := range allTestData {
testData := testData
expectedVhost := "myhost"

if testData.vhostPath != "/myhost" {
expectedVhost = "%2F"
var expectedVhostPath string
switch testData.vhostPath {
case "/myhost":
expectedVhostPath = "/myhost"
case "/%2F", "//":
expectedVhostPath = "/%2F"
default:
expectedVhostPath = ""
}

var apiStub = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
expectedPath := "/api/queues/" + expectedVhost + "/evaluate_trials"
expectedPath := fmt.Sprintf("/api/queues%s/evaluate_trials", expectedVhostPath)
if r.RequestURI != expectedPath {
t.Error("Expect request path to =", expectedPath, "but it is", r.RequestURI)
}
Expand Down Expand Up @@ -326,6 +330,8 @@ var testRegexQueueInfoTestData = []getQueueInfoTestData{
{`{"items":[]}`, http.StatusOK, false, map[string]string{"mode": "MessageRate", "value": "1000", "useRegex": "true", "operation": "avg"}, ""},
}

var vhostPathesForRegex = []string{"", "/test-vh", "/%2F"}

func TestGetQueueInfoWithRegex(t *testing.T) {
allTestData := []getQueueInfoTestData{}
for _, testData := range testRegexQueueInfoTestData {
Expand Down

0 comments on commit 5f7e741

Please sign in to comment.