|
| 1 | +import {afterEach, describe, expect, it, jest} from '@jest/globals'; |
| 2 | +import {HttpClient} from '@actions/http-client'; |
| 3 | +import packageDetails from '../data/redhat-package-details.json' with {type: 'json'}; |
| 4 | +import packages from '../data/redhat-packages.json' with {type: 'json'}; |
| 5 | +import {RedHatDistribution} from '../../src/distributions/redhat/installer.js'; |
| 6 | + |
| 7 | +const createDistribution = ( |
| 8 | + version: string, |
| 9 | + packageType = 'jdk' |
| 10 | +): RedHatDistribution => { |
| 11 | + const distribution = new RedHatDistribution({ |
| 12 | + version, |
| 13 | + architecture: 'x64', |
| 14 | + packageType, |
| 15 | + checkLatest: false |
| 16 | + }); |
| 17 | + distribution['getOperatingSystem'] = () => 'linux'; |
| 18 | + distribution['getArchiveType'] = () => 'tar.xz'; |
| 19 | + return distribution; |
| 20 | +}; |
| 21 | + |
| 22 | +const mockDisco = ( |
| 23 | + packageResponse: unknown = packages, |
| 24 | + detailsResponse: unknown = packageDetails |
| 25 | +) => |
| 26 | + jest.spyOn(HttpClient.prototype, 'getJson').mockImplementation(async url => ({ |
| 27 | + result: url.includes('/packages?') ? packageResponse : detailsResponse, |
| 28 | + statusCode: 200, |
| 29 | + headers: {} |
| 30 | + })); |
| 31 | + |
| 32 | +afterEach(() => { |
| 33 | + jest.restoreAllMocks(); |
| 34 | +}); |
| 35 | + |
| 36 | +describe('Red Hat package resolution', () => { |
| 37 | + it('resolves the newest matching release and authoritative checksum', async () => { |
| 38 | + const getJson = mockDisco(); |
| 39 | + const distribution = createDistribution('21'); |
| 40 | + |
| 41 | + const release = await distribution['findPackageForDownload']('21'); |
| 42 | + |
| 43 | + expect(release).toEqual({ |
| 44 | + version: '21.0.8+9', |
| 45 | + url: packageDetails.result[0].direct_download_uri, |
| 46 | + checksum: { |
| 47 | + algorithm: 'sha256', |
| 48 | + value: packageDetails.result[0].checksum, |
| 49 | + source: |
| 50 | + 'https://api.foojay.io/disco/v3.0/ids/redhat-21.0.8-jdk-linux-x64' |
| 51 | + } |
| 52 | + }); |
| 53 | + expect(getJson).toHaveBeenNthCalledWith( |
| 54 | + 1, |
| 55 | + expect.stringContaining( |
| 56 | + 'distro=redhat&release_status=ga&operating_system=linux&architecture=x64&package_type=jdk&archive_type=tar.xz&directly_downloadable=true' |
| 57 | + ) |
| 58 | + ); |
| 59 | + expect(getJson).toHaveBeenNthCalledWith( |
| 60 | + 2, |
| 61 | + 'https://api.foojay.io/disco/v3.0/ids/redhat-21.0.8-jdk-linux-x64' |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it('normalizes feature-only Disco versions before matching', async () => { |
| 66 | + mockDisco(undefined, { |
| 67 | + result: [ |
| 68 | + { |
| 69 | + ...packageDetails.result[0], |
| 70 | + direct_download_uri: 'https://developers.redhat.com/openjdk-9.tar.xz' |
| 71 | + } |
| 72 | + ], |
| 73 | + message: '' |
| 74 | + }); |
| 75 | + const distribution = createDistribution('9'); |
| 76 | + |
| 77 | + const release = await distribution['findPackageForDownload']('9'); |
| 78 | + |
| 79 | + expect(release.version).toBe('9.0.0+181'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('selects the requested package type', async () => { |
| 83 | + const getJson = mockDisco(); |
| 84 | + const distribution = createDistribution('21', 'jre'); |
| 85 | + |
| 86 | + await distribution['findPackageForDownload']('21'); |
| 87 | + |
| 88 | + expect(getJson).toHaveBeenNthCalledWith( |
| 89 | + 1, |
| 90 | + expect.stringContaining('package_type=jre') |
| 91 | + ); |
| 92 | + expect(getJson).toHaveBeenNthCalledWith( |
| 93 | + 2, |
| 94 | + 'https://api.foojay.io/disco/v3.0/ids/redhat-21.0.8-jre-linux-x64' |
| 95 | + ); |
| 96 | + }); |
| 97 | + |
| 98 | + it('maps Windows to ZIP packages', async () => { |
| 99 | + const getJson = mockDisco(); |
| 100 | + const distribution = createDistribution('17'); |
| 101 | + distribution['getOperatingSystem'] = () => 'windows'; |
| 102 | + distribution['getArchiveType'] = () => 'zip'; |
| 103 | + |
| 104 | + await distribution['findPackageForDownload']('17'); |
| 105 | + |
| 106 | + expect(getJson).toHaveBeenNthCalledWith( |
| 107 | + 1, |
| 108 | + expect.stringContaining( |
| 109 | + 'operating_system=windows&architecture=x64&package_type=jdk&archive_type=zip' |
| 110 | + ) |
| 111 | + ); |
| 112 | + expect(getJson).toHaveBeenNthCalledWith( |
| 113 | + 2, |
| 114 | + 'https://api.foojay.io/disco/v3.0/ids/redhat-17.0.16-jdk-windows-x64' |
| 115 | + ); |
| 116 | + }); |
| 117 | + |
| 118 | + it('rejects Alpine before requesting glibc package metadata', async () => { |
| 119 | + const getJson = mockDisco(); |
| 120 | + const distribution = new RedHatDistribution({ |
| 121 | + version: '21', |
| 122 | + architecture: 'x64', |
| 123 | + packageType: 'jdk', |
| 124 | + checkLatest: false |
| 125 | + }); |
| 126 | + |
| 127 | + expect(() => distribution['getOperatingSystem'](true)).toThrow( |
| 128 | + "Distribution 'redhat' does not support Alpine Linux because Red Hat portable archives require glibc." |
| 129 | + ); |
| 130 | + expect(getJson).not.toHaveBeenCalled(); |
| 131 | + }); |
| 132 | + |
| 133 | + it('rejects early-access versions before requesting metadata', async () => { |
| 134 | + const getJson = mockDisco(); |
| 135 | + const distribution = createDistribution('21-ea'); |
| 136 | + |
| 137 | + await expect(distribution['findPackageForDownload']('21')).rejects.toThrow( |
| 138 | + 'Early access versions are not supported' |
| 139 | + ); |
| 140 | + expect(getJson).not.toHaveBeenCalled(); |
| 141 | + }); |
| 142 | + |
| 143 | + it('reports available versions when no release matches', async () => { |
| 144 | + mockDisco(); |
| 145 | + const distribution = createDistribution('25'); |
| 146 | + |
| 147 | + await expect(distribution['findPackageForDownload']('25')).rejects.toThrow( |
| 148 | + "No matching version found for SemVer '25'.\nDistribution: RedHat" |
| 149 | + ); |
| 150 | + }); |
| 151 | + |
| 152 | + it('fails when the package detail has no direct Red Hat URL', async () => { |
| 153 | + mockDisco(packages, {result: [], message: ''}); |
| 154 | + const distribution = createDistribution('21'); |
| 155 | + |
| 156 | + await expect(distribution['findPackageForDownload']('21')).rejects.toThrow( |
| 157 | + 'returned no direct Red Hat download URL' |
| 158 | + ); |
| 159 | + }); |
| 160 | + |
| 161 | + it('fails when the package detail has no SHA-256 checksum', async () => { |
| 162 | + mockDisco(packages, { |
| 163 | + result: [{...packageDetails.result[0], checksum: ''}], |
| 164 | + message: '' |
| 165 | + }); |
| 166 | + const distribution = createDistribution('21'); |
| 167 | + |
| 168 | + await expect(distribution['findPackageForDownload']('21')).rejects.toThrow( |
| 169 | + 'returned no SHA-256 checksum' |
| 170 | + ); |
| 171 | + }); |
| 172 | + |
| 173 | + it('fails with a targeted error when package metadata is missing', async () => { |
| 174 | + mockDisco(null); |
| 175 | + const distribution = createDistribution('21'); |
| 176 | + |
| 177 | + await expect(distribution['findPackageForDownload']('21')).rejects.toThrow( |
| 178 | + 'Could not fetch Red Hat package metadata from Foojay Disco' |
| 179 | + ); |
| 180 | + }); |
| 181 | +}); |
0 commit comments