|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2020 Google LLC. |
| 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 | +declare(strict_types=1); |
| 19 | + |
| 20 | +namespace Google\Cloud\Samples\Functions\ImageMagick\Test; |
| 21 | + |
| 22 | +use Google\Cloud\Storage\StorageClient; |
| 23 | +use Google\Cloud\Logging\LoggingClient; |
| 24 | +use Google\Cloud\TestUtils\CloudFunctionDeploymentTrait; |
| 25 | +use Google\Cloud\TestUtils\EventuallyConsistentTestTrait; |
| 26 | +use Google\Cloud\TestUtils\GcloudWrapper\CloudFunction; |
| 27 | +use PHPUnit\Framework\ExpectationFailedException; |
| 28 | +use PHPUnit\Framework\TestCase; |
| 29 | + |
| 30 | +require_once __DIR__ . '/TestCasesTrait.php'; |
| 31 | + |
| 32 | +/** |
| 33 | + * Class DeployTest. |
| 34 | + * |
| 35 | + * This test is not run by the CI system. |
| 36 | + * |
| 37 | + * To skip deployment of a new function, run with "GOOGLE_SKIP_DEPLOYMENT=true". |
| 38 | + * To skip deletion of the tested function, run with "GOOGLE_KEEP_DEPLOYMENT=true". |
| 39 | + */ |
| 40 | +class DeployTest extends TestCase |
| 41 | +{ |
| 42 | + use CloudFunctionDeploymentTrait; |
| 43 | + use EventuallyConsistentTestTrait; |
| 44 | + use TestCasesTrait; |
| 45 | + |
| 46 | + /** @var string */ |
| 47 | + private static $entryPoint = 'blurOffensiveImages'; |
| 48 | + |
| 49 | + /** @var string */ |
| 50 | + private static $functionSignatureType = 'cloudevent'; |
| 51 | + |
| 52 | + /** @var string */ |
| 53 | + // The test starts by copying images from this bucket. |
| 54 | + private const FIXTURE_SOURCE_BUCKET = 'cloud-devrel-public'; |
| 55 | + |
| 56 | + /** @var string */ |
| 57 | + // This is the bucket the deployed function monitors. |
| 58 | + // The test copies image from FIXTURE_SOURCE_BUCKET to this one. |
| 59 | + private static $monitoredBucket; |
| 60 | + |
| 61 | + /** @var string */ |
| 62 | + // The function saves any blurred images to this bucket. |
| 63 | + private static $blurredBucket; |
| 64 | + |
| 65 | + /** @var StorageClient */ |
| 66 | + private static $storageClient; |
| 67 | + |
| 68 | + /** @var LoggingClient */ |
| 69 | + private static $loggingClient; |
| 70 | + |
| 71 | + /** |
| 72 | + * @dataProvider cases |
| 73 | + */ |
| 74 | + public function testFunction( |
| 75 | + $cloudevent, |
| 76 | + $label, |
| 77 | + $fileName, |
| 78 | + $expected, |
| 79 | + $statusCode |
| 80 | + ): void { |
| 81 | + // Upload target file. |
| 82 | + $fixtureBucket = self::$storageClient->bucket(self::FIXTURE_SOURCE_BUCKET); |
| 83 | + $object = $fixtureBucket->object($fileName); |
| 84 | + |
| 85 | + $object->copy(self::$monitoredBucket, ['name' => $fileName]); |
| 86 | + |
| 87 | + // Give event and log systems a head start. |
| 88 | + // If log retrieval fails to find logs for our function within retry limit, increase sleep time. |
| 89 | + sleep(5); |
| 90 | + |
| 91 | + $fiveMinAgo = date(\DateTime::RFC3339, strtotime('-5 minutes')); |
| 92 | + $this->processFunctionLogs(self::$fn, $fiveMinAgo, function (\Iterator $logs) use ($expected, $label) { |
| 93 | + // Concatenate all relevant log messages. |
| 94 | + $actual = ''; |
| 95 | + foreach ($logs as $log) { |
| 96 | + $info = $log->info(); |
| 97 | + $actual .= $info['textPayload']; |
| 98 | + } |
| 99 | + |
| 100 | + // Only testing one property to decrease odds the expected logs are |
| 101 | + // split between log requests. |
| 102 | + $this->assertStringContainsString($expected, $actual, $label . ':'); |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Retrieve and process logs for the defined function. |
| 108 | + * |
| 109 | + * @param CloudFunction $fn function whose logs should be checked. |
| 110 | + * @param string $startTime RFC3339 timestamp marking start of time range to retrieve. |
| 111 | + * @param callable $process callback function to run on the logs. |
| 112 | + */ |
| 113 | + private function processFunctionLogs(CloudFunction $fn, string $startTime, callable $process) |
| 114 | + { |
| 115 | + $projectId = self::requireEnv('GOOGLE_PROJECT_ID'); |
| 116 | + |
| 117 | + if (empty(self::$loggingClient)) { |
| 118 | + self::$loggingClient = new LoggingClient([ |
| 119 | + 'projectId' => $projectId |
| 120 | + ]); |
| 121 | + } |
| 122 | + |
| 123 | + // Define the log search criteria. |
| 124 | + $logFullName = 'projects/' . $projectId . '/logs/cloudfunctions.googleapis.com%2Fcloud-functions'; |
| 125 | + $filter = sprintf( |
| 126 | + 'logName="%s" resource.labels.function_name="%s" timestamp>="%s"', |
| 127 | + $logFullName, |
| 128 | + $fn->getFunctionName(), |
| 129 | + $startTime |
| 130 | + ); |
| 131 | + |
| 132 | + echo "\nRetrieving logs [$filter]...\n"; |
| 133 | + |
| 134 | + // Check for new logs for the function. |
| 135 | + $attempt = 1; |
| 136 | + $this->runEventuallyConsistentTest(function () use ($filter, $process, &$attempt) { |
| 137 | + $entries = self::$loggingClient->entries(['filter' => $filter]); |
| 138 | + |
| 139 | + // If no logs came in try again. |
| 140 | + if (empty($entries->current())) { |
| 141 | + echo 'Logs not found, attempting retry #' . $attempt++ . PHP_EOL; |
| 142 | + throw new ExpectationFailedException('Log Entries not available'); |
| 143 | + } |
| 144 | + echo 'Processing logs...' . PHP_EOL; |
| 145 | + |
| 146 | + $process($entries); |
| 147 | + }, $retries = 10); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Deploy the Function. |
| 152 | + * |
| 153 | + * Overrides CloudFunctionLocalTestTrait::doDeploy(). |
| 154 | + */ |
| 155 | + private static function doDeploy() |
| 156 | + { |
| 157 | + // Initialize variables |
| 158 | + if (empty(self::$monitoredBucket)) { |
| 159 | + self::$monitoredBucket = self::requireEnv('GOOGLE_STORAGE_BUCKET'); |
| 160 | + } |
| 161 | + if (empty(self::$blurredBucket)) { |
| 162 | + self::$blurredBucket = self::requireEnv('BLURRED_BUCKET_NAME'); |
| 163 | + } |
| 164 | + |
| 165 | + if (empty(self::$storageClient)) { |
| 166 | + self::$storageClient = new StorageClient(); |
| 167 | + } |
| 168 | + |
| 169 | + // Forward required env variables to Cloud Functions. |
| 170 | + $envVars = 'GOOGLE_STORAGE_BUCKET=' . self::$monitoredBucket . ','; |
| 171 | + $envVars .= 'BLURRED_BUCKET_NAME=' . self::$blurredBucket; |
| 172 | + |
| 173 | + self::$fn->deploy( |
| 174 | + ['--update-env-vars' => $envVars], |
| 175 | + '--trigger-bucket=' . self::$monitoredBucket |
| 176 | + ); |
| 177 | + } |
| 178 | +} |
0 commit comments