diff --git a/lib/manager/flux/artifacts.spec.ts b/lib/manager/flux/artifacts.spec.ts index b80f0bfd51d69643216855ea13c0dbad9571c9dd..16e81ce062910b2d231919c3bf74a0298a9bc31f 100644 --- a/lib/manager/flux/artifacts.spec.ts +++ b/lib/manager/flux/artifacts.spec.ts @@ -1,15 +1,22 @@ import { exec, mockExecAll } from '../../../test/exec-util'; +import { fs } from '../../../test/util'; +import { GlobalConfig } from '../../config/global'; import { updateArtifacts } from '.'; jest.mock('child_process'); +jest.mock('../../util/fs'); describe('manager/flux/artifacts', () => { - beforeEach(() => { - jest.resetAllMocks(); + beforeAll(() => { + GlobalConfig.set({ + localDir: '', + }); }); it('replaces existing value', async () => { - mockExecAll(exec, { stdout: 'test', stderr: '' }); + mockExecAll(exec, { stdout: '', stderr: '' }); + fs.readLocalFile.mockResolvedValueOnce('old'); + fs.readLocalFile.mockResolvedValueOnce('test'); const res = await updateArtifacts({ packageFileName: 'clusters/my-cluster/flux-system/gotk-components.yaml', @@ -40,6 +47,19 @@ describe('manager/flux/artifacts', () => { expect(res).toBeNull(); }); + it('ignores unchanged system manifests', async () => { + fs.readLocalFile.mockResolvedValueOnce('old'); + fs.readLocalFile.mockResolvedValueOnce('old'); + const res = await updateArtifacts({ + packageFileName: 'clusters/my-cluster/flux-system/gotk-components.yaml', + updatedDeps: [{ newVersion: '1.0.1' }], + newPackageFileContent: undefined, + config: {}, + }); + + expect(res).toBeNull(); + }); + it('ignores system manifests without a new version', async () => { const res = await updateArtifacts({ packageFileName: 'clusters/my-cluster/flux-system/gotk-components.yaml', @@ -51,7 +71,7 @@ describe('manager/flux/artifacts', () => { expect(res).toBeNull(); }); - it('failed to generate manifests', async () => { + it('failed to generate system manifest', async () => { mockExecAll(exec, new Error('failed')); const res = await updateArtifacts({ packageFileName: 'clusters/my-cluster/flux-system/gotk-components.yaml', @@ -60,8 +80,34 @@ describe('manager/flux/artifacts', () => { config: {}, }); - expect(res[0].artifactError.lockFile).toBe( - 'clusters/my-cluster/flux-system/gotk-components.yaml' - ); + expect(res).toStrictEqual([ + { + artifactError: { + lockFile: 'clusters/my-cluster/flux-system/gotk-components.yaml', + stderr: 'failed', + }, + }, + ]); + }); + + it('failed to read system manifest', async () => { + mockExecAll(exec, { stdout: '', stderr: 'Error' }); + fs.readLocalFile.mockResolvedValueOnce('old'); + fs.readLocalFile.mockResolvedValueOnce(''); + const res = await updateArtifacts({ + packageFileName: 'clusters/my-cluster/flux-system/gotk-components.yaml', + updatedDeps: [{ newVersion: '1.0.1' }], + newPackageFileContent: undefined, + config: {}, + }); + + expect(res).toStrictEqual([ + { + artifactError: { + lockFile: 'clusters/my-cluster/flux-system/gotk-components.yaml', + stderr: 'Error', + }, + }, + ]); }); }); diff --git a/lib/manager/flux/artifacts.ts b/lib/manager/flux/artifacts.ts index 1e2170878b544fd6fbb0ef2e943f988fa6fa4813..85f984c422e9ad0b1930ddc681caafda9b009207 100644 --- a/lib/manager/flux/artifacts.ts +++ b/lib/manager/flux/artifacts.ts @@ -1,6 +1,8 @@ +import { quote } from 'shlex'; import { logger } from '../../logger'; import { exec } from '../../util/exec'; import type { ExecOptions } from '../../util/exec/types'; +import { readLocalFile } from '../../util/fs'; import type { UpdateArtifact, UpdateArtifactsResult } from '../types'; import { isSystemManifest } from './common'; @@ -11,9 +13,10 @@ export async function updateArtifacts({ if (!isSystemManifest(packageFileName) || !updatedDeps[0]?.newVersion) { return null; } + const existingFileContent = await readLocalFile(packageFileName); try { logger.debug(`Updating Flux system manifests`); - const cmd = 'flux install --export'; + const cmd = `flux install --export > ${quote(packageFileName)}`; const execOptions: ExecOptions = { docker: { image: 'sidecar', @@ -27,12 +30,29 @@ export async function updateArtifacts({ }; const result = await exec(cmd, execOptions); + const newFileContent = await readLocalFile(packageFileName); + if (!newFileContent) { + logger.debug('Cannot read new flux file content'); + return [ + { + artifactError: { + lockFile: packageFileName, + stderr: result.stderr, + }, + }, + ]; + } + if (newFileContent === existingFileContent) { + logger.debug('Flux contents are unchanged'); + return null; + } + return [ { file: { type: 'addition', path: packageFileName, - contents: result.stdout, + contents: newFileContent, }, }, ];