Skip to content

Commit

Permalink
Merge branch 'main' into revert-164574-task-manager/force-task-state-…
Browse files Browse the repository at this point in the history
…validation
  • Loading branch information
kibanamachine authored Oct 3, 2023
2 parents 1df0ec8 + 8e7b233 commit 7ddbd5c
Show file tree
Hide file tree
Showing 30 changed files with 514 additions and 493 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ const AwsAccountTypeSelect = ({
);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [input]);
}, [input, updatePolicy]);

return (
<>
Expand Down Expand Up @@ -341,7 +341,7 @@ const GcpAccountTypeSelect = ({
);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [input]);
}, [input, updatePolicy]);

return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('SecurityUsageReportingTask', () => {
let usageRecord: UsageRecord;

function buildMockTaskInstance(overrides?: Partial<ConcreteTaskInstance>): ConcreteTaskInstance {
const timestamp = new Date(new Date().setMinutes(-15));
const timestamp = new Date(new Date().setMinutes(-15)).toISOString();
return assign(
{
id: `${TYPE}:${VERSION}`,
Expand Down Expand Up @@ -173,7 +173,7 @@ describe('SecurityUsageReportingTask', () => {
cloudSetup: taskArgs.cloudSetup,
taskId: TASK_ID,
config: taskArgs.config,
lastSuccessfulReport: task?.state.lastSuccessfulReport,
lastSuccessfulReport: new Date(task?.state.lastSuccessfulReport as string),
})
);
});
Expand Down Expand Up @@ -203,7 +203,16 @@ describe('SecurityUsageReportingTask', () => {
const taskInstance = buildMockTaskInstance();
const task = await runTask(taskInstance);
const newLastSuccessfulReport = task?.state.lastSuccessfulReport;
expect(newLastSuccessfulReport).toEqual(expect.any(Date));
expect(newLastSuccessfulReport).toEqual(expect.any(String));
expect(newLastSuccessfulReport).not.toEqual(taskInstance.state.lastSuccessfulReport);
});

it('should set lastSuccessfulReport correctly if no usage records found', async () => {
meteringCallbackMock.mockResolvedValueOnce([]);
const taskInstance = buildMockTaskInstance({ state: { lastSuccessfulReport: null } });
const task = await runTask(taskInstance);
const newLastSuccessfulReport = task?.state.lastSuccessfulReport;
expect(newLastSuccessfulReport).toEqual(expect.any(String));
expect(newLastSuccessfulReport).not.toEqual(taskInstance.state.lastSuccessfulReport);
});

Expand All @@ -213,23 +222,31 @@ describe('SecurityUsageReportingTask', () => {
});

it('should set lastSuccessfulReport correctly', async () => {
const lastSuccessfulReport = new Date(new Date().setMinutes(-15));
const lastSuccessfulReport = new Date(new Date().setMinutes(-15)).toISOString();
const taskInstance = buildMockTaskInstance({ state: { lastSuccessfulReport } });
const task = await runTask(taskInstance);
const newLastSuccessfulReport = task?.state.lastSuccessfulReport as Date;
const newLastSuccessfulReport = task?.state.lastSuccessfulReport;

expect(newLastSuccessfulReport).toEqual(taskInstance.state.lastSuccessfulReport);
});

it('should set lastSuccessfulReport correctly if previously null', async () => {
const taskInstance = buildMockTaskInstance({ state: { lastSuccessfulReport: null } });
const task = await runTask(taskInstance);
const newLastSuccessfulReport = task?.state.lastSuccessfulReport;

expect(newLastSuccessfulReport).toEqual(expect.any(String));
});

describe('and lookBackLimitMinutes is set', () => {
it('should limit lastSuccessfulReport if past threshold', async () => {
taskArgs = buildTaskArgs({ options: { lookBackLimitMinutes: 5 } });
mockTask = new SecurityUsageReportingTask(taskArgs);

const lastSuccessfulReport = new Date(new Date().setMinutes(-30));
const lastSuccessfulReport = new Date(new Date().setMinutes(-30)).toISOString();
const taskInstance = buildMockTaskInstance({ state: { lastSuccessfulReport } });
const task = await runTask(taskInstance, 1);
const newLastSuccessfulReport = task?.state.lastSuccessfulReport as Date;
const newLastSuccessfulReport = new Date(task?.state.lastSuccessfulReport as string);

// should be ~5 minutes so asserting between 4-6 minutes ago
const sixMinutesAgo = new Date().setMinutes(-6);
Expand All @@ -242,10 +259,10 @@ describe('SecurityUsageReportingTask', () => {
taskArgs = buildTaskArgs({ options: { lookBackLimitMinutes: 30 } });
mockTask = new SecurityUsageReportingTask(taskArgs);

const lastSuccessfulReport = new Date(new Date().setMinutes(-15));
const lastSuccessfulReport = new Date(new Date().setMinutes(-15)).toISOString();
const taskInstance = buildMockTaskInstance({ state: { lastSuccessfulReport } });
const task = await runTask(taskInstance, 1);
const newLastSuccessfulReport = task?.state.lastSuccessfulReport as Date;
const newLastSuccessfulReport = task?.state.lastSuccessfulReport;

expect(newLastSuccessfulReport).toEqual(taskInstance.state.lastSuccessfulReport);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ export class SecurityUsageReportingTask {
const [{ elasticsearch }] = await core.getStartServices();
const esClient = elasticsearch.client.asInternalUser;

const lastSuccessfulReport = taskInstance.state.lastSuccessfulReport;
const lastSuccessfulReport =
taskInstance.state.lastSuccessfulReport && new Date(taskInstance.state.lastSuccessfulReport);

let usageRecords: UsageRecord[] = [];
// save usage record query time so we can use it to know where
Expand Down Expand Up @@ -168,14 +169,13 @@ export class SecurityUsageReportingTask {
}

const state = {
lastSuccessfulReport:
usageReportResponse?.status === 201
? meteringCallbackTime
: this.getFailedLastSuccessfulReportTime(
meteringCallbackTime,
taskInstance.state.lastSuccessfulReport,
lookBackLimitMinutes
),
lastSuccessfulReport: this.shouldUpdateLastSuccessfulReport(usageRecords, usageReportResponse)
? meteringCallbackTime.toISOString()
: this.getFailedLastSuccessfulReportTime(
meteringCallbackTime,
lastSuccessfulReport,
lookBackLimitMinutes
).toISOString(),
};
return { state };
};
Expand Down Expand Up @@ -203,6 +203,13 @@ export class SecurityUsageReportingTask {
return lookBackLimitTime;
}

private shouldUpdateLastSuccessfulReport(
usageRecords: UsageRecord[],
usageReportResponse: Response | undefined
): boolean {
return !usageRecords.length || usageReportResponse?.status === 201;
}

private get taskId() {
return `${this.taskType}:${this.version}`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ export default function ({ getService }: FtrProviderContext) {
describe('forecasts', function () {
this.tags(['ml']);

// FLAKY: https://github.com/elastic/kibana/issues/164381
describe.skip('with single metric job', function () {
describe('with single metric job', function () {
before(async () => {
await esArchiver.loadIfNeeded('x-pack/test/functional/es_archives/ml/farequote');
await ml.testResources.createIndexPatternIfNeeded('ft_farequote', '@timestamp');
Expand Down
21 changes: 15 additions & 6 deletions x-pack/test/functional/services/ml/forecast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { FtrProviderContext } from '../../ftr_provider_context';

export function MachineLearningForecastProvider({ getService }: FtrProviderContext) {
const testSubjects = getService('testSubjects');
const retry = getService('retry');

return {
async assertForecastButtonExists() {
Expand All @@ -32,14 +33,22 @@ export function MachineLearningForecastProvider({ getService }: FtrProviderConte
},

async assertForecastChartElementsExists() {
await testSubjects.existOrFail(`mlForecastArea`, {
timeout: 30 * 1000,
await retry.tryForTime(3000, async () => {
await testSubjects.existOrFail(`mlForecastArea`, {
timeout: 30 * 1000,
});
});
await testSubjects.existOrFail(`mlForecastValuesline`, {
timeout: 30 * 1000,

await retry.tryForTime(3000, async () => {
await testSubjects.existOrFail(`mlForecastValuesline`, {
timeout: 30 * 1000,
});
});
await testSubjects.existOrFail(`mlForecastMarkers`, {
timeout: 30 * 1000,

await retry.tryForTime(3000, async () => {
await testSubjects.existOrFail(`mlForecastMarkers`, {
timeout: 30 * 1000,
});
});
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,33 @@ import { login } from '../../../tasks/login';
import { visitWithTimeRange } from '../../../tasks/navigation';
import { ALERTS_URL } from '../../../urls/navigation';

describe(
'Alerts Table Action column',
{ tags: ['@ess', '@serverless', '@brokenInServerless'] },
() => {
before(() => {
cleanKibana();
cy.task('esArchiverLoad', {
archiveName: 'process_ancestry',
useCreate: true,
docsOnly: true,
});
describe('Alerts Table Action column', { tags: ['@ess', '@serverless'] }, () => {
before(() => {
cleanKibana();
cy.task('esArchiverLoad', {
archiveName: 'process_ancestry',
useCreate: true,
docsOnly: true,
});
});

beforeEach(() => {
login();
visitWithTimeRange(ALERTS_URL);
waitForAlertsToPopulate();
});
beforeEach(() => {
login();
visitWithTimeRange(ALERTS_URL);
waitForAlertsToPopulate();
});

after(() => {
cy.task('esArchiverUnload', 'process_ancestry');
});
after(() => {
cy.task('esArchiverUnload', 'process_ancestry');
});

it('should have session viewer button visible & open session viewer on click', () => {
openSessionViewerFromAlertTable();
cy.get(OVERLAY_CONTAINER).should('be.visible');
});
it('should have session viewer button visible & open session viewer on click', () => {
openSessionViewerFromAlertTable();
cy.get(OVERLAY_CONTAINER).should('be.visible');
});

it('should have analyzer button visible & open analyzer on click', () => {
openAnalyzerForFirstAlertInTimeline();
cy.get(OVERLAY_CONTAINER).should('be.visible');
});
}
);
it('should have analyzer button visible & open analyzer on click', () => {
openAnalyzerForFirstAlertInTimeline();
cy.get(OVERLAY_CONTAINER).should('be.visible');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
switchAlertTableToGridView,
waitForAlerts,
} from '../../../tasks/alerts';
import { navigateFromHeaderTo } from '../../../tasks/security_header';
import { FIELDS_BROWSER_BTN } from '../../../screens/rule_details';
import {
addsFields,
Expand All @@ -32,9 +31,8 @@ import { cleanKibana } from '../../../tasks/common';
import { waitForAlertsToPopulate } from '../../../tasks/create_new_rule';
import { login } from '../../../tasks/login';
import { visit } from '../../../tasks/navigation';
import { ALERTS_URL } from '../../../urls/navigation';
import { ALERTS_URL, TIMELINES_URL } from '../../../urls/navigation';
import { DATAGRID_HEADER } from '../../../screens/timeline';
import { TIMELINES, ALERTS } from '../../../screens/security_header';

/*
*
Expand All @@ -43,7 +41,7 @@ import { TIMELINES, ALERTS } from '../../../screens/security_header';
*
* */

describe(`Alert Table Controls`, () => {
describe(`Alert Table Controls`, { tags: ['@ess', '@serverless'] }, () => {
before(() => {
cleanKibana();
});
Expand Down Expand Up @@ -102,8 +100,8 @@ describe(`Alert Table Controls`, () => {
closeFieldsBrowser();
cy.get(DATAGRID_HEADER(fieldName)).should('not.exist');

navigateFromHeaderTo(TIMELINES);
navigateFromHeaderTo(ALERTS);
visit(TIMELINES_URL);
visit(ALERTS_URL);
waitForAlerts();
cy.get(DATAGRID_HEADER('_id')).should('not.exist');
});
Expand All @@ -115,8 +113,8 @@ describe(`Alert Table Controls`, () => {
closeFieldsBrowser();
cy.get(DATAGRID_HEADER('_id')).should('be.visible');

navigateFromHeaderTo(TIMELINES);
navigateFromHeaderTo(ALERTS);
visit(TIMELINES_URL);
visit(ALERTS_URL);
waitForAlerts();
cy.get(DATAGRID_HEADER('_id')).should('be.visible');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import { waitForAlertsToPopulate } from '../../../tasks/create_new_rule';
import { login } from '../../../tasks/login';
import { visit } from '../../../tasks/navigation';
import {
clearKqlQueryBar,
fillAddFilterForm,
fillKqlQueryBar,
openAddFilterPopover,
Expand All @@ -38,7 +37,7 @@ import { openActiveTimeline } from '../../../tasks/timeline';

import { ALERTS_URL } from '../../../urls/navigation';

describe('Alerts cell actions', { tags: ['@ess', '@brokenInServerless'] }, () => {
describe('Alerts cell actions', { tags: ['@ess', '@serverless'] }, () => {
before(() => {
cleanKibana();
createRule(getNewRule());
Expand Down Expand Up @@ -70,8 +69,6 @@ describe('Alerts cell actions', { tags: ['@ess', '@brokenInServerless'] }, () =>
filterForAlertProperty(ALERT_TABLE_FILE_NAME_VALUES, 0);

cy.get(FILTER_BADGE).first().should('have.text', 'NOT file.name: exists');

clearKqlQueryBar();
});

it('should filter out a non-empty property', () => {
Expand All @@ -95,8 +92,6 @@ describe('Alerts cell actions', { tags: ['@ess', '@brokenInServerless'] }, () =>
filterOutAlertProperty(ALERT_TABLE_FILE_NAME_VALUES, 0);

cy.get(FILTER_BADGE).first().should('have.text', 'file.name: exists');

clearKqlQueryBar();
});
});

Expand Down
Loading

0 comments on commit 7ddbd5c

Please sign in to comment.