Skip to content

Commit

Permalink
test(middleware-sdk-s3): only delete the s3-express bucket created by…
Browse files Browse the repository at this point in the history
… the test (#6077)
  • Loading branch information
trivikr authored May 8, 2024
1 parent 6bb0904 commit 20dd731
Showing 1 changed file with 35 additions and 74 deletions.
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;
}
}
}

0 comments on commit 20dd731

Please sign in to comment.