Skip to content

Commit 6f95c8b

Browse files
author
Takashi Matsuo
committed
Complete modules sample.
1 parent 8d271d9 commit 6f95c8b

File tree

13 files changed

+172
-46
lines changed

13 files changed

+172
-46
lines changed

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ script:
7373
- composer install
7474
- phpunit
7575
- popd
76+
# run modules tests
77+
- pushd appengine/standard/modules
78+
- if [ ${TRAVIS_PHP_VERSION} == '5.5' ]; then composer install; fi;
79+
- if [ ${TRAVIS_PHP_VERSION} == '5.5' ]; then env PHP_CGI_PATH=`which php-cgi` LOCAL_TEST_TARGETS='app.yaml backend.yaml' phpunit; fi;
80+
- popd
7681

7782
after_script:
7883
- composer install

appengine/standard/modules/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Google App Engine Modules API
2+
3+
This sample application demonstrates how to use Google App Engine's
4+
modules API.
5+
6+
## Prerequisites
7+
8+
- Install [`composer`](https://getcomposer.org)
9+
- Install dependencies by running:
10+
11+
```sh
12+
composer install
13+
```
14+
15+
## Deploy to App Engine
16+
17+
**Prerequisites**
18+
19+
- Install the [Google Cloud SDK](https://developers.google.com/cloud/sdk/).
20+
21+
**Deploy with gcloud**
22+
23+
```
24+
gcloud config set project YOUR_PROJECT_ID
25+
gcloud preview app deploy app.yaml backend.yaml
26+
gcloud preview app browse
27+
```
28+
29+
The last command will open `https://{YOUR_PROJECT_ID}.appspot.com/`
30+
in your browser.

appengine/standard/modules/app.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,19 @@
2525
$app = new Application();
2626

2727
$app->get('/', function () use ($app) {
28-
// [START modules_methods]
28+
// [START simple_methods]
2929
$module = ModulesService::getCurrentModuleName();
3030
$instance = ModulesService::getCurrentInstanceId();
31-
// [END modules_methods]
31+
// [END simple_methods]
3232
return "$module:$instance";
3333
});
3434

35+
$app->get('/access_backend', function () use ($app) {
36+
// [START access_another_module]
37+
$url = 'http://' . ModulesService::getHostname('my-backend') . '/';
38+
$result = file_get_contents($url);
39+
// [END access_another_module]
40+
return $result;
41+
});
42+
3543
return $app;

appengine/standard/modules/app.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
runtime: php55
2+
threadsafe: yes
23
api_version: 1
34

45
handlers:
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
/**
3+
* Copyright 2016 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+
/*
19+
* This application is only for testig purpose. Please deploy this app with
20+
* backend.yaml if you want to run tests/DeployTests.php.
21+
*/
22+
echo 'This is my backend.';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
runtime: php55
2+
api_version: 1
3+
threadsafe: true
4+
module: my-backend
5+
6+
handlers:
7+
- url: /.*
8+
script: backend.php

appengine/standard/modules/tests/DeployTest.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,5 @@
2121
class DeployTest extends \PHPUnit_Framework_TestCase
2222
{
2323
use E2EDeploymentTrait;
24-
25-
public function testIndex()
26-
{
27-
// Access the modules app top page.
28-
$resp = $this->client->get('');
29-
$this->assertEquals('200', $resp->getStatusCode(),
30-
'top page status code');
31-
$this->assertContains(
32-
'default:',
33-
$resp->getBody()->getContents());
34-
}
24+
use ModulesTestTrait;
3525
}

appengine/standard/modules/tests/LocalTest.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,5 @@
2121
class LocalTest extends \PHPUnit_Framework_TestCase
2222
{
2323
use LocalTestTrait;
24-
25-
public function testIndex()
26-
{
27-
// Access the modules app top page.
28-
$resp = $this->client->get('');
29-
$this->assertEquals('200', $resp->getStatusCode(),
30-
'top page status code');
31-
$this->assertContains(
32-
'default:',
33-
$resp->getBody()->getContents());
34-
}
24+
use ModulesTestTrait;
3525
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
/**
3+
* Copyright 2016 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+
namespace Google\Cloud\Test;
18+
19+
trait ModulesTestTrait
20+
{
21+
public function testIndex()
22+
{
23+
// Access the modules app top page.
24+
try {
25+
$resp = $this->client->get('');
26+
} catch (\GuzzleHttp\Exception\ServerException $e) {
27+
$this->fail($e->getResponse()->getBody());
28+
}
29+
$this->assertEquals('200', $resp->getStatusCode(),
30+
'top page status code');
31+
$this->assertContains(
32+
'default:',
33+
$resp->getBody()->getContents());
34+
}
35+
36+
public function testAccessBackEnd()
37+
{
38+
// Access the '/access_backend'
39+
try {
40+
$resp = $this->client->get('/access_backend');
41+
} catch (\GuzzleHttp\Exception\ServerException $e) {
42+
$this->fail($e->getResponse()->getBody());
43+
}
44+
$this->assertEquals('200', $resp->getStatusCode(),
45+
'/access_backend status code');
46+
$this->assertContains(
47+
'This is my backend.',
48+
$resp->getBody()->getContents());
49+
}
50+
}

testing/E2EDeploymentTrait.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,27 @@ trait E2EDeploymentTrait
3030
private static $gaeApp;
3131
private $client;
3232

33-
public static $projectEnv = 'GOOGLE_PROJECT_ID';
34-
public static $versionEnv = 'GOOGLE_VERSION_ID';
33+
private static $projectEnv = 'GOOGLE_PROJECT_ID';
34+
private static $versionEnv = 'GOOGLE_VERSION_ID';
35+
private static $projectId;
36+
private static $versionId;
3537

3638
/**
3739
* @beforeClass
3840
*/
3941
public static function deployApp()
4042
{
41-
$project_id = getenv(self::$projectEnv);
42-
$e2e_test_version = getenv(self::$versionEnv);
43-
if ($project_id === false) {
43+
self::$projectId = getenv(self::$projectEnv);
44+
self::$versionId = getenv(self::$versionEnv);
45+
if (self::$projectId === false) {
4446
self::fail('Please set ' . self::$projectEnv . ' env var.');
4547
}
46-
if ($e2e_test_version === false) {
48+
if (self::$versionId === false) {
4749
self::fail('Please set ' . self::$versionEnv . ' env var.');
4850
}
4951
self::$gaeApp = new GaeApp(
50-
$project_id,
51-
$e2e_test_version
52+
self::$projectId,
53+
self::$versionId
5254
);
5355
if (self::$gaeApp->deploy() === false) {
5456
self::fail('Deployment failed.');

testing/GaeApp.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ public function __construct(
8888
/**
8989
* Deploys the app to the Google Cloud Platform.
9090
*
91-
* @param string $target yaml files for deployments
91+
* @param string $targets optional yaml files for deployments
9292
* @param bool $promote optional true if you want to promote the new app
9393
* @param int $retry optional number of retries upon failure
9494
*
9595
* @return bool true if deployment suceeds, false upon failure
9696
*/
9797
public function deploy(
98-
$target = 'app.yaml',
98+
$targets = 'app.yaml',
9999
$promote = false,
100100
$retry = self::DEFAULT_RETRY
101101
) {
@@ -104,6 +104,10 @@ public function deploy(
104104
return false;
105105
}
106106
$orgDir = getcwd();
107+
if (chdir($this->dir) === false) {
108+
$this->errorLog('Can not chdir to ' . $this->dir);
109+
return false;
110+
}
107111
$cmd = "gcloud -q " . self::GCLOUD_APP . " deploy "
108112
. "--project " . $this->project . " "
109113
. "--version " . $this->version . " ";
@@ -112,7 +116,7 @@ public function deploy(
112116
} else {
113117
$cmd .= "--no-promote ";
114118
}
115-
$cmd .= $target;
119+
$cmd .= $targets;
116120
$ret = $this->execWithRetry($cmd, $retry);
117121
chdir($orgDir);
118122
if ($ret) {
@@ -124,17 +128,25 @@ public function deploy(
124128
/**
125129
* Runs the app with dev_appserver.
126130
*
131+
* @param string $targets optional yaml files for local run
132+
* @param string $phpCgiPath optional path to php-cgi
127133
* @return bool true if the app is running, otherwise false
128134
*/
129-
public function run()
130-
{
131-
$options = '--port ' . $this->port
132-
. ' --php_executable_path ' . PHP_BINARY;
135+
public function run(
136+
$targets = 'app.yaml',
137+
$phpCgiPath = '/usr/bin/php-cgi'
138+
) {
133139
$cmd = 'dev_appserver.py --port ' . $this->port
134-
. ' --php_executable_path ' . PHP_BINARY
135-
. ' ' . $this->dir;
140+
. ' --php_executable_path ' . $phpCgiPath
141+
. ' ' . $targets;
142+
$orgDir = getcwd();
143+
if (chdir($this->dir) === false) {
144+
$this->errorLog('Can not chdir to ' . $this->dir);
145+
return false;
146+
}
136147
$this->process = new Process($cmd);
137148
$this->process->start();
149+
chdir($orgDir);
138150
sleep(3);
139151
if (! $this->process->isRunning()) {
140152
$this->errorLog('dev_appserver failed to run.');

testing/LocalTestTrait.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,16 @@ trait LocalTestTrait
3535
*/
3636
public static function startServer()
3737
{
38-
self::markTestIncomplete(
39-
'dev_appserver.py is not working for us now.'
40-
. ' TODO: Make this work with specifying --php_gae_extension_path'
41-
);
38+
$phpCgi = getenv('PHP_CGI_PATH');
39+
if ($phpCgi === false) {
40+
$phpCgi = '/usr/bin/php-cgi';
41+
}
42+
$targets = getenv('LOCAL_TEST_TARGETS');
43+
if ($targets === false) {
44+
$targets = 'app.yaml';
45+
}
4246
self::$gaeApp = new GaeApp('', '');
43-
if (self::$gaeApp->run() === false) {
47+
if (self::$gaeApp->run($targets, $phpCgi) === false) {
4448
self::fail('dev_appserver failed');
4549
}
4650
}

testing/install_test_deps.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ gcloud config set app/promote_by_default false
3030
gcloud config set app/use_cloud_build true
3131
gcloud auth activate-service-account --key-file \
3232
"${GOOGLE_APPLICATION_CREDENTIALS}"
33+
gcloud -q components install app-engine-python
34+
gcloud -q components install app-engine-php
35+
# pinning to 104.0.0 because 105.0.0 is broken for php app
36+
gcloud -q components update --version 104.0.0

0 commit comments

Comments
 (0)