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 18, 2024
1 parent 345cbcf commit 8047e95
Show file tree
Hide file tree
Showing 13 changed files with 1,161 additions and 25 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,102 @@
import { describe, it, expect } from 'vitest';
import { v6 } from '@ovh-ux/manager-core-api';
import {
getL7Policies,
getPolicy,
deletePolicy,
createPolicy,
updatePolicy,
TL7Policy,
} from './l7Policies';
import {
LoadBalancerOperatingStatusEnum,
LoadBalancerProvisioningStatusEnum,
} from './load-balancer';

describe('l7Policies API', () => {
const projectId = 'test-project';
const region = 'test-region';
const listenerId = 'test-listener';
const policyId = 'test-policy';
const policy: TL7Policy = {
id: 'test-policy',
name: 'Test Policy',
description: 'Test Description',
operatingStatus: LoadBalancerOperatingStatusEnum.ONLINE,
provisioningStatus: LoadBalancerProvisioningStatusEnum.ACTIVE,
redirectHttpCode: 301,
redirectPoolId: null,
redirectPrefix: null,
redirectUrl: null,
action: 'REDIRECT_TO_URL',
position: 1,
listenerId: 'test-listener',
};

it('should fetch L7 policies', async () => {
const mockData = [policy];
(v6.get as any).mockResolvedValue({ data: mockData });

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

it('should fetch a single policy', async () => {
(v6.get as any).mockResolvedValue({ data: policy });

const result = await getPolicy(projectId, region, policyId);
expect(result).toEqual(policy);
expect(v6.get).toHaveBeenCalledWith(
`/cloud/project/${projectId}/region/${region}/loadbalancing/l7Policy/${policyId}`,
);
});

it('should delete a policy', async () => {
const mockData = { success: true };
(v6.delete as any).mockResolvedValue({ data: mockData });

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

it('should create a policy', async () => {
const mockData = { id: 'new-policy' };
(v6.post as any).mockResolvedValue({ data: mockData });

const result = await createPolicy(projectId, region, listenerId, policy);
expect(result).toEqual(mockData);
expect(v6.post).toHaveBeenCalledWith(
`/cloud/project/${projectId}/region/${region}/loadbalancing/l7Policy`,
{
listenerId,
...policy,
},
);
});

it('should update a policy', async () => {
const mockData = { success: true };
(v6.put as any).mockResolvedValue({ data: mockData });

const result = await updatePolicy(projectId, region, policy);
expect(result).toEqual(mockData);
expect(v6.put).toHaveBeenCalledWith(
`/cloud/project/${projectId}/region/${region}/loadbalancing/l7Policy/${policy.id}`,
{
name: policy.name,
position: policy.position,
action: policy.action,
redirectHttpCode: policy.redirectHttpCode,
redirectPoolId: policy.redirectPoolId,
redirectPrefix: policy.redirectPrefix,
redirectUrl: policy.redirectUrl,
},
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { describe, it, expect } from 'vitest';
import { v6 } from '@ovh-ux/manager-core-api';
import {
getL7Rules,
deleteL7Rule,
createL7Rule,
updateL7Rule,
getL7Rule,
TL7Rule,
} from './l7Rules';
import {
LoadBalancerOperatingStatusEnum,
LoadBalancerProvisioningStatusEnum,
} from './load-balancer';

describe('L7 Rules API', () => {
const projectId = 'test-project';
const region = 'test-region';
const policyId = 'test-policy';
const ruleId = 'test-rule';
const rule: TL7Rule = {
id: ruleId,
operatingStatus: LoadBalancerOperatingStatusEnum.ONLINE,
provisioningStatus: LoadBalancerProvisioningStatusEnum.ACTIVE,
key: 'test-key',
value: 'test-value',
invert: false,
ruleType: 'HOST_NAME',
compareType: 'EQUAL_TO',
};

it('should get L7 rules', async () => {
const mockData = [rule];
(v6.get as any).mockResolvedValue({ data: mockData });

const result = await getL7Rules(projectId, region, policyId);
expect(result).toEqual(mockData);
expect(v6.get).toHaveBeenCalledWith(
`/cloud/project/${projectId}/region/${region}/loadbalancing/l7Policy/${policyId}/l7Rule`,
);
});

it('should delete an L7 rule', async () => {
const mockData = {};
(v6.delete as any).mockResolvedValue({ data: mockData });

const result = await deleteL7Rule(projectId, region, policyId, ruleId);
expect(result).toEqual(mockData);
expect(v6.delete).toHaveBeenCalledWith(
`/cloud/project/${projectId}/region/${region}/loadbalancing/l7Policy/${policyId}/l7Rule/${ruleId}`,
);
});

it('should create an L7 rule', async () => {
const mockData = rule;
(v6.post as any).mockResolvedValue({ data: mockData });

const result = await createL7Rule(projectId, region, policyId, rule);
expect(result).toEqual(mockData);
expect(v6.post).toHaveBeenCalledWith(
`/cloud/project/${projectId}/region/${region}/loadbalancing/l7Policy/${policyId}/l7Rule`,
rule,
);
});

it('should update an L7 rule', async () => {
const updatedRule = { ...rule, value: 'updated-value' };
const mockData = updatedRule;
(v6.put as any).mockResolvedValue({ data: mockData });

const result = await updateL7Rule(projectId, region, policyId, updatedRule);
expect(result).toEqual(mockData);
expect(v6.put).toHaveBeenCalledWith(
`/cloud/project/${projectId}/region/${region}/loadbalancing/l7Policy/${policyId}/l7Rule/${rule.id}`,
{
ruleType: updatedRule.ruleType,
compareType: updatedRule.compareType,
key: updatedRule.key,
value: updatedRule.value,
invert: updatedRule.invert,
},
);
});

it('should get a specific L7 rule', async () => {
const mockData = rule;
(v6.get as any).mockResolvedValue({ data: mockData });

const result = await getL7Rule(projectId, region, policyId, ruleId);
expect(result).toEqual(mockData);
expect(v6.get).toHaveBeenCalledWith(
`/cloud/project/${projectId}/region/${region}/loadbalancing/l7Policy/${policyId}/l7Rule/${ruleId}`,
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, it, expect, vi } from 'vitest';
import { v6 } from '@ovh-ux/manager-core-api';
import { deleteListener, getListener } from './listener';

describe('listener API', () => {
const projectId = 'test-project';
const region = 'test-region';
const listenerId = 'test-listener';

describe('deleteListener', () => {
it('should delete a listener and return data', async () => {
const mockResponse = { success: true };
vi.mocked(v6.delete).mockResolvedValueOnce({ data: mockResponse });

const result = await deleteListener(projectId, region, listenerId);

expect(v6.delete).toHaveBeenCalledWith(
`/cloud/project/${projectId}/region/${region}/loadbalancing/listener/${listenerId}`,
);
expect(result).toEqual(mockResponse);
});
});

describe('getListener', () => {
it('should get a listener and return data', async () => {
const mockListener = { id: listenerId, name: 'test-listener' };
vi.mocked(v6.get).mockResolvedValueOnce({ data: mockListener });

const result = await getListener(projectId, region, listenerId);

expect(v6.get).toHaveBeenCalledWith(
`/cloud/project/${projectId}/region/${region}/loadbalancing/listener/${listenerId}`,
);
expect(result).toEqual(mockListener);
});
});
});
Loading

0 comments on commit 8047e95

Please sign in to comment.