Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(middleware-sdk-s3): only delete the s3-express bucket created by the test #6077

Merged
merged 3 commits into from
May 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,11 @@ describe("s3 express CRUD test suite", () => {
beforeAll(async () => {
({ s3, controller, bucketName, recorder } = await createClientAndRecorder());

await deleteBuckets(controller);
await s3.createBucket({
Bucket: bucketName,
CreateBucketConfiguration: {
Location: {
Type: "AvailabilityZone",
Name: zone,
},
Bucket: {
Type: "Directory",
DataRedundancy: "SingleAvailabilityZone",
},
Location: { Type: "AvailabilityZone", Name: zone },
Bucket: { Type: "Directory", DataRedundancy: "SingleAvailabilityZone" },
},
});

Expand Down Expand Up @@ -99,34 +92,22 @@ describe("s3 express CRUD test suite", () => {
});

afterAll(async () => {
await deleteBuckets(controller);
await emptyAndDeleteBucket(controller, bucketName);
});

it("can create a bucket", () => {
expect(createRecorder).toEqual({
"CreateBucketCommand (normal)": {
[bucketName]: 1,
},
"HeadBucketCommand (s3 express)": {
[bucketName]: 1,
},
"CreateSessionCommand (normal)": {
[bucketName]: 1,
},
"CreateBucketCommand (normal)": { [bucketName]: 1 },
"HeadBucketCommand (s3 express)": { [bucketName]: 1 },
"CreateSessionCommand (normal)": { [bucketName]: 1 },
});
});

it("can read/write/delete from a bucket", () => {
expect(readWriteDeleteRecorder).toEqual({
"PutObjectCommand (s3 express)": {
[bucketName]: SCALE,
},
"GetObjectCommand (s3 express)": {
[bucketName]: SCALE,
},
"DeleteObjectCommand (s3 express)": {
[bucketName]: SCALE,
},
"PutObjectCommand (s3 express)": { [bucketName]: SCALE },
"GetObjectCommand (s3 express)": { [bucketName]: SCALE },
"DeleteObjectCommand (s3 express)": { [bucketName]: SCALE },
});
});

Expand Down Expand Up @@ -209,12 +190,10 @@ describe("s3 express CRUD test suite", () => {
});

async function createClientAndRecorder() {
const sts = new STS({
region,
});
const sts = new STS({ region });
const accountId = (await sts.getCallerIdentity({})).Account;

const bucketName = `${accountId}-js-test-bucket-${(Date.now() / 1000) | 0}--${suffix}`;
const bucketName = `${accountId}-js-test-bucket-${(Math.random() + 1).toString(36).substring(2)}--${suffix}`;

const s3 = new S3({
region,
Expand Down Expand Up @@ -261,52 +240,34 @@ async function createClientAndRecorder() {
};
}

async function deleteBuckets(s3: S3) {
const buckets = await s3.listDirectoryBuckets({});

for (const bucket of buckets.Buckets ?? []) {
const Bucket = bucket.Name;
async function emptyAndDeleteBucket(s3: S3, bucketName: string) {
const Bucket = bucketName;
try {
await s3.headBucket({ Bucket });
} catch (e) {
return;
}

try {
await s3.headBucket({
Bucket,
});
} catch (e) {
return;
const list = await s3.listObjectsV2({ Bucket }).catch((e) => {
if (!String(e).includes("NoSuchBucket")) {
throw e;
}
return {
Contents: [],
};
});

const list = await s3
.listObjectsV2({
Bucket,
})
.catch((e) => {
if (!String(e).includes("NoSuchBucket")) {
throw e;
}
return {
Contents: [],
};
});

const promises = [] as Promise<any>[];
for (const key of list.Contents ?? []) {
promises.push(
s3.deleteObject({
Bucket,
Key: key.Key,
})
);
}
await Promise.all(promises);
const promises = [] as Promise<any>[];
for (const key of list.Contents ?? []) {
promises.push(s3.deleteObject({ Bucket, Key: key.Key }));
}
await Promise.all(promises);

try {
return await s3.deleteBucket({
Bucket,
});
} catch (e) {
if (!String(e).includes("NoSuchBucket")) {
throw e;
}
try {
return await s3.deleteBucket({ Bucket });
} catch (e) {
if (!String(e).includes("NoSuchBucket")) {
throw e;
}
}
}
Loading