-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathputBucketRateLimit.js
More file actions
167 lines (144 loc) · 6.5 KB
/
putBucketRateLimit.js
File metadata and controls
167 lines (144 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const AWS = require('aws-sdk');
const S3 = AWS.S3;
const assert = require('assert');
const getConfig = require('../support/config');
const { sendRateLimitRequest, skipIfRateLimitDisabled } = require('../rateLimit/tooling');
const { config } = require('../../../../../lib/Config');
const bucket = 'putratelimitestbucket';
const nonExistentBucket = 'putratelimitestnonexistentbucket';
const rateLimitConfig = { RequestsPerSecond: 200 };
const invalidConfig = { RequestsPerSecond: -100 };
const invalidConfigNotInteger = { RequestsPerSecond: 10.5 };
const missingLimitConfig = {};
skipIfRateLimitDisabled('Test put bucket rate limit', () => {
let s3;
before(() => {
const config = getConfig('lisa', { signatureVersion: 'v4' });
s3 = new S3(config);
AWS.config.update(config);
});
beforeEach(done => s3.createBucket({ Bucket: bucket }, done));
afterEach(done => s3.deleteBucket({ Bucket: bucket }, done));
it('should set the rate limit config', async () => {
try {
await sendRateLimitRequest('PUT', '127.0.0.1:8000',
`/${bucket}/?rate-limit`, JSON.stringify(rateLimitConfig));
assert.ok(true);
} catch (err) {
assert.ifError(err);
}
});
it('should update existing rate limit config', async () => {
try {
const initialConfig = { RequestsPerSecond: 100 };
await sendRateLimitRequest('PUT', '127.0.0.1:8000',
`/${bucket}/?rate-limit`, JSON.stringify(initialConfig));
await sendRateLimitRequest('PUT', '127.0.0.1:8000',
`/${bucket}/?rate-limit`, JSON.stringify(rateLimitConfig));
// Verify the update
const data = await sendRateLimitRequest('GET', '127.0.0.1:8000',
`/${bucket}/?rate-limit`);
assert.strictEqual(data.RequestsPerSecond.Limit, 200);
} catch (err) {
assert.ifError(err);
}
});
it('should return NoSuchBucket error when bucket does not exist', async () => {
try {
await sendRateLimitRequest('PUT', '127.0.0.1:8000',
`/${nonExistentBucket}/?rate-limit`, JSON.stringify(rateLimitConfig));
} catch (err) {
assert.strictEqual(err.Error.Code[0], 'NoSuchBucket');
}
});
it('should return InvalidArgument error when RequestsPerSecond is negative', async () => {
try {
await sendRateLimitRequest('PUT', '127.0.0.1:8000',
`/${bucket}/?rate-limit`, JSON.stringify(invalidConfig));
} catch (err) {
assert.strictEqual(err.Error.Code[0], 'InvalidArgument');
}
});
it('should return InvalidArgument error when RequestsPerSecond is not an integer', async () => {
try {
await sendRateLimitRequest('PUT', '127.0.0.1:8000',
`/${bucket}/?rate-limit`, JSON.stringify(invalidConfigNotInteger));
} catch (err) {
assert.strictEqual(err.Error.Code[0], 'InvalidArgument');
}
});
it('should return InvalidArgument error when RequestsPerSecond is missing', async () => {
try {
await sendRateLimitRequest('PUT', '127.0.0.1:8000',
`/${bucket}/?rate-limit`, JSON.stringify(missingLimitConfig));
} catch (err) {
assert.strictEqual(err.Error.Code[0], 'InvalidArgument');
}
});
it('should return InvalidArgument error when request body is invalid JSON', async () => {
try {
await sendRateLimitRequest('PUT', '127.0.0.1:8000',
`/${bucket}/?rate-limit`, 'invalid json{');
} catch (err) {
assert.strictEqual(err.Error.Code[0], 'InvalidArgument');
}
});
it('should allow zero as a valid RequestsPerSecond value', async () => {
try {
const zeroConfig = { RequestsPerSecond: 0 };
await sendRateLimitRequest('PUT', '127.0.0.1:8000',
`/${bucket}/?rate-limit`, JSON.stringify(zeroConfig));
const data = await sendRateLimitRequest('GET', '127.0.0.1:8000',
`/${bucket}/?rate-limit`);
assert.deepStrictEqual(data, { RequestsPerSecond: { Limit: 0 } });
} catch (err) {
assert.ifError(err);
}
});
describe('validation against node and worker count', () => {
const nodes = config.rateLimiting?.nodes || 1;
const workers = config.clusters || 1;
const minLimit = nodes * workers;
const skipIfSingleNode = nodes === 1 ? it.skip : it;
// Test requires multiple nodes to pass
// With only 1 node and 1 worker the minLimit is 1.
// This leaves no invalid values to test as 0 is also a valid setting (unlimited)
skipIfSingleNode('should reject limits less than (nodes x workers)', async () => {
let error;
try {
const invalidConfig = { RequestsPerSecond: minLimit - 1 };
await sendRateLimitRequest('PUT', '127.0.0.1:8000',
`/${bucket}/?rate-limit`, JSON.stringify(invalidConfig));
} catch (err) {
error = err;
} finally {
assert(error !== undefined, 'error expected');
assert.strictEqual(error.Error.Code[0], 'InvalidArgument');
}
});
it('should accept limits equal to (nodes x workers)', async () => {
try {
const validConfig = { RequestsPerSecond: minLimit };
await sendRateLimitRequest('PUT', '127.0.0.1:8000',
`/${bucket}/?rate-limit`, JSON.stringify(validConfig));
const data = await sendRateLimitRequest('GET', '127.0.0.1:8000',
`/${bucket}/?rate-limit`);
assert.strictEqual(data.RequestsPerSecond.Limit, minLimit);
} catch (err) {
assert.ifError(err);
}
});
it('should accept limits greater than (nodes x workers)', async () => {
try {
const validConfig = { RequestsPerSecond: minLimit + 1000 };
await sendRateLimitRequest('PUT', '127.0.0.1:8000',
`/${bucket}/?rate-limit`, JSON.stringify(validConfig));
const data = await sendRateLimitRequest('GET', '127.0.0.1:8000',
`/${bucket}/?rate-limit`);
assert.strictEqual(data.RequestsPerSecond.Limit, minLimit + 1000);
} catch (err) {
assert.ifError(err);
}
});
});
});