Skip to content

Commit

Permalink
Update / Add tests for exclude event data
Browse files Browse the repository at this point in the history
  • Loading branch information
lucabelluccini committed Aug 22, 2024
1 parent c5411ef commit 1d53ea8
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ describe('AgentRequestDiagnosticsModal', () => {

expect(mockSendPostRequestDiagnostics).toHaveBeenCalledWith('agent1', {
additional_metrics: ['CPU'],
exclude_events_log: true,
});
});

Expand All @@ -70,6 +71,23 @@ describe('AgentRequestDiagnosticsModal', () => {

expect(mockSendPostRequestDiagnostics).toHaveBeenCalledWith('agent1', {
additional_metrics: [],
exclude_events_log: true,
});
});

it('should have Exclude Events Log set to false when checkbox is checked', async () => {
const { utils } = render();

act(() => {
fireEvent.click(utils.getByTestId('includeEventsLogCheckbox'));
});
act(() => {
fireEvent.click(utils.getByTestId('confirmModalConfirmButton'));
});

expect(mockSendPostRequestDiagnostics).toHaveBeenCalledWith('agent1', {
additional_metrics: [],
exclude_events_log: false,
});
});

Expand All @@ -86,6 +104,7 @@ describe('AgentRequestDiagnosticsModal', () => {
expect(mockSendPostBulkRequestDiagnostics).toHaveBeenCalledWith({
additional_metrics: ['CPU'],
agents: ['agent1', 'agent2'],
exclude_events_log: true,
});
});

Expand All @@ -98,9 +117,25 @@ describe('AgentRequestDiagnosticsModal', () => {

expect(mockSendPostBulkRequestDiagnostics).toHaveBeenCalledWith({
additional_metrics: [],
exclude_events_log: true,
agents: ['agent1', 'agent2'],
});
});

// TODO Create test
it('should have Exclude Events Log set to false when checkbox is checked within bulk action', async () => {
const { utils } = render({ agents: [{ id: 'agent1' }, { id: 'agent2' }], agentCount: 2 });

act(() => {
fireEvent.click(utils.getByTestId('includeEventsLogCheckbox'));
});
act(() => {
fireEvent.click(utils.getByTestId('confirmModalConfirmButton'));
});

expect(mockSendPostBulkRequestDiagnostics).toHaveBeenCalledWith({
additional_metrics: [],
exclude_events_log: false,
agents: ['agent1', 'agent2'],
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -133,24 +133,20 @@ export const AgentRequestDiagnosticsModal: React.FunctionComponent<Props> = ({
defaultMessage="Diagnostics files are stored in Elasticsearch, and as such can incur storage costs. By default, files are periodically deleted via an ILM policy."
/>
</p>
<p>
<EuiCheckbox
id="cpuMetricsCheckbox"
data-test-subj="cpuMetricsCheckbox"
label="Collect additional CPU metrics"
checked={cpuMetricsEnabled}
onChange={() => setCPUMetricsEnabled(!cpuMetricsEnabled)}
/>
</p>
<p>
<EuiCheckbox
id="includeEventsLogCheckbox"
data-test-subj="includeEventsLogCheckbox"
label="Include Events Logs (might contain sensible information)"
checked={includeEventsLogEnabled}
onChange={() => setIncludeEventsLog(!includeEventsLogEnabled)}
/>
</p>
<EuiCheckbox
id="cpuMetricsCheckbox"
data-test-subj="cpuMetricsCheckbox"
label="Collect additional CPU metrics"
checked={cpuMetricsEnabled}
onChange={() => setCPUMetricsEnabled(!cpuMetricsEnabled)}
/>
<EuiCheckbox
id="includeEventsLogCheckbox"
data-test-subj="includeEventsLogCheckbox"
label="Include Events Logs (might contain sensible information)"
checked={includeEventsLogEnabled}
onChange={() => setIncludeEventsLog(!includeEventsLogEnabled)}
/>
</EuiConfirmModal>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('request diagnostics handler', () => {
let mockRequest: KibanaRequest<
{ agentId: string },
undefined,
{ additional_metrics: RequestDiagnosticsAdditionalMetrics[] },
{ additional_metrics: RequestDiagnosticsAdditionalMetrics[]; exclude_events_log?: boolean },
any
>;

Expand All @@ -59,7 +59,7 @@ describe('request diagnostics handler', () => {
} as unknown as RequestHandlerContext;
mockRequest = httpServerMock.createKibanaRequest({
params: { agentId: 'agent1' },
body: { additional_metrics: ['CPU'] },
body: { additional_metrics: ['CPU'], exclude_events_log: false },
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,85 @@ export default function (providerContext: FtrProviderContext) {
const action: any = actionsRes.hits.hits[0]._source;
expect(action.data.additional_metrics).contain('CPU');
});

it('should create action with exclude_events_log when api contains exclude_events_log option', async () => {
await supertest
.post(`/api/fleet/agents/agent1/request_diagnostics`)
.set('kbn-xsrf', 'xxx')
.send({
exclude_events_log: false,
})
.expect(200);
const actionsRes = await es.search({
index: '.fleet-actions',
body: {
sort: [{ '@timestamp': { order: 'desc' } }],
},
});
const action: any = actionsRes.hits.hits[0]._source;
expect(action.data.exclude_events_log).equal(false);
});

it('/agents/bulk_request_diagnostics should add exclude_events_log option to action doc', async () => {
await supertestWithoutAuth
.post(`/api/fleet/agents/bulk_request_diagnostics`)
.set('kbn-xsrf', 'xxx')
.auth(testUsers.fleet_agents_read_only.username, testUsers.fleet_agents_read_only.password)
.send({
agents: ['agent2', 'agent3'],
exclude_events_log: true,
});

const actionsRes = await es.search({
index: '.fleet-actions',
body: {
sort: [{ '@timestamp': { order: 'desc' } }],
},
});
const action: any = actionsRes.hits.hits[0]._source;
expect(action.data.exclude_events_log).equal(true);
});

it('should create action with exclude_events_log when api contains exclude_events_log and collect CPU option', async () => {
await supertest
.post(`/api/fleet/agents/agent1/request_diagnostics`)
.set('kbn-xsrf', 'xxx')
.send({
additional_metrics: ['CPU'],
exclude_events_log: false,
})
.expect(200);
const actionsRes = await es.search({
index: '.fleet-actions',
body: {
sort: [{ '@timestamp': { order: 'desc' } }],
},
});
const action: any = actionsRes.hits.hits[0]._source;
expect(action.data.exclude_events_log).equal(false);
expect(action.data.additional_metrics).contain('CPU');
});

it('/agents/bulk_request_diagnostics should add exclude_events_log and collect CPU option to action doc', async () => {
await supertestWithoutAuth
.post(`/api/fleet/agents/bulk_request_diagnostics`)
.set('kbn-xsrf', 'xxx')
.auth(testUsers.fleet_agents_read_only.username, testUsers.fleet_agents_read_only.password)
.send({
agents: ['agent2', 'agent3'],
additional_metrics: ['CPU'],
exclude_events_log: true,
});

const actionsRes = await es.search({
index: '.fleet-actions',
body: {
sort: [{ '@timestamp': { order: 'desc' } }],
},
});
const action: any = actionsRes.hits.hits[0]._source;
expect(action.data.exclude_events_log).equal(true);
expect(action.data.additional_metrics).contain('CPU');
});
});
}

// TODO Add test

0 comments on commit 1d53ea8

Please sign in to comment.