forked from projectkudu/kudu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeploymentManagerTests.cs
922 lines (754 loc) · 50.2 KB
/
DeploymentManagerTests.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Kudu.Client;
using Kudu.Client.Deployment;
using Kudu.Client.Infrastructure;
using Kudu.Contracts.Settings;
using Kudu.Contracts.SourceControl;
using Kudu.Core.Deployment;
using Kudu.FunctionalTests.Infrastructure;
using Kudu.TestHarness;
using Newtonsoft.Json.Linq;
using Xunit;
using Xunit.Extensions;
namespace Kudu.FunctionalTests
{
public class DeploymentManagerTests
{
[Fact]
public async Task DeploymentApisReturn404IfDeploymentIdDoesntExist()
{
string appName = "Rtn404IfDeployIdNotExist";
await ApplicationManager.RunAsync(appName, async appManager =>
{
string id = "foo";
var ex = await ExceptionAssert.ThrowsAsync<HttpUnsuccessfulRequestException>(() => appManager.DeploymentManager.DeleteAsync(id));
Assert.Equal(HttpStatusCode.NotFound, ex.ResponseMessage.StatusCode);
ex = await ExceptionAssert.ThrowsAsync<HttpUnsuccessfulRequestException>(() => appManager.DeploymentManager.DeployAsync(id));
Assert.Equal(HttpStatusCode.NotFound, ex.ResponseMessage.StatusCode);
ex = await ExceptionAssert.ThrowsAsync<HttpUnsuccessfulRequestException>(() => appManager.DeploymentManager.DeployAsync(id, clean: true));
Assert.Equal(HttpStatusCode.NotFound, ex.ResponseMessage.StatusCode);
ex = await ExceptionAssert.ThrowsAsync<HttpUnsuccessfulRequestException>(() => appManager.DeploymentManager.GetLogEntriesAsync(id));
Assert.Equal(HttpStatusCode.NotFound, ex.ResponseMessage.StatusCode);
ex = await ExceptionAssert.ThrowsAsync<HttpUnsuccessfulRequestException>(() => appManager.DeploymentManager.GetResultAsync(id));
Assert.Equal(HttpStatusCode.NotFound, ex.ResponseMessage.StatusCode);
ex = await ExceptionAssert.ThrowsAsync<HttpUnsuccessfulRequestException>(() => appManager.DeploymentManager.GetLogEntryDetailsAsync(id, "fakeId"));
Assert.Equal(HttpStatusCode.NotFound, ex.ResponseMessage.StatusCode);
});
}
[Fact]
public async Task DeploymentApis()
{
// Arrange
string appName = "DeploymentApis";
using (var repo = Git.Clone("HelloWorld"))
{
await ApplicationManager.RunAsync(appName, async appManager =>
{
appManager.GitDeploy(repo.PhysicalPath);
var results = (await appManager.DeploymentManager.GetResultsAsync()).ToList();
Assert.Equal(1, results.Count);
var result = results[0];
Assert.Equal("davidebbo", result.Author);
Assert.Equal("[email protected]", result.AuthorEmail);
Assert.True(result.Current);
Assert.Equal(DeployStatus.Success, result.Status);
Assert.NotNull(result.Url);
Assert.NotNull(result.LogUrl);
Assert.True(String.IsNullOrEmpty(result.Deployer));
ICredentials cred = appManager.DeploymentManager.Credentials;
KuduAssert.VerifyUrl(result.Url, cred);
KuduAssert.VerifyUrl(result.LogUrl, cred);
var resultAgain = await appManager.DeploymentManager.GetResultAsync(result.Id);
Assert.Equal("davidebbo", resultAgain.Author);
Assert.Equal("[email protected]", resultAgain.AuthorEmail);
Assert.True(resultAgain.Current);
Assert.Equal(DeployStatus.Success, resultAgain.Status);
Assert.NotNull(resultAgain.Url);
Assert.NotNull(resultAgain.LogUrl);
KuduAssert.VerifyUrl(resultAgain.Url, cred);
KuduAssert.VerifyUrl(resultAgain.LogUrl, cred);
repo.WriteFile("HelloWorld.txt", "This is a test");
Git.Commit(repo.PhysicalPath, "Another commit");
appManager.GitDeploy(repo.PhysicalPath);
results = (await appManager.DeploymentManager.GetResultsAsync()).ToList();
Assert.Equal(2, results.Count);
string oldId = results[1].Id;
// Delete one
await appManager.DeploymentManager.DeleteAsync(oldId);
results = (await appManager.DeploymentManager.GetResultsAsync()).ToList();
Assert.Equal(1, results.Count);
Assert.NotEqual(oldId, results[0].Id);
result = results[0];
// Redeploy
await appManager.DeploymentManager.DeployAsync(result.Id);
// Clean deploy
await appManager.DeploymentManager.DeployAsync(result.Id, clean: true);
var entries = (await appManager.DeploymentManager.GetLogEntriesAsync(result.Id)).ToList();
Assert.True(entries.Count > 0);
// First entry is always null
Assert.Null(entries[0].DetailsUrl);
var entryWithDetails = entries.First(e => e.DetailsUrl != null);
var nested = (await appManager.DeploymentManager.GetLogEntryDetailsAsync(result.Id, entryWithDetails.Id)).ToList();
Assert.True(nested.Count > 0);
KuduAssert.VerifyLogOutput(appManager, result.Id, "Cleaning Git repository");
// Can't delete the active one
var ex = await ExceptionAssert.ThrowsAsync<HttpUnsuccessfulRequestException>(() => appManager.DeploymentManager.DeleteAsync(result.Id));
Assert.Equal(HttpStatusCode.Conflict, ex.ResponseMessage.StatusCode);
// Corrupt git repository by removing HEAD file from it
// And verify git repository is not identified
appManager.VfsManager.Delete("site\\repository\\.git\\HEAD");
var notFoundException = await ExceptionAssert.ThrowsAsync<HttpUnsuccessfulRequestException>(() => appManager.DeploymentManager.DeployAsync(null));
// Expect a not found failure as no repository is found (since the git repository is now corrupted)
Assert.Equal(HttpStatusCode.NotFound, notFoundException.ResponseMessage.StatusCode);
// Another got push should reinitialize the git repository
appManager.GitDeploy(repo.PhysicalPath);
results = (await appManager.DeploymentManager.GetResultsAsync()).ToList();
Assert.Equal(1, results.Count);
Assert.Equal(DeployStatus.Success, results[0].Status);
// Make sure running this again doesn't throw an exception
await appManager.DeploymentManager.DeployAsync(null);
});
}
}
[Fact]
public async Task DeploymentVerifyEtag()
{
string appName = "VerifyEtag";
using (var repo = Git.Clone("HelloWorld"))
{
await ApplicationManager.RunAsync(appName, async appManager =>
{
EntityTagHeaderValue etag = null;
EntityTagHeaderValue etagWithQuery = null;
// no etag
etag = await VerifyEtagAsync(appManager, "/deployments", null, HttpStatusCode.OK);
etagWithQuery = await VerifyEtagAsync(appManager, "/deployments/?$top=1", null, HttpStatusCode.OK);
Assert.NotEqual(etag, etagWithQuery);
// match etag
etag = await VerifyEtagAsync(appManager, "/deployments", etag, HttpStatusCode.NotModified);
etagWithQuery = await VerifyEtagAsync(appManager, "/deployments/?$top=1", etagWithQuery, HttpStatusCode.NotModified);
Assert.NotEqual(etag, etagWithQuery);
appManager.GitDeploy(repo.PhysicalPath);
var results = (await appManager.DeploymentManager.GetResultsAsync()).ToList();
Assert.Equal(1, results.Count);
Assert.Equal(DeployStatus.Success, results[0].Status);
Assert.True(results[0].Current);
// mismatch etag
etag = await VerifyEtagAsync(appManager, "/deployments", etag, HttpStatusCode.OK);
etagWithQuery = await VerifyEtagAsync(appManager, "/deployments/?$top=1", etagWithQuery, HttpStatusCode.OK);
Assert.NotEqual(etag, etagWithQuery);
// match etag
etag = await VerifyEtagAsync(appManager, "/deployments", etag, HttpStatusCode.NotModified);
etagWithQuery = await VerifyEtagAsync(appManager, "/deployments/?$top=1", etagWithQuery, HttpStatusCode.NotModified);
Assert.NotEqual(etag, etagWithQuery);
});
}
}
private async Task<EntityTagHeaderValue> VerifyEtagAsync(ApplicationManager appManager, string uri, EntityTagHeaderValue input, HttpStatusCode statusCode)
{
using (var request = new HttpRequestMessage(HttpMethod.Get, uri))
{
if (input != null)
{
request.Headers.IfNoneMatch.Add(input);
}
using (var response = await appManager.DeploymentManager.Client.SendAsync(request))
{
Assert.Equal(statusCode, response.StatusCode);
Assert.NotNull(response.Headers.ETag);
if (response.StatusCode == HttpStatusCode.NotModified)
{
Assert.Equal(input, response.Headers.ETag);
}
else
{
Assert.NotEqual(input, response.Headers.ETag);
}
return response.Headers.ETag;
}
}
}
[Fact]
public async Task DeploymentManagerExtensibility()
{
// Arrange
string repositoryName = "Mvc3Application";
string appName = "DeploymentApis";
using (var repo = Git.CreateLocalRepository(repositoryName))
{
await ApplicationManager.RunAsync(appName, async appManager =>
{
var handler = new FakeMessageHandler()
{
InnerHandler = HttpClientHelper.CreateClientHandler(appManager.DeploymentManager.ServiceUrl, appManager.DeploymentManager.Credentials)
};
var manager = new RemoteDeploymentManager(appManager.DeploymentManager.ServiceUrl, appManager.DeploymentManager.Credentials, handler);
var results = (await manager.GetResultsAsync()).ToList();
Assert.Equal(0, results.Count);
Assert.NotNull(handler.Url);
});
}
}
[Fact]
public async Task DeleteKuduSiteCleansProperly()
{
string appName = "DeleteKuduSiteCleansProperly";
string defaultHtmFile = "default.htm";
using (var repo = Git.Clone("HelloWorld"))
{
await ApplicationManager.RunAsync(appName, async appManager =>
{
// Deploy HelloWorld repository
appManager.GitDeploy(repo.PhysicalPath);
var results = (await appManager.DeploymentManager.GetResultsAsync()).ToList();
// Verify deployed properly
Assert.Equal(1, results.Count);
Assert.Equal(DeployStatus.Success, results[0].Status);
Assert.NotNull(results[0].LastSuccessEndTime);
// Verify default.htm file from HelloWorld exists
bool defaultHtmExists = appManager.VfsWebRootManager.Exists(defaultHtmFile);
Assert.True(defaultHtmExists, defaultHtmFile + " doesn't exist");
// Add file to wwwroot not through deployment/repository
string extraFileName = "extra.file";
appManager.VfsWebRootManager.WriteAllText(extraFileName, "extra content");
// Delete repository without removing wwwroot
await appManager.RepositoryManager.Delete();
results = (await appManager.DeploymentManager.GetResultsAsync()).ToList();
// Verify deployments were cleaned
Assert.Equal(0, results.Count);
// Verify extra.file was not cleaned
bool extraFileExists = appManager.VfsWebRootManager.Exists(extraFileName);
Assert.True(extraFileExists, extraFileName + " doesn't exist");
// Verify default.htm was not cleaned
defaultHtmExists = appManager.VfsWebRootManager.Exists(defaultHtmFile);
Assert.True(defaultHtmExists, defaultHtmFile + " doesn't exist");
// Redeploy HelloWorld repository
appManager.GitDeploy(repo.PhysicalPath);
results = (await appManager.DeploymentManager.GetResultsAsync()).ToList();
// Verify deployed properly
Assert.Equal(1, results.Count);
Assert.Equal(DeployStatus.Success, results[0].Status);
Assert.NotNull(results[0].LastSuccessEndTime);
// Verify default.htm file from HelloWorld exists
defaultHtmExists = appManager.VfsWebRootManager.Exists(defaultHtmFile);
Assert.True(defaultHtmExists, defaultHtmFile + " doesn't exist");
// Verify extra.file there
extraFileExists = appManager.VfsWebRootManager.Exists(extraFileName);
Assert.True(extraFileExists, extraFileName + " doesn't exist");
});
}
}
[Fact]
public async Task PullApiTestGitHubFormat()
{
string githubPayload = @"{ ""after"": ""ea1c6d7ea669c816dd5f86206f7b47b228fdcacd"", ""before"": ""7e2a599e2d28665047ec347ab36731c905c95e8b"", ""commits"": [ { ""added"": [], ""author"": { ""email"": ""[email protected]"", ""name"": ""Pranav K"", ""username"": ""pranavkm"" }, ""id"": ""43acf30efa8339103e2bed5c6da1379614b00572"", ""message"": ""Changes from master again"", ""modified"": [ ""Hello.txt"" ], ""timestamp"": ""2012-12-17T17:32:15-08:00"" } ], ""compare"": ""https://github.com/KuduApps/GitHookTest/compare/7e2a599e2d28...7e2a599e2d28"", ""created"": false, ""deleted"": false, ""forced"": false, ""head_commit"": { ""added"": [ "".gitignore"", ""SimpleWebApplication.sln"", ""SimpleWebApplication/About.aspx"", ""SimpleWebApplication/About.aspx.cs"", ""SimpleWebApplication/About.aspx.designer.cs"", ""SimpleWebApplication/Account/ChangePassword.aspx"", ""SimpleWebApplication/Account/ChangePassword.aspx.cs"", ""SimpleWebApplication/Account/ChangePassword.aspx.designer.cs"", ""SimpleWebApplication/Account/ChangePasswordSuccess.aspx"", ""SimpleWebApplication/Account/ChangePasswordSuccess.aspx.cs"", ""SimpleWebApplication/Account/ChangePasswordSuccess.aspx.designer.cs"", ""SimpleWebApplication/Account/Login.aspx"", ""SimpleWebApplication/Account/Login.aspx.cs"", ""SimpleWebApplication/Account/Login.aspx.designer.cs"", ""SimpleWebApplication/Account/Register.aspx"", ""SimpleWebApplication/Account/Register.aspx.cs"", ""SimpleWebApplication/Account/Register.aspx.designer.cs"", ""SimpleWebApplication/Account/Web.config"", ""SimpleWebApplication/Default.aspx"", ""SimpleWebApplication/Default.aspx.cs"", ""SimpleWebApplication/Default.aspx.designer.cs"", ""SimpleWebApplication/Global.asax"", ""SimpleWebApplication/Global.asax.cs"", ""SimpleWebApplication/Properties/AssemblyInfo.cs"", ""SimpleWebApplication/Scripts/jquery-1.4.1-vsdoc.js"", ""SimpleWebApplication/Scripts/jquery-1.4.1.js"", ""SimpleWebApplication/Scripts/jquery-1.4.1.min.js"", ""SimpleWebApplication/SimpleWebApplication.csproj"", ""SimpleWebApplication/Site.Master"", ""SimpleWebApplication/Site.Master.cs"", ""SimpleWebApplication/Site.Master.designer.cs"", ""SimpleWebApplication/Styles/Site.css"", ""SimpleWebApplication/Web.Debug.config"", ""SimpleWebApplication/Web.Release.config"", ""SimpleWebApplication/Web.config"" ], ""author"": { ""email"": ""[email protected]"", ""name"": ""davidebbo"", ""username"": ""davidebbo"" }, ""committer"": { ""email"": ""[email protected]"", ""name"": ""davidebbo"", ""username"": ""davidebbo"" }, ""distinct"": false, ""id"": ""7e2a599e2d28665047ec347ab36731c905c95e8b"", ""message"": ""Initial"", ""modified"": [], ""removed"": [], ""timestamp"": ""2011-11-21T23:07:42-08:00"", ""url"": ""https://github.com/KuduApps/GitHookTest/commit/7e2a599e2d28665047ec347ab36731c905c95e8b"" }, ""pusher"": { ""name"": ""none"" }, ""ref"": ""refs/heads/master"", ""repository"": { ""created_at"": ""2012-06-28T00:07:55-07:00"", ""description"": """", ""fork"": false, ""forks"": 1, ""has_downloads"": true, ""has_issues"": true, ""has_wiki"": true, ""language"": ""ASP"", ""name"": ""GitHookTest"", ""open_issues"": 0, ""organization"": ""KuduApps"", ""owner"": { ""email"": ""[email protected]"", ""name"": ""KuduApps"" }, ""private"": false, ""pushed_at"": ""2012-06-28T00:11:48-07:00"", ""size"": 188, ""url"": ""https://github.com/KuduApps/SimpleWebApplication"", ""watchers"": 1 } }";
string appName = "PullApiTestGitHubFormat";
await ApplicationManager.RunAsync(appName, async appManager =>
{
var post = new Dictionary<string, string>
{
{ "payload", githubPayload }
};
await DeployPayloadHelperAsync(appManager, client =>
{
client.DefaultRequestHeaders.Add("X-Github-Event", "push");
return client.PostAsync("deploy?scmType=GitHub", new FormUrlEncodedContent(post));
});
var results = (await appManager.DeploymentManager.GetResultsAsync()).ToList();
Assert.Equal(1, results.Count);
Assert.Equal(DeployStatus.Success, results[0].Status);
Assert.Equal("GitHub", results[0].Deployer);
KuduAssert.VerifyUrl(appManager.SiteUrl, "Welcome to ASP.NET!");
});
}
[Fact]
public async Task PullApiTestBitbucketFormat()
{
string bitbucketPayload = @"{ ""canon_url"": ""https://github.com"", ""commits"": [ { ""author"": ""davidebbo"", ""branch"": ""master"", ""files"": [ { ""file"": ""Mvc3Application/Views/Home/Index.cshtml"", ""type"": ""modified"" } ], ""message"": ""Blah2\n"", ""node"": ""e550351c5188"", ""parents"": [ ""297fcc65308c"" ], ""raw_author"": ""davidebbo <[email protected]>"", ""raw_node"": ""ea1c6d7ea669c816dd5f86206f7b47b228fdcacd"", ""revision"": null, ""size"": -1, ""timestamp"": ""2012-09-20 03:11:20"", ""utctimestamp"": ""2012-09-20 01:11:20+00:00"" } ], ""repository"": { ""absolute_url"": ""/KuduApps/SimpleWebApplication"", ""fork"": false, ""is_private"": false, ""name"": ""Mvc3Application"", ""owner"": ""davidebbo"", ""scm"": ""git"", ""slug"": ""mvc3application"", ""website"": """" }, ""user"": ""davidebbo"" }";
string appName = "PullApiTestBitbucketFormat";
await ApplicationManager.RunAsync(appName, async appManager =>
{
var post = new Dictionary<string, string>
{
{ "payload", bitbucketPayload }
};
await appManager.SettingsManager.SetValue(SettingsKeys.UseShallowClone, "true");
await DeployPayloadHelperAsync(appManager, client =>
{
client.DefaultRequestHeaders.Add("User-Agent", "Bitbucket.org");
return client.PostAsync("deploy?scmType=BitbucketGit", new FormUrlEncodedContent(post));
});
var resultsTask = appManager.DeploymentManager.GetResultsAsync();
var verifyUrl = KuduAssert.VerifyUrlAsync(appManager.SiteUrl, "Welcome to ASP.NET!");
await Task.WhenAll(resultsTask, verifyUrl);
var results = (await resultsTask).ToList();
Assert.Equal(1, results.Count);
Assert.Equal(DeployStatus.Success, results[0].Status);
Assert.True(results[0].Deployer.StartsWith("Bitbucket"));
});
}
[Fact]
public async Task PullApiTestBitbucketFormatWithMercurial()
{
string bitbucketPayload = @"{""canon_url"":""https://bitbucket.org"",""commits"":[{""author"":""pranavkm"",""branch"":""default"",""files"":[{""file"":""Hello.txt"",""type"":""modified""}],""message"":""Some more changes"",""node"":""0bbefd70c4c4"",""parents"":[""3cb8bf8aec0a""],""raw_author"":""Pranav <[email protected]>"",""raw_node"":""0bbefd70c4c4213bba1e91998141f6e861cec24d"",""revision"":4,""size"":-1,""timestamp"":""2012-12-17 19:41:28"",""utctimestamp"":""2012-12-17 18:41:28+00:00""}],""repository"":{""absolute_url"":""/kudutest/hellomercurial/"",""fork"":false,""is_private"":false,""name"":""HelloMercurial"",""owner"":""kudutest"",""scm"":""hg"",""slug"":""hellomercurial"",""website"":""""},""user"":""kudutest""}";
string appName = "PullApiTestBitbucketFormatWithMercurial";
await ApplicationManager.RunAsync(appName, async appManager =>
{
var client = CreateClient(appManager);
await appManager.SettingsManager.SetValue("branch", "default");
client.DefaultRequestHeaders.Add("User-Agent", "Bitbucket.org");
var post = new Dictionary<string, string>
{
{ "payload", bitbucketPayload }
};
(await client.PostAsync("deploy?scmType=BitbucketHg", new FormUrlEncodedContent(post))).EnsureSuccessful();
var results = (await appManager.DeploymentManager.GetResultsAsync()).ToList();
Assert.Equal(1, results.Count);
Assert.Equal(DeployStatus.Success, results[0].Status);
Assert.True(results[0].Deployer.StartsWith("Bitbucket"));
KuduAssert.VerifyUrl(appManager.SiteUrl + "Hello.txt", "Hello mercurial");
});
}
[Fact]
public async Task PullApiTestBitbucketFormatWithPrivateMercurialRepository()
{
string bitbucketPayload = @"{ ""canon_url"": ""https://bitbucket.org"", ""commits"": [ { ""author"": ""pranavkm"", ""branch"": ""Test-Branch"", ""files"": [ { ""file"": ""Hello.txt"", ""type"": ""modified"" } ], ""message"": ""Hello Mercurial! change"", ""node"": ""ee26963f2e54"", ""parents"": [ ""16ea3237dbcd"" ], ""raw_author"": ""Pranav <[email protected]>"", ""raw_node"": ""ee26963f2e54b9db5c0cd160600b29c4f7a7eff7"", ""revision"": 10, ""size"": -1, ""timestamp"": ""2012-12-24 18:22:14"", ""utctimestamp"": ""2012-12-24 17:22:14+00:00"" } ], ""repository"": { ""absolute_url"": ""/kudutest/privatemercurial/"", ""fork"": false, ""is_private"": true, ""name"": ""PrivateMercurial"", ""owner"": ""kudutest"", ""scm"": ""hg"", ""slug"": ""privatemercurial"", ""website"": """" }, ""user"": ""kudutest"" }";
string appName = "PullApiTestBitbucketFormatWithMercurial";
await ApplicationManager.RunAsync(appName, async appManager =>
{
if (!SshHelper.PrepareSSHEnv(appManager.SSHKeyManager))
{
// Run SSH tests only if the key is present
return;
}
var client = CreateClient(appManager);
await appManager.SettingsManager.SetValue("branch", "Test-Branch");
client.DefaultRequestHeaders.Add("User-Agent", "Bitbucket.org");
var post = new Dictionary<string, string>
{
{ "payload", bitbucketPayload }
};
(await client.PostAsync("deploy?scmType=BitbucketHg", new FormUrlEncodedContent(post))).EnsureSuccessful();
var results = (await appManager.DeploymentManager.GetResultsAsync()).ToList();
Assert.Equal(1, results.Count);
Assert.Equal(DeployStatus.Success, results[0].Status);
Assert.True(results[0].Deployer.StartsWith("Bitbucket"));
KuduAssert.VerifyUrl(appManager.SiteUrl + "Hello.txt", "Hello mercurial!");
});
}
[Fact]
public async Task PullApiTestGitlabHQFormat()
{
string payload = @"{ ""before"":""7e2a599e2d28665047ec347ab36731c905c95e8b"", ""after"":""ea1c6d7ea669c816dd5f86206f7b47b228fdcacd"", ""ref"":""refs/heads/master"", ""user_id"":1, ""user_name"":""Remco Ros"", ""commits"":[ { ""id"":""ea1c6d7ea669c816dd5f86206f7b47b228fdcacd"", ""message"":""Settings as content file"", ""timestamp"":""2012-11-11T14:32:02+01:00"", ""url"":""http://gitlab.proscat.nl/inspectbin/commits/4109312962bb269ecc3a0d7a3c82a119dcd54c8b"", ""author"":{ ""name"":""Remco Ros"", ""email"":""[email protected]"" }}], ""repository"":{ ""name"":""testing"", ""private"":false, ""url"":""https://github.com/KuduApps/SimpleWebApplication"", ""description"":null, ""homepage"":""https://github.com/KuduApps/SimpleWebApplication"" }}";
string appName = "PullApiTestGitlabHQFormat";
await ApplicationManager.RunAsync(appName, async appManager =>
{
await DeployPayloadHelperAsync(appManager, client => client.PostAsync("deploy", new StringContent(payload)));
var results = (await appManager.DeploymentManager.GetResultsAsync()).ToList();
Assert.Equal(1, results.Count);
Assert.Equal(DeployStatus.Success, results[0].Status);
Assert.Equal("GitlabHQ", results[0].Deployer);
KuduAssert.VerifyUrl(appManager.SiteUrl, "Welcome to ASP.NET!");
});
}
[Fact]
public async Task PullApiTestCodebaseFormat()
{
string payload = @"{ ""before"":""7e2a599e2d28665047ec347ab36731c905c95e8b"", ""after"":""ea1c6d7ea669c816dd5f86206f7b47b228fdcacd"", ""ref"":""refs/heads/master"", ""repository"":{ ""name"":""testing"", ""public_access"":true, ""url"":""http://test.codebasehq.com/projects/test-repositories/repositories/git1"", ""clone_urls"": {""ssh"": ""[email protected]:test/test-repositories/git1.git"", ""http"": ""https://github.com/KuduApps/SimpleWebApplication""}}}";
string appName = "PullApiTestCodebaseFormat";
await ApplicationManager.RunAsync(appName, async appManager =>
{
var post = new Dictionary<string, string>
{
{ "payload", payload }
};
await DeployPayloadHelperAsync(appManager, client =>
{
client.DefaultRequestHeaders.Add("User-Agent", "Codebasehq.com");
return client.PostAsync("deploy", new FormUrlEncodedContent(post));
});
var results = (await appManager.DeploymentManager.GetResultsAsync()).ToList();
Assert.Equal(1, results.Count);
Assert.Equal(DeployStatus.Success, results[0].Status);
Assert.Equal("CodebaseHQ", results[0].Deployer);
KuduAssert.VerifyUrl(appManager.SiteUrl, "Welcome to ASP.NET!");
});
}
[Fact]
public async Task PullApiTestKilnHgFormat()
{
string kilnPayload = @"{ ""commits"": [ { ""author"": ""Brian Surowiec <[email protected]>"", ""branch"": ""default"", ""id"": ""0bbefd70c4c4213bba1e91998141f6e861cec24d"", ""message"": ""more fun text"", ""revision"": 20, ""tags"": [ ""tip"" ], ""timestamp"": ""1/16/2013 3:32:04 AM"", ""url"": ""https://13degrees.kilnhg.com/Code/Kudu-Public/Group/Site/History/d2415cbaa78e"" } ], ""pusher"": { ""accesstoken"": false, ""email"": ""[email protected]"", ""fullName"": ""Brian Surowiec"" }, ""repository"": { ""central"": true, ""description"": """", ""id"": 113336, ""name"": ""Site"", ""url"": ""https://bitbucket.org/kudutest/hellomercurial/"" } }";
string appName = "PullApiTestKilnHgFormat";
await ApplicationManager.RunAsync(appName, async appManager =>
{
var client = CreateClient(appManager);
// since we're pulling against bitbucket we need to simulate a self-hosted setup of kiln
await appManager.SettingsManager.SetValue("kiln.domain", "bitbucket\\.org");
await appManager.SettingsManager.SetValue("branch", "default");
var post = new Dictionary<string, string>
{
{ "payload", kilnPayload }
};
(await client.PostAsync("deploy", new FormUrlEncodedContent(post))).EnsureSuccessful();
var results = (await appManager.DeploymentManager.GetResultsAsync()).ToList();
Assert.Equal(1, results.Count);
Assert.Equal(DeployStatus.Success, results[0].Status);
Assert.Equal("Kiln", results[0].Deployer);
KuduAssert.VerifyUrl(appManager.SiteUrl + "Hello.txt", "Hello mercurial");
});
}
[Fact]
public async Task PullApiTestGenericFormat()
{
string payload = @"{ ""oldRef"": ""0000000000000000000"", ""newRef"": ""ea1c6d7ea669c816dd5f86206f7b47b228fdcacd"", ""url"": ""https://github.com/KuduApps/SimpleWebApplication.git"", ""deployer"" : ""CodePlex"", ""branch"":""master"" }";
string appName = "PullApiTestGenericFormat";
await ApplicationManager.RunAsync(appName, async appManager =>
{
var post = new Dictionary<string, string>
{
{ "payload", payload }
};
await DeployPayloadHelperAsync(appManager, client => client.PostAsync("deploy", new FormUrlEncodedContent(post)));
var results = (await appManager.DeploymentManager.GetResultsAsync()).ToList();
Assert.Equal(1, results.Count);
Assert.Equal(DeployStatus.Success, results[0].Status);
Assert.Equal("davidebbo", results[0].Author);
Assert.Equal("[email protected]", results[0].AuthorEmail);
Assert.Equal("Settings as content file", results[0].Message.Trim());
Assert.Equal("ea1c6d7ea669c816dd5f86206f7b47b228fdcacd", results[0].Id);
KuduAssert.VerifyUrl(appManager.SiteUrl, "Welcome to ASP.NET!");
Assert.Equal("CodePlex", results[0].Deployer);
// Verify the deployment status
});
}
[Fact]
public async Task PullApiTestGenericFormatCustomBranch()
{
string payload = @"{ ""oldRef"": ""0000000000000000000"", ""newRef"": ""ad21595c668f3de813463df17c04a3b23065fedc"", ""url"": ""https://github.com/KuduApps/RepoWithMultipleBranches.git"", ""deployer"" : ""CodePlex"", branch: ""test"" }";
string appName = "PullApiTestGenericCustomBranch";
await ApplicationManager.RunAsync(appName, async appManager =>
{
await appManager.SettingsManager.SetValue("branch", "test");
var post = new Dictionary<string, string>
{
{ "payload", payload }
};
await DeployPayloadHelperAsync(appManager, client => client.PostAsync("deploy", new FormUrlEncodedContent(post)));
var results = (await appManager.DeploymentManager.GetResultsAsync()).ToList();
Assert.Equal(1, results.Count);
Assert.Equal(DeployStatus.Success, results[0].Status);
KuduAssert.VerifyUrl(appManager.SiteUrl, "Test branch");
Assert.Equal("CodePlex", results[0].Deployer);
});
}
[Fact]
public async Task DeployingBranchThatExists()
{
string payload = @"{ ""url"": ""https://github.com/KuduApps/RepoWithMultipleBranches.git"", ""deployer"" : ""CodePlex"", branch: ""test"", newRef: ""ad21595c668f3de813463df17c04a3b23065fedc"" }";
string appName = "DeployingBranchThatExists";
await ApplicationManager.RunAsync(appName, async appManager =>
{
await appManager.SettingsManager.SetValue("branch", "test");
var post = new Dictionary<string, string>
{
{ "payload", payload }
};
await DeployPayloadHelperAsync(appManager, client => client.PostAsync("deploy", new FormUrlEncodedContent(post)));
var results = (await appManager.DeploymentManager.GetResultsAsync()).ToList();
Assert.Equal(1, results.Count);
Assert.Equal(DeployStatus.Success, results[0].Status);
KuduAssert.VerifyUrl(appManager.SiteUrl, "Test branch");
Assert.Equal("CodePlex", results[0].Deployer);
});
}
[Fact]
public async Task PullApiTestSimpleFormatMultiBranchWithUpdates()
{
var payload = new JObject();
payload["url"] = "https://github.com/KuduApps/HelloKudu";
payload["format"] = "basic";
string appName = "HelloKudu";
await ApplicationManager.RunAsync(appName, async appManager =>
{
// Fetch master branch from first repo
await DeployPayloadHelperAsync(appManager, client => client.PostAsJsonAsync("deploy", payload));
var results = (await appManager.DeploymentManager.GetResultsAsync()).ToList();
Assert.Equal(1, results.Count);
Assert.Equal(DeployStatus.Success, results[0].Status);
Assert.Equal("GitHub", results[0].Deployer);
KuduAssert.VerifyUrl(appManager.SiteUrl, "<h1>Hello Kudu</h1>");
// Switch to foo branch from first repo
await appManager.SettingsManager.SetValue("branch", "foo");
await DeployPayloadHelperAsync(appManager, client => client.PostAsJsonAsync("deploy", payload));
KuduAssert.VerifyUrl(appManager.SiteUrl, "<h1>Hello Kudu - foo</h1>");
// Fetch master branch from second repo to simulate update. It has one more commit over first repo
payload["url"] = "https://github.com/KuduApps/HelloKudu2";
await appManager.SettingsManager.SetValue("branch", "master");
await DeployPayloadHelperAsync(appManager, client => client.PostAsJsonAsync("deploy", payload));
KuduAssert.VerifyUrl(appManager.SiteUrl, "<h1>Hello again Kudu</h1>");
// Fetch foo branch from second repo to simulate update. It has a different commit that cannot be
// fast-forwarded from the foo branch in the first repo
await appManager.SettingsManager.SetValue("branch", "foo");
await DeployPayloadHelperAsync(appManager, client => client.PostAsJsonAsync("deploy", payload));
KuduAssert.VerifyUrl(appManager.SiteUrl, "<h1>Hi Kudu foo</h1>");
});
}
[Fact]
public async Task PullApiTestSimpleFormatWithMercurial()
{
string payload = @"{""url"":""https://bitbucket.org/kudutest/hellomercurial/"",""format"":""basic"",""scm"":""hg""}";
string appName = "PullApiTestSimpleFormatWithMercurial";
await ApplicationManager.RunAsync(appName, async appManager =>
{
var client = CreateClient(appManager);
await appManager.SettingsManager.SetValue("branch", "default");
var post = new Dictionary<string, string>
{
{ "payload", payload }
};
(await client.PostAsync("deploy", new FormUrlEncodedContent(post))).EnsureSuccessful();
var results = (await appManager.DeploymentManager.GetResultsAsync()).ToList();
Assert.Equal(1, results.Count);
Assert.Equal(DeployStatus.Success, results[0].Status);
Assert.Equal("Bitbucket", results[0].Deployer);
KuduAssert.VerifyUrl(appManager.SiteUrl + "Hello.txt", "Hello mercurial");
});
}
[Fact]
public async Task PullApiTestSimpleFormatWithScmTypeNone()
{
var payload = new JObject();
payload["url"] = "https://github.com/KuduApps/HelloKudu";
payload["format"] = "basic";
string appName = "HelloKudu";
await ApplicationManager.RunAsync(appName, async appManager =>
{
await appManager.SettingsManager.SetValue(SettingsKeys.ScmType, ScmType.None.ToString());
await DeployPayloadHelperAsync(appManager, client => client.PostAsJsonAsync("deploy", payload));
var results = (await appManager.DeploymentManager.GetResultsAsync()).ToList();
Assert.Equal(1, results.Count);
Assert.Equal(DeployStatus.Success, results[0].Status);
Assert.Equal("GitHub", results[0].Deployer);
KuduAssert.VerifyUrl(appManager.SiteUrl, "Hello Kudu");
});
}
[Fact]
public async Task PullApiTestRepoWithLongPath()
{
var payload = new JObject();
payload["url"] = "https://github.com/KuduApps/RepoWithLongPath.git";
payload["format"] = "basic";
string appName = "RepoWithLongPath";
await ApplicationManager.RunAsync(appName, async appManager =>
{
var exception = await ExceptionAssert.ThrowsAsync<HttpUnsuccessfulRequestException>(async () =>
{
await PostPayloadHelperAsync(appManager, client => client.PostAsJsonAsync("deploy", payload));
});
Assert.Contains("unable to create file symfony", exception.Message);
var results = (await appManager.DeploymentManager.GetResultsAsync()).ToList();
Assert.Equal(1, results.Count);
Assert.Equal(DeployStatus.Failed, results[0].Status);
var entries = (await appManager.DeploymentManager.GetLogEntriesAsync(results[0].Id)).ToList();
Assert.Equal(1, entries.Count);
Assert.Equal("Fetching changes.", entries[0].Message);
Assert.Equal(LogEntryType.Error, entries[0].Type);
var details = (await appManager.DeploymentManager.GetLogEntryDetailsAsync(results[0].Id, entries[0].Id)).ToList();
Assert.True(details.Count > 0, "must have at one log detail entry.");
Assert.Contains("unable to create file symfony", details[0].Message);
Assert.Equal(LogEntryType.Error, details[0].Type);
// Must not have entry with "An unknown error has occurred"
foreach (var detail in details)
{
Assert.False(detail.Message.Contains("An unknown error has occurred"), "Must not contain unknow error!");
}
});
}
[Theory]
[InlineData("https://github.com/KuduApps/EmptyGitRepo", null)]
[InlineData("https://bitbucket.org/kudutest/emptyhgrepo", "hg")]
public async Task PullApiTestEmptyRepo(string url, string scm)
{
var payload = new JObject();
payload["url"] = url;
payload["format"] = "basic";
if (!String.IsNullOrEmpty(scm))
{
payload["scm"] = scm;
}
string appName = "PullApiTestGitEmptyRepo";
await ApplicationManager.RunAsync(appName, async appManager =>
{
// Fetch master branch from first repo
await DeployPayloadHelperAsync(appManager, client => client.PostAsJsonAsync("deploy", payload));
var results = (await appManager.DeploymentManager.GetResultsAsync()).ToList();
Assert.Equal(0, results.Count);
});
}
[Fact]
public async Task DeployHookWithInvalidHttpMethod()
{
string appName = "HelloKudu";
await ApplicationManager.RunAsync(appName, async appManager =>
{
var client = CreateClient(appManager);
HttpResponseMessage response = await client.GetAsync("deploy");
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
response = await client.DeleteAsync("deploy");
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
try
{
response = await client.PutAsync("deploy");
}
catch (HttpRequestException ex)
{
Assert.Contains("404", ex.Message);
}
});
}
[Theory]
[InlineData(null)]
[InlineData("hg")]
public async Task PullApiTestRepoInvalidUrl(string scm)
{
Random random = new Random();
string appName = "RepoInvalidUrl";
await ApplicationManager.RunAsync(appName, async appManager =>
{
await appManager.SSHKeyManager.GetPublicKey(ensurePublicKey: true);
// Run per each scm in random order.
foreach (var info in GetRepoInvalidInfos().Where(r => r.Scm == scm).OrderBy(r => random.Next()))
{
TestTracer.Trace("Scenario: " + info);
// Test
var exception = await ExceptionAssert.ThrowsAsync<HttpUnsuccessfulRequestException>(async () =>
{
await PostPayloadHelperAsync(appManager, client => client.PostAsJsonAsync("deploy", info.Payload));
});
// Assert
KuduAssert.Match(info.Expect, exception.Message, info.ToString());
}
});
}
private static IEnumerable<RepoInvalidInfo> GetRepoInvalidInfos()
{
yield return new RepoInvalidInfo("InvalidUrl", "Repository url 'InvalidUrl' is invalid.", null);
yield return new RepoInvalidInfo("InvalidUrl", "Repository url 'InvalidUrl' is invalid.", null);
yield return new RepoInvalidInfo(".", "Repository url '.' is invalid.", null);
yield return new RepoInvalidInfo("http://google.com/", "fatal:.*http://google.com.* not found", null);
yield return new RepoInvalidInfo("http://google.com/", "abort: 'http://www.google.com/' does not appear to be an hg repository", "hg");
yield return new RepoInvalidInfo("InvalidScheme://abcdefghigkl.com/", "fatal: Unable to find remote helper for 'InvalidScheme'", null);
yield return new RepoInvalidInfo("InvalidScheme://abcdefghigkl.com/", "abort: repository InvalidScheme://abcdefghigkl.com/ not found", "hg");
yield return new RepoInvalidInfo("http://abcdefghigkl.com/", "Could.*n.*t resolve host.*abcdefghigkl.com", null);
yield return new RepoInvalidInfo("http://abcdefghigkl.com/", "abort: error: getaddrinfo failed.*hg.exe pull", "hg");
yield return new RepoInvalidInfo("https://abcdefghigkl.com/", "Could.*n.*t resolve host.*abcdefghigkl.com", null);
yield return new RepoInvalidInfo("https://abcdefghigkl.com/", "abort: error: getaddrinfo failed.*hg.exe pull", "hg");
yield return new RepoInvalidInfo("[email protected]:Invalid/Invalid.git", "ssh: abcdefghigkl.com: no address associated with name", null);
yield return new RepoInvalidInfo("ssh://[email protected]/Invalid/Invalid.git", "abort: no suitable response from remote hg.*hg.exe pull", "hg");
yield return new RepoInvalidInfo("[email protected]:Invalid/Invalid.git", "Permission denied [(]publickey[)]", null);
yield return new RepoInvalidInfo("[email protected]:Invalid/Invalid.git", "Permission denied [(]publickey[)]", null);
yield return new RepoInvalidInfo("[email protected]:KuduApps/Invalid.git", "Permission denied [(]publickey[)]", null);
yield return new RepoInvalidInfo("[email protected]:kudutest/Invalid.git", "Permission denied [(]publickey[)]", null);
yield return new RepoInvalidInfo("[email protected]:KuduApps/HelloKudu.git", "Permission denied [(]publickey[)]", null);
yield return new RepoInvalidInfo("[email protected]:kudutest/jeanprivate.git", "Permission denied [(]publickey[)]", null);
yield return new RepoInvalidInfo("https://github.com/KuduApps/HelloKudu.git", "abort: HTTP Error 406: Not Acceptable.*hg.exe pull https://github.com/KuduApps/HelloKudu.git", "hg");
yield return new RepoInvalidInfo("https://bitbucket.org/kudutest/hellomercurial/", "fatal:.*https://bitbucket.org/kudutest/hellomercurial.* not found", null);
yield return new RepoInvalidInfo("https://github.com/Invalid/Invalid.git", "fatal: Authentication failed.*git.exe fetch", null);
yield return new RepoInvalidInfo("https://github.com/KuduQAOrg/Invalid.git", "fatal: Authentication failed.*git.exe fetch", null);
yield return new RepoInvalidInfo("https://github.com/KuduQAOrg/PrivateSubModule.git", "fatal: Authentication failed.*git.exe fetch", null);
yield return new RepoInvalidInfo("https://[email protected]/KuduQAOrg/PrivateSubModule.git", "fatal: Authentication failed.*git.exe fetch", null);
yield return new RepoInvalidInfo("https://[email protected]/KuduQAOrg/PrivateSubModule.git", "fatal: Authentication failed.*git.exe fetch", null);
yield return new RepoInvalidInfo("https://KuduQAOrg:[email protected]/KuduQAOrg/PrivateSubModule.git", "fatal: Authentication failed.*git.exe fetch external", null);
yield return new RepoInvalidInfo("https://bitbucket.org/Invalid/Invalid.git", "fatal:.*https://bitbucket.org/Invalid/Invalid.git.* not found", null);
yield return new RepoInvalidInfo("https://bitbucket.org/kudutest/Invalid.git", "fatal:.*https://bitbucket.org/kudutest/Invalid.git.* not found", null);
yield return new RepoInvalidInfo("https://bitbucket.org/kudutest/jeanprivate.git", "fatal: Authentication failed.*git.exe fetch", null);
yield return new RepoInvalidInfo("https://[email protected]/kudutest/jeanprivate.git", "fatal: Authentication failed.*git.exe fetch", null);
yield return new RepoInvalidInfo("https://[email protected]/kudutest/jeanprivate.git", "fatal: Authentication failed.*git.exe fetch", null);
yield return new RepoInvalidInfo("https://kudutest:[email protected]/kudutest/jeanprivate.git", "fatal: Authentication failed.*git.exe fetch external", null);
}
public class RepoInvalidInfo
{
public RepoInvalidInfo(string url, string expect, string scm)
{
this.Url = url;
this.Expect = expect;
this.Scm = scm;
this.Payload = new JObject();
this.Payload["url"] = url;
this.Payload["format"] = "basic";
if (!String.IsNullOrEmpty(scm))
{
this.Payload["scm"] = scm;
}
}
public string Url { get; set; }
public string Expect { get; set; }
public string Scm { get; set; }
public JObject Payload { get; set; }
public override string ToString()
{
return String.Format("RepoInvalidInfo(url: \"{0},\" expect: \"{1}\", scm: \"{2}\")", this.Url, this.Expect, this.Scm);
}
}
private static async Task DeployPayloadHelperAsync(ApplicationManager appManager, Func<HttpClient, Task<HttpResponseMessage>> func)
{
(await PostPayloadHelperAsync(appManager, func)).EnsureSuccessful().Dispose();
}
private static async Task<HttpResponseMessage> PostPayloadHelperAsync(ApplicationManager appManager, Func<HttpClient, Task<HttpResponseMessage>> func)
{
using (HttpClient client = CreateClient(appManager))
{
HttpResponseMessage response = await func(client);
if (response.StatusCode == HttpStatusCode.InternalServerError)
{
response.EnsureSuccessful();
}
return response;
}
}
private static HttpClient CreateClient(ApplicationManager appManager)
{
HttpClientHandler handler = HttpClientHelper.CreateClientHandler(appManager.ServiceUrl, appManager.DeploymentManager.Credentials);
return new HttpClient(handler)
{
BaseAddress = new Uri(appManager.ServiceUrl),
Timeout = TimeSpan.FromMinutes(5)
};
}
private async Task WaitForAnyBuildingDeploymentAsync(ApplicationManager appManager)
{
bool deploying = false;
int breakLoop = 0;
do
{
Thread.Sleep(100);
var results = (await appManager.DeploymentManager.GetResultsAsync()).ToList();
deploying =
results != null &&
results.Any(r => r.Status == DeployStatus.Building);
breakLoop++;
if (breakLoop > 200)
{
Assert.True(false, "No deployment result in pending state");
}
}
while (!deploying);
}
private class FakeMessageHandler : DelegatingHandler
{
public Uri Url { get; set; }
protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
Url = request.RequestUri;
return base.SendAsync(request, cancellationToken);
}
}
}
}