Skip to content

Commit

Permalink
SCANNPM-2 Change wasJreCacheHit from bool to enum and mark it disable…
Browse files Browse the repository at this point in the history
…d when skipping JRE provisioning explicitly
  • Loading branch information
7PH committed May 3, 2024
1 parent f0aa233 commit 635750c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 20 deletions.
5 changes: 4 additions & 1 deletion src/java.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { download, fetch } from './request';
import {
AnalysisJreMetaData,
AnalysisJresResponseType,
CacheStatus,
ScannerProperties,
ScannerProperty,
} from './types';
Expand Down Expand Up @@ -113,7 +114,9 @@ export async function fetchJRE(properties: ScannerProperties): Promise<string> {
checksum: jreMetaData.sha256,
filename: jreMetaData.filename + UNARCHIVE_SUFFIX,
});
properties[ScannerProperty.SonarScannerWasJreCacheHit] = Boolean(cachedJrePath).toString();
properties[ScannerProperty.SonarScannerWasJreCacheHit] = cachedJrePath
? CacheStatus.Hit
: CacheStatus.Miss;
if (cachedJrePath) {
log(LogLevel.INFO, 'Using Cached JRE');
return path.join(cachedJrePath, jreMetaData.javaPath);
Expand Down
9 changes: 5 additions & 4 deletions src/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ import {
SONAR_DIR_DEFAULT,
SONAR_PROJECT_FILENAME,
} from './constants';
import { log, LogLevel } from './logging';
import { LogLevel, log } from './logging';
import { getArch, getSupportedOS } from './platform';
import { CliArgs, ScannerProperties, ScannerProperty, ScanOptions } from './types';
import { CacheStatus, CliArgs, ScanOptions, ScannerProperties, ScannerProperty } from './types';

function getDefaultProperties(): ScannerProperties {
return {
Expand Down Expand Up @@ -307,8 +307,9 @@ function getBootstrapperProperties(startTimestampMs: number): ScannerProperties
'sonar.scanner.app': SCANNER_BOOTSTRAPPER_NAME,
'sonar.scanner.appVersion': version,
'sonar.scanner.bootstrapStartTime': startTimestampMs.toString(),
// Bootstrap cache hit/miss is set later after the bootstrapper has run and before scanner engine is started
'sonar.scanner.wasJreCacheHit': 'false',
// These cache statuses are set during the bootstrapping process.
// We already set them here to prevent them from being overridden.
'sonar.scanner.wasJreCacheHit': CacheStatus.Disabled,
'sonar.scanner.wasEngineCacheHit': 'false',
};
}
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,9 @@ export type AnalysisEngineResponseType = {
sha256: string;
downloadUrl?: string;
};

export enum CacheStatus {
Hit = 'hit',
Miss = 'miss',
Disabled = 'disabled',
}
24 changes: 13 additions & 11 deletions test/unit/java.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ import { API_V2_JRE_ENDPOINT, SONARQUBE_JRE_PROVISIONING_MIN_VERSION } from '../
import * as file from '../../src/file';
import { fetchJRE, fetchServerVersion, serverSupportsJREProvisioning } from '../../src/java';
import * as request from '../../src/request';
import { AnalysisJresResponseType, ScannerProperties, ScannerProperty } from '../../src/types';
import {
AnalysisJresResponseType,
CacheStatus,
ScannerProperties,
ScannerProperty,
} from '../../src/types';

const mock = new MockAdapter(axios);

Expand Down Expand Up @@ -142,15 +147,14 @@ describe('java', () => {

describe('when the JRE is cached', () => {
it('should fetch the latest supported JRE and use the cached version', async () => {
await fetchJRE(MOCKED_PROPERTIES);
const properties = { ...MOCKED_PROPERTIES };
await fetchJRE(properties);

expect(request.fetch).toHaveBeenCalledTimes(1);
expect(request.download).not.toHaveBeenCalled();

// Check for the cache
expect(file.getCacheFileLocation).toHaveBeenCalledTimes(1);

expect(file.extractArchive).not.toHaveBeenCalled();
expect(properties[ScannerProperty.SonarScannerWasJreCacheHit]).toBe(CacheStatus.Hit);
});
});

Expand All @@ -167,7 +171,8 @@ describe('java', () => {
});

it('should download the JRE', async () => {
await fetchJRE({ ...MOCKED_PROPERTIES });
const properties = { ...MOCKED_PROPERTIES };
await fetchJRE(properties);

expect(request.fetch).toHaveBeenCalledWith({
url: API_V2_JRE_ENDPOINT,
Expand All @@ -176,17 +181,14 @@ describe('java', () => {
arch: MOCKED_PROPERTIES[ScannerProperty.SonarScannerArch],
},
});

expect(file.getCacheFileLocation).toHaveBeenCalledTimes(1);

expect(request.download).toHaveBeenCalledWith(
`${API_V2_JRE_ENDPOINT}/${serverResponse[0].id}`,
mockCacheDirectories.archivePath,
);

expect(file.validateChecksum).toHaveBeenCalledTimes(1);

expect(file.extractArchive).toHaveBeenCalledTimes(1);
expect(properties[ScannerProperty.SonarScannerWasJreCacheHit]).toBe(CacheStatus.Miss);
});

it('should fail if no JRE matches', async () => {
Expand All @@ -200,7 +202,7 @@ describe('java', () => {
.reply(200, []);

// Check that it rejects with a specific error
expect(fetchJRE({ ...MOCKED_PROPERTIES })).rejects.toThrowError(
expect(fetchJRE({ ...MOCKED_PROPERTIES })).rejects.toThrow(
'No JREs available for your platform linux arm64',
);
});
Expand Down
3 changes: 2 additions & 1 deletion test/unit/mocks/FakeProjectMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import path from 'path';
import sinon from 'sinon';
import { SCANNER_BOOTSTRAPPER_NAME } from '../../../src/constants';
import { CacheStatus } from '../../../src/types';

const baseEnvVariables = process.env;

Expand Down Expand Up @@ -59,7 +60,7 @@ export class FakeProjectMock {
'sonar.scanner.app': SCANNER_BOOTSTRAPPER_NAME,
'sonar.scanner.appVersion': '1.2.3',
'sonar.scanner.wasEngineCacheHit': 'false',
'sonar.scanner.wasJreCacheHit': 'false',
'sonar.scanner.wasJreCacheHit': CacheStatus.Disabled,
'sonar.userHome': expect.stringMatching(/\.sonar$/),
'sonar.scanner.os': 'windows',
'sonar.scanner.arch': 'aarch64',
Expand Down
6 changes: 3 additions & 3 deletions test/unit/properties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from '../../src/constants';
import { LogLevel, log } from '../../src/logging';
import { getHostProperties, getProperties } from '../../src/properties';
import { ScannerProperty } from '../../src/types';
import { CacheStatus, ScannerProperty } from '../../src/types';
import { FakeProjectMock } from './mocks/FakeProjectMock';

jest.mock('../../src/logging');
Expand Down Expand Up @@ -615,7 +615,7 @@ describe('getProperties', () => {
'sonar.scanner.app': 'ignored',
'sonar.scanner.appVersion': 'ignored',
'sonar.scanner.bootstrapStartTime': '0000',
'sonar.scanner.wasJreCacheHit': 'true',
'sonar.scanner.wasJreCacheHit': CacheStatus.Hit,
'sonar.scanner.wasEngineCacheHit': 'true',
}),
});
Expand All @@ -627,7 +627,7 @@ describe('getProperties', () => {
'sonar.scanner.app': 'ignored',
'sonar.scanner.appVersion': 'ignored',
'sonar.scanner.bootstrapStartTime': '0000',
'sonar.scanner.wasJreCacheHit': 'true',
'sonar.scanner.wasJreCacheHit': CacheStatus.Hit,
'sonar.scanner.wasEngineCacheHit': 'true',
},
},
Expand Down

0 comments on commit 635750c

Please sign in to comment.