Browse Source

fix-alertmanager-message-formatter

set-sast-config-1 0.2.2
Ilya Fedorov 4 years ago
parent
commit
183a1b82a8
  1. 1
      .gitignore
  2. 1
      app/.gitignore
  3. 5
      app/src/Service/Mattermost/MattermostService.php
  4. 30
      app/tests/Unit/Model/Grafana/GrafanaMessageTest.php
  5. 116
      app/tests/Unit/Service/Mattermost/MattermostServiceTest.php

1
.gitignore

@ -1,2 +1 @@
/app/.phpunit.result.cache
/.phpunit.result.cache

1
app/.gitignore

@ -1,2 +1,3 @@
/.env
/.phpunit.result.cache
/phpunit.xml

5
app/src/Service/Mattermost/MattermostService.php

@ -42,8 +42,11 @@ class MattermostService extends BaseService implements MattermostServiceInterfac
$string = '';
foreach ($alertmanagerMessage->getAlerts() as $num => $message) {
if ('' !== $string) {
$string .= "\n --- \n";
}
$string .= sprintf(
"message %s\n - description: %s\n - summary: %s\n\n --- ",
"message %s\n - description: %s\n - summary: %s\n",
$num,
$message->getAnnotations()->getDescription(),
$message->getAnnotations()->getSummary()

30
app/tests/Unit/Model/Grafana/GrafanaMessageTest.php

@ -251,6 +251,36 @@ final class GrafanaMessageTest extends UnitTester
);
}
/**
* @throws ReflectionException
*/
public function testGetRuleUrl(): void
{
$grafanaMessage = new GrafanaMessage();
$value = $this->faker->sha256();
$property = $this->getClassProperty($grafanaMessage, 'ruleUrl');
$property->setValue($grafanaMessage, $value);
self::assertEquals($value, $grafanaMessage->getRuleUrl());
}
/**
* @throws ReflectionException
*/
public function testSetRuleUrl(): void
{
$grafanaMessage = new GrafanaMessage();
$value = $this->faker->sha256();
self::assertEquals($grafanaMessage, $grafanaMessage->setRuleUrl($value));
self::assertEquals(
$value,
($this->getClassProperty($grafanaMessage, 'ruleUrl'))
->getValue($grafanaMessage)
);
}
/**
* @throws ReflectionException
*/

116
app/tests/Unit/Service/Mattermost/MattermostServiceTest.php

@ -4,12 +4,16 @@ declare(strict_types=1);
namespace App\Tests\Unit\Service\Mattermost;
use App\Model\Alertmanager\Alert;
use App\Model\Alertmanager\AlertmanagerMessage;
use App\Model\Alertmanager\Annotation;
use App\Model\Grafana\GrafanaMessage;
use App\Service\BaseService;
use App\Service\Mattermost\MattermostService;
use App\Service\Mattermost\MattermostServiceInterface;
use Faker\Factory;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Request;
use App\Tests\Unit\UnitTester;
use Psr\Http\Message\ResponseInterface;
@ -31,18 +35,118 @@ final class MattermostServiceTest extends UnitTester
self::assertInstanceOf(MattermostServiceInterface::class, $stub);
}
/**
* @dataProvider sendAlertmanagerMessageDataProvider
* @throws GuzzleException
* @throws \JsonException
* @throws ReflectionException
*/
public function testSendAlertmanagerMessage(
string $mattermostUrl,
string $mattermostUri,
string $channelName,
string $botName,
string $botIcon,
AlertmanagerMessage $alertmanagerMessage,
array $options
): void
{
$mattermostService = new MattermostService(
$mattermostUrl, $mattermostUri, $channelName, $botName, $botIcon
);
$clientMock = $this->getMockBuilder(Client::class)
->disableOriginalConstructor()
->onlyMethods(['send'])
->getMock();
$clientProperty = $this->getClassProperty($mattermostService, 'client');
$clientProperty->setValue($mattermostService, $clientMock);
$responseMock = $this->getMockForAbstractClass(ResponseInterface::class);
$request = new Request('POST', $mattermostUri);
$clientMock->expects(self::once())
->method('send')
->with($request, $options)
->willReturn($responseMock);
self::assertEquals(
$responseMock,
$mattermostService->sendAlertmanagerMessage($alertmanagerMessage)
);
}
/**
* @throws GuzzleException
* @throws \JsonException
*/
public function sendAlertmanagerMessageDataProvider(): array
{
$faker = (Factory::create('en_EN'));
$channelName = $faker->sha256();
$botName = $faker->sha256();
$botIcon = $faker->sha256();
$description = [
$faker->sha256(),
$faker->sha256()
];
$summary = [
$faker->sha256(),
$faker->sha256()
];
return [
[
'mattermostUrl' => $faker->sha256(),
'mattermostUri' => $faker->sha256(),
'channelName' => $channelName,
'botName' => $botName,
'botIcon' => $botIcon,
'message' =>
(new AlertmanagerMessage())
->setAlerts([
(new Alert())
->setAnnotations(
(new Annotation())
->setDescription($description[0])
->setSummary($summary[0])
),
(new Alert())
->setAnnotations(
(new Annotation())
->setDescription($description[1])
->setSummary($summary[1])
)
]
),
'options' => [
'headers' => [
'Content-Type' => 'application/json',
],
'body' => json_encode([
'channel' => $channelName,
'username' => $botName,
'icon_url' => $botIcon,
'text' => sprintf(
"message %s\n - description: %s\n - summary: %s\n\n --- \nmessage %s\n - description: %s\n - summary: %s\n",
0, $description[0], $summary[0], 1, $description[1], $summary[1],
),
], JSON_THROW_ON_ERROR),
]
]
];
}
/**
* @dataProvider sendGrafanaMessageDataProvider
* @throws ReflectionException
*/
public function testSendGrafanaMessage(
string $mattermostUrl,
string $mattermostUri,
string $channelName,
string $botName,
string $botIcon,
string $mattermostUrl,
string $mattermostUri,
string $channelName,
string $botName,
string $botIcon,
GrafanaMessage $grafanaMessage,
array $options
array $options
): void
{
$mattermostService = new MattermostService(

Loading…
Cancel
Save