[Fleet] remove old assets for manual upgrade too (#115926)

* remove old assets for manual upgarde too

* fixed tests

* fixed tests
This commit is contained in:
Julia Bardi 2021-10-21 19:44:41 +02:00 committed by GitHub
parent e3aba08ea9
commit b6c3762556
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 8 deletions

View file

@ -17,7 +17,7 @@ import { removeOldAssets } from './cleanup';
jest.mock('../..', () => ({
appContextService: {
getLogger: () => ({
info: jest.fn(),
debug: jest.fn(),
}),
},
}));

View file

@ -57,7 +57,7 @@ async function removeAssetsFromVersion(
if (total > 0) {
appContextService
.getLogger()
.info(`Package "${pkgName}-${oldVersion}" still being used by policies`);
.debug(`Package "${pkgName}-${oldVersion}" still being used by policies`);
return;
}

View file

@ -128,6 +128,12 @@ jest.mock('./agent_policy', () => {
};
});
jest.mock('./epm/packages/cleanup', () => {
return {
removeOldAssets: jest.fn(),
};
});
const mockedFetchInfo = fetchInfo as jest.Mock<ReturnType<typeof fetchInfo>>;
type CombinedExternalCallback = PutPackagePolicyUpdateCallback | PostPackagePolicyCreateCallback;

View file

@ -424,7 +424,17 @@ class PackagePolicyService {
user: options?.user,
});
return (await this.get(soClient, id)) as PackagePolicy;
const newPolicy = (await this.get(soClient, id)) as PackagePolicy;
if (packagePolicy.package) {
await removeOldAssets({
soClient,
pkgName: packagePolicy.package.name,
currentVersion: packagePolicy.package.version,
});
}
return newPolicy;
}
public async delete(
@ -596,11 +606,6 @@ class PackagePolicyService {
name: packagePolicy.name,
success: true,
});
await removeOldAssets({
soClient,
pkgName: packageInfo.name,
currentVersion: packageInfo.version,
});
} catch (error) {
// We only want to specifically handle validation errors for the new package policy. If a more severe or
// general error is thrown elsewhere during the upgrade process, we want to surface that directly in