Skip to content

Commit aaadc83

Browse files
author
Ace Nassri
authored
chore(functions): use processFunctionLogs in helper library (GoogleCloudPlatform#1288)
This **will not pass** until [this `php-tools` PR](GoogleCloudPlatform/php-tools#99) is merged. (It may _also_ require other dependency updates!) Part of GoogleCloudPlatform#1271
1 parent cbbeeaf commit aaadc83

File tree

7 files changed

+7
-342
lines changed

7 files changed

+7
-342
lines changed

functions/firebase_firestore/test/DeployTest.php

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@
2222
use Google\Cloud\Firestore\FirestoreClient;
2323
use Google\Cloud\Logging\LoggingClient;
2424
use Google\Cloud\TestUtils\CloudFunctionDeploymentTrait;
25-
use Google\Cloud\TestUtils\EventuallyConsistentTestTrait;
26-
use Google\Cloud\TestUtils\GcloudWrapper\CloudFunction;
2725
use PHPUnit\Framework\TestCase;
28-
use PHPUnit\Framework\ExpectationFailedException;
2926

3027
/**
3128
* Class DeployTest.
@@ -38,7 +35,6 @@
3835
class DeployTest extends TestCase
3936
{
4037
use CloudFunctionDeploymentTrait;
41-
use EventuallyConsistentTestTrait;
4238

4339
/** @var string */
4440
private static $entryPoint = 'firebaseFirestore';
@@ -108,7 +104,7 @@ public function testFirebaseFirestore(array $data, string $expected): void
108104
sleep(5);
109105

110106
$fiveMinAgo = date(\DateTime::RFC3339, strtotime('-5 minutes'));
111-
$this->processFunctionLogs(self::$fn, $fiveMinAgo, function (\Iterator $logs) use ($expected) {
107+
$this->processFunctionLogs($fiveMinAgo, function (\Iterator $logs) use ($expected) {
112108
// Concatenate all relevant log messages.
113109
$actual = '';
114110
foreach ($logs as $log) {
@@ -124,50 +120,6 @@ public function testFirebaseFirestore(array $data, string $expected): void
124120
});
125121
}
126122

127-
/**
128-
* Retrieve and process logs for the defined function.
129-
*
130-
* @param CloudFunction $fn function whose logs should be checked.
131-
* @param string $startTime RFC3339 timestamp marking start of time range to retrieve.
132-
* @param callable $process callback function to run on the logs.
133-
*/
134-
private function processFunctionLogs(CloudFunction $fn, string $startTime, callable $process)
135-
{
136-
$projectId = self::requireEnv('GOOGLE_PROJECT_ID');
137-
138-
if (empty(self::$loggingClient)) {
139-
self::$loggingClient = new LoggingClient([
140-
'projectId' => $projectId
141-
]);
142-
}
143-
144-
// Define the log search criteria.
145-
$logFullName = 'projects/' . $projectId . '/logs/cloudfunctions.googleapis.com%2Fcloud-functions';
146-
$filter = sprintf(
147-
'logName="%s" resource.labels.function_name="%s" timestamp>="%s"',
148-
$logFullName,
149-
$fn->getFunctionName(),
150-
$startTime
151-
);
152-
153-
echo "\nRetrieving logs [$filter]...\n";
154-
155-
// Check for new logs for the function.
156-
$attempt = 1;
157-
$this->runEventuallyConsistentTest(function () use ($filter, $process, &$attempt) {
158-
$entries = self::$loggingClient->entries(['filter' => $filter]);
159-
160-
// If no logs came in try again.
161-
if (empty($entries->current())) {
162-
echo 'Logs not found, attempting retry #' . $attempt++ . PHP_EOL;
163-
throw new ExpectationFailedException('Log Entries not available');
164-
}
165-
echo 'Processing logs...' . PHP_EOL;
166-
167-
$process($entries);
168-
}, $retries = 10);
169-
}
170-
171123
/**
172124
* Update a value in Firebase Realtime Database (RTDB).
173125
*

functions/firebase_remote_config/test/DeployTest.php

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323
use Google\Auth\CredentialsLoader;
2424
use Google\Cloud\Logging\LoggingClient;
2525
use Google\Cloud\TestUtils\CloudFunctionDeploymentTrait;
26-
use Google\Cloud\TestUtils\EventuallyConsistentTestTrait;
27-
use Google\Cloud\TestUtils\GcloudWrapper\CloudFunction;
2826
use PHPUnit\Framework\TestCase;
29-
use PHPUnit\Framework\ExpectationFailedException;
3027

3128
/**
3229
* Class DeployTest.
@@ -39,7 +36,6 @@
3936
class DeployTest extends TestCase
4037
{
4138
use CloudFunctionDeploymentTrait;
42-
use EventuallyConsistentTestTrait;
4339

4440
/** @var string */
4541
private static $entryPoint = 'firebaseRemoteConfig';
@@ -110,7 +106,7 @@ public function testFirebaseRemoteConfig(
110106
sleep(5);
111107

112108
$fiveMinAgo = date(\DateTime::RFC3339, strtotime('-5 minutes'));
113-
$this->processFunctionLogs(self::$fn, $fiveMinAgo, function (\Iterator $logs) use ($expected, $label) {
109+
$this->processFunctionLogs($fiveMinAgo, function (\Iterator $logs) use ($expected, $label) {
114110
// Concatenate all relevant log messages.
115111
$actual = '';
116112
foreach ($logs as $log) {
@@ -126,50 +122,6 @@ public function testFirebaseRemoteConfig(
126122
});
127123
}
128124

129-
/**
130-
* Retrieve and process logs for the defined function.
131-
*
132-
* @param CloudFunction $fn function whose logs should be checked.
133-
* @param string $startTime RFC3339 timestamp marking start of time range to retrieve.
134-
* @param callable $process callback function to run on the logs.
135-
*/
136-
private function processFunctionLogs(CloudFunction $fn, string $startTime, callable $process)
137-
{
138-
$projectId = self::requireEnv('GOOGLE_PROJECT_ID');
139-
140-
if (empty(self::$loggingClient)) {
141-
self::$loggingClient = new LoggingClient([
142-
'projectId' => $projectId
143-
]);
144-
}
145-
146-
// Define the log search criteria.
147-
$logFullName = 'projects/' . $projectId . '/logs/cloudfunctions.googleapis.com%2Fcloud-functions';
148-
$filter = sprintf(
149-
'logName="%s" resource.labels.function_name="%s" timestamp>="%s"',
150-
$logFullName,
151-
$fn->getFunctionName(),
152-
$startTime
153-
);
154-
155-
echo "\nRetrieving logs [$filter]...\n";
156-
157-
// Check for new logs for the function.
158-
$attempt = 1;
159-
$this->runEventuallyConsistentTest(function () use ($filter, $process, &$attempt) {
160-
$entries = self::$loggingClient->entries(['filter' => $filter]);
161-
162-
// If no logs came in try again.
163-
if (empty($entries->current())) {
164-
echo 'Logs not found, attempting retry #' . $attempt++ . PHP_EOL;
165-
throw new ExpectationFailedException('Log Entries not available');
166-
}
167-
echo 'Processing logs...' . PHP_EOL;
168-
169-
$process($entries);
170-
}, $retries = 10);
171-
}
172-
173125
/**
174126
* Update a value in Firebase Remote Config.
175127
*

functions/helloworld_pubsub/test/DeployTest.php

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@
2121

2222
use Google\Cloud\Logging\LoggingClient;
2323
use Google\Cloud\TestUtils\CloudFunctionDeploymentTrait;
24-
use Google\Cloud\TestUtils\EventuallyConsistentTestTrait;
25-
use Google\Cloud\TestUtils\GcloudWrapper\CloudFunction;
2624
use Google\Cloud\PubSub\PubSubClient;
2725
use PHPUnit\Framework\TestCase;
28-
use PHPUnit\Framework\ExpectationFailedException;
2926

3027
/**
3128
* Class DeployTest.
@@ -38,7 +35,6 @@
3835
class DeployTest extends TestCase
3936
{
4037
use CloudFunctionDeploymentTrait;
41-
use EventuallyConsistentTestTrait;
4238

4339
private static $entryPoint = 'helloworldPubsub';
4440

@@ -80,7 +76,7 @@ public function testHelloworldPubsub(string $name, string $expected, string $lab
8076
sleep(60);
8177

8278
$fiveMinAgo = date(\DateTime::RFC3339, strtotime('-5 minutes'));
83-
$this->processFunctionLogs(self::$fn, $fiveMinAgo, function (\Iterator $logs) use ($name, $expected, $label) {
79+
$this->processFunctionLogs($fiveMinAgo, function (\Iterator $logs) use ($name, $expected, $label) {
8480
// Concatenate all relevant log messages.
8581
$actual = '';
8682
foreach ($logs as $log) {
@@ -106,50 +102,6 @@ private function publishMessage(string $name): void
106102
]);
107103
}
108104

109-
/**
110-
* Retrieve and process logs for the defined function.
111-
*
112-
* @param CloudFunction $fn function whose logs should be checked.
113-
* @param string $startTime RFC3339 timestamp marking start of time range to retrieve.
114-
* @param callable $process callback function to run on the logs.
115-
*/
116-
private function processFunctionLogs(CloudFunction $fn, string $startTime, callable $process)
117-
{
118-
$projectId = self::requireEnv('GOOGLE_PROJECT_ID');
119-
120-
if (empty(self::$loggingClient)) {
121-
self::$loggingClient = new LoggingClient([
122-
'projectId' => $projectId
123-
]);
124-
}
125-
126-
// Define the log search criteria.
127-
$logFullName = 'projects/' . $projectId . '/logs/cloudfunctions.googleapis.com%2Fcloud-functions';
128-
$filter = sprintf(
129-
'logName="%s" resource.labels.function_name="%s" timestamp>="%s"',
130-
$logFullName,
131-
$fn->getFunctionName(),
132-
$startTime
133-
);
134-
135-
echo "\nRetrieving logs [$filter]...\n";
136-
137-
// Check for new logs for the function.
138-
$attempt = 1;
139-
$this->runEventuallyConsistentTest(function () use ($filter, $process, &$attempt) {
140-
$entries = self::$loggingClient->entries(['filter' => $filter]);
141-
142-
// If no logs came in try again.
143-
if (empty($entries->current())) {
144-
echo 'Logs not found, attempting retry #' . $attempt++ . PHP_EOL;
145-
throw new ExpectationFailedException('Log Entries not available');
146-
}
147-
echo 'Processing logs...' . PHP_EOL;
148-
149-
$process($entries);
150-
}, $retries = 10);
151-
}
152-
153105
/**
154106
* Deploy the Cloud Function, called from DeploymentTrait::deployApp().
155107
*

functions/helloworld_storage/test/DeployTest.php

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@
2222
use Google\Cloud\Logging\LoggingClient;
2323
use Google\Cloud\Storage\StorageClient;
2424
use Google\Cloud\TestUtils\CloudFunctionDeploymentTrait;
25-
use Google\Cloud\TestUtils\EventuallyConsistentTestTrait;
26-
use Google\Cloud\TestUtils\GcloudWrapper\CloudFunction;
2725
use PHPUnit\Framework\TestCase;
28-
use PHPUnit\Framework\ExpectationFailedException;
2926

3027
/**
3128
* Class DeployTest.
@@ -38,7 +35,6 @@
3835
class DeployTest extends TestCase
3936
{
4037
use CloudFunctionDeploymentTrait;
41-
use EventuallyConsistentTestTrait;
4238

4339
/** @var string */
4440
private static $entryPoint = 'helloGCS';
@@ -90,7 +86,7 @@ public function testHelloGCS(string $name, string $expected): void
9086
sleep(5);
9187

9288
$fiveMinAgo = date(\DateTime::RFC3339, strtotime('-5 minutes'));
93-
$this->processFunctionLogs(self::$fn, $fiveMinAgo, function (\Iterator $logs) use ($expected) {
89+
$this->processFunctionLogs($fiveMinAgo, function (\Iterator $logs) use ($expected) {
9490
// Concatenate all relevant log messages.
9591
$actual = '';
9692
foreach ($logs as $log) {
@@ -106,49 +102,6 @@ public function testHelloGCS(string $name, string $expected): void
106102
unlink($objectUri);
107103
}
108104

109-
/**
110-
* Retrieve and process logs for the defined function.
111-
*
112-
* @param CloudFunction $fn function whose logs should be checked.
113-
* @param string $startTime RFC3339 timestamp marking start of time range to retrieve.
114-
* @param callable $process callback function to run on the logs.
115-
*/
116-
private function processFunctionLogs(CloudFunction $fn, string $startTime, callable $process)
117-
{
118-
$projectId = self::requireEnv('GOOGLE_PROJECT_ID');
119-
120-
if (empty(self::$loggingClient)) {
121-
self::$loggingClient = new LoggingClient([
122-
'projectId' => $projectId
123-
]);
124-
}
125-
126-
// Define the log search criteria.
127-
$logFullName = 'projects/' . $projectId . '/logs/cloudfunctions.googleapis.com%2Fcloud-functions';
128-
$filter = sprintf('logName="%s" resource.labels.function_name="%s" timestamp>="%s"',
129-
$logFullName,
130-
$fn->getFunctionName(),
131-
$startTime
132-
);
133-
134-
echo "\nRetrieving logs [$filter]...\n";
135-
136-
// Check for new logs for the function.
137-
$attempt = 1;
138-
$this->runEventuallyConsistentTest(function () use ($filter, $process, &$attempt) {
139-
$entries = self::$loggingClient->entries(['filter' => $filter]);
140-
141-
// If no logs came in try again.
142-
if (empty($entries->current())) {
143-
echo 'Logs not found, attempting retry #' . $attempt++ . PHP_EOL;
144-
throw new ExpectationFailedException('Log Entries not available');
145-
}
146-
echo 'Processing logs...' . PHP_EOL;
147-
148-
$process($entries);
149-
}, $retries = 10);
150-
}
151-
152105
/**
153106
* Upload data to the storage bucket.
154107
*

functions/imagemagick/test/DeployTest.php

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
use Google\Cloud\Storage\StorageClient;
2323
use Google\Cloud\Logging\LoggingClient;
2424
use Google\Cloud\TestUtils\CloudFunctionDeploymentTrait;
25-
use Google\Cloud\TestUtils\EventuallyConsistentTestTrait;
26-
use Google\Cloud\TestUtils\GcloudWrapper\CloudFunction;
27-
use PHPUnit\Framework\ExpectationFailedException;
2825
use PHPUnit\Framework\TestCase;
2926

3027
require_once __DIR__ . '/TestCasesTrait.php';
@@ -40,7 +37,6 @@
4037
class DeployTest extends TestCase
4138
{
4239
use CloudFunctionDeploymentTrait;
43-
use EventuallyConsistentTestTrait;
4440
use TestCasesTrait;
4541

4642
/** @var string */
@@ -89,7 +85,7 @@ public function testFunction(
8985
sleep(5);
9086

9187
$fiveMinAgo = date(\DateTime::RFC3339, strtotime('-5 minutes'));
92-
$this->processFunctionLogs(self::$fn, $fiveMinAgo, function (\Iterator $logs) use ($expected, $label) {
88+
$this->processFunctionLogs($fiveMinAgo, function (\Iterator $logs) use ($expected, $label) {
9389
// Concatenate all relevant log messages.
9490
$actual = '';
9591
foreach ($logs as $log) {
@@ -103,50 +99,6 @@ public function testFunction(
10399
});
104100
}
105101

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-
150102
/**
151103
* Deploy the Function.
152104
*

0 commit comments

Comments
 (0)