Skip to content

Commit

Permalink
Merge branch 'master' into elastic#60333
Browse files Browse the repository at this point in the history
  • Loading branch information
elasticmachine authored Mar 27, 2020
2 parents 775eaaf + 878ab20 commit 70ccb9c
Show file tree
Hide file tree
Showing 28 changed files with 817 additions and 567 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ module.exports = {
'x-pack/dev-tools/mocha/setup_mocha.js',
'x-pack/scripts/*.js',
],
excludedFiles: ['**/integration_tests/**/*'],
rules: {
'import/no-commonjs': 'off',
'prefer-object-spread/prefer-object-spread': 'off',
Expand Down
173 changes: 28 additions & 145 deletions packages/kbn-plugin-generator/integration_tests/generate_plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,163 +17,46 @@
* under the License.
*/

/* eslint-disable no-restricted-syntax */
import { spawn } from 'child_process';
import Fs from 'fs';
import { resolve } from 'path';
import util from 'util';
import { stat, readFileSync } from 'fs';
import { snakeCase } from 'lodash';
import { promisify } from 'util';

import del from 'del';
import { ProcRunner, ToolingLog } from '@kbn/dev-utils';
import { createLegacyEsTestCluster } from '@kbn/test';
import execa from 'execa';
import { snakeCase } from 'lodash';

const statP = util.promisify(stat);
const statAsync = promisify(Fs.stat);
const ROOT_DIR = resolve(__dirname, '../../../');
const oneMinute = 60000;

describe(`running the plugin-generator via 'node scripts/generate_plugin.js plugin-name' with default config`, () => {
const pluginName = 'ispec-plugin';
const snakeCased = snakeCase(pluginName);
const generatedPath = resolve(ROOT_DIR, `plugins/${snakeCased}`);
const collect = xs => data => xs.push(data + ''); // Coerce from Buffer to String

beforeAll(() => {
jest.setTimeout(oneMinute * 10);
});

beforeAll(done => {
const create = spawn(process.execPath, ['scripts/generate_plugin.js', pluginName], {
cwd: ROOT_DIR,
});
create.stdout.on('data', function selectDefaults() {
create.stdin.write('\n'); // Generate a plugin with default options.
});
create.on('close', done);
});

afterAll(() => {
del.sync(generatedPath, { force: true });
});

it(`should succeed on creating a plugin in a directory named 'plugins/${snakeCased}`, async () => {
const stats = await statP(generatedPath);
expect(stats.isDirectory()).toBe(true);
});

// skipped until internationalization is re-introduced
it.skip(`should create an internationalization config file with a blank line appended to satisfy the parser`, async () => {
// Link to the error that happens when the blank line is not there:
// https://github.com/elastic/kibana/pull/45044#issuecomment-530092627
const intlFile = `${generatedPath}/.i18nrc.json`;
expect(readFileSync(intlFile, 'utf8').endsWith('\n\n')).toBe(true);
});

describe(`then running`, () => {
it(`'yarn test:karma' should exit 0`, async () => {
await execa('yarn', ['test:karma'], {
cwd: generatedPath,
env: {
DISABLE_JUNIT_REPORTER: '1',
},
});
});

it.skip(`'yarn build' should exit 0`, async () => {
await execa('yarn', ['build'], { cwd: generatedPath });
});
const pluginName = 'ispec-plugin';
const snakeCased = snakeCase(pluginName);
const generatedPath = resolve(ROOT_DIR, `plugins/${snakeCased}`);

describe('with es instance', () => {
const log = new ToolingLog({
level: 'verbose',
writeTo: process.stdout,
});
const pr = new ProcRunner(log);

const es = createLegacyEsTestCluster({ license: 'basic', log });
beforeAll(es.start);
afterAll(es.stop);
afterAll(() => pr.teardown());
beforeAll(async () => {
await del(generatedPath, { force: true });
});

it(`'yarn start' should result in the spec plugin being initialized on kibana's stdout`, async () => {
await pr.run('kibana', {
cmd: 'yarn',
args: [
'start',
'--optimize.enabled=false',
'--logging.json=false',
'--logging.verbose=true',
'--migrations.skip=true',
],
cwd: generatedPath,
wait: new RegExp('\\[ispecPlugin\\]\\[plugins\\] Setting up plugin'),
});
await pr.stop('kibana');
});
});
afterAll(async () => {
await del(generatedPath, { force: true });
});

it(`'yarn preinstall' should exit 0`, async () => {
await execa('yarn', ['preinstall'], { cwd: generatedPath });
it('generates a plugin', async () => {
await new Promise((resolve, reject) => {
const proc = spawn(process.execPath, ['scripts/generate_plugin.js', pluginName], {
cwd: ROOT_DIR,
stdio: 'pipe',
});

it.skip(`'yarn lint' should exit 0`, async () => {
await execa('yarn', ['lint'], { cwd: generatedPath });
proc.stdout.on('data', function selectDefaults() {
proc.stdin.write('\n'); // Generate a plugin with default options.
});

it(`'yarn kbn --help' should print out the kbn help msg`, done => {
const helpMsg = `
usage: kbn <command> [<args>]
By default commands are run for Kibana itself, all packages in the 'packages/'
folder and for all plugins in './plugins' and '../kibana-extra'.
Available commands:
bootstrap - Install dependencies and crosslink projects
clean - Remove the node_modules and target directories from all projects.
run - Run script defined in package.json in each package that contains that script.
watch - Runs \`kbn:watch\` script for every project.
Global options:
-e, --exclude Exclude specified project. Can be specified multiple times to exclude multiple projects, e.g. '-e kibana -e @kbn/pm'.
-i, --include Include only specified projects. If left unspecified, it defaults to including all projects.
--oss Do not include the x-pack when running command.
--skip-kibana-plugins Filter all plugins in ./plugins and ../kibana-extra when running command.
`;
const outData = [];
const kbnHelp = spawn('yarn', ['kbn', '--help'], { cwd: generatedPath });
kbnHelp.stdout.on('data', collect(outData));
kbnHelp.on('close', () => {
expect(outData.join('\n')).toContain(helpMsg);
done();
});
});

it(`'yarn es --help' should print out the es help msg`, done => {
const helpMsg = `
usage: es <command> [<args>]
Assists with running Elasticsearch for Kibana development
Available commands:
snapshot - Downloads and run from a nightly snapshot
source - Build and run from source
archive - Install and run from an Elasticsearch tar
build_snapshots - Build and collect ES snapshots
Global options:
--help
`;
const outData = [];
const kbnHelp = spawn('yarn', ['es', '--help'], { cwd: generatedPath });
kbnHelp.stdout.on('data', collect(outData));
kbnHelp.on('close', () => {
expect(outData.join('\n')).toContain(helpMsg);
done();
});
});
proc.on('close', resolve);
proc.on('error', reject);
});

const stats = await statAsync(generatedPath);
if (!stats.isDirectory()) {
throw new Error(`Expected [${generatedPath}] to be a directory`);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,19 @@ describe('handleUpdateIncident', () => {
const res = await handleUpdateIncident({
incidentId: '123',
serviceNow,
params,
params: {
...params,
updatedAt: '2020-03-15T08:34:53.450Z',
updatedBy: { fullName: 'Another User', username: 'anotherUser' },
},
comments: [],
mapping: finalMapping,
});

expect(serviceNow.updateIncident).toHaveBeenCalled();
expect(serviceNow.updateIncident).toHaveBeenCalledWith('123', {
short_description: 'a title (updated at 2020-03-13T08:34:53.450Z by Elastic User)',
description: 'a description (updated at 2020-03-13T08:34:53.450Z by Elastic User)',
short_description: 'a title (updated at 2020-03-15T08:34:53.450Z by Another User)',
description: 'a description (updated at 2020-03-15T08:34:53.450Z by Another User)',
});
expect(serviceNow.updateIncident).toHaveReturned();
expect(serviceNow.batchCreateComments).not.toHaveBeenCalled();
Expand All @@ -256,7 +260,11 @@ describe('handleUpdateIncident', () => {
const res = await handleUpdateIncident({
incidentId: '123',
serviceNow,
params,
params: {
...params,
updatedAt: '2020-03-15T08:34:53.450Z',
updatedBy: { fullName: 'Another User', username: 'anotherUser' },
},
comments: [
{
comment: 'first comment',
Expand All @@ -278,10 +286,10 @@ describe('handleUpdateIncident', () => {
fullName: 'Elastic User',
username: 'elastic',
},
updatedAt: '2020-03-13T08:34:53.450Z',
updatedAt: '2020-03-16T08:34:53.450Z',
updatedBy: {
fullName: 'Elastic User',
username: 'elastic',
fullName: 'Another User',
username: 'anotherUser',
},
version: 'WzU3LDFd',
},
Expand All @@ -291,8 +299,8 @@ describe('handleUpdateIncident', () => {

expect(serviceNow.updateIncident).toHaveBeenCalled();
expect(serviceNow.updateIncident).toHaveBeenCalledWith('123', {
description: 'a description (updated at 2020-03-13T08:34:53.450Z by Elastic User)',
short_description: 'a title (updated at 2020-03-13T08:34:53.450Z by Elastic User)',
description: 'a description (updated at 2020-03-15T08:34:53.450Z by Another User)',
short_description: 'a title (updated at 2020-03-15T08:34:53.450Z by Another User)',
});
expect(serviceNow.updateIncident).toHaveReturned();
expect(serviceNow.batchCreateComments).toHaveBeenCalled();
Expand All @@ -312,17 +320,17 @@ describe('handleUpdateIncident', () => {
version: 'WzU3LDFd',
},
{
comment: 'second comment (added at 2020-03-13T08:34:53.450Z by Elastic User)',
comment: 'second comment (added at 2020-03-16T08:34:53.450Z by Another User)',
commentId: '789',
createdAt: '2020-03-13T08:34:53.450Z',
createdBy: {
fullName: 'Elastic User',
username: 'elastic',
},
updatedAt: '2020-03-13T08:34:53.450Z',
updatedAt: '2020-03-16T08:34:53.450Z',
updatedBy: {
fullName: 'Elastic User',
username: 'elastic',
fullName: 'Another User',
username: 'anotherUser',
},
version: 'WzU3LDFd',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,23 +191,31 @@ describe('transformFields', () => {

test('transform fields for update correctly', () => {
const fields = prepareFieldsForTransformation({
params: fullParams,
params: {
...fullParams,
updatedAt: '2020-03-15T08:34:53.450Z',
updatedBy: { username: 'anotherUser', fullName: 'Another User' },
},
mapping: finalMapping,
defaultPipes: ['informationUpdated'],
});

const res = transformFields({
params: fullParams,
params: {
...fullParams,
updatedAt: '2020-03-15T08:34:53.450Z',
updatedBy: { username: 'anotherUser', fullName: 'Another User' },
},
fields,
currentIncident: {
short_description: 'first title (created at 2020-03-13T08:34:53.450Z by Elastic User)',
description: 'first description (created at 2020-03-13T08:34:53.450Z by Elastic User)',
},
});
expect(res).toEqual({
short_description: 'a title (updated at 2020-03-13T08:34:53.450Z by Elastic User)',
short_description: 'a title (updated at 2020-03-15T08:34:53.450Z by Another User)',
description:
'first description (created at 2020-03-13T08:34:53.450Z by Elastic User) \r\na description (updated at 2020-03-13T08:34:53.450Z by Elastic User)',
'first description (created at 2020-03-13T08:34:53.450Z by Elastic User) \r\na description (updated at 2020-03-15T08:34:53.450Z by Another User)',
});
});

Expand All @@ -229,7 +237,7 @@ describe('transformFields', () => {
expect(res.description?.includes('\r\n')).toBe(true);
});

test('append username if fullname is undefined', () => {
test('append username if fullname is undefined when create', () => {
const fields = prepareFieldsForTransformation({
params: fullParams,
mapping: finalMapping,
Expand All @@ -245,6 +253,32 @@ describe('transformFields', () => {
description: 'a description (created at 2020-03-13T08:34:53.450Z by elastic)',
});
});

test('append username if fullname is undefined when update', () => {
const fields = prepareFieldsForTransformation({
params: {
...fullParams,
updatedAt: '2020-03-15T08:34:53.450Z',
updatedBy: { username: 'anotherUser', fullName: 'Another User' },
},
mapping: finalMapping,
defaultPipes: ['informationUpdated'],
});

const res = transformFields({
params: {
...fullParams,
updatedAt: '2020-03-15T08:34:53.450Z',
updatedBy: { username: 'anotherUser', fullName: null },
},
fields,
});

expect(res).toEqual({
short_description: 'a title (updated at 2020-03-15T08:34:53.450Z by anotherUser)',
description: 'a description (updated at 2020-03-15T08:34:53.450Z by anotherUser)',
});
});
});

describe('appendField', () => {
Expand Down Expand Up @@ -330,20 +364,20 @@ describe('transformComments', () => {
comment: 'first comment',
createdAt: '2020-03-13T08:34:53.450Z',
createdBy: { fullName: 'Elastic User', username: 'elastic' },
updatedAt: null,
updatedBy: null,
updatedAt: '2020-03-15T08:34:53.450Z',
updatedBy: { fullName: 'Another User', username: 'anotherUser' },
},
];
const res = transformComments(comments, fullParams, ['informationUpdated']);
expect(res).toEqual([
{
commentId: 'b5b4c4d0-574e-11ea-9e2e-21b90f8a9631',
version: 'WzU3LDFd',
comment: 'first comment (updated at 2020-03-13T08:34:53.450Z by Elastic User)',
comment: 'first comment (updated at 2020-03-15T08:34:53.450Z by Another User)',
createdAt: '2020-03-13T08:34:53.450Z',
createdBy: { fullName: 'Elastic User', username: 'elastic' },
updatedAt: null,
updatedBy: null,
updatedAt: '2020-03-15T08:34:53.450Z',
updatedBy: { fullName: 'Another User', username: 'anotherUser' },
},
]);
});
Expand Down
Loading

0 comments on commit 70ccb9c

Please sign in to comment.