From c8216de9d547889e7d5443e6a4c1eaffad2d8951 Mon Sep 17 00:00:00 2001 From: Jonathan Budzenski Date: Fri, 8 Jun 2018 11:35:09 -0500 Subject: [PATCH] [plugin installer] Allow x-pack removal if it exists (#19327) * [plugin installer] Allow x-pack removal if it exists * remove lingering exit --- .../lib/__tests__/error_if_x_pack.js | 72 ------------------- src/cli_plugin/lib/error_if_x_pack.js | 12 ++-- src/cli_plugin/lib/error_if_x_pack.test.js | 51 +++++++++++++ .../{__tests__/is_oss.js => is_oss.test.js} | 6 +- src/cli_plugin/remove/remove.js | 4 +- src/cli_plugin/remove/remove.test.js | 19 ++++- 6 files changed, 78 insertions(+), 86 deletions(-) delete mode 100644 src/cli_plugin/lib/__tests__/error_if_x_pack.js create mode 100644 src/cli_plugin/lib/error_if_x_pack.test.js rename src/cli_plugin/lib/{__tests__/is_oss.js => is_oss.test.js} (89%) diff --git a/src/cli_plugin/lib/__tests__/error_if_x_pack.js b/src/cli_plugin/lib/__tests__/error_if_x_pack.js deleted file mode 100644 index d169f16a3beb..000000000000 --- a/src/cli_plugin/lib/__tests__/error_if_x_pack.js +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import expect from 'expect.js'; -import sinon from 'sinon'; - -import { errorIfXPackInstall, errorIfXPackRemove } from '../error_if_x_pack'; - -describe('error_if_xpack', () => { - const logger = { - error: sinon.stub() - }; - - beforeEach(() => { - sinon.stub(process, 'exit'); - }); - - it('should exit on install if x-pack by name', () => { - errorIfXPackInstall({ - plugin: 'x-pack' - }, logger); - expect(process.exit.called).to.be(true); - }); - - it('should exit on install if x-pack by url', () => { - errorIfXPackInstall({ - plugin: ' http://localhost/x-pack/x-pack-7.0.0-alpha1-SNAPSHOT.zip' - }, logger); - expect(process.exit.called).to.be(true); - }); - - it('should not exit on install if not x-pack', () => { - errorIfXPackInstall({ - plugin: 'foo' - }, logger); - expect(process.exit.called).to.be(false); - }); - - it('should exit on remove if x-pack', () => { - errorIfXPackRemove({ - plugin: 'x-pack' - }, logger); - expect(process.exit.called).to.be(true); - }); - - it('should not exit on remove if not x-pack', () => { - errorIfXPackRemove({ - plugin: 'bar' - }, logger); - expect(process.exit.called).to.be(false); - }); - - afterEach(() => { - process.exit.restore(); - }); -}); diff --git a/src/cli_plugin/lib/error_if_x_pack.js b/src/cli_plugin/lib/error_if_x_pack.js index 4c808ac34d52..b005ab8fef79 100644 --- a/src/cli_plugin/lib/error_if_x_pack.js +++ b/src/cli_plugin/lib/error_if_x_pack.js @@ -23,28 +23,26 @@ function isXPack(plugin) { return /x-pack/.test(plugin); } -export function errorIfXPackInstall(settings, logger) { +export function errorIfXPackInstall(settings) { if (isXPack(settings.plugin)) { if (isOSS()) { - logger.error( + throw new Error( 'You are using the OSS-only distribution of Kibana. ' + 'As of version 6.3+ X-Pack is bundled in the standard distribution of this software by default; ' + 'consequently it is no longer available as a plugin. Please use the standard distribution of Kibana to use X-Pack features.' ); } else { - logger.error( + throw new Error( 'Kibana now contains X-Pack by default, there is no longer any need to install it as it is already present.' ); } - process.exit(1); } } -export function errorIfXPackRemove(settings, logger) { +export function errorIfXPackRemove(settings) { if (isXPack(settings.plugin) && !isOSS()) { - logger.error( + throw new Error( 'You are using the standard distrbution of Kibana. Please install the OSS-only distribution to remove X-Pack features.' ); - process.exit(1); } } diff --git a/src/cli_plugin/lib/error_if_x_pack.test.js b/src/cli_plugin/lib/error_if_x_pack.test.js new file mode 100644 index 000000000000..e3489161e9bb --- /dev/null +++ b/src/cli_plugin/lib/error_if_x_pack.test.js @@ -0,0 +1,51 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +import { errorIfXPackInstall, errorIfXPackRemove } from './error_if_x_pack'; + +describe('error_if_xpack', () => { + it('should error on install if x-pack by name', () => { + expect(() => errorIfXPackInstall({ plugin: 'x-pack' })).toThrow(); + }); + + it('should error on install if x-pack by url', () => { + expect(() => ( + errorIfXPackInstall({ + plugin: 'http://localhost/x-pack/x-pack-7.0.0-alpha1-SNAPSHOT.zip' + })) + ).toThrow(); + }); + + it('should not error on install if not x-pack', () => { + expect(() => ( + errorIfXPackInstall({ + plugin: 'foo' + })) + ).not.toThrow(); + }); + + it('should error on remove if x-pack', () => { + expect(() => errorIfXPackRemove({ plugin: 'x-pack' })).toThrow(); + }); + + it('should not error on remove if not x-pack', () => { + expect(() => errorIfXPackRemove({ plugin: 'bar' })).not.toThrow(); + }); +}); diff --git a/src/cli_plugin/lib/__tests__/is_oss.js b/src/cli_plugin/lib/is_oss.test.js similarity index 89% rename from src/cli_plugin/lib/__tests__/is_oss.js rename to src/cli_plugin/lib/is_oss.test.js index 117d622789de..a4673710c63c 100644 --- a/src/cli_plugin/lib/__tests__/is_oss.js +++ b/src/cli_plugin/lib/is_oss.test.js @@ -17,14 +17,12 @@ * under the License. */ -import expect from 'expect.js'; - -import { isOSS } from '../is_oss'; +import { isOSS } from './is_oss'; describe('is_oss', () => { describe('x-pack installed', () => { it('should return false', () => { - expect(isOSS()).to.be(false); + expect(isOSS()).toEqual(false); }); }); }); diff --git a/src/cli_plugin/remove/remove.js b/src/cli_plugin/remove/remove.js index ae1bf245e34d..7aaccd8ca05f 100644 --- a/src/cli_plugin/remove/remove.js +++ b/src/cli_plugin/remove/remove.js @@ -24,12 +24,11 @@ import rimraf from 'rimraf'; export default function remove(settings, logger) { try { - errorIfXPackRemove(settings, logger); - let stat; try { stat = statSync(settings.pluginPath); } catch (e) { + errorIfXPackRemove(settings, logger); throw new Error(`Plugin [${settings.plugin}] is not installed`); } @@ -39,6 +38,7 @@ export default function remove(settings, logger) { logger.log(`Removing ${settings.plugin}...`); rimraf.sync(settings.pluginPath); + logger.log('Plugin removal complete'); } catch (err) { logger.error(`Unable to remove plugin because of error: "${err.message}"`); process.exit(74); // eslint-disable-line no-process-exit diff --git a/src/cli_plugin/remove/remove.test.js b/src/cli_plugin/remove/remove.test.js index 1208fcb55973..971881dd0b87 100644 --- a/src/cli_plugin/remove/remove.test.js +++ b/src/cli_plugin/remove/remove.test.js @@ -24,7 +24,7 @@ import mkdirp from 'mkdirp'; import Logger from '../lib/logger'; import remove from './remove'; import { join } from 'path'; -import { writeFileSync } from 'fs'; +import { writeFileSync, existsSync } from 'fs'; describe('kibana cli', function () { @@ -69,6 +69,23 @@ describe('kibana cli', function () { expect(process.exit.called).toBe(true); }); + it('remove x-pack if it exists', () => { + settings.pluginPath = join(pluginDir, 'x-pack'); + settings.plugin = 'x-pack'; + mkdirp.sync(join(pluginDir, 'x-pack')); + expect(existsSync(settings.pluginPath)).toEqual(true); + remove(settings, logger); + expect(existsSync(settings.pluginPath)).toEqual(false); + }); + + it('distribution error if x-pack does not exist', () => { + settings.pluginPath = join(pluginDir, 'x-pack'); + settings.plugin = 'x-pack'; + expect(existsSync(settings.pluginPath)).toEqual(false); + remove(settings, logger); + expect(logger.error.getCall(0).args[0]).toMatch(/Please install the OSS-only distribution to remove X-Pack features/); + }); + it('delete the specified folder.', function () { settings.pluginPath = join(pluginDir, 'foo'); mkdirp.sync(join(pluginDir, 'foo'));