diff --git a/Dockerfile.integration b/Dockerfile.integration new file mode 100644 index 0000000..7b6892b --- /dev/null +++ b/Dockerfile.integration @@ -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 [] \ No newline at end of file diff --git a/cloudbuild.yaml b/cloudbuild.yaml index 5eb0250..a3932fa 100644 --- a/cloudbuild.yaml +++ b/cloudbuild.yaml @@ -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'] diff --git a/composer.json b/composer.json index bb93fcf..1a05801 100644 --- a/composer.json +++ b/composer.json @@ -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": [ diff --git a/phpunit-integration.xml.dist b/phpunit-integration.xml.dist new file mode 100644 index 0000000..0fbec59 --- /dev/null +++ b/phpunit-integration.xml.dist @@ -0,0 +1,13 @@ + + + + + tests/integration + + + + + src + + + diff --git a/tests/integration/JaegerExporterTest.php b/tests/integration/JaegerExporterTest.php new file mode 100644 index 0000000..d892be6 --- /dev/null +++ b/tests/integration/JaegerExporterTest.php @@ -0,0 +1,75 @@ + 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' + ] + ]); + } +} diff --git a/tests/integration/bootstrap.php b/tests/integration/bootstrap.php new file mode 100644 index 0000000..311b279 --- /dev/null +++ b/tests/integration/bootstrap.php @@ -0,0 +1,64 @@ +/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); +}); diff --git a/tests/integration/web/index.php b/tests/integration/web/index.php new file mode 100644 index 0000000..3874bc3 --- /dev/null +++ b/tests/integration/web/index.php @@ -0,0 +1,44 @@ + $host, + 'tags' => [ + 'asdf' => 'qwer' + ] +]); + +Tracer::start($exporter, [ + 'attributes' => [ + 'foo' => 'bar' + ] +]); + +Tracer::inSpan( + ['name' => 'slow_function'], + function () { + usleep(50); + } +); + +echo 'Hello world!';