Skip to content

Commit

Permalink
More tests + refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-shelkovnikov committed Dec 25, 2024
1 parent 7c31a15 commit 0500e73
Show file tree
Hide file tree
Showing 5 changed files with 635 additions and 129 deletions.
13 changes: 8 additions & 5 deletions x-pack/plugins/search_connectors/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,14 @@ export class SearchConnectorsPlugin
// There seems to be no way to check for agentless here
// So we register a task, but do not execute it
this.log.debug('Registering agentless connectors infra sync task');
this.agentlessConnectorDeploymentsSyncService.registerInfraSyncTask(
this.config,
plugins,
coreStartServices
);

coreStartServices.then(([coreStart, searchConnectorsPluginStartDependencies]) => {
this.agentlessConnectorDeploymentsSyncService.registerInfraSyncTask(
plugins,
coreStart,
searchConnectorsPluginStartDependencies
);
});

return {
getConnectorTypes: () => this.connectors,
Expand Down
259 changes: 240 additions & 19 deletions x-pack/plugins/search_connectors/server/services/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@ import {
ElasticsearchClientMock,
elasticsearchClientMock,
} from '@kbn/core-elasticsearch-client-server-mocks';
import { AgentlessConnectorsInfraService } from '.';
import {
AgentlessConnectorsInfraService,
ConnectorMetadata,
PackagePolicyMetadata,
getConnectorsWithoutPolicies,
getPoliciesWithoutConnectors,
} from '.';
import { savedObjectsClientMock } from '@kbn/core/server/mocks';
import { MockedLogger, loggerMock } from '@kbn/logging-mocks';
import {
createPackagePolicyServiceMock,
createMockAgentPolicyService,
} from '@kbn/fleet-plugin/server/mocks';
import { AgentPolicyServiceInterface, PackagePolicyClient } from '@kbn/fleet-plugin/server';
import { PackagePolicy, PackagePolicyInput } from '@kbn/fleet-plugin/common';
import { createPackagePolicyMock } from '@kbn/fleet-plugin/common/mocks';
import { AgentPolicy, PackagePolicy, PackagePolicyInput } from '@kbn/fleet-plugin/common';
import { createAgentPolicyMock, createPackagePolicyMock } from '@kbn/fleet-plugin/common/mocks';

describe('AgentlessConnectorsInfraService', () => {
let soClient: SavedObjectsClientContract;
Expand Down Expand Up @@ -118,7 +124,6 @@ describe('AgentlessConnectorsInfraService', () => {
expect(nativeConnectors[1].service_type).toBe(mockResult.results[1].service_type);
});
});

describe('getConnectorPackagePolicies', () => {
const getMockPolicyFetchAllItems = (pages: PackagePolicy[][]) => {
return {
Expand Down Expand Up @@ -164,17 +169,17 @@ describe('AgentlessConnectorsInfraService', () => {
const policies = await service.getConnectorPackagePolicies();

expect(policies.length).toBe(1);
expect(policies[0].packagePolicyId).toBe(firstPackagePolicy.id);
expect(policies[0].connectorMetadata.id).toBe(
expect(policies[0].package_policy_id).toBe(firstPackagePolicy.id);
expect(policies[0].connector_metadata.id).toBe(
firstPackagePolicy.inputs[0].compiled_input.connector_id
);
expect(policies[0].connectorMetadata.name).toBe(
expect(policies[0].connector_metadata.name).toBe(
firstPackagePolicy.inputs[0].compiled_input.connector_name
);
expect(policies[0].connectorMetadata.service_type).toBe(
expect(policies[0].connector_metadata.service_type).toBe(
firstPackagePolicy.inputs[0].compiled_input.service_type
);
expect(policies[0].agentPolicyIds).toBe(firstPackagePolicy.policy_ids);
expect(policies[0].agent_policy_ids).toBe(firstPackagePolicy.policy_ids);
});

test('Lists policies if they are returned over multiple pages', async () => {
Expand Down Expand Up @@ -215,29 +220,29 @@ describe('AgentlessConnectorsInfraService', () => {
const policies = await service.getConnectorPackagePolicies();

expect(policies.length).toBe(2);
expect(policies[0].packagePolicyId).toBe(firstPackagePolicy.id);
expect(policies[0].connectorMetadata.id).toBe(
expect(policies[0].package_policy_id).toBe(firstPackagePolicy.id);
expect(policies[0].connector_metadata.id).toBe(
firstPackagePolicy.inputs[0].compiled_input.connector_id
);
expect(policies[0].connectorMetadata.name).toBe(
expect(policies[0].connector_metadata.name).toBe(
firstPackagePolicy.inputs[0].compiled_input.connector_name
);
expect(policies[0].connectorMetadata.service_type).toBe(
expect(policies[0].connector_metadata.service_type).toBe(
firstPackagePolicy.inputs[0].compiled_input.service_type
);
expect(policies[0].agentPolicyIds).toBe(firstPackagePolicy.policy_ids);
expect(policies[0].agent_policy_ids).toBe(firstPackagePolicy.policy_ids);

expect(policies[1].packagePolicyId).toBe(thirdPackagePolicy.id);
expect(policies[1].connectorMetadata.id).toBe(
expect(policies[1].package_policy_id).toBe(thirdPackagePolicy.id);
expect(policies[1].connector_metadata.id).toBe(
thirdPackagePolicy.inputs[0].compiled_input.connector_id
);
expect(policies[1].connectorMetadata.name).toBe(
expect(policies[1].connector_metadata.name).toBe(
thirdPackagePolicy.inputs[0].compiled_input.connector_name
);
expect(policies[1].connectorMetadata.service_type).toBe(
expect(policies[1].connector_metadata.service_type).toBe(
thirdPackagePolicy.inputs[0].compiled_input.service_type
);
expect(policies[1].agentPolicyIds).toBe(thirdPackagePolicy.policy_ids);
expect(policies[1].agent_policy_ids).toBe(thirdPackagePolicy.policy_ids);
});

test('Skips policies that have missing fields', async () => {
Expand Down Expand Up @@ -274,6 +279,27 @@ describe('AgentlessConnectorsInfraService', () => {
});
});
describe('deployConnector', () => {
let agentPolicy: AgentPolicy;
let sharepointOnlinePackagePolicy: PackagePolicy;

beforeAll(() => {
agentPolicy = createAgentPolicyMock();

sharepointOnlinePackagePolicy = createPackagePolicyMock();
sharepointOnlinePackagePolicy.id = 'this-is-package-policy-id';
sharepointOnlinePackagePolicy.policy_ids = ['this-is-agent-policy-id'];
sharepointOnlinePackagePolicy.inputs = [
{
type: 'connectors-py',
compiled_input: {
connector_id: '00000001',
connector_name: 'Sharepoint Online Production Connector',
service_type: 'sharepoint_online',
},
} as PackagePolicyInput,
];
});

test('Raises an error if connector.id is missing', async () => {
const connector = {
id: '',
Expand Down Expand Up @@ -348,6 +374,7 @@ describe('AgentlessConnectorsInfraService', () => {
};
const errorMessage = 'Failed to create a package policy hehe';

agentPolicyInterface.create.mockResolvedValue(agentPolicy);
packagePolicyService.create.mockImplementation(() => {
throw new Error(errorMessage);
});
Expand All @@ -359,5 +386,199 @@ describe('AgentlessConnectorsInfraService', () => {
expect(e.message).toEqual(errorMessage);
}
});

test('Returns a created package policy when all goes well', async () => {
const connector = {
id: '000000001',
name: 'something',
service_type: 'github',
};

agentPolicyInterface.create.mockResolvedValue(agentPolicy);
packagePolicyService.create.mockResolvedValue(sharepointOnlinePackagePolicy);

const result = await service.deployConnector(connector);
expect(result).toBe(sharepointOnlinePackagePolicy);
});
});
describe('removeDeployment', () => {
const packagePolicyId = 'this-is-package-policy-id';
const agentPolicyId = 'this-is-agent-policy-id';
let sharepointOnlinePackagePolicy: PackagePolicy;

beforeAll(() => {
sharepointOnlinePackagePolicy = createPackagePolicyMock();
sharepointOnlinePackagePolicy.id = packagePolicyId;
sharepointOnlinePackagePolicy.policy_ids = [agentPolicyId];
sharepointOnlinePackagePolicy.inputs = [
{
type: 'connectors-py',
compiled_input: {
connector_id: '00000001',
connector_name: 'Sharepoint Online Production Connector',
service_type: 'sharepoint_online',
},
} as PackagePolicyInput,
];
});

test('Calls for deletion of both agent policy and package policy', async () => {
packagePolicyService.get.mockResolvedValue(sharepointOnlinePackagePolicy);

await service.removeDeployment(packagePolicyId);

expect(agentPolicyInterface.delete).toBeCalledWith(soClient, esClient, agentPolicyId);
expect(packagePolicyService.delete).toBeCalledWith(soClient, esClient, [packagePolicyId]);
});

test('Raises an error if deletion of agent policy failed', async () => {
packagePolicyService.get.mockResolvedValue(sharepointOnlinePackagePolicy);

const errorMessage = 'Failed to create a package policy hehe';

agentPolicyInterface.delete.mockImplementation(() => {
throw new Error(errorMessage);
});

try {
await service.removeDeployment(packagePolicyId);
expect(true).toBe(false);
} catch (e) {
expect(e.message).toEqual(errorMessage);
}
});

test('Raises an error if deletion of package policy failed', async () => {
packagePolicyService.get.mockResolvedValue(sharepointOnlinePackagePolicy);

const errorMessage = 'Failed to create a package policy hehe';

packagePolicyService.delete.mockImplementation(() => {
throw new Error(errorMessage);
});

try {
await service.removeDeployment(packagePolicyId);
expect(true).toBe(false);
} catch (e) {
expect(e.message).toEqual(errorMessage);
}
});

test('Raises an error if a policy is not found', async () => {
packagePolicyService.get.mockResolvedValue(null);

try {
await service.removeDeployment(packagePolicyId);
expect(true).toBe(false);
} catch (e) {
expect(e.message).toContain('Failed to delete policy');
expect(e.message).toContain(packagePolicyId);
}
});
});
});

describe('module', () => {
const githubConnector: ConnectorMetadata = {
id: '000001',
name: 'Github Connector',
service_type: 'github',
};

const sharepointConnector: ConnectorMetadata = {
id: '000002',
name: 'Sharepoint Connector',
service_type: 'sharepoint_online',
};

const mysqlConnector: ConnectorMetadata = {
id: '000003',
name: 'MySQL Connector',
service_type: 'mysql',
};

const githubPackagePolicy: PackagePolicyMetadata = {
package_policy_id: 'agent-001',
agent_policy_ids: ['agent-package-001'],
connector_metadata: githubConnector,
};

const sharepointPackagePolicy: PackagePolicyMetadata = {
package_policy_id: 'agent-002',
agent_policy_ids: ['agent-package-002'],
connector_metadata: sharepointConnector,
};

const mysqlPackagePolicy: PackagePolicyMetadata = {
package_policy_id: 'agent-003',
agent_policy_ids: ['agent-package-003'],
connector_metadata: mysqlConnector,
};

describe('getPoliciesWithoutConnectors', () => {
test('Returns a missing policy if one is missing', async () => {
const missingPolicies = getPoliciesWithoutConnectors(
[githubPackagePolicy, sharepointPackagePolicy, mysqlPackagePolicy],
[githubConnector, sharepointConnector]
);

expect(missingPolicies.length).toBe(1);
expect(missingPolicies).toContain(mysqlPackagePolicy);
});

test('Returns empty array if no policies are missing', async () => {
const missingPolicies = getPoliciesWithoutConnectors(
[githubPackagePolicy, sharepointPackagePolicy, mysqlPackagePolicy],
[githubConnector, sharepointConnector, mysqlConnector]
);

expect(missingPolicies.length).toBe(0);
});

test('Returns all policies if all are missing', async () => {
const missingPolicies = getPoliciesWithoutConnectors(
[githubPackagePolicy, sharepointPackagePolicy, mysqlPackagePolicy],
[]
);

expect(missingPolicies.length).toBe(3);
expect(missingPolicies).toContain(githubPackagePolicy);
expect(missingPolicies).toContain(sharepointPackagePolicy);
expect(missingPolicies).toContain(mysqlPackagePolicy);
});
});

describe('getConnectorsWithoutPolicies', () => {
test('Returns a missing policy if one is missing', async () => {
const missingConnectors = getConnectorsWithoutPolicies(
[githubPackagePolicy, sharepointPackagePolicy],
[githubConnector, sharepointConnector, mysqlConnector]
);

expect(missingConnectors.length).toBe(1);
expect(missingConnectors).toContain(mysqlConnector);
});

test('Returns empty array if no policies are missing', async () => {
const missingConnectors = getConnectorsWithoutPolicies(
[githubPackagePolicy, sharepointPackagePolicy, mysqlPackagePolicy],
[githubConnector, sharepointConnector, mysqlConnector]
);

expect(missingConnectors.length).toBe(0);
});

test('Returns all policies if all are missing', async () => {
const missingConnectors = getConnectorsWithoutPolicies(
[],
[githubConnector, sharepointConnector, mysqlConnector]
);

expect(missingConnectors.length).toBe(3);
expect(missingConnectors).toContain(githubConnector);
expect(missingConnectors).toContain(sharepointConnector);
expect(missingConnectors).toContain(mysqlConnector);
});
});
});
Loading

0 comments on commit 0500e73

Please sign in to comment.