|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const assert = require('assert'); |
| 4 | +const sinon = require('sinon'); |
| 5 | +const http = require('http'); |
| 6 | +const https = require('https'); |
| 7 | +const arsenal = require('arsenal'); |
| 8 | +const uuid = require('uuid'); |
| 9 | +const logger = require('../../lib/utilities/logger'); |
| 10 | +const { config: defaultConfig } = require('../../lib/Config'); |
| 11 | +const { S3Server } = require('../../lib/server'); |
| 12 | + |
| 13 | +describe('S3Server', () => { |
| 14 | + let server; |
| 15 | + let startServerStub; |
| 16 | + let log; |
| 17 | + let config; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + log = logger.newRequestLogger(); |
| 21 | + |
| 22 | + config = { |
| 23 | + ...defaultConfig, |
| 24 | + port: undefined, |
| 25 | + listenOn: [], |
| 26 | + internalPort: undefined, |
| 27 | + internalListenOn: [], |
| 28 | + metricsListenOn: [], |
| 29 | + metricsPort: 8002 |
| 30 | + }; |
| 31 | + server = new S3Server(config); |
| 32 | + |
| 33 | + // Stub the _startServer method to verify it's called correctly |
| 34 | + startServerStub = sinon.stub(server, '_startServer'); |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(() => { |
| 38 | + sinon.restore(); |
| 39 | + }); |
| 40 | + |
| 41 | + const waitReady = () => new Promise(resolve => { |
| 42 | + const interval = setInterval(() => { |
| 43 | + if (server.started) { |
| 44 | + clearInterval(interval); |
| 45 | + resolve(); |
| 46 | + } |
| 47 | + }, 100); |
| 48 | + }); |
| 49 | + |
| 50 | + describe('initiateStartup', () => { |
| 51 | + beforeEach(() => { |
| 52 | + sinon.stub(server, 'routeRequest'); |
| 53 | + sinon.stub(server, 'internalRouteRequest'); |
| 54 | + sinon.stub(server, 'routeAdminRequest'); |
| 55 | + }); |
| 56 | + |
| 57 | + // `sinon` matcher to match when the callback argument actually invokes the expected |
| 58 | + // function |
| 59 | + const wrapperFor = expected => sinon.match(actual => { |
| 60 | + const req = uuid.v4(); |
| 61 | + const res = uuid.v4(); |
| 62 | + actual(req, res); |
| 63 | + return expected.calledWith(req, res); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should start API server with default port if no listenOn is provided', async () => { |
| 67 | + config.port = 8000; |
| 68 | + |
| 69 | + server.initiateStartup(log); |
| 70 | + |
| 71 | + await waitReady(); |
| 72 | + |
| 73 | + assert.strictEqual(startServerStub.callCount, 2); |
| 74 | + assert(startServerStub.calledWith(wrapperFor(server.routeRequest), 8000)); |
| 75 | + assert(startServerStub.calledWith(wrapperFor(server.routeAdminRequest))); |
| 76 | + |
| 77 | + }); |
| 78 | + |
| 79 | + it('should start API servers from listenOn array', async () => { |
| 80 | + config.listenOn = [ |
| 81 | + { port: 8000, ip: '127.0.0.1' }, |
| 82 | + { port: 8001, ip: '0.0.0.0' } |
| 83 | + ]; |
| 84 | + config.port = 9999; // Should be ignored since listenOn is provided |
| 85 | + |
| 86 | + server.initiateStartup(log); |
| 87 | + |
| 88 | + await waitReady(); |
| 89 | + |
| 90 | + assert.strictEqual(startServerStub.callCount, 3); |
| 91 | + assert(startServerStub.calledWith(wrapperFor(server.routeRequest), 8000, '127.0.0.1')); |
| 92 | + assert(startServerStub.calledWith(wrapperFor(server.routeRequest), 8001, '0.0.0.0')); |
| 93 | + assert(startServerStub.calledWith(wrapperFor(server.routeAdminRequest))); |
| 94 | + assert.strictEqual(startServerStub.neverCalledWith(sinon.any, 9999), true); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should start internal API server with internalPort if no internalListenOn is provided', async () => { |
| 98 | + config.internalPort = 9000; |
| 99 | + |
| 100 | + server.initiateStartup(log); |
| 101 | + |
| 102 | + await waitReady(); |
| 103 | + |
| 104 | + assert.strictEqual(startServerStub.callCount, 2); |
| 105 | + assert(startServerStub.calledWith(wrapperFor(server.internalRouteRequest), 9000)); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should start internal API servers from internalListenOn array', async () => { |
| 109 | + config.internalListenOn = [ |
| 110 | + { port: 9000, ip: '127.0.0.1' }, |
| 111 | + { port: 9001, ip: '0.0.0.0' } |
| 112 | + ]; |
| 113 | + config.internalPort = 9999; // Should be ignored since internalListenOn is provided |
| 114 | + |
| 115 | + server.initiateStartup(log); |
| 116 | + |
| 117 | + await waitReady(); |
| 118 | + |
| 119 | + assert.strictEqual(startServerStub.callCount, 3); |
| 120 | + assert(startServerStub.calledWith(wrapperFor(server.internalRouteRequest), 9000, '127.0.0.1')); |
| 121 | + assert(startServerStub.calledWith(wrapperFor(server.internalRouteRequest), 9001, '0.0.0.0')); |
| 122 | + assert(startServerStub.calledWith(wrapperFor(server.routeAdminRequest))); |
| 123 | + assert.strictEqual(startServerStub.neverCalledWith(sinon.any, 9999), true); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should start metrics server with metricsPort if no metricsListenOn is provided', async () => { |
| 127 | + config.metricsPort = 8012; |
| 128 | + |
| 129 | + server.initiateStartup(log); |
| 130 | + |
| 131 | + await waitReady(); |
| 132 | + |
| 133 | + assert.strictEqual(startServerStub.callCount, 1); |
| 134 | + assert(startServerStub.calledWith(wrapperFor(server.routeAdminRequest), 8012)); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should start metrics servers from metricsListenOn array', async () => { |
| 138 | + config.metricsListenOn = [ |
| 139 | + { port: 8002, ip: '127.0.0.1' }, |
| 140 | + { port: 8003, ip: '0.0.0.0' } |
| 141 | + ]; |
| 142 | + config.metricsPort = 9999; // Should be ignored since metricsListenOn is provided |
| 143 | + |
| 144 | + server.initiateStartup(log); |
| 145 | + |
| 146 | + await waitReady(); |
| 147 | + |
| 148 | + assert.strictEqual(startServerStub.callCount, 2); |
| 149 | + assert(startServerStub.calledWith(wrapperFor(server.routeAdminRequest), 8002, '127.0.0.1')); |
| 150 | + assert(startServerStub.calledWith(wrapperFor(server.routeAdminRequest), 8003, '0.0.0.0')); |
| 151 | + assert.strictEqual(startServerStub.neverCalledWith(server.any, 9999), true); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should start all servers with the correct parameters', async () => { |
| 155 | + config.port = 8000; |
| 156 | + config.internalPort = 9000; |
| 157 | + config.metricsPort = 8002; |
| 158 | + |
| 159 | + server.initiateStartup(log); |
| 160 | + |
| 161 | + await waitReady(); |
| 162 | + |
| 163 | + assert.strictEqual(startServerStub.callCount, 3); |
| 164 | + assert(startServerStub.calledWith(wrapperFor(server.routeRequest), 8000)); |
| 165 | + assert(startServerStub.calledWith(wrapperFor(server.internalRouteRequest), 9000)); |
| 166 | + assert(startServerStub.calledWith(wrapperFor(server.routeAdminRequest), 8002)); |
| 167 | + }); |
| 168 | + }); |
| 169 | + |
| 170 | + describe('internalRouteRequest', () => { |
| 171 | + const resp = { |
| 172 | + on: () => { }, |
| 173 | + setHeader: () => { }, |
| 174 | + writeHead: () => { }, |
| 175 | + end: () => { }, |
| 176 | + }; |
| 177 | + |
| 178 | + let req; |
| 179 | + |
| 180 | + beforeEach(() => { |
| 181 | + req = { |
| 182 | + headers: {}, |
| 183 | + socket: { |
| 184 | + setNoDelay: () => { }, |
| 185 | + }, |
| 186 | + url: 'http://localhost:8000', |
| 187 | + }; |
| 188 | + }); |
| 189 | + |
| 190 | + afterEach(() => { |
| 191 | + sinon.restore(); |
| 192 | + }); |
| 193 | + |
| 194 | + it('should bypass bucket policy for internal requests', () => { |
| 195 | + const routesMock = sinon.stub().callsFake(req => { |
| 196 | + assert(req.bypassUserBucketPolicies); |
| 197 | + }); |
| 198 | + sinon.stub(arsenal.s3routes, 'routes').value(routesMock); |
| 199 | + |
| 200 | + server.internalRouteRequest(req, resp); |
| 201 | + sinon.assert.calledOnce(routesMock); |
| 202 | + }); |
| 203 | + |
| 204 | + it('should bypass bucket policy for routes requests', () => { |
| 205 | + const routesMock = sinon.stub().callsFake(req => { |
| 206 | + assert(!req.bypassUserBucketPolicies); |
| 207 | + }); |
| 208 | + sinon.stub(arsenal.s3routes, 'routes').value(routesMock); |
| 209 | + |
| 210 | + server.routeRequest(req, resp); |
| 211 | + sinon.assert.calledOnce(routesMock); |
| 212 | + }); |
| 213 | + }); |
| 214 | +}); |
| 215 | + |
| 216 | +describe('S3Server request timeout', () => { |
| 217 | + let sandbox; |
| 218 | + let mockServer; |
| 219 | + |
| 220 | + beforeEach(() => { |
| 221 | + sandbox = sinon.createSandbox(); |
| 222 | + |
| 223 | + // Create a mock server to capture the requestTimeout setting |
| 224 | + mockServer = { |
| 225 | + requestTimeout: null, |
| 226 | + on: sandbox.stub(), |
| 227 | + listen: sandbox.stub(), |
| 228 | + address: sandbox.stub().returns({ address: '127.0.0.1', port: 8000 }), |
| 229 | + }; |
| 230 | + |
| 231 | + // Mock server creation to return our mock |
| 232 | + sandbox.stub(http, 'createServer').returns(mockServer); |
| 233 | + sandbox.stub(https, 'createServer').returns(mockServer); |
| 234 | + }); |
| 235 | + |
| 236 | + afterEach(() => { |
| 237 | + sandbox.restore(); |
| 238 | + }); |
| 239 | + |
| 240 | + it('should set server.requestTimeout to 0 when starting server', () => { |
| 241 | + const server = new S3Server({ |
| 242 | + ...defaultConfig, |
| 243 | + https: false |
| 244 | + }); |
| 245 | + |
| 246 | + // Call _startServer which should set requestTimeout = 0 |
| 247 | + server._startServer(() => {}, 8000, '127.0.0.1'); |
| 248 | + |
| 249 | + // Verify that requestTimeout was set to 0 |
| 250 | + assert.strictEqual(mockServer.requestTimeout, 0); |
| 251 | + }); |
| 252 | +}); |
0 commit comments