Skip to content

Commit

Permalink
Add IBM MQ scaler e2e test (#5854)
Browse files Browse the repository at this point in the history
* Add e2e test for ibmmq scaler

Signed-off-by: ayoyu <khaliayoub9@gmail.com>

* Add error handling for missing command response parameters in ibmmq scaler

Signed-off-by: ayoyu <khaliayoub9@gmail.com>

* Remove embedded nil error

Signed-off-by: ayoyu <khaliayoub9@gmail.com>

* Fix deprecated nodeAffinity keys in ibmmq helm chart

Signed-off-by: ayoyu <khaliayoub9@gmail.com>

* Add getQueueDepthViaHTTP tests

Signed-off-by: ayoyu <khaliayoub9@gmail.com>

---------

Signed-off-by: ayoyu <khaliayoub9@gmail.com>
Signed-off-by: Zbynek Roubalik <zroubalik@gmail.com>
Co-authored-by: Zbynek Roubalik <zroubalik@gmail.com>
  • Loading branch information
ayoyu and zroubalik committed Jun 25, 2024
1 parent 024548b commit 13f309a
Show file tree
Hide file tree
Showing 4 changed files with 534 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ New deprecation(s):

- **General**: Added Pre Regex check before building image in e2e test ([#5783](https://github.com/kedacore/keda/issues/5783))
- **General**: Reduce the number of ScaledObject.Status updates in the fallback ([#5624](https://github.com/kedacore/keda/issues/5624))
- **IBM MQ Scaler**: Adding e2e test ([#1287](https://github.com/kedacore/keda/issues/1287))

## v2.14.0

Expand Down
16 changes: 14 additions & 2 deletions pkg/scalers/ibmmq_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"net/url"
"strconv"
"strings"
"time"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -53,7 +54,8 @@ type CommandResponse struct {

// Response The body of the response returned from the MQ admin query
type Response struct {
Parameters Parameters `json:"parameters"`
Parameters *Parameters `json:"parameters"`
Message []string `json:"message"`
}

// Parameters Contains the current depth of the IBM MQ Queue
Expand Down Expand Up @@ -198,8 +200,18 @@ func (s *IBMMQScaler) getQueueDepthViaHTTP(ctx context.Context) (int64, error) {
}

if response.CommandResponse == nil || len(response.CommandResponse) == 0 {
return 0, fmt.Errorf("failed to parse response from REST call: %w", err)
return 0, fmt.Errorf("failed to parse response from REST call")
}

if response.CommandResponse[0].Parameters == nil {
var reason string
message := strings.Join(response.CommandResponse[0].Message, " ")
if message != "" {
reason = fmt.Sprintf(", reason: %s", message)
}
return 0, fmt.Errorf("failed to get the current queue depth parameter%s", reason)
}

return int64(response.CommandResponse[0].Parameters.Curdepth), nil
}

Expand Down
101 changes: 101 additions & 0 deletions pkg/scalers/ibmmq_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package scalers

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/kedacore/keda/v2/pkg/scalers/scalersconfig"
)

Expand Down Expand Up @@ -128,3 +133,99 @@ func TestIBMMQGetMetricSpecForScaling(t *testing.T) {
}
}
}

type queueDepthResultTestData struct {
name string
bodyStr string
responseStatus int
expectedValue int64
isError bool
}

var testQueueDepthResults = []queueDepthResultTestData{
{
name: "valid response queue exists",
bodyStr: `{
"commandResponse": [{
"completionCode": 0,
"reasonCode": 0,
"parameters": {
"curdepth": 10,
"type": "QLOCAL",
"queue": "DEV.QUEUE.1"
}
}],
"overallReasonCode": 0,
"overallCompletionCode": 0
}`,
responseStatus: http.StatusOK,
expectedValue: 10,
isError: false,
},
{
name: "invalid response queue not found",
bodyStr: `{
"commandResponse": [{
"completionCode": 2,
"reasonCode": 2085,
"message": ["AMQ8147E: IBM MQ object FAKE.QUEUE not found."]
}],
"overallReasonCode": 3008,
"overallCompletionCode": 2
}`,
responseStatus: http.StatusOK,
expectedValue: 0,
isError: true,
},
{
name: "invalid response failed to parse commandResponse from REST call",
bodyStr: `{
"error": [{
"msgId": "MQWB0009E",
"action": "Resubmit the request with a valid queue manager name.",
"completionCode": 2,
"reasonCode": 2058,
"type": "rest",
"message": "MQWB0009E: Could not query the queue manager 'testqmgR'.",
"explanation": "The REST API was invoked specifying a queue manager name which cannot be located."}]
}`,
responseStatus: http.StatusOK,
expectedValue: 0,
isError: true,
},
}

func TestIBMMQScalerGetQueueDepthViaHTTP(t *testing.T) {
for _, testData := range testQueueDepthResults {
t.Run(testData.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(testData.responseStatus)

var body any
if err := json.Unmarshal([]byte(testData.bodyStr), &body); err != nil {
t.Fatal(err)
}
if err := json.NewEncoder(writer).Encode(body); err != nil {
t.Fatal(err)
}
}))
defer server.Close()

scaler := IBMMQScaler{
metadata: &IBMMQMetadata{
host: server.URL,
},
}

value, err := scaler.getQueueDepthViaHTTP(context.Background())
assert.Equal(t, testData.expectedValue, value)

if testData.isError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
Loading

0 comments on commit 13f309a

Please sign in to comment.