-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathAlertHookControllerTests.cs
More file actions
127 lines (108 loc) · 4.11 KB
/
AlertHookControllerTests.cs
File metadata and controls
127 lines (108 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using AwesomeAssertions;
using DotNet.Status.Web.Controllers;
using DotNet.Status.Web.Models;
using DotNet.Status.Web.Options;
using Microsoft.DotNet.Internal.AzureDevOps;
using Microsoft.DotNet.Internal.DependencyInjection;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Moq;
using NUnit.Framework;
namespace DotNet.Status.Web.Tests;
[TestFixture]
public class AlertHookControllerTests
{
[Test]
public void GenerateDescription_WithMissingEvalMatches_DoesNotThrow()
{
AlertHookController controller = CreateController();
GrafanaNotification notification = new GrafanaNotification
{
Title = "Alert title",
State = "alerting",
Message = "Something went wrong",
RuleUrl = "https://example/rule",
EvalMatches = null,
};
Action action = () => controller.GenerateDescription(notification);
action.Should().NotThrow();
string description = controller.GenerateDescription(notification);
description.Should().Contain("Supplemental text");
description.Should().Contain("Grafana-Automated-Alert-Id-");
}
[Test]
public void GenerateComment_WithMissingEvalMatches_DoesNotThrow()
{
AlertHookController controller = CreateController();
GrafanaNotification notification = new GrafanaNotification
{
Title = "Alert title",
State = "alerting",
Message = "Something went wrong",
RuleUrl = "https://example/rule",
EvalMatches = null,
};
Action action = () => controller.GenerateComment(notification);
action.Should().NotThrow();
string comment = controller.GenerateComment(notification);
comment.Should().Contain("Metric state changed to");
comment.Should().Contain("alerting");
}
[Test]
public void GenerateTitle_WithPrefix_PrependsPrefixToTitle()
{
AlertHookController controller = CreateController();
GrafanaNotification notification = new GrafanaNotification
{
Title = "CPU High",
State = "alerting",
};
string title = controller.GenerateTitle(notification);
title.Should().Be("[test] CPU High");
}
[Test]
public void GenerateDescription_WithEvalMatches_IncludesMetrics()
{
AlertHookController controller = CreateController();
GrafanaNotification notification = new GrafanaNotification
{
Title = "Alert title",
State = "alerting",
Message = "High CPU",
RuleUrl = "https://example/rule",
EvalMatches = new List<GrafanaNotificationMatch>
{
new GrafanaNotificationMatch { Metric = "cpu_usage", Value = 95.5 },
}.ToImmutableList(),
};
string description = controller.GenerateDescription(notification);
description.Should().Contain("cpu_usage");
description.Should().Contain("95.5");
}
private static AlertHookController CreateController()
{
Mock<IAzureDevOpsClient> azureDevOpsClient = new(MockBehavior.Strict);
Mock<IClientFactory<IAzureDevOpsClient>> clientFactory = new(MockBehavior.Strict);
IOptions<AzureDevOpsAlertOptions> alertOptions = Microsoft.Extensions.Options.Options.Create(new AzureDevOpsAlertOptions
{
Organization = "dnceng",
Project = "internal",
AreaPath = @"internal\.NET Engineering Services\First Responders",
WorkItemType = "DNCENG Task",
TitlePrefix = "[test] ",
SupplementalBodyText = "Supplemental text",
});
IOptions<GrafanaOptions> grafanaOptions = Microsoft.Extensions.Options.Options.Create(new GrafanaOptions
{
WebhookSecret = "test-secret",
});
return new AlertHookController(
clientFactory.Object,
alertOptions,
grafanaOptions,
NullLogger<AlertHookController>.Instance);
}
}