|
1 | 1 | const assert = require('assert'); |
2 | 2 | const fs = require('fs'); |
3 | 3 | const sinon = require('sinon'); |
| 4 | +const path = require('path'); |
4 | 5 | const { |
5 | 6 | azureGetStorageAccountName, |
6 | 7 | azureGetLocationCredentials, |
@@ -814,4 +815,82 @@ describe('Config', () => { |
814 | 815 | assert.deepEqual(config.internalListenOn, [{ ip: '127.0.0.1', port: 9000 }]); |
815 | 816 | }); |
816 | 817 | }); |
| 818 | + |
| 819 | + describe('instanceId', () => { |
| 820 | + let defaultConfig; |
| 821 | + |
| 822 | + before(() => { |
| 823 | + setEnv('S3_CONFIG_FILE', 'tests/unit/testConfigs/allOptsConfig/config.json'); |
| 824 | + // Import config |
| 825 | + const defaultConfigPath = path.join(__dirname, './testConfigs/allOptsConfig/config.json'); |
| 826 | + defaultConfig = require(defaultConfigPath); |
| 827 | + }); |
| 828 | + |
| 829 | + afterEach(() => { |
| 830 | + sinon.restore(); |
| 831 | + }); |
| 832 | + |
| 833 | + it('should use the instance id set in the config', () => { |
| 834 | + // Stub fs.readFileSync to return a specific config.json content that includes instanceId |
| 835 | + const originalReadFileSync = fs.readFileSync; |
| 836 | + const readFileSyncStub = sinon.stub(fs, 'readFileSync'); |
| 837 | + // Mock only config.json file |
| 838 | + readFileSyncStub |
| 839 | + .withArgs(sinon.match(/\/config\.json$/)) |
| 840 | + .returns(JSON.stringify({ ...defaultConfig, instanceId: 'test' })); |
| 841 | + // For all other files, use the original readFileSync |
| 842 | + readFileSyncStub |
| 843 | + .callsFake((filePath, ...args) => originalReadFileSync(filePath, ...args)); |
| 844 | + // Create a new ConfigObject instance |
| 845 | + const config = new ConfigObject(); |
| 846 | + assert.strictEqual(config.instanceId, 'test'); |
| 847 | + }); |
| 848 | + |
| 849 | + it('should assert if instanceId is not a string', () => { |
| 850 | + // Stub fs.readFileSync to return a specific config.json content that includes instanceId |
| 851 | + const originalReadFileSync = fs.readFileSync; |
| 852 | + const readFileSyncStub = sinon.stub(fs, 'readFileSync'); |
| 853 | + // Mock only config.json file |
| 854 | + readFileSyncStub |
| 855 | + .withArgs(sinon.match(/\/config\.json$/)) |
| 856 | + .returns(JSON.stringify({ ...defaultConfig, instanceId: 1234 })); |
| 857 | + // For all other files, use the original readFileSync |
| 858 | + readFileSyncStub |
| 859 | + .callsFake((filePath, ...args) => originalReadFileSync(filePath, ...args)); |
| 860 | + // Create a new ConfigObject instance |
| 861 | + assert.throws(() => new ConfigObject()); |
| 862 | + }); |
| 863 | + |
| 864 | + it('should use the instance id set in the env var', () => { |
| 865 | + setEnv('CLOUDSERVER_INSTANCE_ID', '123456'); |
| 866 | + const config = new ConfigObject(); |
| 867 | + assert.strictEqual(config.instanceId, '123456'); |
| 868 | + }); |
| 869 | + |
| 870 | + it('should assert if instanceId is longer than 6 characters', () => { |
| 871 | + setEnv('CLOUDSERVER_INSTANCE_ID', '1234567'); |
| 872 | + assert.throws(() => new ConfigObject()); |
| 873 | + }); |
| 874 | + |
| 875 | + it('should generate a new instanceId for external cloudserver', () => { |
| 876 | + setEnv('HOSTNAME', 'connector-cloudserver-69958b4697-bs5pn'); |
| 877 | + const config = new ConfigObject(); |
| 878 | + assert.strictEqual(config.instanceId, 'ebs5pn'); |
| 879 | + }); |
| 880 | + |
| 881 | + it('should generate a new instanceId for internal cloudserver', () => { |
| 882 | + setEnv('HOSTNAME', 'internal-cloudserver-54dc76b796-48m2x'); |
| 883 | + const config = new ConfigObject(); |
| 884 | + assert.strictEqual(config.instanceId, 'i48m2x'); |
| 885 | + }); |
| 886 | + it('should generate a random instanceId if HOSTNAME has an unexpected format', () => { |
| 887 | + setEnv('HOSTNAME', 'cldsrv1'); |
| 888 | + const config = new ConfigObject(); |
| 889 | + assert.strictEqual(config.instanceId.length, 6); |
| 890 | + }); |
| 891 | + it('should generate a random instanceId if HOSTNAME is not set', () => { |
| 892 | + const config = new ConfigObject(); |
| 893 | + assert.strictEqual(config.instanceId.length, 6); |
| 894 | + }); |
| 895 | + }); |
817 | 896 | }); |
0 commit comments