-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathInteropVerifierProvider.cs
314 lines (280 loc) · 13 KB
/
InteropVerifierProvider.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using PactNet.Exceptions;
using PactNet.Interop;
namespace PactNet.Verifier
{
/// <summary>
/// Native pact verifier
/// </summary>
internal class InteropVerifierProvider : IVerifierProvider
{
private readonly PactVerifierConfig config;
private IntPtr handle;
/// <summary>
/// Initialises a new instance of the <see cref="InteropVerifierProvider"/> class.
/// </summary>
/// <param name="config">Pact verifier config</param>
public InteropVerifierProvider(PactVerifierConfig config)
{
this.config = config;
}
/// <summary>
/// Initialise the verifier
/// </summary>
public void Initialise()
{
NativeInterop.LogToBuffer(config.LogLevel switch
{
PactLogLevel.Trace => LevelFilter.Trace,
PactLogLevel.Debug => LevelFilter.Debug,
PactLogLevel.Information => LevelFilter.Info,
PactLogLevel.Warn => LevelFilter.Warn,
PactLogLevel.Error => LevelFilter.Error,
PactLogLevel.None => LevelFilter.Off,
_ => throw new ArgumentOutOfRangeException(nameof(config.LogLevel), config.LogLevel, "Invalid log level")
});
this.handle = NativeInterop.VerifierNewForApplication("pact-net", typeof(InteropVerifierProvider).Assembly.GetName().Version.ToString());
}
/// <summary>
/// Set provider info
/// </summary>
/// <param name="name">Name of the provider</param>
/// <param name="scheme">Provider URI scheme</param>
/// <param name="host">Provider URI host</param>
/// <param name="port">Provider URI port</param>
/// <param name="path">Provider URI path</param>
public void SetProviderInfo(string name, string scheme, string host, ushort port, string path)
{
NativeInterop.VerifierSetProviderInfo(this.handle, name, scheme, host, port, path);
}
/// <summary>
/// Add an extra transport to the provider
/// </summary>
/// <param name="protocol">Transport protocol</param>
/// <param name="port">Port</param>
/// <param name="path">Path</param>
/// <param name="scheme">Scheme</param>
public void AddTransport(string protocol, ushort port, string path, string scheme)
{
NativeInterop.AddProviderTransport(this.handle, protocol, port, path, scheme);
}
/// <summary>
/// Set filter info. Null arguments indicate the option is unused
/// </summary>
/// <param name="description">Filter by description</param>
/// <param name="state">Filter by provider state</param>
/// <param name="noState">Filter to only interactions with (false) or without (true) provider state</param>
public void SetFilterInfo(string description = null, string state = null, bool? noState = null)
{
NativeInterop.VerifierSetFilterInfo(this.handle, description, state, ToSafeByte(noState));
}
/// <summary>
/// Set the provider state endpoint
/// </summary>
/// <param name="url">URL of the endpoint</param>
/// <param name="teardown">Invoke a teardown to the provider state endpoint after each interaction</param>
/// <param name="body">Use request body for provider state requests instead of query params</param>
public void SetProviderState(Uri url, bool teardown, bool body)
{
NativeInterop.VerifierSetProviderState(this.handle, url.AbsoluteUri, ToSafeByte(teardown), ToSafeByte(body));
}
/// <summary>
/// Set verification options
/// </summary>
/// <param name="disableSslVerification">Disable SSL verification</param>
/// <param name="requestTimeout">Request timeout</param>
public void SetVerificationOptions(bool disableSslVerification, TimeSpan requestTimeout)
{
uint timeout = Convert.ToUInt32(requestTimeout.TotalMilliseconds);
NativeInterop.VerifierSetVerificationOptions(this.handle,
ToSafeByte(disableSslVerification),
timeout);
}
/// <summary>
/// Set publish options
/// </summary>
/// <param name="providerVersion">Provider version</param>
/// <param name="buildUrl">URL of the build that ran the verification</param>
/// <param name="providerTags">Provider tags</param>
/// <param name="providerBranch">Provider branch</param>
public void SetPublishOptions(string providerVersion, Uri buildUrl, ICollection<string> providerTags, string providerBranch)
{
NativeInterop.VerifierSetPublishOptions(this.handle,
providerVersion,
buildUrl?.AbsoluteUri,
providerTags.ToArray(),
(ushort)providerTags.Count,
providerBranch);
}
/// <summary>
/// Set consumer filters
/// </summary>
/// <param name="consumerFilters">Consumer filters</param>
public void SetConsumerFilters(ICollection<string> consumerFilters)
{
NativeInterop.VerifierSetConsumerFilters(this.handle, consumerFilters.ToArray(), (ushort)consumerFilters.Count);
}
/// <summary>
/// Add a header which will be used in all calls from the verifier to the provider, for example
/// an Authorization header with a valid auth token
/// </summary>
/// <param name="name">Header name</param>
/// <param name="value">Header value</param>
/// <returns>Fluent builder</returns>
public void AddCustomHeader(string name, string value)
{
NativeInterop.AddCustomHeader(this.handle, name, value);
}
/// <summary>
/// Add a file source
/// </summary>
/// <param name="file">File</param>
public void AddFileSource(FileInfo file)
{
NativeInterop.VerifierAddFileSource(this.handle, file.FullName);
}
/// <summary>
/// Add a directory source
/// </summary>
/// <param name="directory">Directory</param>
/// <remarks>Can be used with <see cref="IVerifierProvider.SetConsumerFilters"/> to filter the files in the directory</remarks>
public void AddDirectorySource(DirectoryInfo directory)
{
NativeInterop.VerifierAddDirectorySource(this.handle, directory.FullName);
}
/// <summary>
/// Add a URL source
/// </summary>
/// <param name="url">URL to the pact file</param>
/// <param name="username">Username</param>
/// <param name="password">Password</param>
/// <param name="token">Authentication token</param>
public void AddUrlSource(Uri url, string username, string password, string token)
{
NativeInterop.VerifierUrlSource(this.handle, url.AbsoluteUri, username, password, token);
}
/// <summary>
/// Add a pact broker source
/// </summary>
/// <param name="url">Pact broker URL</param>
/// <param name="username">Username</param>
/// <param name="password">Password</param>
/// <param name="token">Authentication token</param>
/// <param name="enablePending">Enable pending pacts</param>
/// <param name="includeWipPactsSince">Include WIP pacts since this date</param>
/// <param name="providerTags">Provider tags</param>
/// <param name="providerBranch">Provider branch</param>
/// <param name="consumerVersionSelectors">Consumer version selectors</param>
/// <param name="consumerVersionTags">Consumer version tags</param>
public void AddBrokerSource(Uri url,
string username,
string password,
string token,
bool enablePending,
DateTime? includeWipPactsSince,
ICollection<string> providerTags,
string providerBranch,
ICollection<string> consumerVersionSelectors,
ICollection<string> consumerVersionTags)
{
NativeInterop.VerifierBrokerSourceWithSelectors(this.handle,
url.AbsoluteUri,
username,
password,
token,
ToSafeByte(enablePending),
includeWipPactsSince?.ToString("yyyy-MM-dd"),
providerTags.ToArray(),
(ushort)providerTags.Count,
providerBranch,
consumerVersionSelectors.ToArray(),
(ushort)consumerVersionSelectors.Count,
consumerVersionTags.ToArray(),
(ushort)consumerVersionTags.Count);
}
/// <summary>
/// Verify the pact from the given args
/// </summary>
/// <exception cref="PactFailureException">Verification failed</exception>
public void Execute()
{
int result = NativeInterop.VerifierExecute(this.handle);
if (result == 0)
{
this.config.WriteLine("Pact verification successful\n");
this.PrintOutput();
return;
}
this.config.WriteLine("Pact verification failed\n");
this.PrintOutput();
string error = result switch
{
1 => throw new PactVerificationFailedException("Pact verification failed"),
2 => "Failed to run the verification",
_ => $"An unknown error occurred: {result}"
};
throw new PactFailureException(error);
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
this.ReleaseUnmanagedResources();
GC.SuppressFinalize(this);
}
/// <summary>
/// Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
/// </summary>
~InteropVerifierProvider()
{
this.ReleaseUnmanagedResources();
}
/// <summary>
/// Release unmanaged resources
/// </summary>
private void ReleaseUnmanagedResources()
{
if (this.handle != IntPtr.Zero)
{
NativeInterop.VerifierShutdown(this.handle);
}
this.handle = IntPtr.Zero;
}
/// <summary>
/// Convert a nullable bool to a byte indicating true/false
/// </summary>
/// <param name="value">Bool value</param>
/// <returns>Byte indicating true or false</returns>
private static byte ToSafeByte(bool? value)
{
return value.GetValueOrDefault(false)
? (byte)1
: (byte)0;
}
/// <summary>
/// Print output and logs of the verifier
/// </summary>
private void PrintOutput()
{
IntPtr outputPtr = NativeInterop.VerifierOutput(this.handle, 1);
string output = outputPtr == IntPtr.Zero
? "ERROR: Unable to retrieve verifier output"
: Marshal.PtrToStringAnsi(outputPtr);
this.config.WriteLine("Verifier Output");
this.config.WriteLine("---------------");
this.config.WriteLine(output);
IntPtr logsPtr = NativeInterop.VerifierLogs(this.handle);
string logs = logsPtr == IntPtr.Zero
? "ERROR: Unable to retrieve verifier logs"
: Marshal.PtrToStringAnsi(logsPtr);
this.config.WriteLine("Verifier Logs");
this.config.WriteLine("-------------");
this.config.WriteLine(logs);
}
}
}