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