|
| 1 | +const assert = require('assert'); |
| 2 | +const sinon = require('sinon'); |
| 3 | +const UtilizationService = require('../../../lib/utilization/instance'); |
| 4 | +const { fetchCapacityMetrics } = require('../../../lib/routes/veeam/utils'); |
| 5 | +const { DummyRequestLogger } = require('../helpers'); |
| 6 | + |
| 7 | +describe('fetchCapacityMetrics', () => { |
| 8 | + let utilizationStub; |
| 9 | + let log; |
| 10 | + let logWarnSpy; |
| 11 | + let logErrorSpy; |
| 12 | + |
| 13 | + const bucketMd = { |
| 14 | + _name: 'test-bucket', |
| 15 | + _creationDate: '2024-01-01T00:00:00.000Z', |
| 16 | + }; |
| 17 | + |
| 18 | + const request = { |
| 19 | + bucketName: 'test-bucket', |
| 20 | + }; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + log = new DummyRequestLogger(); |
| 24 | + logWarnSpy = sinon.spy(); |
| 25 | + logErrorSpy = sinon.spy(); |
| 26 | + log.warn = logWarnSpy; |
| 27 | + log.error = logErrorSpy; |
| 28 | + |
| 29 | + utilizationStub = sinon.stub(UtilizationService, 'getUtilizationMetrics'); |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + sinon.restore(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should call UtilizationService with the correct bucket key', done => { |
| 37 | + utilizationStub.callsArgWith(4, null, {}); |
| 38 | + |
| 39 | + fetchCapacityMetrics(bucketMd, request, log, 'testMethod', () => { |
| 40 | + const expectedKey = `test-bucket_${new Date('2024-01-01T00:00:00.000Z').getTime()}`; |
| 41 | + assert.strictEqual(utilizationStub.getCall(0).args[0], 'bucket'); |
| 42 | + assert.strictEqual(utilizationStub.getCall(0).args[1], expectedKey); |
| 43 | + done(); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should call back with metrics on success', done => { |
| 48 | + const bucketMetrics = { bytesTotal: 42, date: '2026-03-26T19:00:08.996Z' }; |
| 49 | + utilizationStub.callsArgWith(4, null, bucketMetrics); |
| 50 | + |
| 51 | + fetchCapacityMetrics(bucketMd, request, log, 'testMethod', (err, metrics) => { |
| 52 | + assert.ifError(err); |
| 53 | + assert.strictEqual(metrics, bucketMetrics); |
| 54 | + assert(!logWarnSpy.called); |
| 55 | + assert(!logErrorSpy.called); |
| 56 | + done(); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should call back with no error and no metrics on 404', done => { |
| 61 | + const error404 = new Error('Not Found'); |
| 62 | + error404.response = { status: 404 }; |
| 63 | + utilizationStub.callsArgWith(4, error404); |
| 64 | + |
| 65 | + fetchCapacityMetrics(bucketMd, request, log, 'testMethod', (err, metrics) => { |
| 66 | + assert.ifError(err); |
| 67 | + assert.strictEqual(metrics, undefined); |
| 68 | + assert(logWarnSpy.calledOnce); |
| 69 | + assert(logWarnSpy.getCall(0).args[0].includes('404')); |
| 70 | + assert.strictEqual(logWarnSpy.getCall(0).args[1].method, 'testMethod'); |
| 71 | + assert.strictEqual(logWarnSpy.getCall(0).args[1].bucket, 'test-bucket'); |
| 72 | + assert(!logErrorSpy.called); |
| 73 | + done(); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should also handle 404 via statusCode property', done => { |
| 78 | + const error404 = new Error('Not Found'); |
| 79 | + error404.statusCode = 404; |
| 80 | + utilizationStub.callsArgWith(4, error404); |
| 81 | + |
| 82 | + fetchCapacityMetrics(bucketMd, request, log, 'testMethod', (err, metrics) => { |
| 83 | + assert.ifError(err); |
| 84 | + assert.strictEqual(metrics, undefined); |
| 85 | + assert(logWarnSpy.calledOnce); |
| 86 | + done(); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should call back with error on non-404 failures', done => { |
| 91 | + const error500 = new Error('Internal Server Error'); |
| 92 | + error500.response = { status: 500 }; |
| 93 | + utilizationStub.callsArgWith(4, error500); |
| 94 | + |
| 95 | + fetchCapacityMetrics(bucketMd, request, log, 'testMethod', (err, metrics) => { |
| 96 | + assert.strictEqual(err, error500); |
| 97 | + assert.strictEqual(metrics, undefined); |
| 98 | + assert(logErrorSpy.calledOnce); |
| 99 | + assert.strictEqual(logErrorSpy.getCall(0).args[1].method, 'testMethod'); |
| 100 | + assert.strictEqual(logErrorSpy.getCall(0).args[1].bucket, 'test-bucket'); |
| 101 | + assert.strictEqual(logErrorSpy.getCall(0).args[1].statusCode, 500); |
| 102 | + assert(!logWarnSpy.called); |
| 103 | + done(); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should call back with error on connection errors', done => { |
| 108 | + const connError = new Error('Connection refused'); |
| 109 | + connError.code = 'ECONNREFUSED'; |
| 110 | + utilizationStub.callsArgWith(4, connError); |
| 111 | + |
| 112 | + fetchCapacityMetrics(bucketMd, request, log, 'testMethod', (err, metrics) => { |
| 113 | + assert.strictEqual(err, connError); |
| 114 | + assert.strictEqual(metrics, undefined); |
| 115 | + assert(logErrorSpy.calledOnce); |
| 116 | + assert.strictEqual(logErrorSpy.getCall(0).args[1].statusCode, 'ECONNREFUSED'); |
| 117 | + done(); |
| 118 | + }); |
| 119 | + }); |
| 120 | +}); |
0 commit comments