Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
madirey committed Jul 9, 2020
1 parent 62b1a2c commit cbe39ec
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
21 changes: 20 additions & 1 deletion x-pack/plugins/ingest_manager/common/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { NewPackageConfig } from './types/models/package_config';
import { NewPackageConfig, PackageConfig } from './types/models/package_config';

export const createNewPackageConfigMock = () => {
return {
Expand All @@ -22,3 +22,22 @@ export const createNewPackageConfigMock = () => {
inputs: [],
} as NewPackageConfig;
};

export const createPackageConfigMock = () => {
const newPackageConfig = createNewPackageConfigMock();
return {
...newPackageConfig,
id: 'c6d16e42-c32d-4dce-8a88-113cfe276ad1',
version: 'abcd',
revision: 1,
updated_at: '2020-06-25T16:03:38.159292',
updated_by: 'kibana',
created_at: '2020-06-25T16:03:38.159292',
created_by: 'kibana',
inputs: [
{
config: {},
},
],
} as PackageConfig;
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ describe('ingest_integration tests ', () => {
expect(newPolicyConfig.inputs[0]!.config!.artifact_manifest.value).toEqual({
artifacts: {
'endpoint-exceptionlist-linux-v1': {
compression_algorithm: 'none',
compression_algorithm: 'zlib',
decoded_sha256: '1a8295e6ccb93022c6f5ceb8997b29f2912389b3b38f52a8f5a2ff7b0154b1bc',
decoded_size: 287,
encoded_sha256: '1a8295e6ccb93022c6f5ceb8997b29f2912389b3b38f52a8f5a2ff7b0154b1bc',
encoded_size: 287,
encoded_sha256: 'c3dec543df1177561ab2aa74a37997ea3c1d748d532a597884f5a5c16670d56c',
encoded_size: 133,
encryption_algorithm: 'none',
relative_url:
'/api/endpoint/artifacts/download/endpoint-exceptionlist-linux-v1/1a8295e6ccb93022c6f5ceb8997b29f2912389b3b38f52a8f5a2ff7b0154b1bc',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { savedObjectsClientMock, loggingSystemMock } from 'src/core/server/mocks';
import { Logger } from 'src/core/server';
import { createPackageConfigMock } from '../../../../../../ingest_manager/common/mocks';
import { PackageConfigServiceInterface } from '../../../../../../ingest_manager/server';
import { createPackageConfigServiceMock } from '../../../../../../ingest_manager/server/mocks';
import { getFoundExceptionListItemSchemaMock } from '../../../../../../lists/common/schemas/response/found_exception_list_item_schema.mock';
Expand Down Expand Up @@ -45,8 +46,6 @@ export class ManifestManagerMock extends ManifestManager {
protected getManifestClient = jest
.fn()
.mockReturnValue(getManifestClientMock(this.savedObjectsClient));

public syncArtifacts = jest.fn().mockResolvedValue([]);
}

export const getManifestManagerMock = (opts?: {
Expand All @@ -63,6 +62,10 @@ export const getManifestManagerMock = (opts?: {
if (opts?.packageConfigService !== undefined) {
packageConfigService = opts.packageConfigService;
}
packageConfigService.list = jest.fn().mockResolvedValue({
total: 1,
items: [createPackageConfigMock()],
});

let savedObjectsClient = savedObjectsClientMock.create();
if (opts?.savedObjectsClient !== undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

import { inflateSync } from 'zlib';
import { savedObjectsClientMock } from 'src/core/server/mocks';
import { createPackageConfigServiceMock } from '../../../../../../ingest_manager/server/mocks';
import {
ArtifactConstants,
ManifestConstants,
Manifest,
ExceptionsCache,
} from '../../../lib/artifacts';
import { getPackageConfigServiceMock, getManifestManagerMock } from './manifest_manager.mock';
import { getManifestManagerMock } from './manifest_manager.mock';

describe('manifest_manager', () => {
describe('ManifestManager sanity checks', () => {
Expand Down Expand Up @@ -73,15 +74,15 @@ describe('manifest_manager', () => {
});

test('ManifestManager can dispatch manifest', async () => {
const packageConfigService = getPackageConfigServiceMock();
const packageConfigService = createPackageConfigServiceMock();
const manifestManager = getManifestManagerMock({ packageConfigService });
const snapshot = await manifestManager.getSnapshot();
const dispatched = await manifestManager.dispatch(snapshot!.manifest);
expect(dispatched).toEqual([]);
const entries = snapshot!.manifest.getEntries();
const artifact = Object.values(entries)[0].getArtifact();
expect(
packageConfigService.update.mock.calls[0][2].inputs[0].config.artifact_manifest.value
packageConfigService.update.mock.calls[0][2].inputs[0].config!.artifact_manifest.value
).toEqual({
manifest_version: ManifestConstants.INITIAL_VERSION,
schema_version: 'v1',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ export class ManifestManager {
const errors: Error[] = [];
for (const diff of snapshot.diffs) {
const artifact = snapshot.manifest.getArtifact(diff.id);
if (artifact === undefined) {
throw new Error(
`Corrupted manifest detected. Diff contained artifact ${diff.id} not in manifest.`
);
}

const compressedArtifact = await compressExceptionList(Buffer.from(artifact.body, 'base64'));
artifact.body = compressedArtifact.toString('base64');
artifact.encodedSize = compressedArtifact.byteLength;
Expand Down

0 comments on commit cbe39ec

Please sign in to comment.