forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fleet] Add install_format_version to package and reinstall on setup (e…
- Loading branch information
Showing
17 changed files
with
435 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
194 changes: 194 additions & 0 deletions
194
x-pack/plugins/fleet/server/integration_tests/upgrade_package_install_version.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import Path from 'path'; | ||
|
||
import type { KibanaRequest, SavedObjectsClientContract } from '@kbn/core/server'; | ||
import { loggerMock } from '@kbn/logging-mocks'; | ||
|
||
import * as kbnTestServer from '@kbn/core/test_helpers/kbn_server'; | ||
|
||
import { upgradePackageInstallVersion } from '../services/setup/upgrade_package_install_version'; | ||
import { | ||
FLEET_INSTALL_FORMAT_VERSION, | ||
PACKAGES_SAVED_OBJECT_TYPE, | ||
SO_SEARCH_LIMIT, | ||
} from '../constants'; | ||
import type { Installation } from '../types'; | ||
|
||
import { useDockerRegistry, waitForFleetSetup } from './helpers'; | ||
|
||
const logFilePath = Path.join(__dirname, 'logs.log'); | ||
|
||
const fakeRequest = { | ||
headers: {}, | ||
getBasePath: () => '', | ||
path: '/', | ||
route: { settings: {} }, | ||
url: { | ||
href: '/', | ||
}, | ||
raw: { | ||
req: { | ||
url: '/', | ||
}, | ||
}, | ||
} as unknown as KibanaRequest; | ||
|
||
describe('Uprade package install version', () => { | ||
let esServer: kbnTestServer.TestElasticsearchUtils; | ||
let kbnServer: kbnTestServer.TestKibanaUtils; | ||
|
||
const registryUrl = useDockerRegistry(); | ||
|
||
const startServers = async () => { | ||
const { startES } = kbnTestServer.createTestServers({ | ||
adjustTimeout: (t) => jest.setTimeout(t), | ||
settings: { | ||
es: { | ||
license: 'trial', | ||
}, | ||
kbn: {}, | ||
}, | ||
}); | ||
|
||
esServer = await startES(); | ||
const startKibana = async () => { | ||
const root = kbnTestServer.createRootWithCorePlugins( | ||
{ | ||
xpack: { | ||
fleet: { | ||
registryUrl, | ||
packages: [ | ||
{ | ||
name: 'fleet_server', | ||
version: 'latest', | ||
}, | ||
{ | ||
name: 'system', | ||
version: 'latest', | ||
}, | ||
{ | ||
name: 'nginx', | ||
version: 'latest', | ||
}, | ||
{ | ||
name: 'apache', | ||
version: 'latest', | ||
}, | ||
], | ||
}, | ||
}, | ||
logging: { | ||
appenders: { | ||
file: { | ||
type: 'file', | ||
fileName: logFilePath, | ||
layout: { | ||
type: 'json', | ||
}, | ||
}, | ||
}, | ||
loggers: [ | ||
{ | ||
name: 'root', | ||
appenders: ['file'], | ||
}, | ||
{ | ||
name: 'plugins.fleet', | ||
level: 'all', | ||
}, | ||
], | ||
}, | ||
}, | ||
{ oss: false } | ||
); | ||
|
||
await root.preboot(); | ||
const coreSetup = await root.setup(); | ||
const coreStart = await root.start(); | ||
|
||
return { | ||
root, | ||
coreSetup, | ||
coreStart, | ||
stop: async () => await root.shutdown(), | ||
}; | ||
}; | ||
kbnServer = await startKibana(); | ||
|
||
await waitForFleetSetup(kbnServer.root); | ||
}; | ||
|
||
const stopServers = async () => { | ||
if (kbnServer) { | ||
await kbnServer.stop(); | ||
} | ||
|
||
if (esServer) { | ||
await esServer.stop(); | ||
} | ||
|
||
await new Promise((res) => setTimeout(res, 10000)); | ||
}; | ||
|
||
// Share the same servers for all the test to make test a lot faster (but test are not isolated anymore) | ||
beforeAll(async () => { | ||
await startServers(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await stopServers(); | ||
}); | ||
|
||
describe('with package installed with a previous format install version', () => { | ||
let soClient: SavedObjectsClientContract; | ||
|
||
const OUTDATED_PACKAGES = ['nginx', 'apache']; | ||
|
||
beforeAll(async () => { | ||
soClient = kbnServer.coreStart.savedObjects.getScopedClient(fakeRequest, { | ||
excludedWrappers: ['security'], | ||
}); | ||
|
||
const res = await soClient.find<Installation>({ | ||
type: PACKAGES_SAVED_OBJECT_TYPE, | ||
perPage: SO_SEARCH_LIMIT, | ||
}); | ||
|
||
for (const so of res.saved_objects) { | ||
if (OUTDATED_PACKAGES.includes(so.attributes.name)) { | ||
await soClient.update<Installation>(PACKAGES_SAVED_OBJECT_TYPE, so.id, { | ||
install_format_schema_version: '0.0.1', | ||
}); | ||
} | ||
} | ||
}); | ||
it('should upgrade package install version for outdated packages', async () => { | ||
const now = Date.now(); | ||
await upgradePackageInstallVersion({ | ||
soClient, | ||
esClient: kbnServer.coreStart.elasticsearch.client.asInternalUser, | ||
logger: loggerMock.create(), | ||
}); | ||
|
||
const res = await soClient.find<Installation>({ | ||
type: PACKAGES_SAVED_OBJECT_TYPE, | ||
perPage: SO_SEARCH_LIMIT, | ||
}); | ||
expect(res.saved_objects).toHaveLength(4); | ||
res.saved_objects.forEach((so) => { | ||
expect(so.attributes.install_format_schema_version).toBe(FLEET_INSTALL_FORMAT_VERSION); | ||
if (!OUTDATED_PACKAGES.includes(so.attributes.name)) { | ||
expect(new Date(so.updated_at as string).getTime()).toBeLessThan(now); | ||
} else { | ||
expect(new Date(so.updated_at as string).getTime()).toBeGreaterThan(now); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
x-pack/plugins/fleet/server/services/epm/packages/reinstall.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { savedObjectsClientMock, elasticsearchServiceMock } from '@kbn/core/server/mocks'; | ||
|
||
import type { Installation } from '../../../../common'; | ||
|
||
import { reinstallPackageForInstallation } from './reinstall'; | ||
import { installPackage } from './install'; | ||
|
||
jest.mock('./install'); | ||
|
||
const mockedInstallPackage = installPackage as jest.MockedFunction<typeof installPackage>; | ||
|
||
describe('reinstallPackageForInstallation', () => { | ||
beforeEach(() => { | ||
mockedInstallPackage.mockReset(); | ||
}); | ||
it('should throw an error for uploaded package', async () => { | ||
const soClient = savedObjectsClientMock.create(); | ||
const esClient = elasticsearchServiceMock.createInternalClient(); | ||
await expect( | ||
reinstallPackageForInstallation({ | ||
soClient, | ||
esClient, | ||
installation: { | ||
install_source: 'upload', | ||
} as Installation, | ||
}) | ||
).rejects.toThrow(/Cannot reinstall an uploaded package/); | ||
}); | ||
|
||
it('should install registry package', async () => { | ||
const soClient = savedObjectsClientMock.create(); | ||
const esClient = elasticsearchServiceMock.createInternalClient(); | ||
await expect( | ||
reinstallPackageForInstallation({ | ||
soClient, | ||
esClient, | ||
installation: { | ||
install_source: 'registry', | ||
name: 'test', | ||
version: '1.0.0', | ||
} as Installation, | ||
}) | ||
); | ||
|
||
expect(mockedInstallPackage).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
installSource: 'registry', | ||
pkgkey: 'test-1.0.0', | ||
force: true, | ||
}) | ||
); | ||
}); | ||
|
||
it('should install bundled package', async () => { | ||
const soClient = savedObjectsClientMock.create(); | ||
const esClient = elasticsearchServiceMock.createInternalClient(); | ||
await expect( | ||
reinstallPackageForInstallation({ | ||
soClient, | ||
esClient, | ||
installation: { | ||
install_source: 'bundled', | ||
name: 'test', | ||
version: '1.0.0', | ||
} as Installation, | ||
}) | ||
); | ||
|
||
expect(mockedInstallPackage).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
installSource: 'registry', | ||
pkgkey: 'test-1.0.0', | ||
force: true, | ||
}) | ||
); | ||
}); | ||
}); |
Oops, something went wrong.