-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(pci-load-balancer): add unit tests
ref: DTCORE-2770 Signed-off-by: Yoann Fievez <[email protected]>
- Loading branch information
Showing
41 changed files
with
3,615 additions
and
93 deletions.
There are no files selected for viewing
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
47 changes: 47 additions & 0 deletions
47
packages/manager/apps/pci-load-balancer/src/api/data/flavors.spec.ts
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,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', | ||
); | ||
}); | ||
}); |
46 changes: 46 additions & 0 deletions
46
packages/manager/apps/pci-load-balancer/src/api/data/floating-ips.spec.ts
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,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, | ||
); | ||
}); | ||
}); |
62 changes: 62 additions & 0 deletions
62
packages/manager/apps/pci-load-balancer/src/api/data/gateways.spec.ts
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,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); | ||
}); | ||
}); |
133 changes: 133 additions & 0 deletions
133
packages/manager/apps/pci-load-balancer/src/api/data/health-monitor.spec.ts
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,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 }, | ||
); | ||
}); | ||
}); |
File renamed without changes.
Oops, something went wrong.