Skip to content

Commit

Permalink
test(pci-load-balancer): add unit tests
Browse files Browse the repository at this point in the history
ref: DTCORE-2770
Signed-off-by: Yoann Fievez <[email protected]>
  • Loading branch information
kqesar committed Oct 23, 2024
1 parent c34f680 commit 10c2db8
Show file tree
Hide file tree
Showing 41 changed files with 3,615 additions and 93 deletions.
5 changes: 4 additions & 1 deletion packages/manager/apps/pci-load-balancer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
"start": "lerna exec --stream --scope='@ovh-ux/manager-pci-load-balancer-app' --include-dependencies -- npm run build --if-present",
"start:dev": "lerna exec --stream --scope='@ovh-ux/manager-pci-load-balancer-app' --include-dependencies -- npm run dev --if-present",
"start:watch": "lerna exec --stream --parallel --scope='@ovh-ux/manager-pci-load-balancer-app' --include-dependencies -- npm run dev:watch --if-present",
"test": "vitest run"
"test": "vitest run",
"test:coverage": "vitest run --coverage",
"test:watch": "vitest",
"test:watch:coverage": "vitest --coverage"
},
"dependencies": {
"@ovh-ux/manager-config": "*",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { describe, it, expect, vi } from 'vitest';
import { v6 } from '@ovh-ux/manager-core-api';
import { TAddon } from '@/pages/create/store';
import { getFlavor } from './flavors';
import { TFlavor } from '@/api/data/load-balancer';

describe('getFlavor', () => {
const projectId = 'test-project';
const regionName = 'test-region';
const addon = ({
technicalName: 'test-flavor',
invoiceName: 'invoiceName',
} as unknown) as TAddon;

it('should return the correct flavor', async () => {
const mockFlavors: TFlavor[] = [
{ name: 'test-flavor', region: 'region1', id: 'id1' },
{ name: 'another-flavor', region: 'region2', id: 'id2' },
];

vi.mocked(v6.get).mockResolvedValue({ data: mockFlavors });

const result = await getFlavor(projectId, regionName, addon);

expect(result).toEqual(mockFlavors[0]);
});

it('should return undefined if the flavor is not found', async () => {
const mockFlavors: TFlavor[] = [
{ name: 'another-flavor', region: 'value2', id: 'id2' },
];

vi.mocked(v6.get).mockResolvedValue({ data: mockFlavors });

const result = await getFlavor(projectId, regionName, addon);

expect(result).toBeUndefined();
});

it('should handle API errors gracefully', async () => {
vi.mocked(v6.get).mockRejectedValue(new Error('API Error'));

await expect(getFlavor(projectId, regionName, addon)).rejects.toThrow(
'API Error',
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { describe, it, expect, vi } from 'vitest';
import { v6 } from '@ovh-ux/manager-core-api';
import { getFloatingIps, TFloatingIp } from './floating-ips';

describe('getFloatingIps', () => {
const projectId = 'test-project-id';
const region = 'test-region';
const mockFloatingIps: TFloatingIp[] = [
{
associatedEntity: 'entity1',
id: 'id1',
ip: '192.168.1.1',
networkId: 'network1',
status: 'active',
type: 'public',
},
{
associatedEntity: 'entity2',
id: 'id2',
ip: '192.168.1.2',
networkId: 'network2',
status: 'inactive',
type: 'private',
},
];

it('should fetch floating IPs for a given project and region', async () => {
vi.mocked(v6.get).mockResolvedValue({ data: mockFloatingIps });

const result = await getFloatingIps(projectId, region);

expect(v6.get).toHaveBeenCalledWith(
`/cloud/project/${projectId}/region/${region}/floatingip`,
);
expect(result).toEqual(mockFloatingIps);
});

it('should handle errors when fetching floating IPs', async () => {
const errorMessage = 'Network Error';
vi.mocked(v6.get).mockRejectedValue(new Error(errorMessage));

await expect(getFloatingIps(projectId, region)).rejects.toThrow(
errorMessage,
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { describe, it, expect, vi } from 'vitest';
import { v6 } from '@ovh-ux/manager-core-api';
import { getSubnetGateways, TSubnetGateway } from './gateways';

describe('getSubnetGateways', () => {
const projectId = 'test-project';
const regionName = 'test-region';
const subnetId = 'test-subnet';

const mockGateways: TSubnetGateway[] = [
{
externalInformation: {
ips: [{ ip: '192.168.1.1', subnetId: 'subnet-1' }],
networkId: 'network-1',
},
id: 'gateway-1',
interfaces: [
{
id: 'interface-1',
ip: '192.168.1.2',
networkId: 'network-1',
subnetId: 'subnet-1',
},
],
model: 'model-1',
name: 'gateway-name-1',
region: 'region-1',
status: 'ACTIVE',
},
];

it('should fetch subnet gateways and return data', async () => {
vi.mocked(v6.get).mockResolvedValueOnce({ data: mockGateways });

const result = await getSubnetGateways(projectId, regionName, subnetId);

expect(v6.get).toHaveBeenCalledWith(
`/cloud/project/${projectId}/region/${regionName}/gateway?subnetId=${subnetId}`,
);
expect(result).toEqual(mockGateways);
});

it('should handle empty response', async () => {
vi.mocked(v6.get).mockResolvedValueOnce({ data: [] });

const result = await getSubnetGateways(projectId, regionName, subnetId);

expect(v6.get).toHaveBeenCalledWith(
`/cloud/project/${projectId}/region/${regionName}/gateway?subnetId=${subnetId}`,
);
expect(result).toEqual([]);
});

it('should handle API errors', async () => {
const errorMessage = 'API Error';
vi.mocked(v6.get).mockRejectedValueOnce(new Error(errorMessage));

await expect(
getSubnetGateways(projectId, regionName, subnetId),
).rejects.toThrow(errorMessage);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { describe, it, expect, vi } from 'vitest';
import { v6 } from '@ovh-ux/manager-core-api';
import {
getHealthMonitor,
deleteHealthMonitor,
createHealthMonitor,
editHealthMonitor,
renameHealthMonitor,
THealthMonitorFormState,
} from './health-monitor';

describe('Health Monitor API', () => {
const projectId = 'test-project';
const region = 'test-region';
const poolId = 'test-pool';
const healthMonitorId = 'test-health-monitor';

it('should get health monitor', async () => {
const mockData = [{ id: '1', name: 'test-monitor' }];
vi.mocked(v6.get).mockResolvedValue({ data: mockData });

const result = await getHealthMonitor(projectId, region, poolId);
expect(result).toEqual(mockData);
expect(v6.get).toHaveBeenCalledWith(
`/cloud/project/${projectId}/region/${region}/loadbalancing/healthMonitor?poolId=${poolId}`,
);
});

it('should delete health monitor', async () => {
const mockData = { success: true };
vi.mocked(v6.delete).mockResolvedValue({ data: mockData });

const result = await deleteHealthMonitor(
projectId,
region,
healthMonitorId,
);
expect(result).toEqual(mockData);
expect(v6.delete).toHaveBeenCalledWith(
`/cloud/project/${projectId}/region/${region}/loadbalancing/healthMonitor/${healthMonitorId}`,
);
});

it('should create health monitor', async () => {
const model: THealthMonitorFormState = {
name: 'test-monitor',
type: 'http',
urlPath: '/health',
expectedCode: 200,
maxRetriesDown: 3,
delay: 5,
maxRetries: 3,
timeout: 10,
};
const mockData = { id: '1', name: 'test-monitor' };
(v6.post as any).mockResolvedValue({ data: mockData });

const result = await createHealthMonitor(projectId, region, poolId, model);
expect(result).toEqual({ data: mockData });
expect(v6.post).toHaveBeenCalledWith(
`/cloud/project/${projectId}/region/${region}/loadbalancing/healthMonitor`,
expect.objectContaining({
name: model.name,
monitorType: model.type,
delay: model.delay,
timeout: model.timeout,
poolId,
httpConfiguration: expect.objectContaining({
expectedCodes: `${model.expectedCode}`,
urlPath: model.urlPath,
}),
}),
);
});

it('should edit health monitor', async () => {
const model: THealthMonitorFormState = {
name: 'updated-monitor',
urlPath: '/health',
expectedCode: 200,
maxRetriesDown: 3,
delay: 5,
maxRetries: 3,
timeout: 10,
};
const mockData = { id: '1', name: 'updated-monitor' };
vi.mocked(v6.put).mockResolvedValue({ data: mockData });

const result = await editHealthMonitor(
projectId,
region,
healthMonitorId,
model,
);
expect(result).toEqual({ data: mockData });
expect(v6.put).toHaveBeenCalledWith(
`/cloud/project/${projectId}/region/${region}/loadbalancing/healthMonitor/${healthMonitorId}`,
{
name: model.name,
delay: model.delay,
timeout: model.timeout,
maxRetries: 3,
maxRetriesDown: 3,
httpConfiguration: {
expectedCodes: `${model.expectedCode}`,
httpMethod: 'GET',
httpVersion: '1.0',
urlPath: model.urlPath,
},
},
);
});

it('should rename health monitor', async () => {
const newName = 'renamed-monitor';
const mockData = { id: '1', name: newName };
vi.mocked(v6.put).mockResolvedValue({ data: mockData });

const result = await renameHealthMonitor(
projectId,
region,
healthMonitorId,
newName,
);
expect(result).toEqual({ data: mockData });
expect(
v6.put,
).toHaveBeenCalledWith(
`/cloud/project/${projectId}/region/${region}/loadbalancing/healthMonitor/${healthMonitorId}`,
{ name: newName },
);
});
});
Loading

0 comments on commit 10c2db8

Please sign in to comment.