[k6] Refactoring k6 tests (#1)

This commit moves all common request logic into a common folder.
It is needed to avoid duplication before adding JVM warmup phase.

- move registration-related code into common folder
- move passthrough operations into common folder

Issue-ID: CPS-2208
Signed-off-by: danielhanrahan <daniel.hanrahan@est.tech>
Change-Id: Ia9ebf61d21044b43063bde153f9c526e67d607c8
diff --git a/k6-tests/ncmp/1-create-cmhandles.js b/k6-tests/ncmp/1-create-cmhandles.js
index 60594c7..9a5c22a 100644
--- a/k6-tests/ncmp/1-create-cmhandles.js
+++ b/k6-tests/ncmp/1-create-cmhandles.js
@@ -18,16 +18,9 @@
  *  ============LICENSE_END=========================================================
  */
 
-import http from 'k6/http';
 import exec from 'k6/execution';
-import { check } from 'k6';
-import {
-    NCMP_BASE_URL,
-    DMI_PLUGIN_URL,
-    TOTAL_CM_HANDLES,
-    makeBatchOfCmHandleIds,
-    makeCustomSummaryReport
-} from './utils.js';
+import { TOTAL_CM_HANDLES, makeBatchOfCmHandleIds, makeCustomSummaryReport } from './common/utils.js';
+import { createCmHandles } from './common/cmhandle-crud.js';
 
 const BATCH_SIZE = 100;
 export const options = {
@@ -40,25 +33,9 @@
 };
 
 export default function () {
-    const nextBatchOfCmHandleIds = makeBatchOfCmHandleIds(BATCH_SIZE, exec.scenario.iterationInTest);
-    const payload = {
-        "dmiPlugin": DMI_PLUGIN_URL,
-        "createdCmHandles": nextBatchOfCmHandleIds.map(cmHandleId => ({
-            "cmHandle": cmHandleId,
-            "cmHandleProperties": {"neType": "RadioNode"},
-            "publicCmHandleProperties": {
-                "Color": "yellow",
-                "Size": "small",
-                "Shape": "cube"
-            }
-        })),
-    };
-    const response = http.post(NCMP_BASE_URL + '/ncmpInventory/v1/ch', JSON.stringify(payload), {
-        headers: {'Content-Type': 'application/json'},
-    });
-    check(response, {
-        'status equals 200': (r) => r.status === 200,
-    });
+    const batchNumber = exec.scenario.iterationInTest;
+    const nextBatchOfCmHandleIds = makeBatchOfCmHandleIds(BATCH_SIZE, batchNumber);
+    createCmHandles(nextBatchOfCmHandleIds);
 }
 
 export function handleSummary(data) {
diff --git a/k6-tests/ncmp/10-mixed-load-test.js b/k6-tests/ncmp/10-mixed-load-test.js
index 4bb0297..afa91af 100644
--- a/k6-tests/ncmp/10-mixed-load-test.js
+++ b/k6-tests/ncmp/10-mixed-load-test.js
@@ -18,10 +18,9 @@
  *  ============LICENSE_END=========================================================
  */
 
-import http from 'k6/http';
-import { check } from 'k6';
-import { NCMP_BASE_URL, getRandomCmHandleId, makeCustomSummaryReport } from './utils.js'
-import { executeCmHandleSearch, executeCmHandleIdSearch } from './search-base.js';
+import { makeCustomSummaryReport } from './common/utils.js'
+import { executeCmHandleSearch, executeCmHandleIdSearch } from './common/search-base.js';
+import { passthroughRead } from './common/passthrough-read.js';
 
 export const options = {
     scenarios: {
@@ -56,13 +55,7 @@
 };
 
 export function passthrough_read() {
-    const cmHandleId = getRandomCmHandleId();
-    const datastoreName = 'ncmp-datastore%3Apassthrough-operational';
-    const url = `${NCMP_BASE_URL}/ncmp/v1/ch/${cmHandleId}/data/ds/${datastoreName}?resourceIdentifier=x&include-descendants=true`
-    const response = http.get(url);
-    check(response, {
-        'status equals 200': (r) => r.status === 200,
-    });
+    passthroughRead();
 }
 
 export function id_search_module() {
diff --git a/k6-tests/ncmp/11-delete-cmhandles.js b/k6-tests/ncmp/11-delete-cmhandles.js
index 073d1d0..534b3de 100644
--- a/k6-tests/ncmp/11-delete-cmhandles.js
+++ b/k6-tests/ncmp/11-delete-cmhandles.js
@@ -18,10 +18,9 @@
  *  ============LICENSE_END=========================================================
  */
 
-import http from 'k6/http';
 import exec from 'k6/execution';
-import { check } from 'k6';
-import { NCMP_BASE_URL, TOTAL_CM_HANDLES, makeBatchOfCmHandleIds, makeCustomSummaryReport } from './utils.js';
+import { TOTAL_CM_HANDLES, makeBatchOfCmHandleIds, makeCustomSummaryReport } from './common/utils.js';
+import { deleteCmHandles } from './common/cmhandle-crud.js';
 
 const BATCH_SIZE = 100;
 export const options = {
@@ -34,17 +33,9 @@
 };
 
 export default function () {
-    const nextBatchOfCmHandleIds = makeBatchOfCmHandleIds(BATCH_SIZE, exec.scenario.iterationInTest);
-    const payload = {
-        "dmiPlugin": "http://ncmp-dmi-plugin-demo-and-csit-stub:8092",
-        "removedCmHandles": nextBatchOfCmHandleIds,
-    };
-    const response = http.post(NCMP_BASE_URL + '/ncmpInventory/v1/ch', JSON.stringify(payload), {
-        headers: {'Content-Type': 'application/json'},
-    });
-    check(response, {
-        'status equals 200': (r) => r.status === 200,
-    });
+    const batchNumber = exec.scenario.iterationInTest;
+    const nextBatchOfCmHandleIds = makeBatchOfCmHandleIds(BATCH_SIZE, batchNumber);
+    deleteCmHandles(nextBatchOfCmHandleIds);
 }
 
 export function handleSummary(data) {
diff --git a/k6-tests/ncmp/2-wait-for-cmhandles-to-be-ready.js b/k6-tests/ncmp/2-wait-for-cmhandles-to-be-ready.js
index 5d54c60..cce85ab 100644
--- a/k6-tests/ncmp/2-wait-for-cmhandles-to-be-ready.js
+++ b/k6-tests/ncmp/2-wait-for-cmhandles-to-be-ready.js
@@ -18,9 +18,8 @@
  *  ============LICENSE_END=========================================================
  */
 
-import http from 'k6/http';
-import { sleep, fail } from 'k6';
-import { makeCustomSummaryReport, NCMP_BASE_URL, TOTAL_CM_HANDLES } from './utils.js';
+import { makeCustomSummaryReport } from './common/utils.js';
+import { waitForCmHandlesToBeReady } from './common/cmhandle-crud.js';
 
 export const options = {
     vus: 1,
@@ -32,35 +31,8 @@
 };
 
 export default function () {
-    waitForCmHandlesToBeReady(TOTAL_CM_HANDLES);
-}
-
-function waitForCmHandlesToBeReady(totalCmHandles) {
     const timeOutInSeconds = 6 * 60;
-    const pollingIntervalInSeconds = 10;
-    const maxRetries = Math.ceil(timeOutInSeconds / pollingIntervalInSeconds);
-    let cmHandlesReady = 0;
-    for (let currentTry = 0; currentTry <= maxRetries; currentTry++) {
-        sleep(pollingIntervalInSeconds);
-        try {
-            cmHandlesReady = getNumberOfReadyCmHandles();
-        } catch (error) {
-            console.error(`Attempt ${currentTry + 1} - Error fetching CM handles: ${error.message}`);
-        }
-        console.log(`Attempt ${currentTry + 1} - ${cmHandlesReady}/${totalCmHandles} CM handles are READY`);
-        if (cmHandlesReady === totalCmHandles) {
-            console.log(`All ${totalCmHandles} CM handles are READY`);
-            return;
-        }
-    }
-    fail(`Timed out after ${timeoutInSeconds} seconds waiting for ${totalCmHandles} CM handles to be READY`);
-}
-
-function getNumberOfReadyCmHandles() {
-    const endpointUrl = `${NCMP_BASE_URL}/cps/api/v2/dataspaces/NCMP-Admin/anchors/ncmp-dmi-registry/node?xpath=/dmi-registry&descendants=all`;
-    const jsonData = http.get(endpointUrl).json();
-    const cmHandles = jsonData[0]["dmi-reg:dmi-registry"]["cm-handles"];
-    return cmHandles.filter(cmhandle => cmhandle['state']['cm-handle-state'] === 'READY').length;
+    waitForCmHandlesToBeReady(timeOutInSeconds);
 }
 
 export function handleSummary(data) {
diff --git a/k6-tests/ncmp/3-passthrough-read.js b/k6-tests/ncmp/3-passthrough-read.js
index 84050b8..39eb4ad 100644
--- a/k6-tests/ncmp/3-passthrough-read.js
+++ b/k6-tests/ncmp/3-passthrough-read.js
@@ -18,10 +18,9 @@
  *  ============LICENSE_END=========================================================
  */
 
-import http from 'k6/http';
-import { check } from 'k6';
-import { Trend } from "k6/metrics";
-import { NCMP_BASE_URL, getRandomCmHandleId, makeCustomSummaryReport } from './utils.js'
+import { Trend } from 'k6/metrics';
+import { passthroughRead } from './common/passthrough-read.js'
+import { makeCustomSummaryReport } from './common/utils.js'
 
 let ncmpOverheadTrend = new Trend("ncmp_overhead");
 
@@ -36,14 +35,7 @@
 
 // The function that defines VU logic.
 export default function () {
-    const cmHandleId = getRandomCmHandleId();
-    const datastoreName = 'ncmp-datastore%3Apassthrough-operational';
-    const url = `${NCMP_BASE_URL}/ncmp/v1/ch/${cmHandleId}/data/ds/${datastoreName}?resourceIdentifier=x&include-descendants=true`
-    const response = http.get(url);
-    check(response, {
-        'status equals 200': (r) => r.status === 200,
-    });
-
+    const response = passthroughRead();
     // Calculate overhead assuming DMI data delay is 2500ms.
     const dmiDelay = 2500; // This should be same as value DATA_FOR_CM_HANDLE_DELAY_MS in docker-compose.yml
     const overhead = response.timings.duration - dmiDelay;
diff --git a/k6-tests/ncmp/4-id-search-no-filter.js b/k6-tests/ncmp/4-id-search-no-filter.js
index ed8d77f..3863b5d 100644
--- a/k6-tests/ncmp/4-id-search-no-filter.js
+++ b/k6-tests/ncmp/4-id-search-no-filter.js
@@ -18,8 +18,8 @@
  *  ============LICENSE_END=========================================================
  */
 
-import { executeCmHandleIdSearch } from './search-base.js';
-import { makeCustomSummaryReport } from "./utils.js";
+import { executeCmHandleIdSearch } from './common/search-base.js';
+import { makeCustomSummaryReport } from './common/utils.js';
 
 export const options = {
     vus: 5,
diff --git a/k6-tests/ncmp/5-search-no-filter.js b/k6-tests/ncmp/5-search-no-filter.js
index 73fa827..67c9d59 100644
--- a/k6-tests/ncmp/5-search-no-filter.js
+++ b/k6-tests/ncmp/5-search-no-filter.js
@@ -18,8 +18,8 @@
  *  ============LICENSE_END=========================================================
  */
 
-import { executeCmHandleSearch } from './search-base.js';
-import { makeCustomSummaryReport } from "./utils.js";
+import { executeCmHandleSearch } from './common/search-base.js';
+import { makeCustomSummaryReport } from './common/utils.js';
 
 export const options = {
     vus: 4,
diff --git a/k6-tests/ncmp/6-id-search-public-property.js b/k6-tests/ncmp/6-id-search-public-property.js
index 543ee1a..25bffaf 100644
--- a/k6-tests/ncmp/6-id-search-public-property.js
+++ b/k6-tests/ncmp/6-id-search-public-property.js
@@ -18,8 +18,8 @@
  *  ============LICENSE_END=========================================================
  */
 
-import { executeCmHandleIdSearch } from './search-base.js';
-import { makeCustomSummaryReport } from "./utils.js";
+import { executeCmHandleIdSearch } from './common/search-base.js';
+import { makeCustomSummaryReport } from './common/utils.js';
 
 export const options = {
     vus: 5,
diff --git a/k6-tests/ncmp/7-search-public-property.js b/k6-tests/ncmp/7-search-public-property.js
index cb39b54..53f069d 100644
--- a/k6-tests/ncmp/7-search-public-property.js
+++ b/k6-tests/ncmp/7-search-public-property.js
@@ -18,8 +18,8 @@
  *  ============LICENSE_END=========================================================
  */
 
-import { executeCmHandleSearch } from './search-base.js';
-import { makeCustomSummaryReport } from "./utils.js";
+import { executeCmHandleSearch } from './common/search-base.js';
+import { makeCustomSummaryReport } from './common/utils.js';
 
 export const options = {
     vus: 4,
diff --git a/k6-tests/ncmp/8-id-search-module.js b/k6-tests/ncmp/8-id-search-module.js
index d058a1b..8200ea2 100644
--- a/k6-tests/ncmp/8-id-search-module.js
+++ b/k6-tests/ncmp/8-id-search-module.js
@@ -18,8 +18,8 @@
  *  ============LICENSE_END=========================================================
  */
 
-import { executeCmHandleIdSearch } from './search-base.js';
-import { makeCustomSummaryReport } from "./utils.js";
+import { executeCmHandleIdSearch } from './common/search-base.js';
+import { makeCustomSummaryReport } from './common/utils.js';
 
 export const options = {
     vus: 5,
diff --git a/k6-tests/ncmp/9-search-module.js b/k6-tests/ncmp/9-search-module.js
index c6bbb06..eafef99 100644
--- a/k6-tests/ncmp/9-search-module.js
+++ b/k6-tests/ncmp/9-search-module.js
@@ -18,8 +18,8 @@
  *  ============LICENSE_END=========================================================
  */
 
-import { executeCmHandleSearch } from './search-base.js';
-import { makeCustomSummaryReport } from "./utils.js";
+import { executeCmHandleSearch } from './common/search-base.js';
+import { makeCustomSummaryReport } from './common/utils.js';
 
 export const options = {
     vus: 4,
diff --git a/k6-tests/ncmp/common/cmhandle-crud.js b/k6-tests/ncmp/common/cmhandle-crud.js
new file mode 100644
index 0000000..0c3e116
--- /dev/null
+++ b/k6-tests/ncmp/common/cmhandle-crud.js
@@ -0,0 +1,90 @@
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2024 Nordix Foundation
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+import http from 'k6/http';
+import { check, sleep, fail } from 'k6';
+import { NCMP_BASE_URL, DMI_PLUGIN_URL, TOTAL_CM_HANDLES } from './utils.js';
+
+export function createCmHandles(cmHandleIds) {
+    const url = `${NCMP_BASE_URL}/ncmpInventory/v1/ch`;
+    const payload = {
+        "dmiPlugin": DMI_PLUGIN_URL,
+        "createdCmHandles": cmHandleIds.map(cmHandleId => ({
+            "cmHandle": cmHandleId,
+            "cmHandleProperties": {"neType": "RadioNode"},
+            "publicCmHandleProperties": {
+                "Color": "yellow",
+                "Size": "small",
+                "Shape": "cube"
+            }
+        })),
+    };
+    const params = {
+        headers: {'Content-Type': 'application/json'}
+    };
+    const response = http.post(url, JSON.stringify(payload), params);
+    check(response, {
+        'status equals 200': (r) => r.status === 200,
+    });
+    return response;
+}
+
+export function deleteCmHandles(cmHandleIds) {
+    const url = `${NCMP_BASE_URL}/ncmpInventory/v1/ch`;
+    const payload = {
+        "dmiPlugin": DMI_PLUGIN_URL,
+        "removedCmHandles": cmHandleIds,
+    };
+    const params = {
+        headers: {'Content-Type': 'application/json'}
+    };
+    const response = http.post(url, JSON.stringify(payload), params);
+    check(response, {
+        'status equals 200': (r) => r.status === 200,
+    });
+    return response;
+}
+
+export function waitForCmHandlesToBeReady(timeOutInSeconds) {
+    const pollingIntervalInSeconds = 10;
+    const maxRetries = Math.ceil(timeOutInSeconds / pollingIntervalInSeconds);
+    let cmHandlesReady = 0;
+    for (let currentTry = 0; currentTry <= maxRetries; currentTry++) {
+        sleep(pollingIntervalInSeconds);
+        try {
+            cmHandlesReady = getNumberOfReadyCmHandles();
+        } catch (error) {
+            console.error(`Attempt ${currentTry + 1} - Error fetching CM handles: ${error.message}`);
+        }
+        console.log(`Attempt ${currentTry + 1} - ${cmHandlesReady}/${TOTAL_CM_HANDLES} CM handles are READY`);
+        if (cmHandlesReady === TOTAL_CM_HANDLES) {
+            console.log(`All ${TOTAL_CM_HANDLES} CM handles are READY`);
+            return;
+        }
+    }
+    fail(`Timed out after ${timeOutInSeconds} seconds waiting for ${TOTAL_CM_HANDLES} CM handles to be READY`);
+}
+
+function getNumberOfReadyCmHandles() {
+    const endpointUrl = `${NCMP_BASE_URL}/cps/api/v2/dataspaces/NCMP-Admin/anchors/ncmp-dmi-registry/node?xpath=/dmi-registry&descendants=all`;
+    const jsonData = http.get(endpointUrl).json();
+    const cmHandles = jsonData[0]["dmi-reg:dmi-registry"]["cm-handles"];
+    return cmHandles.filter(cmhandle => cmhandle['state']['cm-handle-state'] === 'READY').length;
+}
diff --git a/k6-tests/ncmp/common/passthrough-read.js b/k6-tests/ncmp/common/passthrough-read.js
new file mode 100644
index 0000000..e4e937c
--- /dev/null
+++ b/k6-tests/ncmp/common/passthrough-read.js
@@ -0,0 +1,36 @@
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2024 Nordix Foundation
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+import http from 'k6/http';
+import { check } from 'k6';
+import { NCMP_BASE_URL, getRandomCmHandleId } from './utils.js';
+
+export function passthroughRead() {
+    const cmHandleId = getRandomCmHandleId();
+    const resourceIdentifier = 'my-resource-identifier';
+    const includeDescendants = true;
+    const datastoreName = 'ncmp-datastore:passthrough-operational';
+    const url = `${NCMP_BASE_URL}/ncmp/v1/ch/${cmHandleId}/data/ds/${datastoreName}?resourceIdentifier=${resourceIdentifier}&include-descendants=${includeDescendants}`
+    const response = http.get(url);
+    check(response, {
+        'status equals 200': (r) => r.status === 200,
+    });
+    return response;
+}
diff --git a/k6-tests/ncmp/search-base.js b/k6-tests/ncmp/common/search-base.js
similarity index 83%
rename from k6-tests/ncmp/search-base.js
rename to k6-tests/ncmp/common/search-base.js
index 04b6444..f833a53 100644
--- a/k6-tests/ncmp/search-base.js
+++ b/k6-tests/ncmp/common/search-base.js
@@ -51,14 +51,18 @@
 }
 
 function executeSearchRequest(searchType, scenario) {
-    const searchParameter = JSON.stringify(SEARCH_PARAMETERS_PER_SCENARIO[scenario]);
-    const response = http.post(NCMP_BASE_URL + '/ncmp/v1/ch/' + searchType, searchParameter, {
-        headers: {'Content-Type': 'application/json'},
-    });
+    const searchParameters = SEARCH_PARAMETERS_PER_SCENARIO[scenario];
+    const payload = JSON.stringify(searchParameters);
+    const url = `${NCMP_BASE_URL}/ncmp/v1/ch/${searchType}`;
+    const params = {
+        headers: {'Content-Type': 'application/json'}
+    };
+    const response = http.post(url, payload, params);
     check(response, {
         'status equals 200': (r) => r.status === 200,
     });
-    check(JSON.parse(response.body), {
+    const responseData = JSON.parse(response.body);
+    check(responseData, {
         'returned list has expected CM-handles': (arr) => arr.length === TOTAL_CM_HANDLES,
     });
 }
diff --git a/k6-tests/ncmp/utils.js b/k6-tests/ncmp/common/utils.js
similarity index 86%
rename from k6-tests/ncmp/utils.js
rename to k6-tests/ncmp/common/utils.js
index 18a8940..1fb9b8e 100644
--- a/k6-tests/ncmp/utils.js
+++ b/k6-tests/ncmp/common/utils.js
@@ -22,9 +22,11 @@
 export const DMI_PLUGIN_URL = 'http://ncmp-dmi-plugin-demo-and-csit-stub:8092';
 export const TOTAL_CM_HANDLES = 20000
 
-/*
- * Makes a batch of CM-handle IDs.
- * Given a batchSize=100 and batchNumber=2, it will generate ['ch-201', 'ch-202' ... 'ch-300']
+/**
+ * Generates a batch of CM-handle IDs based on batch size and number.
+ * @param {number} batchSize - Size of each batch.
+ * @param {number} batchNumber - Number of the batch.
+ * @returns {string[]} Array of CM-handle IDs, for example ['ch-201', 'ch-202' ... 'ch-300']
  */
 export function makeBatchOfCmHandleIds(batchSize, batchNumber) {
     const batchOfIds = [];
@@ -37,7 +39,7 @@
 }
 
 export function getRandomCmHandleId() {
-    return 'ch-' + (Math.floor(Math.random() * TOTAL_CM_HANDLES) + 1);
+    return `ch-${Math.floor(Math.random() * TOTAL_CM_HANDLES) + 1}`;
 }
 
 function removeBracketsAndQuotes(str) {