Skip to content

Commit

Permalink
Add getQueueDepthViaHTTP tests
Browse files Browse the repository at this point in the history
Signed-off-by: ayoyu <khaliayoub9@gmail.com>
  • Loading branch information
ayoyu committed Jun 6, 2024
1 parent 9941f0c commit 90923a5
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions pkg/scalers/ibmmq_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package scalers
import (
"context"
"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 +132,95 @@ 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.WriteHeader(testData.responseStatus)

// nosemgrep: no-direct-write-to-responsewriter
if _, err := writer.Write([]byte(testData.bodyStr)); 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)
}
})
}
}

0 comments on commit 90923a5

Please sign in to comment.