From e203bb44e0553443a2c63f17d83f713f7693fbfb Mon Sep 17 00:00:00 2001 From: Kevin Duret Date: Tue, 9 Aug 2022 11:35:48 +0200 Subject: [PATCH] fix(api): do not init db connection in event subscriber (#11543) Refs: MON-12296 --- doc/API/centreon-api-v22.04.yaml | 2 +- src/EventSubscriber/UpdateEventSubscriber.php | 12 ++--- .../PlatformInstallationStatusContext.php | 52 +++++++++++++++++++ tests/api/behat.yml | 4 ++ .../PlatformInstallationStatus.feature | 18 +++++++ 5 files changed, 80 insertions(+), 8 deletions(-) create mode 100644 tests/api/Context/PlatformInstallationStatusContext.php create mode 100644 tests/api/features/PlatformInstallationStatus.feature diff --git a/doc/API/centreon-api-v22.04.yaml b/doc/API/centreon-api-v22.04.yaml index bb949d8c36b..34f82120597 100644 --- a/doc/API/centreon-api-v22.04.yaml +++ b/doc/API/centreon-api-v22.04.yaml @@ -3543,7 +3543,7 @@ paths: application/json: schema: type: object - required: ["installed_version", "has_upgrade_available"] + required: ["is_installed", "has_upgrade_available"] properties: is_installed: type: boolean diff --git a/src/EventSubscriber/UpdateEventSubscriber.php b/src/EventSubscriber/UpdateEventSubscriber.php index 254b78bc514..9c629695103 100644 --- a/src/EventSubscriber/UpdateEventSubscriber.php +++ b/src/EventSubscriber/UpdateEventSubscriber.php @@ -47,6 +47,10 @@ public function __construct( */ public static function getSubscribedEvents(): array { + if (! file_exists(_CENTREON_ETC_ . DIRECTORY_SEPARATOR . 'centreon.conf.php')) { + return []; + } + return [ KernelEvents::REQUEST => [ ['validateCentreonWebVersionOrFail', 35], @@ -55,19 +59,13 @@ public static function getSubscribedEvents(): array } /** - * validation centreon web installed version + * validate centreon web installed version when update endpoint is called * * @param RequestEvent $event * @throws \Exception */ public function validateCentreonWebVersionOrFail(RequestEvent $event): void { - $this->debug('Checking if database configuration file exists to know if centreon is already installed'); - if (! file_exists(_CENTREON_ETC_ . DIRECTORY_SEPARATOR . 'centreon.conf.php')) { - $this->debug('Centreon database configuration file not found'); - return; - } - $this->debug('Checking if route matches updates endpoint'); if ( $event->getRequest()->getMethod() === Request::METHOD_PATCH diff --git a/tests/api/Context/PlatformInstallationStatusContext.php b/tests/api/Context/PlatformInstallationStatusContext.php new file mode 100644 index 00000000000..e1bf8b4e561 --- /dev/null +++ b/tests/api/Context/PlatformInstallationStatusContext.php @@ -0,0 +1,52 @@ +getContainer()->execute( + 'mysql -e "DROP DATABASE centreon_storage"', + 'web' + ); + $this->getContainer()->execute( + 'mysql -e "DROP DATABASE centreon"', + 'web' + ); + $this->getContainer()->execute( + 'rm -f /etc/centreon/centreon.conf.php', + 'web' + ); + $this->getContainer()->execute( + 'rm -rf /var/cache/centreon/symfony', + 'web' + ); + } +} diff --git a/tests/api/behat.yml b/tests/api/behat.yml index 201f97cd472..8e8ece2e02a 100644 --- a/tests/api/behat.yml +++ b/tests/api/behat.yml @@ -72,6 +72,10 @@ default: paths: [ "%paths.base%/features/PlatformInformation.feature" ] contexts: - Centreon\Test\Api\Context\PlatformInformationContext + platform_fresh_install: + paths: [ "%paths.base%/features/PlatformInstallationStatus.feature" ] + contexts: + - Centreon\Test\Api\Context\PlatformInstallationStatusContext platform_update: paths: [ "%paths.base%/features/PlatformUpdate.feature" ] contexts: diff --git a/tests/api/features/PlatformInstallationStatus.feature b/tests/api/features/PlatformInstallationStatus.feature new file mode 100644 index 00000000000..5df7be8f915 --- /dev/null +++ b/tests/api/features/PlatformInstallationStatus.feature @@ -0,0 +1,18 @@ +Feature: + In order to maintain centreon platform + As an administrator + I want to known the platform installation status + + Background: + Given a running instance of Centreon Web API + And the endpoints are described in Centreon Web API documentation + + Scenario: Update platform information + When I send a GET request to '/api/latest/platform/installation/status' + Then the response code should be "200" + And the JSON node "is_installed" should be equal to true + + Given Centreon Web is not installed + When I send a GET request to '/api/latest/platform/installation/status' + Then the response code should be "200" + And the JSON node "is_installed" should be equal to false