Skip to content

Commit 4e8308b

Browse files
committed
quota related bucket tests migration
Issue: CLDSRV-724
1 parent 420a6f4 commit 4e8308b

File tree

2 files changed

+100
-52
lines changed

2 files changed

+100
-52
lines changed
Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const AWS = require('aws-sdk');
2-
const S3 = AWS.S3;
1+
const { S3Client,
2+
CreateBucketCommand,
3+
DeleteBucketCommand } = require('@aws-sdk/client-s3');
34

45
const assert = require('assert');
56
const getConfig = require('../support/config');
@@ -17,22 +18,15 @@ describe('Test update bucket quota', () => {
1718

1819
before(() => {
1920
const config = getConfig('default', { signatureVersion: 'v4' });
20-
s3 = new S3(config);
21-
AWS.config.update(config);
21+
s3 = new S3Client(config);
2222
});
2323

24-
beforeEach(done => s3.createBucket({ Bucket: bucket }, done));
24+
beforeEach(() => s3.send(new CreateBucketCommand({ Bucket: bucket })));
2525

26-
afterEach(done => s3.deleteBucket({ Bucket: bucket }, done));
26+
afterEach(() => s3.send(new DeleteBucketCommand({ Bucket: bucket })));
2727

28-
it('should update the quota', async () => {
29-
try {
30-
await sendRequest('PUT', '127.0.0.1:8000', `/${bucket}/?quota=true`, JSON.stringify(quota));
31-
assert.ok(true);
32-
} catch (err) {
33-
assert.fail(`Expected no error, but got ${err}`);
34-
}
35-
});
28+
it('should update the quota', () => sendRequest('PUT',
29+
'127.0.0.1:8000', `/${bucket}/?quota=true`, JSON.stringify(quota)));
3630

3731
it('should return no such bucket error', async () => {
3832
try {
@@ -42,7 +36,7 @@ describe('Test update bucket quota', () => {
4236
}
4337
});
4438

45-
it('should return error when quota is negative', async () => {
39+
it('should return invalid request error for negative quota', async () => {
4640
try {
4741
await sendRequest('PUT', '127.0.0.1:8000', `/${bucket}/?quota=true`, JSON.stringify(negativeQuota));
4842
} catch (err) {
@@ -51,20 +45,15 @@ describe('Test update bucket quota', () => {
5145
}
5246
});
5347

54-
it('should return error when quota is not in correct format', async () => {
48+
it('should return invalid request error for wrong quota format', async () => {
5549
try {
56-
await sendRequest('PUT', '127.0.0.1:8000', `/${bucket}/?quota=true`, wrongquotaFromat);
50+
await sendRequest('PUT', '127.0.0.1:8000', `/${bucket}/?quota=true`, JSON.stringify(wrongquotaFromat));
5751
} catch (err) {
5852
assert.strictEqual(err.Error.Code[0], 'InvalidArgument');
5953
assert.strictEqual(err.Error.Message[0], 'Request body must be a JSON object');
6054
}
6155
});
6256

63-
it('should handle large quota values', async () => {
64-
try {
65-
await sendRequest('PUT', '127.0.0.1:8000', `/${bucket}/?quota=true`, JSON.stringify(largeQuota));
66-
} catch (err) {
67-
assert.fail(`Expected no error, but got ${err}`);
68-
}
69-
});
57+
it('should accept large quota', () => sendRequest('PUT',
58+
'127.0.0.1:8000', `/${bucket}/?quota=true`, JSON.stringify(largeQuota)));
7059
});

tests/functional/aws-node-sdk/test/quota/tooling.js

Lines changed: 87 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,104 @@
11
const nodeFetch = require('node-fetch');
2-
const AWS = require('aws-sdk');
2+
const { HttpRequest } = require('@aws-sdk/protocol-http');
3+
const { SignatureV4 } = require('@aws-sdk/signature-v4');
4+
const { Sha256 } = require('@aws-crypto/sha256-js');
35
const xml2js = require('xml2js');
46

5-
const sendRequest = async (method, host, path, body = '', config = null) => {
7+
const sendRequest = async (method, host, path, body = '', config = null, signingDate = new Date()) => {
68
const service = 's3';
7-
const endpoint = new AWS.Endpoint(host);
8-
9-
const request = new AWS.HttpRequest(endpoint);
10-
request.method = method.toUpperCase();
11-
request.path = path;
12-
request.body = body;
13-
request.headers.Host = host;
14-
request.headers['X-Amz-Date'] = new Date().toISOString().replace(/[:\-]|\.\d{3}/g, '');
15-
const sha256hash = AWS.util.crypto.sha256(request.body || '', 'hex');
16-
request.headers['X-Amz-Content-SHA256'] = sha256hash;
17-
request.region = 'us-east-1';
18-
19-
const signer = new AWS.Signers.V4(request, service);
20-
const accessKeyId = config?.accessKey || AWS.config.credentials?.accessKeyId;
21-
const secretAccessKey = config?.secretKey || AWS.config.credentials?.secretAccessKey;
22-
const credentials = new AWS.Credentials(accessKeyId, secretAccessKey);
23-
signer.addAuthorization(credentials, new Date());
24-
25-
const url = `http://${host}${path}`;
9+
const region = 'us-east-1';
10+
11+
// Ensure host includes port for canonical request
12+
const hostname = host.split(':')[0]; // Extract 127.0.0.1
13+
const port = parseInt(host.split(':')[1] || '8000', 10); // Default to 8000
14+
const [pathBase, queryString] = path.split('?');
15+
const query = queryString ? Object.fromEntries(new URLSearchParams(queryString)) : {};
16+
17+
// Create HTTP request (mimics AWS.HttpRequest with v2-like endpoint structure)
18+
const request = new HttpRequest({
19+
protocol: 'http:', // Match Scality CloudServer
20+
hostname, // 127.0.0.1
21+
port, // 8000
22+
method: method.toUpperCase(),
23+
path: pathBase,
24+
query,
25+
body,
26+
headers: {
27+
Host: host, // Explicitly set Host: 127.0.0.1:8000
28+
'X-Amz-Date': signingDate.toISOString().replace(/[:\-]|\.\d{3}/g, ''),
29+
},
30+
});
31+
32+
// Compute SHA256 hash for body
33+
const sha256 = new Sha256();
34+
sha256.update(request.body || '');
35+
const hash = await sha256.digest();
36+
request.headers['X-Amz-Content-SHA256'] = Buffer.from(hash).toString('hex');
37+
request.region = region;
38+
39+
// Get credentials
40+
const accessKeyId = config?.accessKey || config?.accessKeyId || 'accessKey1';
41+
const secretAccessKey = config?.secretKey || config?.secretAccessKey || 'verySecretKey1';
42+
if (!accessKeyId || !secretAccessKey) {
43+
throw new Error('Missing accessKeyId or secretAccessKey in config');
44+
}
45+
const credentials = { accessKeyId, secretAccessKey };
46+
47+
// Create signer
48+
const signer = new SignatureV4({
49+
credentials,
50+
region,
51+
service,
52+
sha256: Sha256,
53+
uriEscapePath: true,
54+
applyChecksum: true,
55+
});
56+
57+
// Sign request
58+
const signedRequest = await signer.sign(request, { signingDate });
59+
60+
// Rename 'authorization' to 'Authorization'
61+
if (signedRequest.headers.authorization) {
62+
signedRequest.headers.Authorization = signedRequest.headers.authorization;
63+
delete signedRequest.headers.authorization;
64+
}
65+
66+
// Send HTTP request
67+
const url = `http://${host}${path}`; // Match Scality CloudServer
2668
const options = {
27-
method: request.method,
28-
headers: request.headers,
69+
method: signedRequest.method,
70+
headers: signedRequest.headers,
2971
};
3072

31-
if (method !== 'GET') {
32-
options.body = request.body;
73+
if (method.toUpperCase() !== 'GET') {
74+
options.body = signedRequest.body;
3375
}
3476

35-
const response = await nodeFetch(url, options);
77+
let response;
78+
try {
79+
response = await (nodeFetch.default || nodeFetch)(url, options);
80+
} catch (error) {
81+
throw new Error(`HTTP request failed: ${error.message}`);
82+
}
3683
const text = await response.text();
37-
const result = await xml2js.parseStringPromise(text);
84+
85+
let result;
86+
try {
87+
result = await xml2js.parseStringPromise(text);
88+
} catch {
89+
result = { Error: { Message: text } };
90+
}
3891
if (result && result.Error) {
3992
throw result;
4093
}
4194

42-
return result;
95+
return {
96+
result,
97+
status: response.status,
98+
ok: response.ok,
99+
error: result?.Error ? text : null,
100+
request: signedRequest,
101+
};
43102
};
44103

45104
module.exports = {

0 commit comments

Comments
 (0)