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

Commit

Permalink
Add integration test (#3)
Browse files Browse the repository at this point in the history
* Add an integration test that reports to a dockerized Jaeger instance

* license header

* Fix test namespace

* Remove unused import
  • Loading branch information
chingor13 committed Apr 5, 2018
1 parent a189f86 commit 5baee43
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 1 deletion.
20 changes: 20 additions & 0 deletions Dockerfile.integration
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2018 OpenCensus Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM gcr.io/google-appengine/php72

COPY . $APP_DIR
RUN composer install

ENTRYPOINT []
29 changes: 29 additions & 0 deletions cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,32 @@ steps:
args: ['build', '--build-arg', 'BASE_IMAGE=gcr.io/php-stackdriver/php71-debug', '.']
waitFor: ['-']
id: php71-debug

# integration test
- name: gcr.io/cloud-builders/docker
args: ['network', 'create', '-d', 'bridge', 'nw_jaeger']
id: test-network
waitFor: ['-']

- name: gcr.io/cloud-builders/docker
args:
- 'run'
- '-d'
- '-e'
- '-p6831:6831/udp'
- '-p16686:16686'
- '--name=jaeger-server'
- '--network=nw_jaeger'
- 'jaegertracing/all-in-one:latest'
id: jaeger-server
waitFor: ['test-network']

- name: gcr.io/cloud-builders/docker
args: ['build', '-t', 'gcr.io/$PROJECT_ID/test-runner', '-f', 'Dockerfile.integration', '.']
id: test-build
waitFor: ['test-network']

- name: gcr.io/cloud-builders/docker
args: ['run', '--network=nw_jaeger', '-e', 'JAEGER_HOST=jaeger-server', 'gcr.io/$PROJECT_ID/test-runner', 'vendor/bin/phpunit', '--config=./phpunit-integration.xml.dist']
id: test-run
waitFor: ['test-build', 'jaeger-server']
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
},
"require-dev": {
"phpunit/phpunit": "^6.0",
"squizlabs/php_codesniffer": "2.*"
"squizlabs/php_codesniffer": "2.*",
"guzzlehttp/guzzle": "~6.0"
},
"license": "Apache-2.0",
"authors": [
Expand Down
13 changes: 13 additions & 0 deletions phpunit-integration.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./tests/integration/bootstrap.php" colors="true">
<testsuites>
<testsuite>
<directory>tests/integration</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
75 changes: 75 additions & 0 deletions tests/integration/JaegerExporterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Copyright 2018 OpenCensus Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace OpenCensus\Tests\Integration\Trace\Exporter;

use GuzzleHttp\Client;
use PHPUnit\Framework\TestCase;

class JaegerExporterTest extends TestCase
{
private static $jaegerClient;

public static function setUpBeforeClass()
{
parent::setUpBeforeClass();

$jaegerHost = getenv('JAEGER_HOST') ?: 'localhost';
$jaegerPort = (int)(getenv('JAEGER_PORT') ?: 16686);
self::$jaegerClient = new Client([
'base_uri' => sprintf('http://%s:%d/', $jaegerHost, $jaegerPort)
]);
}

public function testReportsTraceToJaeger()
{
$rand = mt_rand();
$client = new Client(['base_uri' => 'http://localhost:9000']);
$response = $client->request('GET', '/', [
'query' => [
'rand' => $rand
]
]);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('Hello world!', $response->getBody()->getContents());

$response = $this->findTraces($rand);
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true);
$this->assertCount(1, $data['data']);
$trace = $data['data'][0];
$this->assertCount(2, $trace['spans']);
}

public function testCanReachJaegerServer()
{
$response = self::$jaegerClient->request('GET', '/search');
$this->assertEquals(200, $response->getStatusCode());
}

private function findTraces($rand)
{
return self::$jaegerClient->request('GET', '/api/traces', [
'query' => [
'service' => 'integration-test',
'operation' => "/?rand=$rand",
'limit' => 20,
'lookback' => '1h'
]
]);
}
}
64 changes: 64 additions & 0 deletions tests/integration/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Copyright 2018 OpenCensusAuthors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* This PHPUnit bootstrap file starts a local web server using the built-in
* PHP web server. The PHPUnit tests that run then hit this running server.
* We also register a shutdown function to stop this server after tests have
* run.
*/

require __DIR__ . '/../../vendor/autoload.php';

$host = getenv('TESTHOST') ?: 'localhost';
$port = (int)(getenv('TESTPORT') ?: 9000);
putenv(
sprintf(
'TESTURL=http://%s:%d',
$host,
$port
)
);

$command = sprintf(
'php -S %s:%d -t %s >/dev/null 2>&1 & echo $!',
$host,
$port,
__DIR__ . '/web'
);

$output = [];
printf('Starting web server with command: %s' . PHP_EOL, $command);
exec($command, $output);
$pid = (int) $output[0];

printf(
'%s - Web server started on %s:%d with PID %d',
date('r'),
$host,
$port,
$pid
);

// Give the server time to boot.
sleep(1);

// Kill the web server when the process ends
register_shutdown_function(function() use ($pid) {
echo sprintf('%s - Killing process with ID %d', date('r'), $pid) . PHP_EOL;
exec('kill ' . $pid);
});
44 changes: 44 additions & 0 deletions tests/integration/web/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Copyright 2018 OpenCensus Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

require_once __DIR__ . '/../../../vendor/autoload.php';

use OpenCensus\Trace\Exporter\JaegerExporter;
use OpenCensus\Trace\Tracer;

$host = getenv('JAEGER_HOST') ?: 'localhost';
$exporter = new JaegerExporter('integration-test', [
'host' => $host,
'tags' => [
'asdf' => 'qwer'
]
]);

Tracer::start($exporter, [
'attributes' => [
'foo' => 'bar'
]
]);

Tracer::inSpan(
['name' => 'slow_function'],
function () {
usleep(50);
}
);

echo 'Hello world!';

0 comments on commit 5baee43

Please sign in to comment.