diff --git a/extensions/lifecycle/bucketProcessor/LifecycleBucketProcessor.js b/extensions/lifecycle/bucketProcessor/LifecycleBucketProcessor.js index e402f7bc5..28e73f27a 100644 --- a/extensions/lifecycle/bucketProcessor/LifecycleBucketProcessor.js +++ b/extensions/lifecycle/bucketProcessor/LifecycleBucketProcessor.js @@ -210,10 +210,10 @@ class LifecycleBucketProcessor { } /** - * Return an backbeat client instance + * Return metadata proxy with backbeat client * @param {String} canonicalId - The canonical ID of the bucket owner. * @param {String} accountId - The account ID of the bucket owner . - * @return {BackbeatClient} The S3 client instance to make requests with + * @return {BackbeatMetadataProxy} backbeatMetadataProxy - The metadata proxy with backbeat client */ _getBackbeatClient(canonicalId, accountId) { const credentials = this.credentialsManager.getCredentials({ @@ -228,22 +228,21 @@ class LifecycleBucketProcessor { } const clientId = canonicalId; - const client = this.backbeatClients[clientId]; + let client = this.backbeatClients[clientId]; + + if (!client) { + client = createBackbeatClient({ + transport: this._transport, + port: this._s3Config.port, + host: this._s3Config.host, + credentials, + agent: this.s3Agent, + }); - if (client) { - return client; + this.backbeatClients[clientId] = client; } - this.backbeatClients[clientId] = createBackbeatClient({ - transport: this._transport, - port: this._s3Config.port, - host: this._s3Config.host, - credentials, - agent: this.s3Agent, - }); - - return new BackbeatMetadataProxy(this._lcConfig) - .setBackbeatClient(this.backbeatClients[clientId]); + return new BackbeatMetadataProxy(this._lcConfig).setBackbeatClient(client); } /** diff --git a/tests/unit/lifecycle/LifecycleBucketProcessor.spec.js b/tests/unit/lifecycle/LifecycleBucketProcessor.spec.js new file mode 100644 index 000000000..0826b3de3 --- /dev/null +++ b/tests/unit/lifecycle/LifecycleBucketProcessor.spec.js @@ -0,0 +1,46 @@ +'use strict'; // eslint-disable-line + +const assert = require('assert'); +const LifecycleBucketProcessor = require('../../../extensions/lifecycle/bucketProcessor/LifecycleBucketProcessor'); +const BackbeatMetadataProxy = require('../../../lib/BackbeatMetadataProxy'); + +const zkConfig = {}; +const kafkaConfig = {}; +const lcConfig = { auth: {}, bucketProcessor: {} }; +const repConfig = {}; +const s3Config = { host: 'init.test.host', port: 8000 }; + +describe('LifecycleBucketProcessor', () => { + describe('_getBackbeatClient', () => { + let lbp; + + beforeEach(() => { + lbp = new LifecycleBucketProcessor(zkConfig, kafkaConfig, lcConfig, repConfig, s3Config, 'http'); + lbp.credentialsManager = { + getCredentials() { + return { + accessKeyId: 'ak0', + secretAccessKey: 'sk0', + }; + }, + }; + }); + + it('should return an instance of BackbeatMetadataProxy', () => { + const canonicalId = 'cid0'; + const accountId = 'id0'; + const client = lbp._getBackbeatClient(canonicalId, accountId); + + assert.ok(client instanceof BackbeatMetadataProxy, 'client is not an instance of BackbeatMetadataProxy'); + }); + + it('should return an instance of BackbeatMetadataProxy after caching', () => { + const canonicalId = 'cid0'; + const accountId = 'id0'; + lbp._getBackbeatClient(canonicalId, accountId); + const client = lbp._getBackbeatClient(canonicalId, accountId); + + assert.ok(client instanceof BackbeatMetadataProxy, 'client is not an instance of BackbeatMetadataProxy'); + }); + }); +});