Skip to content

Commit 29ab814

Browse files
author
Krishnan Parthasarathi
committed
Add test for re-created bucket and bucket policy
1 parent 8f01dab commit 29ab814

File tree

1 file changed

+76
-9
lines changed

1 file changed

+76
-9
lines changed

run/core/aws-sdk-php/quick-tests.php

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,12 @@ function initSetup(S3Client $s3Client, $objects) {
373373
$stream->close();
374374
}
375375
}
376+
377+
// Create an empty bucket for bucket policy + delete tests
378+
$result = $s3Client->createBucket(['Bucket' => $GLOBALS['emptyBucket']]);
379+
if (getStatusCode($result) != HTTP_OK)
380+
throw new Exception("createBucket API failed for " . $bucket);
381+
376382
}
377383

378384

@@ -756,6 +762,63 @@ function testBucketPolicy($s3Client, $bucket) {
756762

757763
if ($result['Policy'] != $downloadPolicy)
758764
throw new Exception('bucket policy we got is not we set');
765+
766+
// Delete the bucket, make the bucket (again) and check if policy is none
767+
// Ref: https://github.com/minio/minio/issues/4714
768+
$result = $s3Client->deleteBucket(['Bucket' => $bucket]);
769+
if (getstatuscode($result) != HTTP_NOCONTENT)
770+
throw new Exception('deleteBucket API failed for ' .
771+
$bucket);
772+
773+
$result = $s3Client->createBucket(['Bucket' => $bucket]);
774+
if (getstatuscode($result) != HTTP_OK)
775+
throw new Exception('createBucket API failed for ' .
776+
$bucket);
777+
778+
$params = [
779+
'404' => ['Bucket' => $bucket]
780+
];
781+
runExceptionalTests($s3Client, 'getBucketPolicy', 'getStatusCode', $params);
782+
783+
try {
784+
$MINT_DATA_DIR = $GLOBALS['MINT_DATA_DIR'];
785+
// Create an object to test anonymous GET object
786+
$object = 'test-anon';
787+
if (!file_exists($MINT_DATA_DIR . '/' . FILE_10KB))
788+
throw new Exception('File not found ' . $MINT_DATA_DIR . '/' . FILE_10KB);
789+
790+
$stream = Psr7\stream_for(fopen($MINT_DATA_DIR . '/' . FILE_10KB, 'r'));
791+
$result = $s3Client->putObject([
792+
'Bucket' => $bucket,
793+
'Key' => $object,
794+
'Body' => $stream,
795+
]);
796+
if (getstatuscode($result) != HTTP_OK)
797+
throw new Exception('createBucket API failed for ' .
798+
$bucket);
799+
800+
$anonConfig = new ClientConfig("", "", $GLOBALS['endpoint'], $GLOBALS['secure']);
801+
$anonymousClient = new S3Client([
802+
'credentials' => false,
803+
'endpoint' => $anonConfig->endpoint,
804+
'use_path_style_endpoint' => true,
805+
'region' => 'us-east-1',
806+
'version' => '2006-03-01'
807+
]);
808+
runExceptionalTests($anonymousClient, 'getObject', 'getStatusCode', [
809+
'403' => [
810+
'Bucket' => $bucket,
811+
'Key' => $object,
812+
]
813+
]);
814+
815+
} finally {
816+
// close data file
817+
if (!is_null($stream))
818+
$stream->close();
819+
$s3Client->deleteObject(['Bucket' => $bucket, 'Key' => $object]);
820+
}
821+
759822
}
760823

761824
/**
@@ -774,8 +837,10 @@ function cleanupSetup($s3Client, $objects) {
774837
$s3Client->deleteObject(['Bucket' => $bucket, 'Key' => $object]);
775838
}
776839

777-
// Delete the buckets
778-
foreach (array_keys($objects) as $bucket) {
840+
// Delete the buckets incl. emptyBucket
841+
$allBuckets = array_keys($objects);
842+
array_push($allBuckets, $GLOBALS['emptyBucket']);
843+
foreach ($allBuckets as $bucket) {
779844
// Delete the bucket
780845
$s3Client->deleteBucket(['Bucket' => $bucket]);
781846

@@ -811,10 +876,10 @@ function runTestFunction($myfunc, ...$args) {
811876
}
812877

813878
// Get client configuration from environment variables
814-
$access_key = getenv("ACCESS_KEY");
815-
$secret_key = getenv("SECRET_KEY");
816-
$endpoint = getenv("SERVER_ENDPOINT");
817-
$secure = getenv("ENABLE_HTTPS");
879+
$GLOBALS['access_key'] = getenv("ACCESS_KEY");
880+
$GLOBALS['secret_key'] = getenv("SECRET_KEY");
881+
$GLOBALS['endpoint'] = getenv("SERVER_ENDPOINT");
882+
$GLOBALS['secure'] = getenv("ENABLE_HTTPS");
818883

819884
/**
820885
* @global string $GLOBALS['MINT_DATA_DIR']
@@ -825,8 +890,8 @@ function runTestFunction($myfunc, ...$args) {
825890

826891

827892
// Create config object
828-
$config = new ClientConfig($access_key, $secret_key, $endpoint, $secure);
829-
893+
$config = new ClientConfig($GLOBALS['access_key'], $GLOBALS['secret_key'],
894+
$GLOBALS['endpoint'], $GLOBALS['secure']);
830895

831896
// Create a S3Client
832897
$s3Client = new S3Client([
@@ -837,6 +902,8 @@ function runTestFunction($myfunc, ...$args) {
837902
'version' => '2006-03-01'
838903
]);
839904

905+
// Used by initSetup
906+
$emptyBucket = randomName();
840907
$objects = [
841908
randomName() => 'obj1',
842909
randomName() => 'obj2',
@@ -858,7 +925,7 @@ function runTestFunction($myfunc, ...$args) {
858925
runTestFunction('testMultipartUpload', $s3Client, $firstBucket, $firstObject);
859926
runTestFunction('testMultipartUploadFailure', $s3Client, $firstBucket, $firstObject);
860927
runTestFunction('testAbortMultipartUpload', $s3Client, $firstBucket, $firstObject);
861-
runTestFunction('testBucketPolicy', $s3Client, $firstBucket);
928+
runTestFunction('testBucketPolicy', $s3Client, $emptyBucket);
862929
}
863930
finally {
864931
cleanupSetup($s3Client, $objects);

0 commit comments

Comments
 (0)