From 90923a5f3e823035ac2d78e4a0675991db24d8be Mon Sep 17 00:00:00 2001 From: ayoyu Date: Wed, 5 Jun 2024 17:30:37 +0200 Subject: [PATCH] Add getQueueDepthViaHTTP tests Signed-off-by: ayoyu --- pkg/scalers/ibmmq_scaler_test.go | 96 ++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/pkg/scalers/ibmmq_scaler_test.go b/pkg/scalers/ibmmq_scaler_test.go index 72ac9a71534..4cd0d7bfee1 100644 --- a/pkg/scalers/ibmmq_scaler_test.go +++ b/pkg/scalers/ibmmq_scaler_test.go @@ -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" ) @@ -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) + } + }) + } +}