forked from projectkudu/kudu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebHooksTests.cs
246 lines (194 loc) · 10.9 KB
/
WebHooksTests.cs
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Kudu.Client;
using Kudu.Core;
using Kudu.Core.Deployment;
using Kudu.Core.Hooks;
using Kudu.FunctionalTests.Infrastructure;
using Kudu.TestHarness;
using Newtonsoft.Json;
using Xunit;
namespace Kudu.FunctionalTests
{
public class WebHooksTests
{
[Fact]
public async Task SubscribedWebHooksShouldBeCalledPostDeployment()
{
string testName = "SubscribedWebHooksShouldBeCalledPostDeployment";
var expectedHookAddresses = new List<string>();
string hook1 = "hookCalled/1";
string hook2 = "hookCalled/2";
using (new LatencyLogger(testName))
{
await ApplicationManager.RunAsync("HookSite" + testName, async hookAppManager =>
{
using (var hookAppRepository = Git.Clone("NodeWebHookTest"))
{
string hookAddress1 = hookAppManager.SiteUrl + hook1;
string hookAddress2 = hookAppManager.SiteUrl + hook2;
WebHook webHookAdded1 = await SubscribeWebHook(hookAppManager, hookAddress1, 1);
GitDeployApp(hookAppManager, hookAppRepository);
expectedHookAddresses.Add(hook1);
await VerifyWebHooksCall(expectedHookAddresses, hookAppManager, DeployStatus.Success.ToString(), hookAppRepository.CurrentId);
WebHook webHookAdded2 = await SubscribeWebHook(hookAppManager, hookAddress2, 2);
TestTracer.Trace("Redeploy to allow web hooks to be called");
await hookAppManager.DeploymentManager.DeployAsync(hookAppRepository.CurrentId);
expectedHookAddresses.Add(hook2);
await VerifyWebHooksCall(expectedHookAddresses, hookAppManager, DeployStatus.Success.ToString(), hookAppRepository.CurrentId);
TestTracer.Trace("Make sure web hooks are called for failed deployments");
await hookAppManager.SettingsManager.SetValue("COMMAND", "thisIsAnErrorCommand");
await hookAppManager.DeploymentManager.DeployAsync(hookAppRepository.CurrentId);
await VerifyWebHooksCall(expectedHookAddresses, hookAppManager, DeployStatus.Failed.ToString());
await hookAppManager.SettingsManager.Delete("COMMAND");
TestTracer.Trace("Unsubscribe first hook");
await UnsubscribeWebHook(hookAppManager, webHookAdded1.Id, 1);
TestTracer.Trace("Redeploy to allow web hook to be called");
await hookAppManager.DeploymentManager.DeployAsync(hookAppRepository.CurrentId);
expectedHookAddresses.Remove(hook1);
await VerifyWebHooksCall(expectedHookAddresses, hookAppManager, DeployStatus.Success.ToString(), hookAppRepository.CurrentId);
TestTracer.Trace("Unsubscribe second hook");
await UnsubscribeWebHook(hookAppManager, webHookAdded2.Id, 0);
TestTracer.Trace("Redeploy to verify no web hook was called");
await hookAppManager.DeploymentManager.DeployAsync(hookAppRepository.CurrentId);
TestTracer.Trace("Verify web hook was not called");
expectedHookAddresses.Remove(hook2);
await VerifyWebHooksCall(expectedHookAddresses, hookAppManager, DeployStatus.Success.ToString(), hookAppRepository.CurrentId);
}
});
}
}
[Fact]
public async Task SubscribedWebHooksShouldBeCalledWithPublish()
{
string testName = "SubscribedWebHooksShouldBeCalledWithPublish";
var expectedHookAddresses = new List<string>();
string hook1 = "hookCalled/1";
string hook2 = "hookCalled/2";
using (new LatencyLogger(testName))
{
await ApplicationManager.RunAsync("HookSite" + testName, async hookAppManager =>
{
using (var hookAppRepository = Git.Clone("NodeWebHookTest"))
{
GitDeployApp(hookAppManager, hookAppRepository);
string hookAddress1 = hookAppManager.SiteUrl + hook1;
string hookAddress2 = hookAppManager.SiteUrl + hook2;
string customHookEventType = "CustomEvent";
WebHook webHookAdded1 = await SubscribeWebHook(hookAppManager, hookAddress1, 1, customHookEventType);
WebHook webHookAdded2 = await SubscribeWebHook(hookAppManager, hookAddress2, 2, customHookEventType);
var jsonObject = new
{
TestProperty = "mytest_property",
CustomProperty = "mycustom_property"
};
await hookAppManager.WebHooksManager.PublishEventAsync(customHookEventType, jsonObject);
expectedHookAddresses.Add(hook1);
expectedHookAddresses.Add(hook2);
await VerifyWebHooksCall(expectedHookAddresses, hookAppManager, jsonObject.TestProperty, jsonObject.CustomProperty);
}
});
}
}
[Fact]
public async Task SubscribedWebHooksShouldFailOnErrors()
{
string testName = "SubscribedWebHooksShouldFailOnErrors";
string hook = "hookCalled/1";
using (new LatencyLogger(testName))
{
await ApplicationManager.RunAsync("HookSite" + testName, async hookAppManager =>
{
string customHookEventType = "CustomEvent";
string hookAddress = hookAppManager.SiteUrl + hook;
await hookAppManager.WebHooksManager.SubscribeAsync(new WebHook(customHookEventType, hookAddress));
var thrownException = await ExceptionAssert.ThrowsAsync<HttpUnsuccessfulRequestException>(async () =>
{
await hookAppManager.WebHooksManager.SubscribeAsync(new WebHook(customHookEventType + "_DifferentEvent", hookAddress));
});
Assert.Contains("Conflict", thrownException.Message);
});
}
}
private static void GitDeployApp(ApplicationManager hookAppManager, TestRepository hookAppRepository)
{
TestTracer.Trace("Deploy test app");
hookAppManager.GitDeploy(hookAppRepository.PhysicalPath);
var deploymentResults = hookAppManager.DeploymentManager.GetResultsAsync().Result.ToList();
Assert.Equal(1, deploymentResults.Count);
Assert.Equal(DeployStatus.Success, deploymentResults[0].Status);
}
private static async Task<WebHook> SubscribeWebHook(ApplicationManager hookAppManager, string hookAddress, int expectedHooksCount, string hookEventType = "PostDeployment")
{
TestTracer.Trace("Subscribe web hook to " + hookAddress);
WebHook webHookAdded = await hookAppManager.WebHooksManager.SubscribeAsync(new WebHook(hookEventType, hookAddress));
await VerifyWebHooksCount(hookAppManager, expectedHooksCount);
await VerifyWebHook(hookAppManager, webHookAdded);
return webHookAdded;
}
private static async Task UnsubscribeWebHook(ApplicationManager hookAppManager, string hookAddress, int expectedHooksCount)
{
TestTracer.Trace("Unsubscribe web hook " + hookAddress);
await hookAppManager.WebHooksManager.UnsubscribeAsync(hookAddress);
await VerifyWebHooksCount(hookAppManager, expectedHooksCount);
}
private static async Task VerifyWebHooksCount(ApplicationManager hookAppManager, int expectedHooksCount)
{
TestTracer.Trace("Verify web hook subscribed");
IEnumerable<WebHook> webHooks = await hookAppManager.WebHooksManager.GetWebHooksAsync();
Assert.Equal(expectedHooksCount, webHooks.Count());
}
private static async Task VerifyWebHook(ApplicationManager hookAppManager, WebHook expectedWebHook)
{
TestTracer.Trace("Verify web hook");
WebHook actualWebHooks = await hookAppManager.WebHooksManager.GetWebHookAsync(expectedWebHook.Id);
Assert.Equal(expectedWebHook.HookAddress, actualWebHooks.HookAddress);
Assert.Equal(expectedWebHook.HookEventType, actualWebHooks.HookEventType);
}
private async Task VerifyWebHooksCall(IEnumerable<string> hookAddresses, ApplicationManager hookAppManager, params string[] expectedContents)
{
TestTracer.Trace("Verify web hook was called {0} times".FormatCurrentCulture(hookAddresses.Count()));
string webHookCallResponse = await GetWebHookResponseAsync(hookAppManager.SiteUrl);
string[] webHookResults = webHookCallResponse.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
Assert.Equal(hookAddresses.Count(), webHookResults.Count());
foreach (var hookAddress in hookAddresses)
{
bool found = false;
foreach (var webHookResult in webHookResults)
{
dynamic webHookResultObject = JsonConvert.DeserializeObject(webHookResult);
if (("/" + hookAddress) == (string)webHookResultObject.url)
{
var body = (string)webHookResultObject.body;
found = true;
// Make sure body json
JsonConvert.DeserializeObject(body);
foreach (var expectedContent in expectedContents)
{
Assert.Contains(expectedContent, body, StringComparison.OrdinalIgnoreCase);
}
}
}
Assert.True(found, "Web hook address {0} was not called".FormatCurrentCulture(hookAddress));
}
hookAppManager.VfsWebRootManager.Delete("result.txt");
}
private async Task<string> GetWebHookResponseAsync(string hookAddress)
{
using (var httpClient = new HttpClient())
{
var response = await httpClient.GetAsync(hookAddress);
string responseContent = await response.Content.ReadAsStringAsync();
TestTracer.Trace("Received response: {0}", responseContent);
if (response.IsSuccessStatusCode)
{
return responseContent;
}
return String.Empty;
}
}
}
}