mirror of
https://github.com/actions/setup-java.git
synced 2026-08-05 17:02:57 +00:00
f4bfb3ddea
* Fix floating Oracle JDK version resolution Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Update generated distribution bundles Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Harden floating artifact cache identity Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Regenerate setup bundle after cache hardening Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Temporarily enable hosted full validation Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Export hosted formatting results Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Apply repository formatting Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Run hosted validation after formatting Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Correct floating version regression tests Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Remove temporary validation wiring Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Cache checksum-less floating artifacts by their response fingerprint Oracle and Oracle GraalVM do not always publish a `.sha256` sibling next to a `/latest/` artifact. Those floating releases were excluded from both the resolution cache and the JDK cache, so `cache-jdk` users lost caching entirely for them. A floating URL is a constant string, so it cannot serve as a cache identity on its own — a stale entry would be reused forever. Instead, derive a validator from the headers of the HEAD request that already resolves the artifact: the ETag when present, otherwise `Last-Modified` combined with `Content-Length`. Republishing changes the validator, which changes the cache key, so a new build is downloaded rather than masked. `getJdkReleaseIdentity` now falls back to that fingerprint before the URL, and the floating cache gates ask whether the release has a stable identity (checksum or fingerprint) rather than a checksum specifically. A floating release with neither is still left uncached. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Co-authored-by: Bruno Borges <brborges@microsoft.com>
300 lines
9.0 KiB
TypeScript
300 lines
9.0 KiB
TypeScript
import {
|
|
jest,
|
|
describe,
|
|
it,
|
|
expect,
|
|
beforeEach,
|
|
afterEach,
|
|
beforeAll,
|
|
afterAll
|
|
} from '@jest/globals';
|
|
import os from 'os';
|
|
import {HttpClient} from '@actions/http-client';
|
|
|
|
// Mock @actions/core before importing source modules that depend on it
|
|
jest.unstable_mockModule('@actions/core', () => ({
|
|
info: jest.fn(),
|
|
warning: jest.fn(),
|
|
debug: jest.fn(),
|
|
error: jest.fn(),
|
|
notice: jest.fn(),
|
|
setFailed: jest.fn(),
|
|
setOutput: jest.fn(),
|
|
getInput: jest.fn(),
|
|
getBooleanInput: jest.fn(),
|
|
getMultilineInput: jest.fn(),
|
|
addPath: jest.fn(),
|
|
exportVariable: jest.fn(),
|
|
saveState: jest.fn(),
|
|
getState: jest.fn(),
|
|
setSecret: jest.fn(),
|
|
isDebug: jest.fn(() => false),
|
|
startGroup: jest.fn(),
|
|
endGroup: jest.fn(),
|
|
group: jest.fn((_name: string, fn: () => Promise<unknown>) => fn()),
|
|
toPlatformPath: jest.fn((p: string) => p),
|
|
toWin32Path: jest.fn((p: string) => p),
|
|
toPosixPath: jest.fn((p: string) => p)
|
|
}));
|
|
|
|
// Dynamic imports after mocking
|
|
const core = await import('@actions/core');
|
|
const {OracleDistribution} =
|
|
await import('../../src/distributions/oracle/installer.js');
|
|
const {getDownloadArchiveExtension} = await import('../../src/util.js');
|
|
|
|
describe('findPackageForDownload', () => {
|
|
let distribution: InstanceType<typeof OracleDistribution>;
|
|
let spyDebug: any;
|
|
let spyHttpClient: any;
|
|
let spyHttpClientGet: any;
|
|
let spyCoreError: any;
|
|
|
|
const ORACLE_CHECKSUM = 'f'.repeat(64);
|
|
|
|
beforeEach(() => {
|
|
distribution = new OracleDistribution({
|
|
version: '',
|
|
architecture: 'x64',
|
|
packageType: 'jdk',
|
|
checkLatest: false
|
|
});
|
|
|
|
spyDebug = core.debug as jest.Mock;
|
|
spyDebug.mockImplementation(() => {});
|
|
|
|
// Mock core.error to suppress error logs
|
|
spyCoreError = core.error as jest.Mock;
|
|
spyCoreError.mockImplementation(() => {});
|
|
|
|
// Every resolved release fetches its `${url}.sha256` sibling checksum;
|
|
// stub it so tests never reach the real network.
|
|
spyHttpClientGet = jest.spyOn(HttpClient.prototype, 'get');
|
|
spyHttpClientGet.mockResolvedValue({
|
|
message: {statusCode: 200},
|
|
readBody: async () => ORACLE_CHECKSUM
|
|
});
|
|
});
|
|
|
|
it.each([
|
|
[
|
|
'21',
|
|
'21',
|
|
'https://download.oracle.com/java/21/latest/jdk-21_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}'
|
|
],
|
|
[
|
|
'20',
|
|
'20',
|
|
'https://download.oracle.com/java/20/latest/jdk-20_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}'
|
|
],
|
|
[
|
|
'18',
|
|
'18',
|
|
'https://download.oracle.com/java/18/archive/jdk-18_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}'
|
|
],
|
|
[
|
|
'20.0.1',
|
|
'20.0.1',
|
|
'https://download.oracle.com/java/20/archive/jdk-20.0.1_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}'
|
|
],
|
|
[
|
|
'17',
|
|
'17',
|
|
'https://download.oracle.com/java/17/latest/jdk-17_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}'
|
|
],
|
|
[
|
|
'17.0.1',
|
|
'17.0.1',
|
|
'https://download.oracle.com/java/17/archive/jdk-17.0.1_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}'
|
|
]
|
|
])('version is %s -> %s', async (input, expectedVersion, expectedUrl) => {
|
|
/* Needed only for this particular test because some urls might change */
|
|
spyHttpClient = jest.spyOn(HttpClient.prototype, 'head');
|
|
spyHttpClient.mockReturnValue(
|
|
Promise.resolve({
|
|
message: {
|
|
statusCode: 200
|
|
}
|
|
})
|
|
);
|
|
|
|
/**
|
|
* NOTE - Should fail to retrieve 18 from latest and check archive instead
|
|
*/
|
|
if (input === '18') {
|
|
spyHttpClient.mockReturnValueOnce(
|
|
Promise.resolve({
|
|
message: {
|
|
statusCode: 404
|
|
}
|
|
})
|
|
);
|
|
}
|
|
|
|
const result = await distribution['findPackageForDownload'](input);
|
|
|
|
jest.restoreAllMocks();
|
|
|
|
expect(result.version).toBe(expectedVersion);
|
|
const osType = distribution.getPlatform();
|
|
const archiveType = getDownloadArchiveExtension();
|
|
const url = expectedUrl
|
|
.replace('{{OS_TYPE}}', osType)
|
|
.replace('{{ARCHIVE_TYPE}}', archiveType);
|
|
expect(result.url).toBe(url);
|
|
// Only the `/latest/` path serves changing contents, so only it must be
|
|
// excluded from the resolution cache.
|
|
expect(result.floating).toBe(url.includes('/latest/'));
|
|
});
|
|
|
|
it.each([
|
|
['21', 'etag:"oracle-latest"'],
|
|
['21.0.1', undefined]
|
|
])(
|
|
'fingerprints only the floating artifact for version %s',
|
|
async (input, expected) => {
|
|
spyHttpClient = jest.spyOn(HttpClient.prototype, 'head');
|
|
spyHttpClient.mockResolvedValue({
|
|
message: {statusCode: 200, headers: {etag: '"oracle-latest"'}}
|
|
});
|
|
|
|
const result = await distribution['findPackageForDownload'](input);
|
|
|
|
jest.restoreAllMocks();
|
|
|
|
// Without a fingerprint the constant `/latest/` URL would key a cache
|
|
// entry that never invalidates when Oracle republishes the artifact.
|
|
expect(result.fingerprint).toBe(expected);
|
|
}
|
|
);
|
|
|
|
it('fetches the authoritative sha256 checksum for the resolved archive', async () => {
|
|
spyHttpClient = jest.spyOn(HttpClient.prototype, 'head');
|
|
spyHttpClient.mockResolvedValue({message: {statusCode: 200}});
|
|
|
|
const result = await distribution['findPackageForDownload']('21');
|
|
|
|
jest.restoreAllMocks();
|
|
|
|
expect(result.checksum).toEqual({
|
|
algorithm: 'sha256',
|
|
value: ORACLE_CHECKSUM,
|
|
source: `${result.url}.sha256`
|
|
});
|
|
expect(spyHttpClientGet).toHaveBeenCalledWith(`${result.url}.sha256`);
|
|
expect(spyHttpClientGet).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('always resolves major-only requests remotely', () => {
|
|
expect(distribution['requiresRemoteResolution']()).toBe(true);
|
|
const exactDistribution = new OracleDistribution({
|
|
version: '21.0.8',
|
|
architecture: 'x64',
|
|
packageType: 'jdk',
|
|
checkLatest: false
|
|
});
|
|
expect(exactDistribution['requiresRemoteResolution']()).toBe(false);
|
|
});
|
|
|
|
it.each([
|
|
['amd64', 'x64'],
|
|
['arm64', 'aarch64']
|
|
])(
|
|
'defaults to os.arch(): %s mapped to distro arch: %s',
|
|
async (osArch: string, distroArch: string) => {
|
|
jest
|
|
.spyOn(os, 'arch')
|
|
.mockReturnValue(osArch as ReturnType<typeof os.arch>);
|
|
jest.spyOn(os, 'platform').mockReturnValue('linux');
|
|
|
|
const version = '18';
|
|
const distro = new OracleDistribution({
|
|
version,
|
|
architecture: '', // to get default value
|
|
packageType: 'jdk',
|
|
checkLatest: false
|
|
});
|
|
|
|
const osType = distribution.getPlatform();
|
|
if (osType === 'windows' && distroArch == 'aarch64') {
|
|
return; // skip, aarch64 is not available for Windows
|
|
}
|
|
const archiveType = getDownloadArchiveExtension();
|
|
const result = await distro['findPackageForDownload'](version);
|
|
const expectedUrl = `https://download.oracle.com/java/18/archive/jdk-18_${osType}-${distroArch}_bin.${archiveType}`;
|
|
|
|
expect(result.url).toBe(expectedUrl);
|
|
},
|
|
10000
|
|
);
|
|
|
|
it('should throw an error', async () => {
|
|
await expect(distribution['findPackageForDownload']('8')).rejects.toThrow(
|
|
/Oracle JDK is only supported for JDK 17 and later/
|
|
);
|
|
await expect(distribution['findPackageForDownload']('11')).rejects.toThrow(
|
|
/Oracle JDK is only supported for JDK 17 and later/
|
|
);
|
|
});
|
|
});
|
|
describe('findPackageForDownload with latest', () => {
|
|
let spyHttpClientHead: any;
|
|
let spyHttpClientGetJson: any;
|
|
|
|
beforeEach(() => {
|
|
(core.debug as jest.Mock).mockImplementation(() => {});
|
|
(core.error as jest.Mock).mockImplementation(() => {});
|
|
spyHttpClientGetJson = jest.spyOn(HttpClient.prototype, 'getJson');
|
|
spyHttpClientGetJson.mockResolvedValue({
|
|
statusCode: 200,
|
|
result: {most_recent_feature_release: 25},
|
|
headers: {}
|
|
});
|
|
});
|
|
|
|
afterEach(() => {
|
|
jest.restoreAllMocks();
|
|
});
|
|
|
|
it('resolves the newest major version from the Adoptium API', async () => {
|
|
spyHttpClientHead = jest.spyOn(HttpClient.prototype, 'head');
|
|
spyHttpClientHead.mockResolvedValue({message: {statusCode: 200}});
|
|
jest.spyOn(HttpClient.prototype, 'get').mockResolvedValue({
|
|
message: {statusCode: 200},
|
|
readBody: async () => 'f'.repeat(64)
|
|
} as any);
|
|
|
|
const distribution = new OracleDistribution({
|
|
version: 'latest',
|
|
architecture: 'x64',
|
|
packageType: 'jdk',
|
|
checkLatest: false
|
|
});
|
|
|
|
const result = await distribution['findPackageForDownload']('x');
|
|
const osType = distribution.getPlatform();
|
|
const archiveType = getDownloadArchiveExtension();
|
|
|
|
expect(result.version).toBe('25');
|
|
expect(result.url).toBe(
|
|
`https://download.oracle.com/java/25/latest/jdk-25_${osType}-x64_bin.${archiveType}`
|
|
);
|
|
});
|
|
|
|
it('throws an actionable error when the latest major is not yet available', async () => {
|
|
spyHttpClientHead = jest.spyOn(HttpClient.prototype, 'head');
|
|
spyHttpClientHead.mockResolvedValue({message: {statusCode: 404}});
|
|
|
|
const distribution = new OracleDistribution({
|
|
version: 'latest',
|
|
architecture: 'x64',
|
|
packageType: 'jdk',
|
|
checkLatest: false
|
|
});
|
|
|
|
await expect(distribution['findPackageForDownload']('x')).rejects.toThrow(
|
|
/is not yet available for the Oracle JDK distribution/
|
|
);
|
|
});
|
|
});
|