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

enh(api): add doc and api test for wep update api #11351

Merged
merged 10 commits into from
Jul 8, 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: 2 additions & 0 deletions config/packages/Centreon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ services:

Core\Platform\Application\Repository\ReadUpdateRepositoryInterface:
class: Core\Platform\Infrastructure\Repository\FsReadUpdateRepository
arguments:
$installDir: '%centreon_install_path%'
public: true

Core\Platform\Application\Repository\UpdateLockerRepositoryInterface:
Expand Down
2 changes: 2 additions & 0 deletions doc/API/centreon-api-v22.10.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3582,6 +3582,8 @@ paths:
moduleName:
type: object
$ref: '#/components/schemas/Platform.Versions'
/platform/updates:
$ref: "./v22.10/Administration/updates.yaml"
/platform/installation/status:
get:
tags:
Expand Down
30 changes: 30 additions & 0 deletions doc/API/v22.10/Administration/updates.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
patch:
tags:
- Platform
summary: "Update Centreon web"
description: |
Update Centreon web component
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
components:
type: array
items:
type: object
properties:
name:
type: string
enum: [ centreon-web ]
responses:
204:
description: "Platform updated"
404:
description: "Updates not found"
500:
$ref: "../../centreon-api-v22.10.yaml#/components/responses/InternalServerError"
...
3 changes: 3 additions & 0 deletions lang/fr_FR.UTF-8/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -16940,3 +16940,6 @@ msgstr "Erreur lors du déverrouillage du processus de mise à jour"

msgid "An error occurred when applying post update actions"
msgstr "Une erreur s'est produite lors de l'application des actions postérieures à la mise à jour"

msgid "Updates not found"
msgstr "Les mises à jour n'ont pas été trouvées"
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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
*
*/
declare(strict_types=1);

namespace Core\Platform\Application\Repository;

use Centreon\Domain\Repository\RepositoryException;

class UpdateNotFoundException extends RepositoryException
{
/**
* @return self
*/
public static function updatesNotFound(): self
{
return new self(_('Updates not found'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
use Core\Platform\Application\Repository\ReadVersionRepositoryInterface;
use Core\Platform\Application\Repository\ReadUpdateRepositoryInterface;
use Core\Platform\Application\Repository\WriteUpdateRepositoryInterface;
use Core\Platform\Application\Repository\UpdateNotFoundException;
use Core\Application\Common\UseCase\ErrorResponse;
use Core\Application\Common\UseCase\NotFoundResponse;
use Core\Application\Common\UseCase\NoContentResponse;

class UpdateVersions
Expand Down Expand Up @@ -68,6 +70,15 @@ public function __invoke(
$this->unlockUpdate();

$this->runPostUpdate($this->getCurrentVersionOrFail());
} catch (UpdateNotFoundException $e) {
$this->error(
$e->getMessage(),
['trace' => $e->getTraceAsString()],
);

$presenter->setResponseStatus(new NotFoundResponse('Updates'));

return;
} catch (\Throwable $e) {
$this->error(
$e->getMessage(),
Expand Down Expand Up @@ -145,6 +156,8 @@ private function getAvailableUpdatesOrFail(string $currentVersion): array
);

return $this->readUpdateRepository->findOrderedAvailableUpdates($currentVersion);
} catch (UpdateNotFoundException $e) {
throw $e;
} catch (\Throwable $e) {
throw UpdateVersionsException::errorWhenRetrievingAvailableUpdates($e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,21 @@

use Centreon\Domain\Log\LoggerTrait;
use Core\Platform\Application\Repository\ReadUpdateRepositoryInterface;
use Core\Platform\Application\Repository\UpdateNotFoundException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

class FsReadUpdateRepository implements ReadUpdateRepositoryInterface
{
use LoggerTrait;

private const INSTALL_DIR = __DIR__ . '/../../../../../www/install';

/**
* @param string $installDir
* @param Filesystem $filesystem
* @param Finder $finder
*/
public function __construct(
private string $installDir,
private Filesystem $filesystem,
private Finder $finder,
) {
Expand All @@ -62,15 +63,16 @@ public function findOrderedAvailableUpdates(string $currentVersion): array
*/
private function findAvailableUpdates(string $currentVersion): array
{
if (! $this->filesystem->exists(self::INSTALL_DIR)) {
return [];
if (! $this->filesystem->exists($this->installDir)) {
$this->error('Install directory not found on filesystem: ' . $this->installDir);
throw UpdateNotFoundException::updatesNotFound();
}

$fileNameVersionRegex = '/Update-(?<version>[a-zA-Z0-9\-\.]+)\.php/';
$availableUpdates = [];

$updateFiles = $this->finder->files()
->in(self::INSTALL_DIR)
->in($this->installDir)
->name($fileNameVersionRegex);

foreach ($updateFiles as $updateFile) {
Expand Down
48 changes: 48 additions & 0 deletions tests/api/Context/PlatformUpdateContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* Copyright 2005 - 2020 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 PlatformUpdateContext extends ApiContext
{
/**
* Create an update file
*
* @Given an update is available
*/
public function anUpdateIsAvailable()
{
$this->getContainer()->execute(
'mkdir -p /usr/share/centreon/www/install/php',
'web'
);
$this->getContainer()->execute(
"sh -c 'echo \"<?php\" > /usr/share/centreon/www/install/php/Update-99.99.99.php'",
'web'
);
$this->getContainer()->execute(
'chown -R apache. /usr/share/centreon/www/install',
'web'
);
}
}
4 changes: 4 additions & 0 deletions tests/api/behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ default:
paths: [ "%paths.base%/features/PlatformInformation.feature" ]
contexts:
- Centreon\Test\Api\Context\PlatformInformationContext
platform_update:
paths: [ "%paths.base%/features/PlatformUpdate.feature" ]
contexts:
- Centreon\Test\Api\Context\PlatformUpdateContext
host_groups:
paths: [ "%paths.base%/features/HostGroup.feature" ]
contexts:
Expand Down
41 changes: 41 additions & 0 deletions tests/api/features/PlatformUpdate.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Feature:
In order to maintain easily centreon platform
As a user
I want to update centreon web using api

Background:
Given a running instance of Centreon Web API
And the endpoints are described in Centreon Web API documentation

Scenario: Update platform information
Given I am logged in

When an update is available
And I send a PATCH request to '/api/latest/platform/updates' with body:
"""
{
"components": [
{
"name": "centreon-web"
}
]
}
"""
Then the response code should be "204"

When I send a GET request to '/api/latest/platform/versions'
Then the response code should be "200"
And the JSON node "web.version" should be equal to the string "99.99.99"

When I send a PATCH request to '/api/latest/platform/updates' with body:
"""
{
"components": [
{
"name": "centreon-web"
}
]
}
"""
Then the response code should be "404"
And the JSON node "message" should be equal to the string "Updates not found"
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace Tests\Core\Platform\Infrastructure\Repository;

use Core\Platform\Infrastructure\Repository\FsReadUpdateRepository;
use Core\Platform\Application\Repository\UpdateNotFoundException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

Expand All @@ -32,20 +33,22 @@
$this->finder = $this->createMock(Finder::class);
});

it('should not find updates if install directory does not exist', function () {
$repository = new FsReadUpdateRepository($this->filesystem, $this->finder);
it('should return an error when install directory does not exist', function () {
$repository = new FsReadUpdateRepository(sys_get_temp_dir(), $this->filesystem, $this->finder);

$this->filesystem
->expects($this->once())
->method('exists')
->willReturn(false);

$availableUpdates = $repository->findOrderedAvailableUpdates('22.04.0');
expect($availableUpdates)->toEqual([]);
});
})->throws(
UpdateNotFoundException::class,
UpdateNotFoundException::updatesNotFound()->getMessage(),
);

it('should order found updates', function () {
$repository = new FsReadUpdateRepository($this->filesystem, $this->finder);
$repository = new FsReadUpdateRepository(sys_get_temp_dir(), $this->filesystem, $this->finder);

$this->filesystem
->expects($this->once())
Expand Down