diff --git a/.github/workflows/test.probe.yaml b/.github/workflows/test.probe.yaml index d3a680e7be..fb7b8fc2d1 100644 --- a/.github/workflows/test.probe.yaml +++ b/.github/workflows/test.probe.yaml @@ -19,5 +19,6 @@ jobs: node-version: 18.3.0 - run: cd Common && npm install - run: cd CommonServer && npm install - - run: cd Probe && npm install && npm run test + - run: cd Probe && npm install + - run: cd Probe && npm run test \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index 2bdd3611a0..4820f4d5f5 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -334,6 +334,19 @@ "debug:test" ], }, + { + "name": "Probe: Debug Tests", + "type": "node", + "restart": true, + "autoAttachChildProcesses": true, + "request": "launch", + "cwd": "${workspaceRoot}/Probe", + "runtimeExecutable": "npm", + "runtimeArgs": [ + "run-script", + "debug:test" + ], + }, { "name": "Accounts: Debug Local Files", "type": "node", diff --git a/Common/Types/WebsiteRequest.ts b/Common/Types/WebsiteRequest.ts index 1f2f7e86b9..2c28c8757a 100644 --- a/Common/Types/WebsiteRequest.ts +++ b/Common/Types/WebsiteRequest.ts @@ -57,9 +57,9 @@ export default class WebsiteRequest { return { url: url, requestHeaders: options.headers || {}, - responseHeaders: response.headers as Dictionary, - responseStatusCode: response.status, - responseBody: new HTML(response.data), + responseHeaders: response!.headers as Dictionary, + responseStatusCode: response!.status, + responseBody: new HTML(response!.data), isOnline: true, }; } diff --git a/Probe/Tests/Utils/PingMonitor.test.ts b/Probe/Tests/Utils/PingMonitor.test.ts index 6d01db6049..3a57b27efc 100644 --- a/Probe/Tests/Utils/PingMonitor.test.ts +++ b/Probe/Tests/Utils/PingMonitor.test.ts @@ -4,19 +4,20 @@ import PositiveNumber from 'Common/Types/PositiveNumber'; import Ping, { PingResponse, } from '../../Utils/Monitors/MonitorTypes/PingMonitor'; +import BadDataException from 'Common/Types/Exception/BadDataException'; describe('Ping', () => { - jest.setTimeout(10000); + jest.setTimeout(120000); test('Ping.ping should return appropriate object if the valid hostname is given', async () => { let result: PingResponse | null = await Ping.ping( - new Hostname('google.com', 80) + new Hostname('google.com') ); expect(result).not.toBeNull(); expect(result!.responseTimeInMS?.toNumber()).toBeGreaterThan(0); expect(result!.responseTimeInMS?.toNumber()).toBeLessThanOrEqual(5000); expect(result!.isOnline).toBe(true); - result = await Ping.ping(new Hostname('www.google.com', 80), { + result = await Ping.ping(new Hostname('www.google.com'), { timeout: new PositiveNumber(5000), }); @@ -25,22 +26,24 @@ describe('Ping', () => { expect(result!.responseTimeInMS?.toNumber()).toBeGreaterThan(0); expect(result!.responseTimeInMS?.toNumber()).toBeLessThanOrEqual(5000); - result = await Ping.ping(new Hostname('www.google.com', 65000), { - timeout: new PositiveNumber(5000), - }); - expect(result).not.toBeNull(); - expect(result!.isOnline).toBe(false); - expect(result!.responseTimeInMS).toBeUndefined(); + try { + await Ping.ping(new Hostname('www.google.com', 65000), { + timeout: new PositiveNumber(5000), + }); + } catch (err) { + expect(err).toBeInstanceOf(BadDataException); + } - result = await Ping.ping(new Hostname('www.a.com', 65000), { - timeout: new PositiveNumber(5000), - }); - expect(result).not.toBeNull(); - expect(result!.isOnline).toBe(false); - expect(result!.isOnline).toBe(false); - expect(result!.responseTimeInMS).toBeUndefined(); + try { + await Ping.ping(new Hostname('www.a.com', 65000), { + timeout: new PositiveNumber(5000), + }); + } catch (err) { + expect(err).toBeInstanceOf(BadDataException); + } }); test('Ping.ping should return appropriate object if the valid IPV4 or IPV6 is given', async () => { + // change test timeout to 2 minutes let result: PingResponse | null = null; result = await Ping.ping(new IPv4('172.217.170.206'), { diff --git a/Probe/Utils/Monitors/MonitorTypes/PingMonitor.ts b/Probe/Utils/Monitors/MonitorTypes/PingMonitor.ts index 5d78e90599..6e707aa6b3 100644 --- a/Probe/Utils/Monitors/MonitorTypes/PingMonitor.ts +++ b/Probe/Utils/Monitors/MonitorTypes/PingMonitor.ts @@ -8,6 +8,7 @@ import logger from 'CommonServer/Utils/Logger'; import ping from 'ping'; import UnableToReachServer from 'Common/Types/Exception/UnableToReachServer'; import Sleep from 'Common/Types/Sleep'; +import BadDataException from 'Common/Types/Exception/BadDataException'; // TODO - make sure it work for the IPV6 export interface PingResponse { @@ -79,6 +80,12 @@ export default class PingMonitor { let hostAddress: string = ''; if (host instanceof Hostname) { hostAddress = host.hostname; + + if (host.port) { + throw new BadDataException( + 'Port is not supported for ping monitor' + ); + } } else if (host instanceof URL) { hostAddress = host.hostname.hostname; } else { diff --git a/Probe/package.json b/Probe/package.json index 84a089984b..1de66e0cab 100644 --- a/Probe/package.json +++ b/Probe/package.json @@ -9,7 +9,8 @@ "dev": "npx nodemon", "audit": "npm audit --audit-level=low", "dep-check": "depcheck ./ --skip-missing=true", - "test": "jest --passWithNoTests" + "test": "jest --passWithNoTests", + "debug:test": "node --inspect node_modules/.bin/jest --runInBand ./Tests --detectOpenHandles" }, "author": "", "license": "ISC", diff --git a/Tests/Dockerfile.tpl b/Tests/Dockerfile.tpl index 08937aa60c..96258c9ac9 100644 --- a/Tests/Dockerfile.tpl +++ b/Tests/Dockerfile.tpl @@ -1,4 +1,4 @@ -FROM node:18.18.1-alpine +FROM node:current-alpine USER root RUN mkdir /tmp/npm && chmod 2777 /tmp/npm && chown 1000:1000 /tmp/npm && npm config set cache /tmp/npm --global @@ -12,7 +12,6 @@ ARG APP_VERSION ENV GIT_SHA=${GIT_SHA} ENV APP_VERSION=${APP_VERSION} -RUN npm -g config set user root RUN apk add bash COPY ./Tests .