|
| 1 | +
|
| 2 | +/** |
| 3 | + * Copyright 2021 Google Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace Google\Cloud\Samples\StorageTransfer; |
| 19 | + |
| 20 | +use Google\Cloud\Storage\StorageClient; |
| 21 | +use Google\Cloud\StorageTransfer\V1\StorageTransferServiceClient; |
| 22 | +use Google\Cloud\StorageTransfer\V1\TransferJob\Status; |
| 23 | +use Google\Cloud\TestUtils\TestTrait; |
| 24 | +use Google\Cloud\StorageTransfer\V1\TransferJob; |
| 25 | +use PHPUnit\Framework\TestCase; |
| 26 | + |
| 27 | +class StorageTransferTest extends TestCase |
| 28 | +{ |
| 29 | + use TestTrait; |
| 30 | + |
| 31 | + private static $sourceBucket; |
| 32 | + private static $sinkBucket; |
| 33 | + private static $storage; |
| 34 | + private static $sts; |
| 35 | + |
| 36 | + public static function setUpBeforeClass(): void |
| 37 | + { |
| 38 | + self::checkProjectEnvVars(); |
| 39 | + self::$storage = new StorageClient(); |
| 40 | + self::$sts = new StorageTransferServiceClient(); |
| 41 | + $uniqueBucketId = time() . rand(); |
| 42 | + self::$sourceBucket = self::$storage->createBucket( |
| 43 | + sprintf('php-source-bucket-%s', $uniqueBucketId) |
| 44 | + ); |
| 45 | + self::$sinkBucket = self::$storage->createBucket( |
| 46 | + sprintf('php-sink-bucket-%s', $uniqueBucketId) |
| 47 | + ); |
| 48 | + |
| 49 | + self::grantStsPermissions(self::$sourceBucket); |
| 50 | + self::grantStsPermissions(self::$sinkBucket); |
| 51 | + } |
| 52 | + |
| 53 | + public static function tearDownAfterClass(): void |
| 54 | + { |
| 55 | + self::$sourceBucket->delete(); |
| 56 | + self::$sinkBucket->delete(); |
| 57 | + } |
| 58 | + |
| 59 | + public function testQuickstart() |
| 60 | + { |
| 61 | + $output = $this->runFunctionSnippet('quickstart', [ |
| 62 | + self::$projectId, self::$sinkBucket->name(), self::$sourceBucket->name() |
| 63 | + ]); |
| 64 | + |
| 65 | + $this->assertRegExp('/transferJobs\/.*/', $output); |
| 66 | + |
| 67 | + preg_match('/transferJobs\/\d+/', $output, $match); |
| 68 | + $jobName = $match[0]; |
| 69 | + $transferJob = new TransferJob([ |
| 70 | + 'name' => $jobName, |
| 71 | + 'status' => Status::DELETED |
| 72 | + ]); |
| 73 | + |
| 74 | + self::$sts->updateTransferJob($jobName, self::$projectId, $transferJob); |
| 75 | + } |
| 76 | + |
| 77 | + private static function grantStsPermissions($bucket) |
| 78 | + { |
| 79 | + $googleServiceAccount = self::$sts->getGoogleServiceAccount(self::$projectId); |
| 80 | + $email = $googleServiceAccount->getAccountEmail(); |
| 81 | + $members = ['serviceAccount:' . $email]; |
| 82 | + |
| 83 | + $policy = $bucket->iam()->policy(['requestedPolicyVersion' => 3]); |
| 84 | + $policy['version'] = 3; |
| 85 | + |
| 86 | + array_push( |
| 87 | + $policy['bindings'], |
| 88 | + ['role' => 'roles/storage.objectViewer', 'members' => $members], |
| 89 | + ['role' => 'roles/storage.legacyBucketReader', 'members' => $members], |
| 90 | + ['role' => 'roles/storage.legacyBucketWriter', 'members' => $members] |
| 91 | + ); |
| 92 | + |
| 93 | + $bucket->iam()->setPolicy($policy); |
| 94 | + } |
| 95 | +} |
0 commit comments