danielhanrahan | c0b6f3a | 2024-04-29 16:16:42 +0100 | [diff] [blame] | 1 | /* |
| 2 | * ============LICENSE_START======================================================= |
| 3 | * Copyright (C) 2024 Nordix Foundation |
| 4 | * ================================================================================ |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | * SPDX-License-Identifier: Apache-2.0 |
| 18 | * ============LICENSE_END========================================================= |
| 19 | */ |
| 20 | |
| 21 | import http from 'k6/http'; |
| 22 | import { sleep, fail } from 'k6'; |
halil.cakal | 0613fbd | 2024-05-27 16:06:59 +0100 | [diff] [blame] | 23 | import { makeCustomSummaryReport, NCMP_BASE_URL, TOTAL_CM_HANDLES } from './utils.js'; |
danielhanrahan | c0b6f3a | 2024-04-29 16:16:42 +0100 | [diff] [blame] | 24 | |
| 25 | export const options = { |
halil.cakal | 0613fbd | 2024-05-27 16:06:59 +0100 | [diff] [blame] | 26 | vus: 1, |
| 27 | iterations: 1, |
| 28 | thresholds: { |
| 29 | http_req_failed: ['rate == 0'], |
| 30 | iteration_duration: ['max <= 300_000'], // 5 minutes |
| 31 | }, |
danielhanrahan | c0b6f3a | 2024-04-29 16:16:42 +0100 | [diff] [blame] | 32 | }; |
| 33 | |
halil.cakal | 0613fbd | 2024-05-27 16:06:59 +0100 | [diff] [blame] | 34 | export default function () { |
danielhanrahan | c0b6f3a | 2024-04-29 16:16:42 +0100 | [diff] [blame] | 35 | waitForCmHandlesToBeReady(TOTAL_CM_HANDLES); |
| 36 | } |
| 37 | |
| 38 | function waitForCmHandlesToBeReady(totalCmHandles) { |
| 39 | const timeOutInSeconds = 6 * 60; |
| 40 | const pollingIntervalInSeconds = 10; |
| 41 | const maxRetries = Math.ceil(timeOutInSeconds / pollingIntervalInSeconds); |
| 42 | let cmHandlesReady = 0; |
| 43 | for (let currentTry = 0; currentTry <= maxRetries; currentTry++) { |
| 44 | sleep(pollingIntervalInSeconds); |
| 45 | try { |
| 46 | cmHandlesReady = getNumberOfReadyCmHandles(); |
| 47 | } catch (error) { |
| 48 | console.error(`Attempt ${currentTry + 1} - Error fetching CM handles: ${error.message}`); |
| 49 | } |
| 50 | console.log(`Attempt ${currentTry + 1} - ${cmHandlesReady}/${totalCmHandles} CM handles are READY`); |
| 51 | if (cmHandlesReady === totalCmHandles) { |
| 52 | console.log(`All ${totalCmHandles} CM handles are READY`); |
| 53 | return; |
| 54 | } |
| 55 | } |
| 56 | fail(`Timed out after ${timeoutInSeconds} seconds waiting for ${totalCmHandles} CM handles to be READY`); |
| 57 | } |
| 58 | |
| 59 | function getNumberOfReadyCmHandles() { |
| 60 | const endpointUrl = `${NCMP_BASE_URL}/cps/api/v2/dataspaces/NCMP-Admin/anchors/ncmp-dmi-registry/node?xpath=/dmi-registry&descendants=all`; |
| 61 | const jsonData = http.get(endpointUrl).json(); |
| 62 | const cmHandles = jsonData[0]["dmi-reg:dmi-registry"]["cm-handles"]; |
| 63 | return cmHandles.filter(cmhandle => cmhandle['state']['cm-handle-state'] === 'READY').length; |
| 64 | } |
halil.cakal | 0613fbd | 2024-05-27 16:06:59 +0100 | [diff] [blame] | 65 | |
| 66 | export function handleSummary(data) { |
| 67 | return { |
| 68 | stdout: makeCustomSummaryReport(data, options), |
| 69 | }; |
| 70 | } |