|
| 1 | +/** |
| 2 | + * Copyright 2025 actions-toolkit authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import {describe, expect, it, jest, test, afterEach} from '@jest/globals'; |
| 18 | +import fs from 'fs'; |
| 19 | +import os from 'os'; |
| 20 | +import path from 'path'; |
| 21 | +import * as rimraf from 'rimraf'; |
| 22 | +import osm = require('os'); |
| 23 | + |
| 24 | +import {Install} from '../../src/cosign/install'; |
| 25 | + |
| 26 | +const tmpDir = fs.mkdtempSync(path.join(process.env.TEMP || os.tmpdir(), 'cosign-install-')); |
| 27 | + |
| 28 | +afterEach(function () { |
| 29 | + rimraf.sync(tmpDir); |
| 30 | +}); |
| 31 | + |
| 32 | +describe('download', () => { |
| 33 | + // prettier-ignore |
| 34 | + test.each([ |
| 35 | + ['v2.6.1'], |
| 36 | + ['v3.0.1'], |
| 37 | + ['latest'] |
| 38 | + ])( |
| 39 | + 'acquires %p of cosign', async (version) => { |
| 40 | + const install = new Install(); |
| 41 | + const toolPath = await install.download(version); |
| 42 | + expect(fs.existsSync(toolPath)).toBe(true); |
| 43 | + const cosignBin = await install.install(toolPath, tmpDir); |
| 44 | + expect(fs.existsSync(cosignBin)).toBe(true); |
| 45 | + }, 100000); |
| 46 | + |
| 47 | + // prettier-ignore |
| 48 | + test.each([ |
| 49 | + // following versions are already cached to htc from previous test cases |
| 50 | + ['v2.6.1'], |
| 51 | + ['v3.0.1'], |
| 52 | + ])( |
| 53 | + 'acquires %p of cosign with cache', async (version) => { |
| 54 | + const install = new Install(); |
| 55 | + const toolPath = await install.download(version); |
| 56 | + expect(fs.existsSync(toolPath)).toBe(true); |
| 57 | + }, 100000); |
| 58 | + |
| 59 | + // prettier-ignore |
| 60 | + test.each([ |
| 61 | + ['v2.5.3'], |
| 62 | + ['v2.6.0'], |
| 63 | + ])( |
| 64 | + 'acquires %p of cosign without cache', async (version) => { |
| 65 | + const install = new Install(); |
| 66 | + const toolPath = await install.download(version, true); |
| 67 | + expect(fs.existsSync(toolPath)).toBe(true); |
| 68 | + }, 100000); |
| 69 | + |
| 70 | + // TODO: add tests for arm |
| 71 | + // prettier-ignore |
| 72 | + test.each([ |
| 73 | + ['win32', 'x64'], |
| 74 | + ['darwin', 'x64'], |
| 75 | + ['darwin', 'arm64'], |
| 76 | + ['linux', 'x64'], |
| 77 | + ['linux', 'arm64'] |
| 78 | + ])( |
| 79 | + 'acquires undock for %s/%s', async (os, arch) => { |
| 80 | + jest.spyOn(osm, 'platform').mockImplementation(() => os as NodeJS.Platform); |
| 81 | + jest.spyOn(osm, 'arch').mockImplementation(() => arch); |
| 82 | + const install = new Install(); |
| 83 | + const cosignBin = await install.download('latest'); |
| 84 | + expect(fs.existsSync(cosignBin)).toBe(true); |
| 85 | + }, 100000); |
| 86 | +}); |
| 87 | + |
| 88 | +describe('getDownloadVersion', () => { |
| 89 | + it('returns latest download version', async () => { |
| 90 | + const version = await Install.getDownloadVersion('latest'); |
| 91 | + expect(version.version).toEqual('latest'); |
| 92 | + expect(version.downloadURL).toEqual('https://github.com/sigstore/cosign/releases/download/v%s/%s'); |
| 93 | + expect(version.contentOpts).toEqual({ |
| 94 | + owner: 'docker', |
| 95 | + repo: 'actions-toolkit', |
| 96 | + ref: 'main', |
| 97 | + path: '.github/cosign-releases.json' |
| 98 | + }); |
| 99 | + }); |
| 100 | + it('returns v3.0.2 download version', async () => { |
| 101 | + const version = await Install.getDownloadVersion('v3.0.2'); |
| 102 | + expect(version.version).toEqual('v3.0.2'); |
| 103 | + expect(version.downloadURL).toEqual('https://github.com/sigstore/cosign/releases/download/v%s/%s'); |
| 104 | + expect(version.contentOpts).toEqual({ |
| 105 | + owner: 'docker', |
| 106 | + repo: 'actions-toolkit', |
| 107 | + ref: 'main', |
| 108 | + path: '.github/cosign-releases.json' |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
| 112 | + |
| 113 | +describe('getRelease', () => { |
| 114 | + it('returns latest GitHub release', async () => { |
| 115 | + const version = await Install.getDownloadVersion('latest'); |
| 116 | + const release = await Install.getRelease(version); |
| 117 | + expect(release).not.toBeNull(); |
| 118 | + expect(release?.tag_name).not.toEqual(''); |
| 119 | + }); |
| 120 | + it('returns v3.0.2 GitHub release', async () => { |
| 121 | + const version = await Install.getDownloadVersion('v3.0.2'); |
| 122 | + const release = await Install.getRelease(version); |
| 123 | + expect(release).not.toBeNull(); |
| 124 | + expect(release?.id).toEqual(253720294); |
| 125 | + expect(release?.tag_name).toEqual('v3.0.2'); |
| 126 | + expect(release?.html_url).toEqual('https://github.com/sigstore/cosign/releases/tag/v3.0.2'); |
| 127 | + }); |
| 128 | + it('unknown release', async () => { |
| 129 | + const version = await Install.getDownloadVersion('foo'); |
| 130 | + await expect(Install.getRelease(version)).rejects.toThrow(new Error('Cannot find Cosign release foo in releases JSON')); |
| 131 | + }); |
| 132 | +}); |
0 commit comments