Skip to content

Commit 215be20

Browse files
author
Ace Nassri
authored
feat(functions): add logging helloworld (GoogleCloudPlatform#1198)
1 parent 7861ca2 commit 215be20

File tree

7 files changed

+290
-0
lines changed

7 files changed

+290
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"require": {
3+
"google/cloud-functions-framework": "^0.6"
4+
},
5+
"scripts": {
6+
"start": [
7+
"Composer\\Config::disableProcessTimeout",
8+
"FUNCTION_TARGET=helloLogging php -S localhost:${PORT:-8080} vendor/bin/router.php"
9+
]
10+
}
11+
}

functions/log_helloworld/index.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
// [START functions_log_helloworld]
19+
20+
use Psr\Http\Message\ServerRequestInterface;
21+
22+
function helloLogging(ServerRequestInterface $request): string
23+
{
24+
// Code running in Google Cloud Functions itself writes log entries to
25+
// Cloud Logging. (Default log severity level is INFO.)
26+
$log = fopen('php://stderr', 'wb');
27+
fwrite($log, "Log entry from fwrite().\n");
28+
29+
// You can also specify a severity level explicitly using structured logs.
30+
// See this page for a list of log severity values:
31+
// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity
32+
fwrite($log, json_encode([
33+
'message' => 'Structured log with error severity',
34+
'severity' => 'error'
35+
]) . PHP_EOL);
36+
37+
// This doesn't log anything
38+
error_log('error_log does not log in Cloud Functions!');
39+
40+
// Functions must return a String or PSR-7 Response object
41+
return '';
42+
}
43+
44+
// [END functions_log_helloworld]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
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+
<phpunit bootstrap="../../testing/bootstrap.php" convertWarningsToExceptions="false">
18+
<testsuites>
19+
<testsuite name="Cloud Functions Logging Hello World Test Suite">
20+
<directory>test</directory>
21+
</testsuite>
22+
</testsuites>
23+
<logging>
24+
<log type="coverage-clover" target="build/logs/clover.xml"/>
25+
</logging>
26+
<filter>
27+
<whitelist>
28+
<directory suffix=".php">.</directory>
29+
<exclude>
30+
<directory>./vendor</directory>
31+
</exclude>
32+
</whitelist>
33+
</filter>
34+
</phpunit>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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\HelloLogging\Test;
21+
22+
use Google\Cloud\TestUtils\CloudFunctionDeploymentTrait;
23+
use PHPUnit\Framework\TestCase;
24+
25+
require_once __DIR__ . '/TestCasesTrait.php';
26+
27+
/**
28+
* Class DeployTest.
29+
*
30+
* This test is not run by the CI system.
31+
*
32+
* To skip deployment of a new function, run with "GOOGLE_SKIP_DEPLOYMENT=true".
33+
* To skip deletion of the tested function, run with "GOOGLE_KEEP_DEPLOYMENT=true".
34+
*/
35+
class DeployTest extends TestCase
36+
{
37+
use CloudFunctionDeploymentTrait;
38+
use TestCasesTrait;
39+
40+
private static $name = 'helloLogging';
41+
42+
public function testFunction(): void
43+
{
44+
foreach (self::cases() as $test) {
45+
// Send a request to the function.
46+
$resp = $this->client->get('');
47+
48+
// Assert status code.
49+
$this->assertEquals('200', $resp->getStatusCode());
50+
51+
// Assert function output.
52+
$output = trim((string) $resp->getBody());
53+
54+
if (isset($test['not_contains'])) {
55+
$this->assertNotContains($test['not_contains'], $output);
56+
}
57+
}
58+
}
59+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
declare(strict_types=1);
18+
19+
namespace Google\Cloud\Samples\Functions\HelloLogging\Test;
20+
21+
use PHPUnit\Framework\TestCase;
22+
use Google\Cloud\TestUtils\CloudFunctionLocalTestTrait;
23+
24+
require_once __DIR__ . '/TestCasesTrait.php';
25+
26+
/**
27+
* Class SystemTest.
28+
*/
29+
class SystemTest extends TestCase
30+
{
31+
use CloudFunctionLocalTestTrait;
32+
use TestCasesTrait;
33+
34+
private static $name = 'helloLogging';
35+
36+
public function testFunction(): void
37+
{
38+
foreach (self::cases() as $test) {
39+
// Send a request to the function.
40+
$resp = $this->client->get('/');
41+
42+
// Assert status code.
43+
$this->assertEquals('200', $resp->getStatusCode());
44+
45+
// Assert function output.
46+
$response = trim((string) $resp->getBody());
47+
48+
if (isset($test['not_contains'])) {
49+
$this->assertNotContains($test['not_contains'], $response);
50+
}
51+
}
52+
}
53+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
declare(strict_types=1);
18+
19+
namespace Google\Cloud\Samples\Functions\HelloLogging\Test;
20+
21+
trait TestCasesTrait
22+
{
23+
public static function cases(): array
24+
{
25+
return [
26+
/* These should show up in logs, not the response */
27+
[ 'not_contains' => 'Log entry from fwrite()', ],
28+
[ 'not_contains' => 'Structured log', ],
29+
];
30+
}
31+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
declare(strict_types=1);
18+
19+
namespace Google\Cloud\Samples\Functions\HelloLogging\Test;
20+
21+
use GuzzleHttp\Psr7\ServerRequest;
22+
use PHPUnit\Framework\TestCase;
23+
24+
require_once __DIR__ . '/TestCasesTrait.php';
25+
26+
/**
27+
* Unit tests for the Cloud Function.
28+
*/
29+
class UnitTest extends TestCase
30+
{
31+
use TestCasesTrait;
32+
33+
private static $name = 'helloLogging';
34+
35+
public static function setUpBeforeClass(): void
36+
{
37+
require_once __DIR__ . '/../index.php';
38+
}
39+
40+
public function testFunction(): void
41+
{
42+
foreach (self::cases() as $test) {
43+
$request = new ServerRequest('GET', '/');
44+
45+
$this->runFunction(self::$name, [$request]);
46+
$output = $this->getActualOutput();
47+
48+
if (isset($test['not_contains'])) {
49+
$this->assertNotContains($test['not_contains'], $output);
50+
}
51+
}
52+
}
53+
54+
private static function runFunction($functionName, array $params = []): void
55+
{
56+
call_user_func_array($functionName, $params);
57+
}
58+
}

0 commit comments

Comments
 (0)