[Monitoring] Ensure cloud cannot see setup mode (#49223)

* Ensure cloud cannot see setup mode

* Remove cloud check from collection status, as it's an injected var now

* Man these tests suck
This commit is contained in:
Chris Roberson 2019-11-04 11:22:31 -05:00 committed by GitHub
parent 6cd624f25a
commit f48497b6d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 73 additions and 51 deletions

View file

@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/
import React, { useState } from 'react';
import React, { Fragment, useState } from 'react';
import PropTypes from 'prop-types';
import {
EuiSpacer,
@ -43,7 +43,7 @@ function NoDataMessage(props) {
export function NoData(props) {
const [isLoading, setIsLoading] = useState(false);
const [useInternalCollection, setUseInternalCollection] = useState(false);
const [useInternalCollection, setUseInternalCollection] = useState(props.isOnCloud);
async function startSetup() {
setIsLoading(true);
@ -64,15 +64,19 @@ export function NoData(props) {
<EuiSpacer size="m" />
<NoDataMessage {...props} />
<CheckerErrors errors={props.errors} />
<EuiHorizontalRule size="half" />
<EuiButtonEmpty isDisabled={props.isCollectionEnabledUpdated} onClick={() => setUseInternalCollection(false)}>
<EuiTextColor color="default">
<FormattedMessage
id="xpack.monitoring.noData.setupMetricbeatInstead"
defaultMessage="Or, set up with Metricbeat (recommended)"
/>
</EuiTextColor>
</EuiButtonEmpty>
{ !props.isOnCloud ? (
<Fragment>
<EuiHorizontalRule size="half" />
<EuiButtonEmpty isDisabled={props.isCollectionEnabledUpdated} onClick={() => setUseInternalCollection(false)}>
<EuiTextColor color="default">
<FormattedMessage
id="xpack.monitoring.noData.setupMetricbeatInstead"
defaultMessage="Or, set up with Metricbeat (recommended)"
/>
</EuiTextColor>
</EuiButtonEmpty>
</Fragment>
) : null }
</EuiPageContent>
</EuiPageBody>
</EuiPage>

View file

@ -7,6 +7,7 @@
import { ajaxErrorHandlersProvider } from './ajax_error_handler';
import { get, contains } from 'lodash';
import chrome from 'ui/chrome';
import { i18n } from '@kbn/i18n';
function isOnPage(hash) {
return contains(window.location.hash, hash);
@ -80,7 +81,7 @@ export const updateSetupModeData = async (uuid, fetchWithoutClusterUuid = false)
const oldData = setupModeState.data;
const data = await fetchCollectionData(uuid, fetchWithoutClusterUuid);
setupModeState.data = data;
if (get(data, '_meta.isOnCloud', false)) {
if (chrome.getInjected('isOnCloud')) {
return toggleSetupMode(false); // eslint-disable-line no-use-before-define
}
notifySetupModeDataChange(oldData);
@ -139,15 +140,17 @@ export const setSetupModeMenuItem = () => {
}
const globalState = angularState.injector.get('globalState');
const navItems = globalState.inSetupMode
? []
: [{
const navItems = [];
if (!globalState.inSetupMode && !chrome.getInjected('isOnCloud')) {
navItems.push({
id: 'enter',
label: 'Enter Setup Mode',
description: 'Enter setup',
label: i18n.translate('xpack.monitoring.setupMode.enter', {
defaultMessage: 'Enter Setup Mode'
}),
run: () => toggleSetupMode(true),
testId: 'enterSetupMode'
}];
});
}
angularState.scope.topNavMenu = [...navItems];
// LOL angular

View file

@ -4,13 +4,11 @@
* you may not use this file except in compliance with the Elastic License.
*/
import {
toggleSetupMode,
initSetupModeState,
getSetupModeState,
updateSetupModeData,
setSetupModeMenuItem
} from './setup_mode';
let toggleSetupMode;
let initSetupModeState;
let getSetupModeState;
let updateSetupModeData;
let setSetupModeMenuItem;
jest.mock('./ajax_error_handler', () => ({
ajaxErrorHandlersProvider: err => {
@ -52,16 +50,31 @@ function waitForSetupModeData(action) {
process.nextTick(action);
}
describe('setup_mode', () => {
describe('setup', () => {
afterEach(async () => {
try {
toggleSetupMode(false);
} catch (err) {
// Do nothing...
}
});
function setModules() {
jest.resetModules();
injectorModulesMock.globalState.inSetupMode = false;
const setupMode = require('./setup_mode');
toggleSetupMode = setupMode.toggleSetupMode;
initSetupModeState = setupMode.initSetupModeState;
getSetupModeState = setupMode.getSetupModeState;
updateSetupModeData = setupMode.updateSetupModeData;
setSetupModeMenuItem = setupMode.setSetupModeMenuItem;
}
describe('setup_mode', () => {
beforeEach(async () => {
jest.doMock('ui/chrome', () => ({
getInjected: (key) => {
if (key === 'isOnCloud') {
return false;
}
}
}));
setModules();
});
describe('setup', () => {
it('should require angular state', async () => {
let error;
try {
@ -99,21 +112,25 @@ describe('setup_mode', () => {
describe('in setup mode', () => {
afterEach(async () => {
data = {};
toggleSetupMode(false);
});
it('should enable it through clicking top nav item', async () => {
initSetupModeState(angularStateMock.scope, angularStateMock.injector);
setSetupModeMenuItem();
expect(injectorModulesMock.globalState.inSetupMode).toBe(false);
await angularStateMock.scope.topNavMenu[0].run();
expect(injectorModulesMock.globalState.inSetupMode).toBe(true);
});
it('should not fetch data if on cloud', async (done) => {
data = {
_meta: {
isOnCloud: true
jest.doMock('ui/chrome', () => ({
getInjected: (key) => {
if (key === 'isOnCloud') {
return true;
}
}
};
}));
setModules();
initSetupModeState(angularStateMock.scope, angularStateMock.injector);
await toggleSetupMode(true);
waitForSetupModeData(() => {

View file

@ -5,6 +5,7 @@
*/
import React from 'react';
import chrome from 'ui/chrome';
import {
ClusterSettingsChecker,
NodeSettingsChecker,
@ -99,7 +100,12 @@ export class NoDataController extends MonitoringViewBaseController {
this.renderReact(
<I18nContext>
<NoData {...props} enabler={enabler} changePath={this.changePath} />
<NoData
{...props}
enabler={enabler}
changePath={this.changePath}
isOnCloud={chrome.getInjected('isOnCloud')}
/>
</I18nContext>
);
}

View file

@ -547,7 +547,6 @@ export const getCollectionStatus = async (req, indexPatterns, clusterUuid, nodeU
status._meta = {
secondsAgo: NUMBER_OF_SECONDS_AGO_TO_LOOK,
liveClusterUuid,
isOnCloud: get(req.server.plugins, 'cloud.config.isCloudEnabled', false)
};
return status;

View file

@ -5,6 +5,7 @@
*/
import { i18n } from '@kbn/i18n';
import { get } from 'lodash';
import { resolve } from 'path';
/**
@ -28,7 +29,8 @@ export const getUiExports = () => ({
injectDefaultVars(server) {
const config = server.config();
return {
monitoringUiEnabled: config.get('xpack.monitoring.ui.enabled')
monitoringUiEnabled: config.get('xpack.monitoring.ui.enabled'),
isOnCloud: get(server.plugins, 'cloud.config.isCloudEnabled', false)
};
},
hacks: [ 'plugins/monitoring/hacks/toggle_app_link_in_nav' ],

View file

@ -51,7 +51,6 @@
},
"_meta": {
"secondsAgo": 30,
"isOnCloud": false,
"liveClusterUuid": null
}
}

View file

@ -60,7 +60,6 @@
},
"_meta": {
"secondsAgo": 30,
"isOnCloud": false,
"liveClusterUuid": null
}
}

View file

@ -51,7 +51,6 @@
},
"_meta": {
"secondsAgo": 30,
"isOnCloud": false,
"liveClusterUuid": null
}
}

View file

@ -51,7 +51,6 @@
},
"_meta": {
"secondsAgo": 30,
"isOnCloud": false,
"liveClusterUuid": null
}
}

View file

@ -51,7 +51,6 @@
},
"_meta": {
"secondsAgo": 30,
"isOnCloud": false,
"liveClusterUuid": null
}
}

View file

@ -71,7 +71,6 @@
},
"_meta": {
"secondsAgo": 30,
"isOnCloud": false,
"liveClusterUuid": null
}
}

View file

@ -71,7 +71,6 @@
},
"_meta": {
"secondsAgo": 30,
"isOnCloud": false,
"liveClusterUuid": null
}
}

View file

@ -71,7 +71,6 @@
},
"_meta": {
"secondsAgo": 30,
"isOnCloud": false,
"liveClusterUuid": null
}
}

View file

@ -71,7 +71,6 @@
},
"_meta": {
"secondsAgo": 30,
"isOnCloud": false,
"liveClusterUuid": null
}
}