|
| 1 | +import * as core from '@actions/core'; |
| 2 | +import {getArgs, Inputs} from '../context'; |
| 3 | +import {Toolkit} from '@docker/actions-toolkit/lib/toolkit'; |
| 4 | + |
| 5 | +jest.mock('@actions/core'); |
| 6 | + |
| 7 | +// Mock the Toolkit. |
| 8 | +jest.mock('@docker/actions-toolkit/lib/toolkit'); |
| 9 | + |
| 10 | +describe('eStargz compression', () => { |
| 11 | + let mockToolkit: jest.Mocked<Toolkit>; |
| 12 | + let baseInputs: Inputs; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + jest.clearAllMocks(); |
| 16 | + |
| 17 | + // Create a mock toolkit with all necessary methods. |
| 18 | + mockToolkit = { |
| 19 | + buildx: { |
| 20 | + versionSatisfies: jest.fn(), |
| 21 | + getCommand: jest.fn(), |
| 22 | + printVersion: jest.fn(), |
| 23 | + isAvailable: jest.fn() |
| 24 | + }, |
| 25 | + buildxBuild: { |
| 26 | + getImageIDFilePath: jest.fn().mockReturnValue('/tmp/iidfile'), |
| 27 | + getMetadataFilePath: jest.fn().mockReturnValue('/tmp/metadata'), |
| 28 | + resolveImageID: jest.fn(), |
| 29 | + resolveMetadata: jest.fn(), |
| 30 | + resolveDigest: jest.fn(), |
| 31 | + resolveWarnings: jest.fn(), |
| 32 | + resolveRef: jest.fn() |
| 33 | + }, |
| 34 | + builder: { |
| 35 | + inspect: jest.fn().mockResolvedValue({ |
| 36 | + name: 'default', |
| 37 | + driver: 'docker-container', |
| 38 | + nodes: [] |
| 39 | + }) |
| 40 | + }, |
| 41 | + buildkit: { |
| 42 | + versionSatisfies: jest.fn().mockResolvedValue(false) |
| 43 | + } |
| 44 | + } as any; |
| 45 | + |
| 46 | + // Base inputs for testing. |
| 47 | + baseInputs = { |
| 48 | + 'add-hosts': [], |
| 49 | + allow: [], |
| 50 | + annotations: [], |
| 51 | + attests: [], |
| 52 | + 'build-args': [], |
| 53 | + 'build-contexts': [], |
| 54 | + builder: '', |
| 55 | + 'cache-from': [], |
| 56 | + 'cache-to': [], |
| 57 | + 'cgroup-parent': '', |
| 58 | + context: '.', |
| 59 | + file: '', |
| 60 | + labels: [], |
| 61 | + load: false, |
| 62 | + network: '', |
| 63 | + 'no-cache': false, |
| 64 | + 'no-cache-filters': [], |
| 65 | + outputs: [], |
| 66 | + platforms: [], |
| 67 | + provenance: '', |
| 68 | + pull: false, |
| 69 | + push: false, |
| 70 | + sbom: '', |
| 71 | + secrets: [], |
| 72 | + 'secret-envs': [], |
| 73 | + 'secret-files': [], |
| 74 | + 'shm-size': '', |
| 75 | + ssh: [], |
| 76 | + tags: ['user/app:latest'], |
| 77 | + target: '', |
| 78 | + ulimit: [], |
| 79 | + 'github-token': '', |
| 80 | + estargz: false |
| 81 | + }; |
| 82 | + }); |
| 83 | + |
| 84 | + test('should not add estargz parameters when estargz is false', async () => { |
| 85 | + (mockToolkit.buildx.versionSatisfies as jest.Mock).mockResolvedValue(true); |
| 86 | + |
| 87 | + const inputs = {...baseInputs, push: true, estargz: false}; |
| 88 | + const args = await getArgs(inputs, mockToolkit); |
| 89 | + |
| 90 | + expect(args.join(' ')).not.toContain('compression=estargz'); |
| 91 | + }); |
| 92 | + |
| 93 | + test('should not add estargz parameters when push is false', async () => { |
| 94 | + (mockToolkit.buildx.versionSatisfies as jest.Mock).mockResolvedValue(true); |
| 95 | + |
| 96 | + const inputs = {...baseInputs, push: false, estargz: true}; |
| 97 | + const args = await getArgs(inputs, mockToolkit); |
| 98 | + |
| 99 | + expect(args.join(' ')).not.toContain('compression=estargz'); |
| 100 | + expect(core.warning).toHaveBeenCalledWith("eStargz compression requires push: true; the input 'estargz' is ignored."); |
| 101 | + }); |
| 102 | + |
| 103 | + test('should not add estargz parameters when buildx version is < 0.10.0', async () => { |
| 104 | + (mockToolkit.buildx.versionSatisfies as jest.Mock).mockImplementation(async (version: string) => { |
| 105 | + return version === '>=0.6.0'; // Only 0.6.0 check passes, not 0.10.0. |
| 106 | + }); |
| 107 | + |
| 108 | + const inputs = {...baseInputs, push: true, estargz: true}; |
| 109 | + const args = await getArgs(inputs, mockToolkit); |
| 110 | + |
| 111 | + expect(args.join(' ')).not.toContain('compression=estargz'); |
| 112 | + expect(core.warning).toHaveBeenCalledWith("eStargz compression requires buildx >= 0.10.0; the input 'estargz' is ignored."); |
| 113 | + }); |
| 114 | + |
| 115 | + test('should add estargz output when estargz is true, push is true, and buildx >= 0.10.0', async () => { |
| 116 | + (mockToolkit.buildx.versionSatisfies as jest.Mock).mockResolvedValue(true); |
| 117 | + |
| 118 | + const inputs = {...baseInputs, push: true, estargz: true}; |
| 119 | + const args = await getArgs(inputs, mockToolkit); |
| 120 | + |
| 121 | + expect(args).toContain('--output'); |
| 122 | + const outputIndex = args.indexOf('--output'); |
| 123 | + expect(args[outputIndex + 1]).toBe('type=registry,compression=estargz,force-compression=true,oci-mediatypes=true'); |
| 124 | + }); |
| 125 | + |
| 126 | + test('should modify existing registry output with estargz parameters', async () => { |
| 127 | + (mockToolkit.buildx.versionSatisfies as jest.Mock).mockResolvedValue(true); |
| 128 | + |
| 129 | + const inputs = { |
| 130 | + ...baseInputs, |
| 131 | + push: true, |
| 132 | + estargz: true, |
| 133 | + outputs: ['type=registry,dest=output.txt'] |
| 134 | + }; |
| 135 | + const args = await getArgs(inputs, mockToolkit); |
| 136 | + |
| 137 | + expect(args).toContain('--output'); |
| 138 | + const outputIndex = args.indexOf('--output'); |
| 139 | + expect(args[outputIndex + 1]).toBe('type=registry,dest=output.txt,compression=estargz,force-compression=true,oci-mediatypes=true'); |
| 140 | + }); |
| 141 | + |
| 142 | + test('should not modify non-registry outputs with estargz parameters', async () => { |
| 143 | + (mockToolkit.buildx.versionSatisfies as jest.Mock).mockResolvedValue(true); |
| 144 | + |
| 145 | + const inputs = { |
| 146 | + ...baseInputs, |
| 147 | + push: true, |
| 148 | + estargz: true, |
| 149 | + outputs: ['type=docker'] |
| 150 | + }; |
| 151 | + const args = await getArgs(inputs, mockToolkit); |
| 152 | + |
| 153 | + expect(args).toContain('--output'); |
| 154 | + const outputIndex = args.indexOf('--output'); |
| 155 | + expect(args[outputIndex + 1]).toBe('type=docker'); |
| 156 | + }); |
| 157 | + |
| 158 | + test('should handle multiple outputs correctly', async () => { |
| 159 | + (mockToolkit.buildx.versionSatisfies as jest.Mock).mockResolvedValue(true); |
| 160 | + |
| 161 | + const inputs = { |
| 162 | + ...baseInputs, |
| 163 | + push: true, |
| 164 | + estargz: true, |
| 165 | + outputs: ['type=registry', 'type=docker'] |
| 166 | + }; |
| 167 | + const args = await getArgs(inputs, mockToolkit); |
| 168 | + |
| 169 | + const argsStr = args.join(' '); |
| 170 | + expect(argsStr).toContain('type=registry,compression=estargz,force-compression=true,oci-mediatypes=true'); |
| 171 | + expect(argsStr).toContain('type=docker'); |
| 172 | + }); |
| 173 | + |
| 174 | + test('should work with existing registry output without additional params', async () => { |
| 175 | + (mockToolkit.buildx.versionSatisfies as jest.Mock).mockResolvedValue(true); |
| 176 | + |
| 177 | + const inputs = { |
| 178 | + ...baseInputs, |
| 179 | + push: true, |
| 180 | + estargz: true, |
| 181 | + outputs: ['type=registry'] |
| 182 | + }; |
| 183 | + const args = await getArgs(inputs, mockToolkit); |
| 184 | + |
| 185 | + expect(args).toContain('--output'); |
| 186 | + const outputIndex = args.indexOf('--output'); |
| 187 | + expect(args[outputIndex + 1]).toBe('type=registry,compression=estargz,force-compression=true,oci-mediatypes=true'); |
| 188 | + }); |
| 189 | +}); |
0 commit comments