import {expect} from 'chai'; import {ServerCapacity, ServerConsolidation, VM, Server} from '../src'; interface TestCase { vms: VM[], capacity: ServerCapacity, results: Server[], } describe('ServerConsolidation - fit', () => { const cls = new ServerConsolidation(); const vms: {[name: string]: VM} = { 'a': { cpu: 1, memory: 1, network: 1, }, 'b': { cpu: 2, memory: 2, network: 2, }, 'b-copy': { cpu: 2, memory: 2, network: 2, }, 'large': { cpu: 10, memory: 10, network: 10, }, 'd': { cpu: 3, memory: 3, network: 3, }, }; it('should match server allocation', () => { const cases: TestCase[] = [ // every vm fits on a single server { vms: [vms.a, vms.b], capacity: { limitCpu: 10, limitMemory: 10, limitNetwork: 10, }, results: [ new Set([vms.a, vms.b]), ], }, // multiple servers needed { vms: [vms.a, vms.b, vms.large], capacity: { limitCpu: 10, limitMemory: 10, limitNetwork: 10, }, results: [ new Set([vms.a, vms.b]), new Set([vms.large]), ], }, // reuse of existing servers { vms: [ vms.a, vms.b, vms.large, vms.d], capacity: { limitCpu: 10, limitMemory: 10, limitNetwork: 10, }, results: [ new Set([vms.a, vms.b, vms.d]), new Set([vms.large]), ], }, ]; for (const {vms, capacity, results} of cases) { const servers = cls.fit(vms, capacity); for (const [idx, server] of servers.entries()) { const result = results[idx]; expect(server).to.eqls(result); } } }); it('should match reference', () => { const cases: TestCase[] = [ // same reference of objects { vms: [ vms.a, ], capacity: { limitCpu: 10, limitMemory: 10, limitNetwork: 10, }, results: [ new Set([ vms.a, ]), ], }, ]; for (const {vms, capacity, results} of cases) { const servers = cls.fit(vms, capacity); for (const server of servers[0]) { results[0].has(server); } } }); }); describe('ServerConsolidation - reduceCapacity', () => { const cls = new ServerConsolidation(); it('should equal', () => { const capacity: ServerCapacity = { limitCpu: 10, limitMemory: 3, limitNetwork: 9, }; const vm: VM = { cpu: 3, memory: 1, network: 0, }; const result: ServerCapacity = { limitCpu: 7, limitMemory: 2, limitNetwork: 9, }; cls.reduceCapacity(vm, capacity); expect(capacity).to.deep.equal(result); }); });