import { ResponseWithBodyType , System } from 'common' ; import _ from 'lodash' ; import { Logger } from 'logger' ; import { HealthReport , TestRunResult } from 'service-library' ; import { A11yServiceClient } from 'web-api-client' ; import { ScanReportDownloader } from './scan-report-downloader' ; export class DeploymentHealthChecker { public constructor ( private readonly logger , private readonly client , private readonly reportDownloader , private readonly waitFunc = System . wait , private readonly getCurrentTime = ( ) => new Date ( ) ) { } public async run ( testTimeoutInMinutes , waitTimeBeforeEvaluationInMinutes , evaluationIntervalInMinutes , releaseId ) { this . logger . logInfo ( 'Start evaluation of functional tests result.' ) ; this . logger . logInfo ( `Waiting for ${ waitTimeBeforeEvaluationInMinutes } minutes before evaluating functional tests result.` ) ; await this . waitFunc ( this . minutesToMilliseconds ( waitTimeBeforeEvaluationInMinutes ) ) ; let healthStatus ; let response ; const startTime = this . getCurrentTime ( ) ; while ( healthStatus !== 'pass' ) { try { this . logger . logInfo ( 'Retrieving functional tests result.' ) ; response = await this . client . checkHealth ( `/release/${ releaseId }` ) ; if ( response . statusCode !== 200 ) { throw new Error ( JSON . stringify ( { statusCode : response . statusCode , statusMessage : response . statusMessage , body : response . body } ) ) ; } healthStatus = response . body . healthStatus ; this . logger . logInfo ( `Functional tests result: ${ JSON . stringify ( response . body ) }` ) ; } catch ( error ) { this . logger . logInfo ( `Failed to retrieve functional tests result. ${ System . serializeError ( error ) }` ) ; } if ( healthStatus !== 'pass' ) { if ( this . isTestTimeout ( startTime , testTimeoutInMinutes ) ) { this . logger . logInfo ( 'Functional tests result validation timed out.' ) ; throw new Error ( 'Functional tests result validation timed out.' ) ; } this . logger . logInfo ( `Functional tests health status: ${ healthStatus ? healthStatus : 'unknown' } . Waiting for next evaluation result.` ) ; await this . waitFunc ( this . minutesToMilliseconds ( evaluationIntervalInMinutes ) ) ; } else { this . logger . logInfo ( 'Functional tests succeeded.' ) ; await this . downloadAllReports ( response . body ) ; } } } private isTestTimeout ( startTime , testTimeoutInMinutes ) { const currentTime = this . getCurrentTime ( ) ; return currentTime . getTime ( ) - startTime . getTime ( ) > testTimeoutInMinutes * 60000 ; } private minutesToMilliseconds ( minutes ) { return minutes * 60000 ; } private async downloadAllReports ( healthReport ) { this . logger . logInfo ( 'Downloading E2E scan reports...' ) ; const uniqueScanTestRuns = _ . compact ( _ . uniqBy ( healthReport . testRuns , ( testRun ) => testRun . scanId ) ) ; await Promise . all ( uniqueScanTestRuns . map ( ( testRun ) => { const scanId = testRun . scanId ; const scenarioName = testRun . scenarioName ; return this . reportDownloader . downloadReportsForScan ( scanId , scenarioName ) ; } ) ) ; this . logger . logInfo ( 'All reports downloaded.' ) ; } }