Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 5baee43

Browse files
authored
Add integration test (#3)
* Add an integration test that reports to a dockerized Jaeger instance * license header * Fix test namespace * Remove unused import
1 parent a189f86 commit 5baee43

File tree

7 files changed

+247
-1
lines changed

7 files changed

+247
-1
lines changed

Dockerfile.integration

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2018 OpenCensus Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
FROM gcr.io/google-appengine/php72
16+
17+
COPY . $APP_DIR
18+
RUN composer install
19+
20+
ENTRYPOINT []

cloudbuild.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,32 @@ steps:
2828
args: ['build', '--build-arg', 'BASE_IMAGE=gcr.io/php-stackdriver/php71-debug', '.']
2929
waitFor: ['-']
3030
id: php71-debug
31+
32+
# integration test
33+
- name: gcr.io/cloud-builders/docker
34+
args: ['network', 'create', '-d', 'bridge', 'nw_jaeger']
35+
id: test-network
36+
waitFor: ['-']
37+
38+
- name: gcr.io/cloud-builders/docker
39+
args:
40+
- 'run'
41+
- '-d'
42+
- '-e'
43+
- '-p6831:6831/udp'
44+
- '-p16686:16686'
45+
- '--name=jaeger-server'
46+
- '--network=nw_jaeger'
47+
- 'jaegertracing/all-in-one:latest'
48+
id: jaeger-server
49+
waitFor: ['test-network']
50+
51+
- name: gcr.io/cloud-builders/docker
52+
args: ['build', '-t', 'gcr.io/$PROJECT_ID/test-runner', '-f', 'Dockerfile.integration', '.']
53+
id: test-build
54+
waitFor: ['test-network']
55+
56+
- name: gcr.io/cloud-builders/docker
57+
args: ['run', '--network=nw_jaeger', '-e', 'JAEGER_HOST=jaeger-server', 'gcr.io/$PROJECT_ID/test-runner', 'vendor/bin/phpunit', '--config=./phpunit-integration.xml.dist']
58+
id: test-run
59+
waitFor: ['test-build', 'jaeger-server']

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
},
1010
"require-dev": {
1111
"phpunit/phpunit": "^6.0",
12-
"squizlabs/php_codesniffer": "2.*"
12+
"squizlabs/php_codesniffer": "2.*",
13+
"guzzlehttp/guzzle": "~6.0"
1314
},
1415
"license": "Apache-2.0",
1516
"authors": [

phpunit-integration.xml.dist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="./tests/integration/bootstrap.php" colors="true">
3+
<testsuites>
4+
<testsuite>
5+
<directory>tests/integration</directory>
6+
</testsuite>
7+
</testsuites>
8+
<filter>
9+
<whitelist>
10+
<directory suffix=".php">src</directory>
11+
</whitelist>
12+
</filter>
13+
</phpunit>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* Copyright 2018 OpenCensus Authors
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 OpenCensus\Tests\Integration\Trace\Exporter;
19+
20+
use GuzzleHttp\Client;
21+
use PHPUnit\Framework\TestCase;
22+
23+
class JaegerExporterTest extends TestCase
24+
{
25+
private static $jaegerClient;
26+
27+
public static function setUpBeforeClass()
28+
{
29+
parent::setUpBeforeClass();
30+
31+
$jaegerHost = getenv('JAEGER_HOST') ?: 'localhost';
32+
$jaegerPort = (int)(getenv('JAEGER_PORT') ?: 16686);
33+
self::$jaegerClient = new Client([
34+
'base_uri' => sprintf('http://%s:%d/', $jaegerHost, $jaegerPort)
35+
]);
36+
}
37+
38+
public function testReportsTraceToJaeger()
39+
{
40+
$rand = mt_rand();
41+
$client = new Client(['base_uri' => 'http://localhost:9000']);
42+
$response = $client->request('GET', '/', [
43+
'query' => [
44+
'rand' => $rand
45+
]
46+
]);
47+
$this->assertEquals(200, $response->getStatusCode());
48+
$this->assertEquals('Hello world!', $response->getBody()->getContents());
49+
50+
$response = $this->findTraces($rand);
51+
$this->assertEquals(200, $response->getStatusCode());
52+
$data = json_decode($response->getBody()->getContents(), true);
53+
$this->assertCount(1, $data['data']);
54+
$trace = $data['data'][0];
55+
$this->assertCount(2, $trace['spans']);
56+
}
57+
58+
public function testCanReachJaegerServer()
59+
{
60+
$response = self::$jaegerClient->request('GET', '/search');
61+
$this->assertEquals(200, $response->getStatusCode());
62+
}
63+
64+
private function findTraces($rand)
65+
{
66+
return self::$jaegerClient->request('GET', '/api/traces', [
67+
'query' => [
68+
'service' => 'integration-test',
69+
'operation' => "/?rand=$rand",
70+
'limit' => 20,
71+
'lookback' => '1h'
72+
]
73+
]);
74+
}
75+
}

tests/integration/bootstrap.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Copyright 2018 OpenCensusAuthors
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 PHPUnit bootstrap file starts a local web server using the built-in
20+
* PHP web server. The PHPUnit tests that run then hit this running server.
21+
* We also register a shutdown function to stop this server after tests have
22+
* run.
23+
*/
24+
25+
require __DIR__ . '/../../vendor/autoload.php';
26+
27+
$host = getenv('TESTHOST') ?: 'localhost';
28+
$port = (int)(getenv('TESTPORT') ?: 9000);
29+
putenv(
30+
sprintf(
31+
'TESTURL=http://%s:%d',
32+
$host,
33+
$port
34+
)
35+
);
36+
37+
$command = sprintf(
38+
'php -S %s:%d -t %s >/dev/null 2>&1 & echo $!',
39+
$host,
40+
$port,
41+
__DIR__ . '/web'
42+
);
43+
44+
$output = [];
45+
printf('Starting web server with command: %s' . PHP_EOL, $command);
46+
exec($command, $output);
47+
$pid = (int) $output[0];
48+
49+
printf(
50+
'%s - Web server started on %s:%d with PID %d',
51+
date('r'),
52+
$host,
53+
$port,
54+
$pid
55+
);
56+
57+
// Give the server time to boot.
58+
sleep(1);
59+
60+
// Kill the web server when the process ends
61+
register_shutdown_function(function() use ($pid) {
62+
echo sprintf('%s - Killing process with ID %d', date('r'), $pid) . PHP_EOL;
63+
exec('kill ' . $pid);
64+
});

tests/integration/web/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 2018 OpenCensus Authors
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+
require_once __DIR__ . '/../../../vendor/autoload.php';
19+
20+
use OpenCensus\Trace\Exporter\JaegerExporter;
21+
use OpenCensus\Trace\Tracer;
22+
23+
$host = getenv('JAEGER_HOST') ?: 'localhost';
24+
$exporter = new JaegerExporter('integration-test', [
25+
'host' => $host,
26+
'tags' => [
27+
'asdf' => 'qwer'
28+
]
29+
]);
30+
31+
Tracer::start($exporter, [
32+
'attributes' => [
33+
'foo' => 'bar'
34+
]
35+
]);
36+
37+
Tracer::inSpan(
38+
['name' => 'slow_function'],
39+
function () {
40+
usleep(50);
41+
}
42+
);
43+
44+
echo 'Hello world!';

0 commit comments

Comments
 (0)