forked from projectkudu/kudu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKuduAssert.cs
161 lines (140 loc) · 6.29 KB
/
KuduAssert.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Kudu.Client;
using Kudu.Client.Infrastructure;
using Kudu.Core;
using Kudu.Core.Deployment;
using Kudu.TestHarness;
using Xunit;
namespace Kudu.FunctionalTests.Infrastructure
{
public static class KuduAssert
{
public const string DefaultPageContent = "This web site has been successfully created";
public static T ThrowsUnwrapped<T>(Action action) where T : Exception
{
var ex = Assert.Throws<AggregateException>(() => action());
var baseEx = ex.GetBaseException();
Assert.IsAssignableFrom<T>(baseEx);
return (T)baseEx;
}
public static async Task<T> ThrowsUnwrappedAsync<T>(Func<Task> action) where T : Exception
{
try
{
await action();
}
catch (Exception ex)
{
Assert.IsAssignableFrom<T>(ex);
return (T)ex;
}
throw new Exception("Not throw, expected: " + typeof(T).Name);
}
public static Exception ThrowsMessage(string expected, Action action)
{
try
{
action();
}
catch (Exception ex)
{
Assert.Contains(expected, ex.Message);
return ex;
}
throw new Exception("Not throw, expected: " + expected);
}
public static void VerifyUrl(Uri url, ICredentials cred, params string[] contents)
{
VerifyUrl(url.ToString(), cred, contents);
}
public static void VerifyUrl(string url, ICredentials cred, params string[] contents)
{
HttpClient client = HttpClientHelper.CreateClient(url, cred);
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Kudu-Test", "1.0"));
var response = client.GetAsync(url).Result.EnsureSuccessful();
if (contents.Length > 0)
{
var responseBody = response.Content.ReadAsStringAsync().Result;
foreach (var content in contents)
{
Assert.Contains(content, responseBody, StringComparison.Ordinal);
}
}
}
public static void VerifyUrl(string url, string content = null, HttpStatusCode statusCode = HttpStatusCode.OK, string httpMethod = "GET", string jsonPayload = "")
{
VerifyUrlAsync(url, content, statusCode, httpMethod, jsonPayload).Wait();
}
public static async Task VerifyUrlAsync(string url, string content = null, HttpStatusCode statusCode = HttpStatusCode.OK, string httpMethod = "GET", string jsonPayload = "")
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Kudu-Test", "1.0"));
HttpResponseMessage response = null;
if (String.Equals(httpMethod, "POST"))
{
response = await client.PostAsync(url, new StringContent(jsonPayload, Encoding.UTF8, "application/json"));
}
else
{
response = await client.GetAsync(url);
}
string responseBody = await response.Content.ReadAsStringAsync();
Assert.True(statusCode == response.StatusCode,
String.Format("For {0}, Expected Status Code: {1} Actual Status Code: {2}. \r\n Response: {3}", url, statusCode, response.StatusCode, responseBody));
if (content != null)
{
Assert.Contains(content, responseBody, StringComparison.Ordinal);
}
}
}
/// <summary>
/// Verifies if a text appears in the trace file.
/// </summary>
public static async Task VerifyTraceAsync(ApplicationManager manager, string text)
{
string trace = await manager.VfsManager.ReadAllTextAsync("LogFiles/Git/trace/trace.xml");
// Since our trace files accumulates traces from multiple tests, we'll identify the start of the current test by looking for the call to delete the web root
// and then look for the text starting at that point
int index = trace.LastIndexOf("/scm/?deleteWebRoot=1", StringComparison.OrdinalIgnoreCase);
index = Math.Max(0, index);
Assert.Contains(text, trace.Substring(index));
}
public static void VerifyLogOutput(ApplicationManager appManager, string id, params string[] expectedMatches)
{
var allEntries = GetLogEntries(appManager, id);
foreach (var expectedMatch in expectedMatches)
{
Assert.True(allEntries.Any(e => e.Message.Contains(expectedMatch)), "Didn't find '{0}' in log output".FormatInvariant(expectedMatch));
}
}
public static void VerifyLogOutputWithUnexpected(ApplicationManager appManager, string id, params string[] unexpectedMatches)
{
var allEntries = GetLogEntries(appManager, id);
Assert.True(unexpectedMatches.All(match => allEntries.All(e => !e.Message.Contains(match))));
}
private static List<LogEntry> GetLogEntries(ApplicationManager appManager, string id)
{
var entries = appManager.DeploymentManager.GetLogEntriesAsync(id).Result.ToList();
Assert.True(entries.Count > 0);
var allDetails = entries.Where(e => e.DetailsUrl != null)
.AsParallel()
.SelectMany(e => appManager.DeploymentManager.GetLogEntryDetailsAsync(id, e.Id).Result)
.ToList();
return entries.Concat(allDetails).ToList();
}
public static void Match(string pattern, string actual, string message = null)
{
Assert.True(Regex.IsMatch(actual, pattern), String.Format("{0}\r\npattern: {1}\r\nactual: {2}\r\n", message, pattern, actual));
}
}
}