-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rename console log action to server log
- actually use the server log (passed in to executor) - add some jest tests
- Loading branch information
Showing
7 changed files
with
154 additions
and
31 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
109 changes: 109 additions & 0 deletions
109
x-pack/plugins/actions/server/builtin_action_types/__jest__/server_log.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,109 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { ActionTypeService } from '../../action_type_service'; | ||
|
||
import { registerBuiltInActionTypes } from '../index'; | ||
|
||
const ACTION_TYPE_ID = 'kibana.server-log'; | ||
const NO_OP_FN = () => {}; | ||
|
||
const services = { | ||
log: NO_OP_FN, | ||
}; | ||
|
||
let actionTypeService: ActionTypeService; | ||
|
||
beforeAll(() => { | ||
actionTypeService = new ActionTypeService({ services }); | ||
registerBuiltInActionTypes(actionTypeService); | ||
}); | ||
|
||
beforeEach(() => { | ||
services.log = NO_OP_FN; | ||
}); | ||
|
||
describe('action is registered', () => { | ||
test('gets registered with builtin actions', () => { | ||
expect(actionTypeService.has(ACTION_TYPE_ID)).toEqual(true); | ||
}); | ||
}); | ||
|
||
describe('get()', () => { | ||
test('returns action type', () => { | ||
const actionType = actionTypeService.get(ACTION_TYPE_ID); | ||
expect(actionType.id).toEqual(ACTION_TYPE_ID); | ||
expect(actionType.name).toEqual('server-log'); | ||
}); | ||
}); | ||
|
||
describe('validateParams()', () => { | ||
test('should validate and pass when params is valid', () => { | ||
actionTypeService.validateParams(ACTION_TYPE_ID, { message: 'a message' }); | ||
actionTypeService.validateParams(ACTION_TYPE_ID, { | ||
message: 'a message', | ||
tags: ['info', 'blorg'], | ||
}); | ||
}); | ||
|
||
test('should validate and throw error when params is invalid', () => { | ||
expect(() => { | ||
actionTypeService.validateParams(ACTION_TYPE_ID, {}); | ||
}).toThrowErrorMatchingInlineSnapshot( | ||
`"child \\"message\\" fails because [\\"message\\" is required]"` | ||
); | ||
|
||
expect(() => { | ||
actionTypeService.validateParams(ACTION_TYPE_ID, { message: 1 }); | ||
}).toThrowErrorMatchingInlineSnapshot( | ||
`"child \\"message\\" fails because [\\"message\\" must be a string]"` | ||
); | ||
|
||
expect(() => { | ||
actionTypeService.validateParams(ACTION_TYPE_ID, { message: 'x', tags: 2 }); | ||
}).toThrowErrorMatchingInlineSnapshot( | ||
`"child \\"tags\\" fails because [\\"tags\\" must be an array]"` | ||
); | ||
|
||
expect(() => { | ||
actionTypeService.validateParams(ACTION_TYPE_ID, { message: 'x', tags: [2] }); | ||
}).toThrowErrorMatchingInlineSnapshot( | ||
`"child \\"tags\\" fails because [\\"tags\\" at position 0 fails because [\\"0\\" must be a string]]"` | ||
); | ||
}); | ||
}); | ||
|
||
describe('execute()', () => { | ||
test('calls the executor with proper params', async () => { | ||
const mockLog = jest.fn().mockResolvedValueOnce({ success: true }); | ||
|
||
services.log = mockLog; | ||
await actionTypeService.execute({ | ||
id: ACTION_TYPE_ID, | ||
actionTypeConfig: {}, | ||
params: { message: 'message text here', tags: ['tag1', 'tag2'] }, | ||
}); | ||
expect(mockLog).toMatchInlineSnapshot(` | ||
[MockFunction] { | ||
"calls": Array [ | ||
Array [ | ||
Array [ | ||
"tag1", | ||
"tag2", | ||
], | ||
"message text here", | ||
], | ||
], | ||
"results": Array [ | ||
Object { | ||
"type": "return", | ||
"value": Promise {}, | ||
}, | ||
], | ||
} | ||
`); | ||
}); | ||
}); |
20 changes: 0 additions & 20 deletions
20
x-pack/plugins/actions/server/builtin_action_types/console_log.ts
This file was deleted.
Oops, something went wrong.
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
34 changes: 34 additions & 0 deletions
34
x-pack/plugins/actions/server/builtin_action_types/server_log.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,34 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import Joi from 'joi'; | ||
|
||
import { ActionType, ExecutorOptions } from '../action_type_service'; | ||
|
||
const DEFAULT_TAGS = ['info', 'alerting']; | ||
|
||
const PARAMS_SCHEMA = Joi.object().keys({ | ||
message: Joi.string().required(), | ||
tags: Joi.array() | ||
.items(Joi.string()) | ||
.optional() | ||
.default(DEFAULT_TAGS), | ||
}); | ||
|
||
export const actionType: ActionType = { | ||
id: 'kibana.server-log', | ||
name: 'server-log', | ||
validate: { | ||
params: PARAMS_SCHEMA, | ||
}, | ||
executor, | ||
}; | ||
|
||
async function executor({ params, services }: ExecutorOptions): Promise<any> { | ||
const { message, tags } = params; | ||
|
||
services.log(tags, message); | ||
} |
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