-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Fleet] add DELETED file update task #144494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| /* | ||
| * 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 type { ElasticsearchClientMock } from '@kbn/core/server/mocks'; | ||
| import { elasticsearchServiceMock } from '@kbn/core/server/mocks'; | ||
|
|
||
| import { ES_SEARCH_LIMIT } from '../../../common/constants'; | ||
| import { | ||
| FILE_STORAGE_DATA_INDEX, | ||
| FILE_STORAGE_METADATA_INDEX, | ||
| } from '../../constants/fleet_es_assets'; | ||
|
|
||
| import { fileIdsWithoutChunksByIndex, getFilesByStatus, updateFilesStatus } from '.'; | ||
|
|
||
| const ENDPOINT_FILE_METADATA_INDEX = '.fleet-endpoint-files'; | ||
| const ENDPOINT_FILE_INDEX = '.fleet-endpoint-file-data'; | ||
|
|
||
| describe('files service', () => { | ||
| let esClientMock: ElasticsearchClientMock; | ||
| const abortController = new AbortController(); | ||
|
|
||
| beforeEach(() => { | ||
| esClientMock = elasticsearchServiceMock.createElasticsearchClient(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.resetAllMocks(); | ||
| }); | ||
|
|
||
| describe('#getFilesByStatus()', () => { | ||
| it('should return expected values', async () => { | ||
| const status = 'READY'; | ||
| esClientMock.search.mockResolvedValueOnce({ | ||
| took: 5, | ||
| timed_out: false, | ||
| _shards: { | ||
| total: 1, | ||
| successful: 1, | ||
| skipped: 0, | ||
| failed: 0, | ||
| }, | ||
| hits: { | ||
| hits: [ | ||
| { | ||
| _index: ENDPOINT_FILE_METADATA_INDEX, | ||
|
||
| _id: 'someid1', | ||
| }, | ||
| { | ||
| _index: ENDPOINT_FILE_METADATA_INDEX, | ||
| _id: 'someid2', | ||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
|
|
||
| const result = await getFilesByStatus(esClientMock, abortController, status); | ||
|
|
||
| expect(esClientMock.search).toBeCalledWith( | ||
| { | ||
| index: FILE_STORAGE_METADATA_INDEX, | ||
| body: { | ||
| size: ES_SEARCH_LIMIT, | ||
| query: { | ||
| term: { | ||
| 'file.Status.keyword': status, | ||
| }, | ||
| }, | ||
| _source: false, | ||
| }, | ||
| ignore_unavailable: true, | ||
| }, | ||
| { signal: abortController.signal } | ||
| ); | ||
| expect(result).toEqual([ | ||
| { _index: ENDPOINT_FILE_METADATA_INDEX, _id: 'someid1' }, | ||
| { _index: ENDPOINT_FILE_METADATA_INDEX, _id: 'someid2' }, | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('#fileIdsWithoutChunks()', () => { | ||
| it('should return expected values', async () => { | ||
| esClientMock.search.mockResolvedValueOnce({ | ||
| took: 5, | ||
| timed_out: false, | ||
| _shards: { | ||
| total: 1, | ||
| successful: 1, | ||
| skipped: 0, | ||
| failed: 0, | ||
| }, | ||
| hits: { | ||
| hits: [ | ||
| { | ||
| _index: ENDPOINT_FILE_INDEX, | ||
| _id: 'keep1', | ||
| _source: { | ||
| bid: 'keep1', | ||
| }, | ||
| }, | ||
| { | ||
| _index: ENDPOINT_FILE_INDEX, | ||
| _id: 'keep2', | ||
| _source: { | ||
| bid: 'keep2', | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
|
|
||
| const files = [ | ||
| { _index: ENDPOINT_FILE_METADATA_INDEX, _id: 'keep1' }, | ||
| { _index: ENDPOINT_FILE_METADATA_INDEX, _id: 'keep2' }, | ||
| { _index: ENDPOINT_FILE_METADATA_INDEX, _id: 'delete1' }, | ||
| { _index: ENDPOINT_FILE_METADATA_INDEX, _id: 'delete2' }, | ||
| ]; | ||
| const { fileIdsByIndex: deletedFileIdsByIndex, allFileIds: allDeletedFileIds } = | ||
| await fileIdsWithoutChunksByIndex(esClientMock, abortController, files); | ||
|
|
||
| expect(esClientMock.search).toBeCalledWith( | ||
| { | ||
| index: FILE_STORAGE_DATA_INDEX, | ||
| body: { | ||
| size: ES_SEARCH_LIMIT, | ||
| query: { | ||
| bool: { | ||
| must: [ | ||
| { | ||
| terms: { | ||
| 'bid.keyword': Array.from(files.map((file) => file._id)), | ||
| }, | ||
| }, | ||
| { | ||
| term: { | ||
| last: true, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| _source: ['bid'], | ||
| }, | ||
| }, | ||
| { signal: abortController.signal } | ||
| ); | ||
| expect(deletedFileIdsByIndex).toEqual({ | ||
| [ENDPOINT_FILE_METADATA_INDEX]: new Set(['delete1', 'delete2']), | ||
| }); | ||
| expect(allDeletedFileIds).toEqual(new Set(['delete1', 'delete2'])); | ||
| }); | ||
| }); | ||
|
|
||
| describe('#updateFilesStatus()', () => { | ||
| it('calls esClient.updateByQuery with expected values', () => { | ||
| const FAKE_INTEGRATION_METADATA_INDEX = '.fleet-someintegration-files'; | ||
| const files = { | ||
| [ENDPOINT_FILE_METADATA_INDEX]: new Set(['delete1', 'delete2']), | ||
| [FAKE_INTEGRATION_METADATA_INDEX]: new Set(['delete2', 'delete3']), | ||
| }; | ||
| const status = 'DELETED'; | ||
| updateFilesStatus(esClientMock, abortController, files, status); | ||
|
|
||
| expect(esClientMock.updateByQuery).toHaveBeenNthCalledWith( | ||
| 1, | ||
| { | ||
| index: ENDPOINT_FILE_METADATA_INDEX, | ||
| refresh: true, | ||
| query: { | ||
| ids: { | ||
| values: Array.from(files[ENDPOINT_FILE_METADATA_INDEX]), | ||
| }, | ||
| }, | ||
| script: { | ||
| source: `ctx._source.file.Status = '${status}'`, | ||
| lang: 'painless', | ||
| }, | ||
| }, | ||
| { signal: abortController.signal } | ||
| ); | ||
| expect(esClientMock.updateByQuery).toHaveBeenNthCalledWith( | ||
| 2, | ||
| { | ||
| index: FAKE_INTEGRATION_METADATA_INDEX, | ||
| refresh: true, | ||
| query: { | ||
| ids: { | ||
| values: Array.from(files[FAKE_INTEGRATION_METADATA_INDEX]), | ||
| }, | ||
| }, | ||
| script: { | ||
| source: `ctx._source.file.Status = '${status}'`, | ||
| lang: 'painless', | ||
| }, | ||
| }, | ||
| { signal: abortController.signal } | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: name the
constwith the wordPATTERNsince its matching on wildcards.