From 84e355c9eb6769472d3d7e274e8d71d2502bdea7 Mon Sep 17 00:00:00 2001 From: Rob Landers Date: Sat, 17 Jul 2021 14:19:26 +0200 Subject: [PATCH 01/11] Remove sleep from integration test --- .github/workflows/php.yml | 2 -- src/index.php | 4 ++++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 35d220e..cee3dd0 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -152,8 +152,6 @@ jobs: env: DAPR_VERSION: ${{ matrix.dapr-version }} run: docker-compose up -d - - name: Wait for environment stabilization - run: sleep 10 - name: Execute Tests run: | STATUSCODE=$(curl --silent --output /tmp/test-results.json --write-out "%{http_code}" http://localhost:9502/do_tests) diff --git a/src/index.php b/src/index.php index 0f407c3..f0ce822 100644 --- a/src/index.php +++ b/src/index.php @@ -435,6 +435,10 @@ function ( $app->get( '/do_tests', function (\Dapr\Client\DaprClient $client) { + while (!$client->isDaprHealthy()) { + sleep(1); + } + $test_results = [ 'test/actors' => null, 'test/binding' => null, From 3b5c380d85b3fdfa8485a47d02e266d08f1ac46f Mon Sep 17 00:00:00 2001 From: Rob Landers Date: Sat, 17 Jul 2021 14:21:53 +0200 Subject: [PATCH 02/11] Ignore code coverage here --- src/index.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/index.php b/src/index.php index f0ce822..4ed958b 100644 --- a/src/index.php +++ b/src/index.php @@ -1,5 +1,7 @@ Date: Sat, 17 Jul 2021 14:55:55 +0200 Subject: [PATCH 03/11] Fix isDaprHealthy --- Makefile | 30 ++++++++++++++++++++++++++++++ docker-compose.yml | 2 +- images/caddy.Dockerfile | 1 - images/tests.Dockerfile | 2 -- src/lib/Client/DaprHttpClient.php | 2 +- 5 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..cc374a2 --- /dev/null +++ b/Makefile @@ -0,0 +1,30 @@ + +export GITHUB_SHA=latest +export DAPR_VERSION=1.3.0-rc.1 + +.PHONY: integration-tests +integration-tests: build + docker-compose down -v + docker-compose up & + sleep 10 + curl --silent --output /tmp/test-results.json --write-out "%{http_code}" http://localhost:9502/do_tests + docker-compose down -v + cat /tmp/test-results.json | jq . + +composer.lock: composer.json + composer update + +vendor/autoload.php: composer.lock + composer install + touch vendor/autoload.php + +.PHONY: build +build: build-caddy build-tests + +.PHONY: build-tests +build-tests: vendor/autoload.php + docker build -t tests:latest -f images/tests.Dockerfile . + +.PHONY: build-caddy +build-caddy: vendor/autoload.php + docker build -t caddy:latest -f images/caddy.Dockerfile . diff --git a/docker-compose.yml b/docker-compose.yml index dba3459..e601b48 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,4 @@ -# A Docker Compose file for spinning up integration tests in CI +# A Docker Compose file for spinning up integration tests in CI, you can run these locally by running `make` version: "3" services: placement: diff --git a/images/caddy.Dockerfile b/images/caddy.Dockerfile index 7588a2b..0b5a7cb 100644 --- a/images/caddy.Dockerfile +++ b/images/caddy.Dockerfile @@ -1,4 +1,3 @@ -# syntax=docker/dockerfile:labs FROM caddy AS base COPY images/Caddyfile /etc/caddy/Caddyfile COPY . /tests diff --git a/images/tests.Dockerfile b/images/tests.Dockerfile index c3d6881..3624c8b 100644 --- a/images/tests.Dockerfile +++ b/images/tests.Dockerfile @@ -1,5 +1,3 @@ -# syntax=docker/dockerfile:labs -# withinboredom/php-base-min == docker build --pull -t withinboredom/php-base-min --target base -f images/tests.Dockerfile . FROM php:8.0-fpm AS base COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/ RUN apt-get update && apt-get install -y wget git unzip && apt-get clean diff --git a/src/lib/Client/DaprHttpClient.php b/src/lib/Client/DaprHttpClient.php index 3b43205..de75f74 100644 --- a/src/lib/Client/DaprHttpClient.php +++ b/src/lib/Client/DaprHttpClient.php @@ -49,7 +49,7 @@ public function isDaprHealthy(): bool { try { $result = $this->httpClient->get('/v1.0/healthz'); - if (200 === $result->getStatusCode()) { + if (204 === $result->getStatusCode()) { return true; } return false; From a20c3d1c76177a871eb990ce90da5ca428300757 Mon Sep 17 00:00:00 2001 From: Rob Landers Date: Sat, 17 Jul 2021 14:56:53 +0200 Subject: [PATCH 04/11] Make sure we return a 204 --- tests/HealthTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/HealthTest.php b/tests/HealthTest.php index 0eebff8..d46c2a3 100644 --- a/tests/HealthTest.php +++ b/tests/HealthTest.php @@ -13,7 +13,7 @@ public function testIsHealthy() { $container = $this->get_http_client_stack( [ - new Response(200) + new Response(204) ] ); $client = $this->get_new_client_with_http($container->client); From 6f57f3502febbfee3bdcda706ff5eb27392119aa Mon Sep 17 00:00:00 2001 From: Rob Landers Date: Sat, 17 Jul 2021 14:59:48 +0200 Subject: [PATCH 05/11] Require linting and unit tests before building the long stuff --- .github/workflows/php.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index cee3dd0..c2c28f1 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -72,6 +72,9 @@ jobs: run: composer run-script lint build-integration-test-containers: runs-on: ubuntu-latest + needs: + - lint + - unit-tests strategy: matrix: image: [ 'caddy', 'tests' ] From e697c8ed7f74650d29e41312bd4ea13572ea0025 Mon Sep 17 00:00:00 2001 From: Rob Landers Date: Sat, 17 Jul 2021 15:16:06 +0200 Subject: [PATCH 06/11] Add metadata request and tests --- src/lib/Client/DaprClient.php | 7 ++++ src/lib/Client/DaprHttpClient.php | 11 +++++- src/lib/Client/MetadataResponse.php | 47 ++++++++++++++++++++++++ tests/MetadataTest.php | 55 +++++++++++++++++++++++++++++ 4 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 src/lib/Client/MetadataResponse.php create mode 100644 tests/MetadataTest.php diff --git a/src/lib/Client/DaprClient.php b/src/lib/Client/DaprClient.php index 29b2c2f..8031088 100644 --- a/src/lib/Client/DaprClient.php +++ b/src/lib/Client/DaprClient.php @@ -444,6 +444,13 @@ abstract public function getBulkSecret(string $storeName, array $metadata = []): */ abstract public function isDaprHealthy(): bool; + /** + * Retrieve metadata from the sidecar + * + * @return MetadataResponse + */ + abstract public function getMetadata(): MetadataResponse|null; + /** * @param string $token * @return null|array{dapr-api-token: string} diff --git a/src/lib/Client/DaprHttpClient.php b/src/lib/Client/DaprHttpClient.php index de75f74..a225272 100644 --- a/src/lib/Client/DaprHttpClient.php +++ b/src/lib/Client/DaprHttpClient.php @@ -4,7 +4,6 @@ use Dapr\Deserialization\IDeserializer; use Dapr\Serialization\ISerializer; -use Dapr\State\StateItem; use GuzzleHttp\Client; use Psr\Log\LoggerInterface; @@ -57,4 +56,14 @@ public function isDaprHealthy(): bool return false; } } + + public function getMetadata(): MetadataResponse|null + { + try { + $result = $this->httpClient->get('/v1.0/metadata'); + return $this->deserializer->from_json(MetadataResponse::class, $result->getBody()->getContents()); + } catch (\Throwable $exception) { + return null; + } + } } diff --git a/src/lib/Client/MetadataResponse.php b/src/lib/Client/MetadataResponse.php new file mode 100644 index 0000000..1502898 --- /dev/null +++ b/src/lib/Client/MetadataResponse.php @@ -0,0 +1,47 @@ +get_http_client_stack( + [ + new \GuzzleHttp\Psr7\Response( + 200, body: json_encode( + [ + 'id' => 'demo-actor', + 'actors' => [ + [ + 'type' => 'DemoActor', + 'count' => 1 + ] + ], + 'extended' => [ + 'cliPID' => '12301823', + 'appCommand' => 'uvicorn --port 3000 demo_actor_service:app' + ], + 'components' => [ + [ + 'name' => 'pubsub', + 'type' => 'pubsub.redis', + 'version' => '' + ] + ] + ] + ) + ) + ] + ); + $client = $this->get_new_client_with_http($stack->client); + $result = $client->getMetadata(); + $this->assertEquals( + new \Dapr\Client\MetadataResponse( + 'demo-actor', + [new \Dapr\Client\RegisteredActor('DemoActor', 1)], + [ + 'cliPID' => '12301823', + 'appCommand' => 'uvicorn --port 3000 demo_actor_service:app' + ], + [new \Dapr\Client\RegisteredComponent('pubsub', 'pubsub.redis', '')] + ), + $result + ); + } +} From e60cd22c3e9531eed21e3aa4dac21fb4e6634e28 Mon Sep 17 00:00:00 2001 From: Rob Landers Date: Sat, 17 Jul 2021 15:20:42 +0200 Subject: [PATCH 07/11] Check that actors are registered --- src/index.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/index.php b/src/index.php index 4ed958b..d934825 100644 --- a/src/index.php +++ b/src/index.php @@ -437,8 +437,15 @@ function ( $app->get( '/do_tests', function (\Dapr\Client\DaprClient $client) { - while (!$client->isDaprHealthy()) { + while (true) { sleep(1); + if ($client->isDaprHealthy()) { + $meta = $client->getMetadata(); + error_log(print_r($meta, true)); + if (!empty($meta->actors)) { + break; + } + } } $test_results = [ From 5b2d4c7b6e57f789bc37e168be052f21b5399614 Mon Sep 17 00:00:00 2001 From: Rob Landers Date: Sat, 17 Jul 2021 15:24:42 +0200 Subject: [PATCH 08/11] I guess we're sleeping anyway... --- src/index.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.php b/src/index.php index d934825..eecd816 100644 --- a/src/index.php +++ b/src/index.php @@ -443,6 +443,7 @@ function (\Dapr\Client\DaprClient $client) { $meta = $client->getMetadata(); error_log(print_r($meta, true)); if (!empty($meta->actors)) { + sleep(3); break; } } From 6d984d94ec57342c4d30cf55821aa2784eff572c Mon Sep 17 00:00:00 2001 From: Rob Landers Date: Sat, 17 Jul 2021 15:28:59 +0200 Subject: [PATCH 09/11] Sigh... --- .github/workflows/php.yml | 2 ++ src/index.php | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index c2c28f1..1fa9e71 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -155,6 +155,8 @@ jobs: env: DAPR_VERSION: ${{ matrix.dapr-version }} run: docker-compose up -d + - name: Wait for environment stabilization + run: sleep 10 - name: Execute Tests run: | STATUSCODE=$(curl --silent --output /tmp/test-results.json --write-out "%{http_code}" http://localhost:9502/do_tests) diff --git a/src/index.php b/src/index.php index eecd816..d934825 100644 --- a/src/index.php +++ b/src/index.php @@ -443,7 +443,6 @@ function (\Dapr\Client\DaprClient $client) { $meta = $client->getMetadata(); error_log(print_r($meta, true)); if (!empty($meta->actors)) { - sleep(3); break; } } From 7b5db6e63646e24c4c093033e2ca206eec18789b Mon Sep 17 00:00:00 2001 From: Rob Landers Date: Sat, 17 Jul 2021 15:42:37 +0200 Subject: [PATCH 10/11] Wait for daprd shutdown --- src/index.php | 6 ++++++ src/lib/Client/DaprClient.php | 7 +++++++ src/lib/Client/DaprHttpClient.php | 11 +++++++++++ 3 files changed, 24 insertions(+) diff --git a/src/index.php b/src/index.php index d934825..b972500 100644 --- a/src/index.php +++ b/src/index.php @@ -476,6 +476,12 @@ function (\Dapr\Client\DaprClient $client) { ]; } + $client->shutdown(afterRequest: false); + + while ($client->isDaprHealthy()) { + error_log('waiting for daprd shutdown...'); + } + return new \Nyholm\Psr7\Response($has_failed ? 500 : 200, body: json_encode($test_results)); } ); diff --git a/src/lib/Client/DaprClient.php b/src/lib/Client/DaprClient.php index 8031088..5e137f1 100644 --- a/src/lib/Client/DaprClient.php +++ b/src/lib/Client/DaprClient.php @@ -451,6 +451,13 @@ abstract public function isDaprHealthy(): bool; */ abstract public function getMetadata(): MetadataResponse|null; + /** + * Shutdown the Daprd sidecar + * + * @param bool $afterRequest If true, schedules a php shutdown function, otherwise fires the request immediately. + */ + abstract public function shutdown(bool $afterRequest = true): void; + /** * @param string $token * @return null|array{dapr-api-token: string} diff --git a/src/lib/Client/DaprHttpClient.php b/src/lib/Client/DaprHttpClient.php index a225272..eca7a6f 100644 --- a/src/lib/Client/DaprHttpClient.php +++ b/src/lib/Client/DaprHttpClient.php @@ -66,4 +66,15 @@ public function getMetadata(): MetadataResponse|null return null; } } + + public function shutdown(bool $afterRequest = true): void + { + $shutdown = fn() => $this->httpClient->post('/v1.0/shutdown'); + if ($afterRequest) { + register_shutdown_function($shutdown); + return; + } + + $shutdown(); + } } From 44edb43fe41e339f8f2bfea2c49a9702e9433376 Mon Sep 17 00:00:00 2001 From: Rob Landers Date: Sat, 17 Jul 2021 15:43:57 +0200 Subject: [PATCH 11/11] Wait 1 second before checking again --- src/index.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.php b/src/index.php index b972500..57c59ec 100644 --- a/src/index.php +++ b/src/index.php @@ -479,6 +479,7 @@ function (\Dapr\Client\DaprClient $client) { $client->shutdown(afterRequest: false); while ($client->isDaprHealthy()) { + sleep(1); error_log('waiting for daprd shutdown...'); }