Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

fix(api): do not init db connection in event subscriber (#11543) #11545

Merged
merged 1 commit into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/API/centreon-api-v22.04.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 5 additions & 7 deletions src/EventSubscriber/UpdateEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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
Expand Down
52 changes: 52 additions & 0 deletions tests/api/Context/PlatformInstallationStatusContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* Copyright 2005 - 2022 Centreon (https://www.centreon.com/)
*
* 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.
*
* For more information : contact@centreon.com
*
*/

namespace Centreon\Test\Api\Context;

use Centreon\Test\Behat\Api\Context\ApiContext;

class PlatformInstallationStatusContext extends ApiContext
{
/**
* Remove centreon databases, configuration file and symfony cache
*
* @Given Centreon Web is not installed
*/
public function centreonWebIsNotInstalled()
{
$this->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'
);
}
}
4 changes: 4 additions & 0 deletions tests/api/behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions tests/api/features/PlatformInstallationStatus.feature
Original file line number Diff line number Diff line change
@@ -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