-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade nugets and allow tests to work on linx (#142)
- Loading branch information
Showing
3 changed files
with
189 additions
and
187 deletions.
There are no files selected for viewing
172 changes: 87 additions & 85 deletions
172
src/AzureEventGridSimulator.Tests/ActualSimulatorTests/ActualSimulatorFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,87 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace AzureEventGridSimulator.Tests.ActualSimulatorTests; | ||
|
||
public class ActualSimulatorFixture : IDisposable, IAsyncLifetime | ||
{ | ||
private const string SimulatorFileName = "AzureEventGridSimulator"; | ||
private bool _disposed; | ||
private string _simulatorExePath; | ||
|
||
private Process _simulatorProcess; | ||
|
||
public Task InitializeAsync() | ||
{ | ||
var simulatorDirectory = Directory.GetCurrentDirectory(); | ||
_simulatorExePath = Path.Combine(simulatorDirectory, $"{SimulatorFileName}.exe"); | ||
|
||
KillExistingSimulators(); | ||
|
||
_simulatorProcess = Process.Start(new ProcessStartInfo(_simulatorExePath) | ||
{ | ||
WorkingDirectory = simulatorDirectory, | ||
UseShellExecute = false, | ||
RedirectStandardOutput = true, | ||
CreateNoWindow = true, | ||
Environment = { new KeyValuePair<string, string>("ASPNETCORE_ENVIRONMENT", "Test") } | ||
}); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public Task DisposeAsync() | ||
{ | ||
Dispose(); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (!_disposed) | ||
{ | ||
if (_simulatorProcess?.HasExited == false) | ||
{ | ||
_simulatorProcess?.Kill(true); | ||
_simulatorProcess?.WaitForExit(); | ||
} | ||
|
||
_disposed = true; | ||
GC.SuppressFinalize(this); | ||
} | ||
} | ||
|
||
private void KillExistingSimulators() | ||
{ | ||
try | ||
{ | ||
// Kill any existing instances of the test simulator that may still be hanging around. | ||
// Note: there shouldn't be any unless something went wrong and the test runner didn't exit cleanly. | ||
var simulatorProcesses = Process.GetProcesses() | ||
.Where(o => o.ProcessName == SimulatorFileName) | ||
.Where(o => string.Equals(o.MainModule?.FileName, _simulatorExePath, StringComparison.OrdinalIgnoreCase)) | ||
.ToArray(); | ||
|
||
foreach (var process in simulatorProcesses) | ||
{ | ||
process.Kill(); | ||
} | ||
} | ||
catch | ||
{ | ||
// | ||
} | ||
} | ||
|
||
~ActualSimulatorFixture() | ||
{ | ||
Dispose(); | ||
} | ||
} | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace AzureEventGridSimulator.Tests.ActualSimulatorTests; | ||
|
||
public class ActualSimulatorFixture : IDisposable, IAsyncLifetime | ||
{ | ||
private const string SimulatorFileName = "AzureEventGridSimulator"; | ||
private bool _disposed; | ||
private string _simulatorExePath; | ||
|
||
private Process _simulatorProcess; | ||
|
||
public async Task InitializeAsync() | ||
{ | ||
var simulatorDirectory = Directory.GetCurrentDirectory(); | ||
var executable = Path.GetFileNameWithoutExtension(typeof(Program).Assembly.Location); | ||
_simulatorExePath = Path.Combine(simulatorDirectory, executable); | ||
|
||
KillExistingSimulators(); | ||
|
||
_simulatorProcess = Process.Start(new ProcessStartInfo(_simulatorExePath) | ||
{ | ||
WorkingDirectory = simulatorDirectory, | ||
UseShellExecute = false, | ||
RedirectStandardOutput = true, | ||
CreateNoWindow = true, | ||
Environment = { new KeyValuePair<string, string>("ASPNETCORE_ENVIRONMENT", "Test") } | ||
}); | ||
|
||
await Task.Delay(1000); | ||
} | ||
|
||
public Task DisposeAsync() | ||
{ | ||
Dispose(); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (!_disposed) | ||
{ | ||
if (_simulatorProcess?.HasExited == false) | ||
{ | ||
_simulatorProcess?.Kill(true); | ||
_simulatorProcess?.WaitForExit(); | ||
} | ||
|
||
_disposed = true; | ||
GC.SuppressFinalize(this); | ||
} | ||
} | ||
|
||
private void KillExistingSimulators() | ||
{ | ||
try | ||
{ | ||
// Kill any existing instances of the test simulator that may still be hanging around. | ||
// Note: there shouldn't be any unless something went wrong and the test runner didn't exit cleanly. | ||
var simulatorProcesses = Process.GetProcesses() | ||
.Where(o => o.ProcessName == SimulatorFileName) | ||
.Where(o => string.Equals(o.MainModule?.FileName, _simulatorExePath, StringComparison.OrdinalIgnoreCase)) | ||
.ToArray(); | ||
|
||
foreach (var process in simulatorProcesses) | ||
{ | ||
process.Kill(); | ||
} | ||
} | ||
catch | ||
{ | ||
// | ||
} | ||
} | ||
|
||
~ActualSimulatorFixture() | ||
{ | ||
Dispose(); | ||
} | ||
} |
198 changes: 99 additions & 99 deletions
198
src/AzureEventGridSimulator.Tests/ActualSimulatorTests/AzureMessagingEventGridTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,99 @@ | ||
using System; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Azure; | ||
using Azure.Core; | ||
using Azure.Messaging.EventGrid; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace AzureEventGridSimulator.Tests.ActualSimulatorTests; | ||
|
||
/// <summary> | ||
/// Simple tests to check that we can send an event via Azure.Messaging.EventGrid library. | ||
/// NOTE: These tests require (and automatically start) an actual instance of AzureEventGridSimulator.exe as there is no way to inject an HttpClient (from a WebApplicationFactory) | ||
/// into Azure.Messaging.EventGrid. | ||
/// </summary> | ||
[Collection(nameof(ActualSimulatorFixtureCollection))] | ||
[Trait("Category", "integration-actual")] | ||
public class AzureMessagingEventGridTest | ||
{ | ||
// ReSharper disable once NotAccessedField.Local | ||
private readonly ActualSimulatorFixture _actualSimulatorFixture; | ||
|
||
public AzureMessagingEventGridTest(ActualSimulatorFixture actualSimulatorFixture) | ||
{ | ||
_actualSimulatorFixture = actualSimulatorFixture; | ||
} | ||
|
||
[Fact] | ||
public async Task GivenValidEvent_WhenUriContainsNonStandardPort_ThenItShouldBeAccepted() | ||
{ | ||
var client = new EventGridPublisherClient( | ||
new Uri("https://localhost:60101/api/events"), | ||
new AzureKeyCredential("TheLocal+DevelopmentKey="), | ||
new EventGridPublisherClientOptions | ||
{ Retry = { Mode = RetryMode.Fixed, MaxRetries = 0, NetworkTimeout = TimeSpan.FromSeconds(5) } }); | ||
|
||
var response = await client.SendEventAsync(new EventGridEvent("/the/subject", "The.Event.Type", "v1", new { Id = 1, Foo = "Bar" })); | ||
|
||
response.Status.ShouldBe((int)HttpStatusCode.OK); | ||
} | ||
|
||
[Fact] | ||
public async Task GivenValidEvents_WhenUriContainsNonStandardPort_TheyShouldBeAccepted() | ||
{ | ||
var client = new EventGridPublisherClient( | ||
new Uri("https://localhost:60101/api/events"), | ||
new AzureKeyCredential("TheLocal+DevelopmentKey="), | ||
new EventGridPublisherClientOptions | ||
{ Retry = { Mode = RetryMode.Fixed, MaxRetries = 0, NetworkTimeout = TimeSpan.FromSeconds(5) } }); | ||
|
||
var events = new[] | ||
{ | ||
new EventGridEvent("/the/subject1", "The.Event.Type1", "v1", new { Id = 1, Foo = "Bar" }), | ||
new EventGridEvent("/the/subject2", "The.Event.Type2", "v1", new { Id = 2, Foo = "Baz" }) | ||
}; | ||
|
||
var response = await client.SendEventsAsync(events); | ||
|
||
response.Status.ShouldBe((int)HttpStatusCode.OK); | ||
} | ||
|
||
[Fact] | ||
public async Task GivenValidEvent_WhenUriContainsNonExistentPort_ThenItShouldNotBeAccepted() | ||
{ | ||
var client = new EventGridPublisherClient( | ||
new Uri("https://localhost:19999/api/events"), | ||
new AzureKeyCredential("TheLocal+DevelopmentKey="), | ||
new EventGridPublisherClientOptions | ||
{ Retry = { Mode = RetryMode.Fixed, MaxRetries = 0, NetworkTimeout = TimeSpan.FromSeconds(5) } }); | ||
|
||
var exception = await Should.ThrowAsync<RequestFailedException>(async () => | ||
{ | ||
await client.SendEventAsync(new EventGridEvent("/the/subject", "The.Event.Type", "v1", | ||
new { Id = 1, Foo = "Bar" })); | ||
}); | ||
|
||
exception.Message.ShouldContain("actively refused"); | ||
exception.Status.ShouldBe(0); | ||
} | ||
|
||
[Fact] | ||
public async Task GivenValidEvent_WhenKeyIsWrong_ThenItShouldNotBeAccepted() | ||
{ | ||
var client = new EventGridPublisherClient( | ||
new Uri("https://localhost:60101/api/events"), | ||
new AzureKeyCredential("TheWrongLocal+DevelopmentKey="), | ||
new EventGridPublisherClientOptions | ||
{ Retry = { Mode = RetryMode.Fixed, MaxRetries = 0, NetworkTimeout = TimeSpan.FromSeconds(5) } }); | ||
|
||
var exception = await Should.ThrowAsync<RequestFailedException>(async () => | ||
{ | ||
await client.SendEventAsync(new EventGridEvent("/the/subject", "The.Event.Type", "v1", | ||
new { Id = 1, Foo = "Bar" })); | ||
}); | ||
|
||
exception.Status.ShouldBe((int)HttpStatusCode.Unauthorized); | ||
} | ||
} | ||
using System; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Azure; | ||
using Azure.Core; | ||
using Azure.Messaging.EventGrid; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace AzureEventGridSimulator.Tests.ActualSimulatorTests; | ||
|
||
/// <summary> | ||
/// Simple tests to check that we can send an event via Azure.Messaging.EventGrid library. | ||
/// NOTE: These tests require (and automatically start) an actual instance of AzureEventGridSimulator.exe as there is no way to inject an HttpClient (from a WebApplicationFactory) | ||
/// into Azure.Messaging.EventGrid. | ||
/// </summary> | ||
[Collection(nameof(ActualSimulatorFixtureCollection))] | ||
[Trait("Category", "integration-actual")] | ||
public class AzureMessagingEventGridTest | ||
{ | ||
// ReSharper disable once NotAccessedField.Local | ||
private readonly ActualSimulatorFixture _actualSimulatorFixture; | ||
|
||
public AzureMessagingEventGridTest(ActualSimulatorFixture actualSimulatorFixture) | ||
{ | ||
_actualSimulatorFixture = actualSimulatorFixture; | ||
} | ||
|
||
[Fact] | ||
public async Task GivenValidEvent_WhenUriContainsNonStandardPort_ThenItShouldBeAccepted() | ||
{ | ||
var client = new EventGridPublisherClient( | ||
new Uri("https://localhost:60101/api/events"), | ||
new AzureKeyCredential("TheLocal+DevelopmentKey="), | ||
new EventGridPublisherClientOptions | ||
{ Retry = { Mode = RetryMode.Fixed, MaxRetries = 0, NetworkTimeout = TimeSpan.FromSeconds(5) } }); | ||
|
||
var response = await client.SendEventAsync(new EventGridEvent("/the/subject", "The.Event.Type", "v1", new { Id = 1, Foo = "Bar" })); | ||
|
||
response.Status.ShouldBe((int)HttpStatusCode.OK); | ||
} | ||
|
||
[Fact] | ||
public async Task GivenValidEvents_WhenUriContainsNonStandardPort_TheyShouldBeAccepted() | ||
{ | ||
var client = new EventGridPublisherClient( | ||
new Uri("https://localhost:60101/api/events"), | ||
new AzureKeyCredential("TheLocal+DevelopmentKey="), | ||
new EventGridPublisherClientOptions | ||
{ Retry = { Mode = RetryMode.Fixed, MaxRetries = 0, NetworkTimeout = TimeSpan.FromSeconds(5) } }); | ||
|
||
var events = new[] | ||
{ | ||
new EventGridEvent("/the/subject1", "The.Event.Type1", "v1", new { Id = 1, Foo = "Bar" }), | ||
new EventGridEvent("/the/subject2", "The.Event.Type2", "v1", new { Id = 2, Foo = "Baz" }) | ||
}; | ||
|
||
var response = await client.SendEventsAsync(events); | ||
|
||
response.Status.ShouldBe((int)HttpStatusCode.OK); | ||
} | ||
|
||
[Fact] | ||
public async Task GivenValidEvent_WhenUriContainsNonExistentPort_ThenItShouldNotBeAccepted() | ||
{ | ||
var client = new EventGridPublisherClient( | ||
new Uri("https://localhost:19999/api/events"), | ||
new AzureKeyCredential("TheLocal+DevelopmentKey="), | ||
new EventGridPublisherClientOptions | ||
{ Retry = { Mode = RetryMode.Fixed, MaxRetries = 0, NetworkTimeout = TimeSpan.FromSeconds(5) } }); | ||
|
||
var exception = await Should.ThrowAsync<RequestFailedException>(async () => | ||
{ | ||
await client.SendEventAsync(new EventGridEvent("/the/subject", "The.Event.Type", "v1", | ||
new { Id = 1, Foo = "Bar" })); | ||
}); | ||
|
||
exception.Message.ShouldContain("refused"); | ||
exception.Status.ShouldBe(0); | ||
} | ||
|
||
[Fact] | ||
public async Task GivenValidEvent_WhenKeyIsWrong_ThenItShouldNotBeAccepted() | ||
{ | ||
var client = new EventGridPublisherClient( | ||
new Uri("https://localhost:60101/api/events"), | ||
new AzureKeyCredential("TheWrongLocal+DevelopmentKey="), | ||
new EventGridPublisherClientOptions | ||
{ Retry = { Mode = RetryMode.Fixed, MaxRetries = 0, NetworkTimeout = TimeSpan.FromSeconds(5) } }); | ||
|
||
var exception = await Should.ThrowAsync<RequestFailedException>(async () => | ||
{ | ||
await client.SendEventAsync(new EventGridEvent("/the/subject", "The.Event.Type", "v1", | ||
new { Id = 1, Foo = "Bar" })); | ||
}); | ||
|
||
exception.Status.ShouldBe((int)HttpStatusCode.Unauthorized); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters