forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fleet] Allow some agent actions for managed policies (elastic#155923)
- Loading branch information
Showing
5 changed files
with
381 additions
and
62 deletions.
There are no files selected for viewing
168 changes: 168 additions & 0 deletions
168
...lic/applications/fleet/sections/agents/agent_details_page/components/action_menu.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { fireEvent } from '@testing-library/dom'; | ||
|
||
import { createFleetTestRendererMock } from '../../../../../../mock'; | ||
import type { Agent, AgentPolicy } from '../../../../types'; | ||
import { ExperimentalFeaturesService } from '../../../../services'; | ||
import { useAuthz } from '../../../../../../hooks/use_authz'; | ||
|
||
import { AgentDetailsActionMenu } from './actions_menu'; | ||
|
||
jest.mock('../../../../../../hooks/use_fleet_status', () => ({ | ||
FleetStatusProvider: (props: any) => { | ||
return props.children; | ||
}, | ||
})); | ||
|
||
jest.mock('../../../../../../services/experimental_features'); | ||
jest.mock('../../../../../../hooks/use_authz'); | ||
|
||
const mockedExperimentalFeaturesService = jest.mocked(ExperimentalFeaturesService); | ||
const mockedUseAuthz = jest.mocked(useAuthz); | ||
|
||
function renderActions({ agent, agentPolicy }: { agent: Agent; agentPolicy?: AgentPolicy }) { | ||
const renderer = createFleetTestRendererMock(); | ||
|
||
const utils = renderer.render( | ||
<AgentDetailsActionMenu | ||
agent={agent} | ||
agentPolicy={agentPolicy} | ||
assignFlyoutOpenByDefault={false} | ||
onCancelReassign={jest.fn()} | ||
/> | ||
); | ||
|
||
fireEvent.click(utils.getByRole('button')); | ||
|
||
return { utils }; | ||
} | ||
|
||
describe('AgentDetailsActionMenu', () => { | ||
beforeEach(() => { | ||
mockedExperimentalFeaturesService.get.mockReturnValue({ | ||
diagnosticFileUploadEnabled: true, | ||
} as any); | ||
mockedUseAuthz.mockReturnValue({ | ||
fleet: { | ||
all: true, | ||
}, | ||
} as any); | ||
}); | ||
|
||
describe('Request Diagnotics action', () => { | ||
function renderAndGetDiagnosticsButton({ | ||
agent, | ||
agentPolicy, | ||
}: { | ||
agent: Agent; | ||
agentPolicy?: AgentPolicy; | ||
}) { | ||
const { utils } = renderActions({ | ||
agent, | ||
agentPolicy, | ||
}); | ||
|
||
return utils.queryByTestId('requestAgentDiagnosticsBtn'); | ||
} | ||
|
||
it('should not render action if feature is disabled', async () => { | ||
mockedExperimentalFeaturesService.get.mockReturnValue({ | ||
diagnosticFileUploadEnabled: false, | ||
} as any); | ||
const res = renderAndGetDiagnosticsButton({ | ||
agent: {} as Agent, | ||
agentPolicy: {} as AgentPolicy, | ||
}); | ||
expect(res).toBe(null); | ||
}); | ||
|
||
it('should render an active action button if agent version >= 8.7', async () => { | ||
const res = renderAndGetDiagnosticsButton({ | ||
agent: { | ||
status: 'online', | ||
local_metadata: { elastic: { agent: { version: '8.8.0' } } }, | ||
} as any, | ||
agentPolicy: {} as AgentPolicy, | ||
}); | ||
|
||
expect(res).not.toBe(null); | ||
expect(res).toBeEnabled(); | ||
}); | ||
|
||
it('should render an active action button if agent version >= 8.7 and policy is_managed', async () => { | ||
const res = renderAndGetDiagnosticsButton({ | ||
agent: { | ||
status: 'online', | ||
local_metadata: { elastic: { agent: { version: '8.8.0' } } }, | ||
} as any, | ||
agentPolicy: { | ||
is_managed: true, | ||
} as AgentPolicy, | ||
}); | ||
|
||
expect(res).not.toBe(null); | ||
expect(res).toBeEnabled(); | ||
}); | ||
|
||
it('should render a disabled action button if agent version < 8.7', async () => { | ||
const res = renderAndGetDiagnosticsButton({ | ||
agent: { | ||
status: 'online', | ||
local_metadata: { elastic: { agent: { version: '8.6.0' } } }, | ||
} as any, | ||
agentPolicy: { | ||
is_managed: true, | ||
} as AgentPolicy, | ||
}); | ||
|
||
expect(res).not.toBe(null); | ||
expect(res).not.toBeEnabled(); | ||
}); | ||
}); | ||
|
||
describe('View agent JSON action', () => { | ||
function renderAndGetViewJSONButton({ | ||
agent, | ||
agentPolicy, | ||
}: { | ||
agent: Agent; | ||
agentPolicy?: AgentPolicy; | ||
}) { | ||
const { utils } = renderActions({ | ||
agent, | ||
agentPolicy, | ||
}); | ||
|
||
return utils.queryByTestId('viewAgentDetailsJsonBtn'); | ||
} | ||
|
||
it('should render an active button', async () => { | ||
const res = renderAndGetViewJSONButton({ | ||
agent: {} as any, | ||
agentPolicy: {} as AgentPolicy, | ||
}); | ||
|
||
expect(res).not.toBe(null); | ||
expect(res).toBeEnabled(); | ||
}); | ||
|
||
it('should render an active button for managed agent policy', async () => { | ||
const res = renderAndGetViewJSONButton({ | ||
agent: {} as any, | ||
agentPolicy: { | ||
is_managed: true, | ||
} as AgentPolicy, | ||
}); | ||
|
||
expect(res).not.toBe(null); | ||
expect(res).toBeEnabled(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.