diff --git a/app/src/Controller/Api/v1/MattermostController.php b/app/src/Controller/Api/v1/MattermostController.php index 718a477..ae4aff3 100644 --- a/app/src/Controller/Api/v1/MattermostController.php +++ b/app/src/Controller/Api/v1/MattermostController.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Controller\Api\v1; +use App\Model\Alertmanager\AlertmanagerMessage; use App\Model\Grafana\GrafanaMessage; use App\Service\Mattermost\MattermostServiceInterface; use Nelmio\ApiDocBundle\Annotation\Model; @@ -32,6 +33,47 @@ class MattermostController extends AbstractController $this->mattermostService = $mattermostService; } + /** + * Send message to a mattermost chat from alertmanager. + * + * Docs
+ * - https://prometheus.io/docs/alerting/latest/configuration/#webhook_config
+ * - https://docs.mattermost.com/developer/webhooks-incoming.html + * + * @Route("/api/v1/send_message/alertmanager", methods={"POST"}) + * @OA\Tag(name="mattermost") + * @OA\RequestBody( + * @OA\JsonContent( + * ref=@Model(type=AlertmanagerMessage::class) + * ) + * ) + * @OA\Response( + * response=200, + * description="Success delivery" + * ) + */ + public function sendMessageFromAlertmanager(Request $request): Response + { + try { + $this->logger->notice($request->getContent()); + $message = $this->serializer->deserialize( + $request->getContent(), + AlertmanagerMessage::class, + 'json' + ); + $response = $this->mattermostService->sendAlertmanagerMessage($message); + + return new Response($response->getBody()->getContents()); + } catch (Throwable $e) { + $this->logger->critical($e->getMessage()); + + return new Response( + $e->getMessage(), + 0 === $e->getCode() ? Response::HTTP_INTERNAL_SERVER_ERROR : $e->getCode() + ); + } + } + /** * Send message to a mattermost chat from grafana. * diff --git a/app/src/Model/Alertmanager/Alert.php b/app/src/Model/Alertmanager/Alert.php new file mode 100644 index 0000000..a156b2f --- /dev/null +++ b/app/src/Model/Alertmanager/Alert.php @@ -0,0 +1,136 @@ +status; + } + + public function setStatus(string $status): self + { + $this->status = $status; + + return $this; + } + + public function getLabels(): Label + { + return $this->labels; + } + + public function setLabels(Label $labels): self + { + $this->labels = $labels; + + return $this; + } + + public function getAnnotations(): Annotation + { + return $this->annotations; + } + + public function setAnnotations(Annotation $annotations): self + { + $this->annotations = $annotations; + + return $this; + } + + public function getStartsAt(): string + { + return $this->startsAt; + } + + public function setStartsAt(string $startsAt): self + { + $this->startsAt = $startsAt; + + return $this; + } + + public function getEndsAt(): string + { + return $this->endsAt; + } + + public function setEndsAt(string $endsAt): self + { + $this->endsAt = $endsAt; + + return $this; + } + + public function getGeneratorURL(): string + { + return $this->generatorURL; + } + + public function setGeneratorURL(string $generatorURL): self + { + $this->generatorURL = $generatorURL; + + return $this; + } + + public function getFingerprint(): string + { + return $this->fingerprint; + } + + public function setFingerprint(string $fingerprint): self + { + $this->fingerprint = $fingerprint; + + return $this; + } +} diff --git a/app/src/Model/Alertmanager/AlertmanagerMessage.php b/app/src/Model/Alertmanager/AlertmanagerMessage.php new file mode 100644 index 0000000..be39ff0 --- /dev/null +++ b/app/src/Model/Alertmanager/AlertmanagerMessage.php @@ -0,0 +1,171 @@ +receiver; + } + + public function setReceiver(string $receiver): self + { + $this->receiver = $receiver; + + return $this; + } + + public function getStatus(): string + { + return $this->status; + } + + public function setStatus(string $status): self + { + $this->status = $status; + + return $this; + } + + public function getAlerts(): array + { + return $this->alerts; + } + + public function setAlerts(array $alerts): self + { + $this->alerts = $alerts; + + return $this; + } + + public function getCommonLabels(): Label + { + return $this->commonLabels; + } + + public function setCommonLabels(Label $commonLabels): self + { + $this->commonLabels = $commonLabels; + + return $this; + } + + public function getCommonAnnotations(): Annotation + { + return $this->commonAnnotations; + } + + public function setCommonAnnotations(Annotation $commonAnnotations): self + { + $this->commonAnnotations = $commonAnnotations; + + return $this; + } + + public function getExternalURL(): string + { + return $this->externalURL; + } + + public function setExternalURL(string $externalURL): self + { + $this->externalURL = $externalURL; + + return $this; + } + + public function getVersion(): string + { + return $this->version; + } + + public function setVersion(string $version): self + { + $this->version = $version; + + return $this; + } + + public function getGroupKey(): string + { + return $this->groupKey; + } + + public function setGroupKey(string $groupKey): self + { + $this->groupKey = $groupKey; + + return $this; + } + + public function getTruncatedAlerts(): int + { + return $this->truncatedAlerts; + } + + public function setTruncatedAlerts(int $truncatedAlerts): self + { + $this->truncatedAlerts = $truncatedAlerts; + + return $this; + } +} diff --git a/app/src/Model/Alertmanager/Annotation.php b/app/src/Model/Alertmanager/Annotation.php new file mode 100644 index 0000000..e093379 --- /dev/null +++ b/app/src/Model/Alertmanager/Annotation.php @@ -0,0 +1,51 @@ +description; + } + + public function setDescription(string $description): self + { + $this->description = $description; + + return $this; + } + + public function getSummary(): string + { + return $this->summary; + } + + public function setSummary(string $summary): self + { + $this->summary = $summary; + + return $this; + } +} diff --git a/app/src/Model/Alertmanager/Label.php b/app/src/Model/Alertmanager/Label.php new file mode 100644 index 0000000..7700e7d --- /dev/null +++ b/app/src/Model/Alertmanager/Label.php @@ -0,0 +1,102 @@ +alertname; + } + + public function setAlertName(string $alertName): self + { + $this->alertname = $alertName; + + return $this; + } + + public function getInstance(): string + { + return $this->instance; + } + + public function setInstance(string $instance): self + { + $this->instance = $instance; + + return $this; + } + + public function getJob(): string + { + return $this->job; + } + + public function setJob(string $job): self + { + $this->job = $job; + + return $this; + } + + public function getMonitor(): string + { + return $this->monitor; + } + + public function setMonitor(string $monitor): self + { + $this->monitor = $monitor; + + return $this; + } + + public function getSeverity(): string + { + return $this->severity; + } + + public function setSeverity(string $severity): self + { + $this->severity = $severity; + + return $this; + } +} diff --git a/app/src/Model/Alertmanager/Status.php b/app/src/Model/Alertmanager/Status.php new file mode 100644 index 0000000..f5839b5 --- /dev/null +++ b/app/src/Model/Alertmanager/Status.php @@ -0,0 +1,68 @@ +state; + } + + public function setState(string $state): self + { + $this->state = $state; + + return $this; + } + + public function getSilencedBy(): ?string + { + return $this->silencedBy; + } + + public function setSilencedBy(?string $silencedBy): self + { + $this->silencedBy = $silencedBy; + + return $this; + } + + public function getInhibitedBy(): ?string + { + return $this->inhibitedBy; + } + + public function setInhibitedBy(?string $inhibitedBy): self + { + $this->inhibitedBy = $inhibitedBy; + + return $this; + } +} diff --git a/app/src/Model/Grafana/EvalMatches.php b/app/src/Model/Grafana/EvalMatches.php index e39e589..35f79a5 100644 --- a/app/src/Model/Grafana/EvalMatches.php +++ b/app/src/Model/Grafana/EvalMatches.php @@ -8,7 +8,11 @@ use OpenApi\Annotations as OA; /** * @OA\Schema( - * required={"value", "metric", "tags"} + * required={ + * "value", + * "metric", + * "tags" + * } * ) */ class EvalMatches diff --git a/app/src/Service/Mattermost/MattermostService.php b/app/src/Service/Mattermost/MattermostService.php index 3173cc5..0adae23 100644 --- a/app/src/Service/Mattermost/MattermostService.php +++ b/app/src/Service/Mattermost/MattermostService.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Service\Mattermost; +use App\Model\Alertmanager\AlertmanagerMessage; use App\Model\Grafana\GrafanaMessage; use App\Service\BaseService; use GuzzleHttp\Exception\GuzzleException; @@ -31,6 +32,39 @@ class MattermostService extends BaseService implements MattermostServiceInterfac $this->botIcon = $botIcon; } + /** + * @throws GuzzleException + * @throws \JsonException + */ + public function sendAlertmanagerMessage(AlertmanagerMessage $alertmanagerMessage): ResponseInterface + { + $request = new Request('POST', $this->mattermostUri); + + $string = ''; + foreach ($alertmanagerMessage->getAlerts() as $num => $message) { + $string .= sprintf( + "message %s\n - description: %s\n - summary: %s\n\n --- ", + $num, + $message->getAnnotations()->getDescription(), + $message->getAnnotations()->getSummary() + ); + } + + $options = [ + 'headers' => [ + 'Content-Type' => 'application/json', + ], + 'body' => json_encode([ + 'channel' => $this->channelName, + 'username' => $this->botName, + 'icon_url' => $this->botIcon, + 'text' => $string, + ], JSON_THROW_ON_ERROR), + ]; + + return $this->client->send($request, $options); + } + /** * @throws GuzzleException * @throws \JsonException diff --git a/app/src/Service/Mattermost/MattermostServiceInterface.php b/app/src/Service/Mattermost/MattermostServiceInterface.php index 7803abb..17854e1 100644 --- a/app/src/Service/Mattermost/MattermostServiceInterface.php +++ b/app/src/Service/Mattermost/MattermostServiceInterface.php @@ -4,10 +4,13 @@ declare(strict_types=1); namespace App\Service\Mattermost; +use App\Model\Alertmanager\AlertmanagerMessage; use App\Model\Grafana\GrafanaMessage; use Psr\Http\Message\ResponseInterface; interface MattermostServiceInterface { + public function sendAlertmanagerMessage(AlertmanagerMessage $alertmanagerMessage): ResponseInterface; + public function sendGrafanaMessage(GrafanaMessage $grafanaMessage): ResponseInterface; } diff --git a/app/tests/Unit/Model/Alertmanager/AlertTest.php b/app/tests/Unit/Model/Alertmanager/AlertTest.php new file mode 100644 index 0000000..afd73c2 --- /dev/null +++ b/app/tests/Unit/Model/Alertmanager/AlertTest.php @@ -0,0 +1,224 @@ +faker->sha256(); + + $property = $this->getClassProperty($alert, 'status'); + $property->setValue($alert, $value); + + self::assertEquals($value, $alert->getStatus()); + } + + /** + * @throws ReflectionException + */ + public function testSetStatus(): void + { + $alert = new Alert(); + $value = $this->faker->sha256(); + + self::assertEquals($alert, $alert->setStatus($value)); + self::assertEquals( + $value, + ($this->getClassProperty($alert, 'status')) + ->getValue($alert) + ); + } + + /** + * @throws ReflectionException + */ + public function testGetLabels(): void + { + $alert = new Alert(); + $value = new Label(); + + $property = $this->getClassProperty($alert, 'labels'); + $property->setValue($alert, $value); + + self::assertEquals($value, $alert->getLabels()); + } + + /** + * @throws ReflectionException + */ + public function testSetLabels(): void + { + $alert = new Alert(); + $value = new Label(); + + self::assertEquals($alert, $alert->setLabels($value)); + self::assertEquals( + $value, + ($this->getClassProperty($alert, 'labels')) + ->getValue($alert) + ); + } + + /** + * @throws ReflectionException + */ + public function testGetCommonAnnotations(): void + { + $alert = new Alert(); + $value = new Annotation(); + + $property = $this->getClassProperty($alert, 'annotations'); + $property->setValue($alert, $value); + + self::assertEquals($value, $alert->getAnnotations()); + } + + /** + * @throws ReflectionException + */ + public function testSetCommonAnnotations(): void + { + $alert = new Alert(); + $value = new Annotation(); + + self::assertEquals($alert, $alert->setAnnotations($value)); + self::assertEquals( + $value, + ($this->getClassProperty($alert, 'annotations')) + ->getValue($alert) + ); + } + + /** + * @throws ReflectionException + */ + public function testGetStartsAt(): void + { + $alert = new Alert(); + $value = $this->faker->sha256(); + + $property = $this->getClassProperty($alert, 'startsAt'); + $property->setValue($alert, $value); + + self::assertEquals($value, $alert->getStartsAt()); + } + + /** + * @throws ReflectionException + */ + public function testSetStartsAt(): void + { + $alert = new Alert(); + $value = $this->faker->sha256(); + + self::assertEquals($alert, $alert->setStartsAt($value)); + self::assertEquals( + $value, + ($this->getClassProperty($alert, 'startsAt')) + ->getValue($alert) + ); + } + + /** + * @throws ReflectionException + */ + public function testGetEndsAt(): void + { + $alert = new Alert(); + $value = $this->faker->sha256(); + + $property = $this->getClassProperty($alert, 'endsAt'); + $property->setValue($alert, $value); + + self::assertEquals($value, $alert->getEndsAt()); + } + + /** + * @throws ReflectionException + */ + public function testSetEndsAt(): void + { + $alert = new Alert(); + $value = $this->faker->sha256(); + + self::assertEquals($alert, $alert->setEndsAt($value)); + self::assertEquals( + $value, + ($this->getClassProperty($alert, 'endsAt')) + ->getValue($alert) + ); + } + + /** + * @throws ReflectionException + */ + public function testGetGeneratorURL(): void + { + $alert = new Alert(); + $value = $this->faker->sha256(); + + $property = $this->getClassProperty($alert, 'generatorURL'); + $property->setValue($alert, $value); + + self::assertEquals($value, $alert->getGeneratorURL()); + } + + /** + * @throws ReflectionException + */ + public function testSetGeneratorURL(): void + { + $alert = new Alert(); + $value = $this->faker->sha256(); + + self::assertEquals($alert, $alert->setGeneratorURL($value)); + self::assertEquals( + $value, + ($this->getClassProperty($alert, 'generatorURL')) + ->getValue($alert) + ); + } + + /** + * @throws ReflectionException + */ + public function testGetFingerprint(): void + { + $alert = new Alert(); + $value = $this->faker->sha256(); + + $property = $this->getClassProperty($alert, 'fingerprint'); + $property->setValue($alert, $value); + + self::assertEquals($value, $alert->getFingerprint()); + } + + /** + * @throws ReflectionException + */ + public function testSetFingerprint(): void + { + $alert = new Alert(); + $value = $this->faker->sha256(); + + self::assertEquals($alert, $alert->setFingerprint($value)); + self::assertEquals( + $value, + ($this->getClassProperty($alert, 'fingerprint')) + ->getValue($alert) + ); + } +} diff --git a/app/tests/Unit/Model/Alertmanager/AlertmanagerMessageTest.php b/app/tests/Unit/Model/Alertmanager/AlertmanagerMessageTest.php new file mode 100644 index 0000000..0106db2 --- /dev/null +++ b/app/tests/Unit/Model/Alertmanager/AlertmanagerMessageTest.php @@ -0,0 +1,286 @@ +faker->sha256(); + + $property = $this->getClassProperty($alertmanagerMessage, 'receiver'); + $property->setValue($alertmanagerMessage, $value); + + self::assertEquals($value, $alertmanagerMessage->getReceiver()); + } + + /** + * @throws ReflectionException + */ + public function testSetReceiver(): void + { + $alertmanagerMessage = new AlertmanagerMessage(); + $value = $this->faker->sha256(); + + self::assertEquals($alertmanagerMessage, $alertmanagerMessage->setReceiver($value)); + self::assertEquals( + $value, + ($this->getClassProperty($alertmanagerMessage, 'receiver')) + ->getValue($alertmanagerMessage) + ); + } + + /** + * @throws ReflectionException + */ + public function testGetStatus(): void + { + $alertmanagerMessage = new AlertmanagerMessage(); + $value = $this->faker->sha256(); + + $property = $this->getClassProperty($alertmanagerMessage, 'status'); + $property->setValue($alertmanagerMessage, $value); + + self::assertEquals($value, $alertmanagerMessage->getStatus()); + } + + /** + * @throws ReflectionException + */ + public function testSetStatus(): void + { + $alertmanagerMessage = new AlertmanagerMessage(); + $value = $this->faker->sha256(); + + self::assertEquals($alertmanagerMessage, $alertmanagerMessage->setStatus($value)); + self::assertEquals( + $value, + ($this->getClassProperty($alertmanagerMessage, 'status')) + ->getValue($alertmanagerMessage) + ); + } + + /** + * @throws ReflectionException + */ + public function testGetAlerts(): void + { + $alertmanagerMessage = new AlertmanagerMessage(); + $value = [new Alert()]; + + $property = $this->getClassProperty($alertmanagerMessage, 'alerts'); + $property->setValue($alertmanagerMessage, $value); + + self::assertEquals($value, $alertmanagerMessage->getAlerts()); + } + + /** + * @throws ReflectionException + */ + public function testSetAlerts(): void + { + $alertmanagerMessage = new AlertmanagerMessage(); + $value = [new Alert()]; + + self::assertEquals($alertmanagerMessage, $alertmanagerMessage->setAlerts($value)); + self::assertEquals( + $value, + ($this->getClassProperty($alertmanagerMessage, 'alerts')) + ->getValue($alertmanagerMessage) + ); + } + + + /** + * @throws ReflectionException + */ + public function testGetLabels(): void + { + $alertmanagerMessage = new AlertmanagerMessage(); + $value = new Label(); + + $property = $this->getClassProperty($alertmanagerMessage, 'commonLabels'); + $property->setValue($alertmanagerMessage, $value); + + self::assertEquals($value, $alertmanagerMessage->getCommonLabels()); + } + + /** + * @throws ReflectionException + */ + public function testSetLabels(): void + { + $alertmanagerMessage = new AlertmanagerMessage(); + $value = new Label(); + + self::assertEquals($alertmanagerMessage, $alertmanagerMessage->setCommonLabels($value)); + self::assertEquals( + $value, + ($this->getClassProperty($alertmanagerMessage, 'commonLabels')) + ->getValue($alertmanagerMessage) + ); + } + + /** + * @throws ReflectionException + */ + public function testGetCommonAnnotations(): void + { + $alertmanagerMessage = new AlertmanagerMessage(); + $value = new Annotation(); + + $property = $this->getClassProperty($alertmanagerMessage, 'commonAnnotations'); + $property->setValue($alertmanagerMessage, $value); + + self::assertEquals($value, $alertmanagerMessage->getCommonAnnotations()); + } + + /** + * @throws ReflectionException + */ + public function testSetCommonAnnotations(): void + { + $alertmanagerMessage = new AlertmanagerMessage(); + $value = new Annotation(); + + self::assertEquals($alertmanagerMessage, $alertmanagerMessage->setCommonAnnotations($value)); + self::assertEquals( + $value, + ($this->getClassProperty($alertmanagerMessage, 'commonAnnotations')) + ->getValue($alertmanagerMessage) + ); + } + + /** + * @throws ReflectionException + */ + public function testGetExternalURL(): void + { + $alertmanagerMessage = new AlertmanagerMessage(); + $value = $this->faker->sha256(); + + $property = $this->getClassProperty($alertmanagerMessage, 'externalURL'); + $property->setValue($alertmanagerMessage, $value); + + self::assertEquals($value, $alertmanagerMessage->getExternalURL()); + } + + /** + * @throws ReflectionException + */ + public function testSetExternalURL(): void + { + $alertmanagerMessage = new AlertmanagerMessage(); + $value = $this->faker->sha256(); + + self::assertEquals($alertmanagerMessage, $alertmanagerMessage->setExternalURL($value)); + self::assertEquals( + $value, + ($this->getClassProperty($alertmanagerMessage, 'externalURL')) + ->getValue($alertmanagerMessage) + ); + } + + /** + * @throws ReflectionException + */ + public function testGetVersion(): void + { + $alertmanagerMessage = new AlertmanagerMessage(); + $value = $this->faker->sha256(); + + $property = $this->getClassProperty($alertmanagerMessage, 'version'); + $property->setValue($alertmanagerMessage, $value); + + self::assertEquals($value, $alertmanagerMessage->getVersion()); + } + + /** + * @throws ReflectionException + */ + public function testSetVersion(): void + { + $alertmanagerMessage = new AlertmanagerMessage(); + $value = $this->faker->sha256(); + + self::assertEquals($alertmanagerMessage, $alertmanagerMessage->setVersion($value)); + self::assertEquals( + $value, + ($this->getClassProperty($alertmanagerMessage, 'version')) + ->getValue($alertmanagerMessage) + ); + } + + /** + * @throws ReflectionException + */ + public function testGetGroupKey(): void + { + $alertmanagerMessage = new AlertmanagerMessage(); + $value = $this->faker->sha256(); + + $property = $this->getClassProperty($alertmanagerMessage, 'groupKey'); + $property->setValue($alertmanagerMessage, $value); + + self::assertEquals($value, $alertmanagerMessage->getGroupKey()); + } + + /** + * @throws ReflectionException + */ + public function testSetGroupKey(): void + { + $alertmanagerMessage = new AlertmanagerMessage(); + $value = $this->faker->sha256(); + + self::assertEquals($alertmanagerMessage, $alertmanagerMessage->setGroupKey($value)); + self::assertEquals( + $value, + ($this->getClassProperty($alertmanagerMessage, 'groupKey')) + ->getValue($alertmanagerMessage) + ); + } + + /** + * @throws ReflectionException + */ + public function testGetTruncatedAlerts(): void + { + $alertmanagerMessage = new AlertmanagerMessage(); + $value = $this->faker->randomNumber(); + + $property = $this->getClassProperty($alertmanagerMessage, 'truncatedAlerts'); + $property->setValue($alertmanagerMessage, $value); + + self::assertEquals($value, $alertmanagerMessage->getTruncatedAlerts()); + } + + /** + * @throws ReflectionException + */ + public function testSetTruncatedAlerts(): void + { + $alertmanagerMessage = new AlertmanagerMessage(); + $value = $this->faker->randomNumber(); + + self::assertEquals($alertmanagerMessage, $alertmanagerMessage->setTruncatedAlerts($value)); + self::assertEquals( + $value, + ($this->getClassProperty($alertmanagerMessage, 'truncatedAlerts')) + ->getValue($alertmanagerMessage) + ); + } +} diff --git a/app/tests/Unit/Model/Alertmanager/CommonAnnotationTest.php b/app/tests/Unit/Model/Alertmanager/CommonAnnotationTest.php new file mode 100644 index 0000000..132c00c --- /dev/null +++ b/app/tests/Unit/Model/Alertmanager/CommonAnnotationTest.php @@ -0,0 +1,72 @@ +faker->sha256(); + + $property = $this->getClassProperty($commonAnnotation, 'description'); + $property->setValue($commonAnnotation, $value); + + self::assertEquals($value, $commonAnnotation->getDescription()); + } + + /** + * @throws ReflectionException + */ + public function testSetDescription(): void + { + $commonAnnotation = new Annotation(); + $value = $this->faker->sha256(); + + self::assertEquals($commonAnnotation, $commonAnnotation->setDescription($value)); + self::assertEquals( + $value, + ($this->getClassProperty($commonAnnotation, 'description')) + ->getValue($commonAnnotation) + ); + } + + /** + * @throws ReflectionException + */ + public function testGetSummary(): void + { + $commonAnnotation = new Annotation(); + $value = $this->faker->sha256(); + + $property = $this->getClassProperty($commonAnnotation, 'summary'); + $property->setValue($commonAnnotation, $value); + + self::assertEquals($value, $commonAnnotation->getSummary()); + } + + /** + * @throws ReflectionException + */ + public function testSetSummary(): void + { + $commonAnnotation = new Annotation(); + $value = $this->faker->sha256(); + + self::assertEquals($commonAnnotation, $commonAnnotation->setSummary($value)); + self::assertEquals( + $value, + ($this->getClassProperty($commonAnnotation, 'summary')) + ->getValue($commonAnnotation) + ); + } +} diff --git a/app/tests/Unit/Model/Alertmanager/CommonLabelTest.php b/app/tests/Unit/Model/Alertmanager/CommonLabelTest.php new file mode 100644 index 0000000..2779236 --- /dev/null +++ b/app/tests/Unit/Model/Alertmanager/CommonLabelTest.php @@ -0,0 +1,162 @@ +faker->sha256(); + + $property = $this->getClassProperty($commonLabel, 'alertname'); + $property->setValue($commonLabel, $value); + + self::assertEquals($value, $commonLabel->getAlertName()); + } + + /** + * @throws ReflectionException + */ + public function testSetAlertName(): void + { + $commonLabel = new Label(); + $value = $this->faker->sha256(); + + self::assertEquals($commonLabel, $commonLabel->setAlertName($value)); + self::assertEquals( + $value, + ($this->getClassProperty($commonLabel, 'alertname')) + ->getValue($commonLabel) + ); + } + + /** + * @throws ReflectionException + */ + public function testGetInstance(): void + { + $commonLabel = new Label(); + $value = $this->faker->sha256(); + + $property = $this->getClassProperty($commonLabel, 'instance'); + $property->setValue($commonLabel, $value); + + self::assertEquals($value, $commonLabel->getInstance()); + } + + /** + * @throws ReflectionException + */ + public function testSetInstance(): void + { + $commonLabel = new Label(); + $value = $this->faker->sha256(); + + self::assertEquals($commonLabel, $commonLabel->setInstance($value)); + self::assertEquals( + $value, + ($this->getClassProperty($commonLabel, 'instance')) + ->getValue($commonLabel) + ); + } + + /** + * @throws ReflectionException + */ + public function testGetJob(): void + { + $commonLabel = new Label(); + $value = $this->faker->sha256(); + + $property = $this->getClassProperty($commonLabel, 'job'); + $property->setValue($commonLabel, $value); + + self::assertEquals($value, $commonLabel->getJob()); + } + + /** + * @throws ReflectionException + */ + public function testSetJob(): void + { + $commonLabel = new Label(); + $value = $this->faker->sha256(); + + self::assertEquals($commonLabel, $commonLabel->setJob($value)); + self::assertEquals( + $value, + ($this->getClassProperty($commonLabel, 'job')) + ->getValue($commonLabel) + ); + } + + /** + * @throws ReflectionException + */ + public function testGetMonitor(): void + { + $commonLabel = new Label(); + $value = $this->faker->sha256(); + + $property = $this->getClassProperty($commonLabel, 'monitor'); + $property->setValue($commonLabel, $value); + + self::assertEquals($value, $commonLabel->getMonitor()); + } + + /** + * @throws ReflectionException + */ + public function testSetMonitor(): void + { + $commonLabel = new Label(); + $value = $this->faker->sha256(); + + self::assertEquals($commonLabel, $commonLabel->setMonitor($value)); + self::assertEquals( + $value, + ($this->getClassProperty($commonLabel, 'monitor')) + ->getValue($commonLabel) + ); + } + + /** + * @throws ReflectionException + */ + public function testGetSeverity(): void + { + $commonLabel = new Label(); + $value = $this->faker->sha256(); + + $property = $this->getClassProperty($commonLabel, 'severity'); + $property->setValue($commonLabel, $value); + + self::assertEquals($value, $commonLabel->getSeverity()); + } + + /** + * @throws ReflectionException + */ + public function testSetSeverity(): void + { + $commonLabel = new Label(); + $value = $this->faker->sha256(); + + self::assertEquals($commonLabel, $commonLabel->setSeverity($value)); + self::assertEquals( + $value, + ($this->getClassProperty($commonLabel, 'severity')) + ->getValue($commonLabel) + ); + } +} diff --git a/app/tests/Unit/Model/Alertmanager/StatusTest.php b/app/tests/Unit/Model/Alertmanager/StatusTest.php new file mode 100644 index 0000000..82add5f --- /dev/null +++ b/app/tests/Unit/Model/Alertmanager/StatusTest.php @@ -0,0 +1,121 @@ +faker->sha256(); + + $property = $this->getClassProperty($status, 'state'); + $property->setValue($status, $state); + + self::assertEquals($state, $status->getState()); + } + + /** + * @throws ReflectionException + */ + public function testSetLabels(): void + { + $status = new Status(); + $state = $this->faker->sha256(); + + self::assertEquals($status, $status->setState($state)); + self::assertEquals( + $state, + ($this->getClassProperty($status, 'state')) + ->getValue($status) + ); + } + + /** + * @dataProvider dataProviderSilencedBy + * @throws ReflectionException + */ + public function testGetSilencedBy(?string $silencedBy): void + { + $status = new Status(); + + $property = $this->getClassProperty($status, 'silencedBy'); + $property->setValue($status, $silencedBy); + + self::assertEquals($silencedBy, $status->getSilencedBy()); + } + + /** + * @dataProvider dataProviderSilencedBy + * @throws ReflectionException + */ + public function testSetSilencedBy(?string $silencedBy): void + { + $status = new Status(); + + self::assertEquals($status, $status->setSilencedBy($silencedBy)); + self::assertEquals( + $silencedBy, + ($this->getClassProperty($status, 'silencedBy')) + ->getValue($status) + ); + } + + public function dataProviderSilencedBy(): array + { + return [ + ['silencedBy' => null], + ['silencedBy' => $this->faker->sha256()], + ]; + } + + /** + * @dataProvider dataProviderInhibitedBy + * @throws ReflectionException + */ + public function testGetInhibitedBy(?string $inhibitedBy): void + { + $status = new Status(); + + $property = $this->getClassProperty($status, 'inhibitedBy'); + $property->setValue($status, $inhibitedBy); + + self::assertEquals($inhibitedBy, $status->getInhibitedBy()); + } + + /** + * @dataProvider dataProviderInhibitedBy + * @throws ReflectionException + */ + public function testSetInhibitedBy(?string $inhibitedBy): void + { + $status = new Status(); + + self::assertEquals($status, $status->setInhibitedBy($inhibitedBy)); + self::assertEquals( + $inhibitedBy, + ($this->getClassProperty($status, 'inhibitedBy')) + ->getValue($status) + ); + } + + public function dataProviderInhibitedBy(): array + { + return [ + ['inhibitedBy' => null], + ['inhibitedBy' => $this->faker->sha256()], + ]; + } +}