|
| 1 | +import * as sinon from 'sinon'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { CancellationToken, extensions } from 'vscode'; |
| 4 | +import { CodeQLExtensionInterface } from '../../../extension'; |
| 5 | +import { logger } from '../../../logging'; |
| 6 | +import * as config from '../../../config'; |
| 7 | + |
| 8 | +import * as ghApiClient from '../../../remote-queries/gh-api/gh-api-client'; |
| 9 | +import { VariantAnalysisMonitor } from '../../../remote-queries/variant-analysis-monitor'; |
| 10 | +import { |
| 11 | + VariantAnalysis as VariantAnalysisApiResponse, |
| 12 | + VariantAnalysisScannedRepository as ApiVariantAnalysisScannedRepository, |
| 13 | + VariantAnalysisFailureReason |
| 14 | +} from '../../../remote-queries/gh-api/variant-analysis'; |
| 15 | +import { createFailedMockApiResponse, createMockApiResponse } from '../../factories/remote-queries/gh-api/variant-analysis-api-response'; |
| 16 | +import { VariantAnalysisStatus } from '../../../remote-queries/shared/variant-analysis'; |
| 17 | +import { createMockScannedRepos } from '../../factories/remote-queries/gh-api/scanned-repositories'; |
| 18 | +import { processFailureReason } from '../../../remote-queries/variant-analysis-processor'; |
| 19 | +import { Credentials } from '../../../authentication'; |
| 20 | + |
| 21 | +describe('Variant Analysis Monitor', async function() { |
| 22 | + let sandbox: sinon.SinonSandbox; |
| 23 | + let mockGetVariantAnalysis: sinon.SinonStub; |
| 24 | + let cancellationToken: CancellationToken; |
| 25 | + let variantAnalysisMonitor: VariantAnalysisMonitor; |
| 26 | + let variantAnalysis: any; |
| 27 | + |
| 28 | + beforeEach(async () => { |
| 29 | + sandbox = sinon.createSandbox(); |
| 30 | + sandbox.stub(logger, 'log'); |
| 31 | + sandbox.stub(config, 'isVariantAnalysisLiveResultsEnabled').returns(false); |
| 32 | + |
| 33 | + cancellationToken = { |
| 34 | + isCancellationRequested: false |
| 35 | + } as unknown as CancellationToken; |
| 36 | + |
| 37 | + variantAnalysis = { |
| 38 | + id: 123, |
| 39 | + controllerRepoId: 1, |
| 40 | + }; |
| 41 | + |
| 42 | + try { |
| 43 | + const extension = await extensions.getExtension<CodeQLExtensionInterface | Record<string, never>>('GitHub.vscode-codeql')!.activate(); |
| 44 | + variantAnalysisMonitor = new VariantAnalysisMonitor(extension.ctx, logger); |
| 45 | + } catch (e) { |
| 46 | + fail(e as Error); |
| 47 | + } |
| 48 | + |
| 49 | + limitNumberOfAttemptsToMonitor(); |
| 50 | + }); |
| 51 | + |
| 52 | + afterEach(async () => { |
| 53 | + sandbox.restore(); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('when credentials are invalid', async () => { |
| 57 | + beforeEach(async () => { sandbox.stub(Credentials, 'initialize').resolves(undefined); }); |
| 58 | + |
| 59 | + it('should return early if credentials are wrong', async () => { |
| 60 | + try { |
| 61 | + await variantAnalysisMonitor.monitorVariantAnalysis(variantAnalysis, cancellationToken); |
| 62 | + } catch (error: any) { |
| 63 | + expect(error.message).to.equal('Error authenticating with GitHub'); |
| 64 | + } |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('when credentials are valid', async () => { |
| 69 | + beforeEach(async () => { |
| 70 | + const mockCredentials = { |
| 71 | + getOctokit: () => Promise.resolve({ |
| 72 | + request: mockGetVariantAnalysis |
| 73 | + }) |
| 74 | + } as unknown as Credentials; |
| 75 | + sandbox.stub(Credentials, 'initialize').resolves(mockCredentials); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should return early if variant analysis is cancelled', async () => { |
| 79 | + cancellationToken.isCancellationRequested = true; |
| 80 | + |
| 81 | + const result = await variantAnalysisMonitor.monitorVariantAnalysis(variantAnalysis, cancellationToken); |
| 82 | + |
| 83 | + expect(result).to.eql({ status: 'Cancelled', error: 'Variant Analysis was canceled.' }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('when the variant analysis fails', async () => { |
| 87 | + let mockFailedApiResponse: VariantAnalysisApiResponse; |
| 88 | + |
| 89 | + beforeEach(async function() { |
| 90 | + mockFailedApiResponse = createFailedMockApiResponse('in_progress'); |
| 91 | + mockGetVariantAnalysis = sandbox.stub(ghApiClient, 'getVariantAnalysis').resolves(mockFailedApiResponse); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should mark as failed locally and stop monitoring', async () => { |
| 95 | + const result = await variantAnalysisMonitor.monitorVariantAnalysis(variantAnalysis, cancellationToken); |
| 96 | + variantAnalysis = result.variantAnalysis; |
| 97 | + |
| 98 | + expect(mockGetVariantAnalysis.calledOnce).to.be.true; |
| 99 | + expect(result.status).to.eql('Failed'); |
| 100 | + expect(result.error).to.eql(`Variant Analysis has failed: ${mockFailedApiResponse.failure_reason}`); |
| 101 | + expect(variantAnalysis.status).to.equal(VariantAnalysisStatus.Failed); |
| 102 | + expect(variantAnalysis.failureReason).to.equal(processFailureReason(mockFailedApiResponse.failure_reason as VariantAnalysisFailureReason)); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + describe('when the variant analysis completes', async () => { |
| 107 | + let mockApiResponse: VariantAnalysisApiResponse; |
| 108 | + let scannedRepos: ApiVariantAnalysisScannedRepository[]; |
| 109 | + |
| 110 | + describe('when there are successfully scanned repos', async () => { |
| 111 | + beforeEach(async function() { |
| 112 | + scannedRepos = createMockScannedRepos(['pending', 'in_progress', 'succeeded']); |
| 113 | + mockApiResponse = createMockApiResponse('completed', scannedRepos); |
| 114 | + mockGetVariantAnalysis = sandbox.stub(ghApiClient, 'getVariantAnalysis').resolves(mockApiResponse); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should succeed and return a list of scanned repo ids', async () => { |
| 118 | + const result = await variantAnalysisMonitor.monitorVariantAnalysis(variantAnalysis, cancellationToken); |
| 119 | + const scannedRepoIds = scannedRepos.filter(r => r.analysis_status == 'succeeded').map(r => r.repository.id); |
| 120 | + |
| 121 | + expect(result.status).to.equal('CompletedSuccessfully'); |
| 122 | + expect(result.scannedReposDownloaded).to.eql(scannedRepoIds); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + describe('when there are only in progress repos', async () => { |
| 127 | + let scannedRepos: ApiVariantAnalysisScannedRepository[]; |
| 128 | + |
| 129 | + beforeEach(async function() { |
| 130 | + scannedRepos = createMockScannedRepos(['pending', 'in_progress']); |
| 131 | + mockApiResponse = createMockApiResponse('in_progress', scannedRepos); |
| 132 | + mockGetVariantAnalysis = sandbox.stub(ghApiClient, 'getVariantAnalysis').resolves(mockApiResponse); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should succeed and return an empty list of scanned repo ids', async () => { |
| 136 | + const result = await variantAnalysisMonitor.monitorVariantAnalysis(variantAnalysis, cancellationToken); |
| 137 | + |
| 138 | + expect(result.status).to.equal('CompletedSuccessfully'); |
| 139 | + expect(result.scannedReposDownloaded).to.eql([]); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + describe('when there are no repos to scan', async () => { |
| 144 | + beforeEach(async function() { |
| 145 | + scannedRepos = []; |
| 146 | + mockApiResponse = createMockApiResponse('completed', scannedRepos); |
| 147 | + mockGetVariantAnalysis = sandbox.stub(ghApiClient, 'getVariantAnalysis').resolves(mockApiResponse); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should succeed and return an empty list of scanned repo ids', async () => { |
| 151 | + const result = await variantAnalysisMonitor.monitorVariantAnalysis(variantAnalysis, cancellationToken); |
| 152 | + |
| 153 | + expect(result.status).to.equal('CompletedSuccessfully'); |
| 154 | + expect(result.scannedReposDownloaded).to.eql([]); |
| 155 | + }); |
| 156 | + }); |
| 157 | + }); |
| 158 | + }); |
| 159 | + |
| 160 | + function limitNumberOfAttemptsToMonitor() { |
| 161 | + VariantAnalysisMonitor.maxAttemptCount = 3; |
| 162 | + VariantAnalysisMonitor.sleepTime = 1; |
| 163 | + } |
| 164 | +}); |
| 165 | + |
0 commit comments