Skip to content

Commit c9d740e

Browse files
author
Travis Nickels
committed
Update ManagementClient tests
1 parent e710dc2 commit c9d740e

File tree

1 file changed

+50
-114
lines changed

1 file changed

+50
-114
lines changed

src/NServiceBus.Transport.RabbitMQ.Tests/ManagementClientTests.cs

Lines changed: 50 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@ namespace NServiceBus.Transport.RabbitMQ.Tests
44
{
55
using System;
66
using System.Collections.Generic;
7+
using System.Diagnostics;
78
using System.Net;
89
using System.Net.Http;
9-
using System.Text;
10-
using System.Threading;
1110
using System.Threading.Tasks;
1211
using NServiceBus.Transport.RabbitMQ.ManagementClient;
1312
using NUnit.Framework;
1413
using NUnit.Framework.Internal;
14+
using static NServiceBus.Transport.RabbitMQ.Tests.FakeHttpClient;
1515

1616
[TestFixture]
1717
class ManagementClientTests
1818
{
1919
static readonly string connectionString = Environment.GetEnvironmentVariable("RabbitMQTransport_ConnectionString") ?? "host=localhost";
2020
static readonly ConnectionConfiguration connectionConfiguration = ConnectionConfiguration.Create(connectionString);
2121
static readonly ConnectionFactory connectionFactory = new(typeof(ManagementClientTests).FullName, connectionConfiguration, null, false, false, TimeSpan.FromSeconds(60), TimeSpan.FromSeconds(10), []);
22-
ManagementClient managementClient;
22+
string defaultManagementUrl;
23+
ManagementClient? managementClient;
2324

2425
const int defaultBrokerPort = 5672;
2526
const int defaultBrokerTlsPort = 5671;
@@ -30,132 +31,55 @@ class ManagementClientTests
3031
const string defaultVirtualHost = "/";
3132

3233
[SetUp]
33-
public void SetUp()
34-
{
35-
var defaultManagementUrl = ManagementClient.CreateManagementConnectionString(connectionConfiguration);
36-
managementClient = new(defaultManagementUrl, defaultVirtualHost);
37-
}
34+
public void SetUp() => defaultManagementUrl = ManagementClient.CreateManagementConnectionString(connectionConfiguration);
3835

3936
[Test]
4037
[TestCase("http://localhost", "guest", "guest", "http://localhost:15672")]
38+
[TestCase("https://localhost", "guest", "guest", "https://localhost:15671")]
4139
[TestCase("http://localhost:15672", "guest", "guest", "http://localhost:15672")]
42-
[TestCase("http://copa:abc123xyz@localhost", "copa", "abc123xyz", "http://localhost:15672")]
43-
[TestCase("http://copa:abc123xyz@localhost", "guest", "guest", "http://localhost:15672")] // The management client will try guest:guest if the provided credentials fail first
44-
[TestCase("http://copa:abc123xyz@localhost:15672", "guest", "guest", "http://localhost:15672")]
40+
[TestCase("https://localhost:15671", "guest", "guest", "https://localhost:15671")]
4541
[TestCase("http://guest:guest@localhost", "guest", "guest", "http://localhost:15672")]
42+
[TestCase("https://guest:guest@localhost", "guest", "guest", "https://localhost:15671")]
4643
[TestCase("http://guest:guest@localhost:15672", "guest", "guest", "http://localhost:15672")]
47-
public void ValidateManagementConnection_Should_Not_Throw_With_Default_Management_Api_Connection(
44+
[TestCase("https://guest:guest@localhost:15671", "guest", "guest", "https://localhost:15671")]
45+
public async Task GetOverview_Should_Return_Success_With_Valid_Default_Connection_Values(
4846
string managementApiUrl,
4947
string expectedUserName,
5048
string expectedPassword,
5149
string expectedUrl)
5250
{
53-
var HttpClient = CreateFakeHttpClient(request => FakeResponses.CheckRequestMessageConnection(request, expectedUserName, expectedPassword, expectedUrl));
51+
var HttpClient = CreateFakeHttpClient(request => FakeResponses.GetOverview(request, expectedUserName, expectedPassword, expectedUrl));
5452
managementClient = CreateManagementClient(managementApiUrl, HttpClient);
5553

56-
Assert.DoesNotThrowAsync(async () => await managementClient.ValidateManagementConnection());
57-
}
54+
var result = await managementClient.GetOverview();
5855

59-
[Test]
60-
[TestCase("https://localhost", "guest", "guest", "https://localhost:15671")]
61-
[TestCase("https://localhost:15671", "guest", "guest", "https://localhost:15671")]
62-
[TestCase("https://copa:abc123xyz@localhost", "copa", "abc123xyz", "https://localhost:15671")]
63-
[TestCase("https://copa:abc123xyz@localhost", "guest", "guest", "https://localhost:15671")] // The management client will try guest:guest if the provided credentials fail first
64-
[TestCase("https://guest:guest@localhost", "guest", "guest", "https://localhost:15671")]
65-
[TestCase("https://guest:guest@localhost:15671", "guest", "guest", "https://localhost:15671")]
66-
public void ValidateManagementConnection_Should_Not_Throw_With_Default_Management_Api_Tls_Connection(
67-
string managementApiUrl,
68-
string expectedUserName,
69-
string expectedPassword,
70-
string expectedUrl)
71-
{
72-
var HttpClient = CreateFakeHttpClient(request => FakeResponses.CheckRequestMessageConnection(request, expectedUserName, expectedPassword, expectedUrl));
73-
var managementClient = CreateManagementClient(managementApiUrl, HttpClient);
74-
75-
Assert.DoesNotThrowAsync(async () => await managementClient.ValidateManagementConnection());
56+
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.OK));
7657
}
7758

7859
[Test]
79-
[TestCase("http://localhost", "admin", "admin")]
80-
[TestCase("http://localhost:15672", "admin", "admin")]
81-
[TestCase("https://localhost:15671", "admin", "admin")]
82-
[TestCase("http://copa:abc123xyz@localhost", "admin", "admin")]
83-
[TestCase("http://guest:guest@localhost", "admin", "admin")]
84-
[TestCase("http://guest:guest@localhost:15672", "admin", "admin")]
85-
[TestCase("https://guest:guest@localhost:15671", "admin", "admin")]
86-
public void ValidateManagementConnection_Should_Throw_With_Invalid_Credentials(
60+
[TestCase("http://localhost", "user", "password", "http://localhost:15672")]
61+
[TestCase("https://localhost", "user", "password", "https://localhost:15671")]
62+
[TestCase("http://localhost:15672", "user", "password", "http://localhost:15672")]
63+
[TestCase("https://localhost:15671", "user", "password", "https://localhost:15671")]
64+
[TestCase("http://guest:guest@localhost", "user", "password", "http://localhost:15672")]
65+
[TestCase("https://guest:guest@localhost", "user", "password", "https://localhost:15671")]
66+
[TestCase("http://guest:guest@localhost:15672", "user", "password", "http://localhost:15672")]
67+
[TestCase("https://guest:guest@localhost:15671", "user", "password", "https://localhost:15671")]
68+
public async Task GetOverview_Should_Return_Unauthorized_With_Invalid_Credentials(
8769
string managementApiUrl,
8870
string expectedUserName,
89-
string expectedPassword)
90-
{
91-
var httpClient = CreateFakeHttpClient(request => FakeResponses.CheckAuthentication(request, expectedUserName, expectedPassword));
92-
var managementClient = CreateManagementClient(managementApiUrl, httpClient);
93-
94-
var exception = Assert.ThrowsAsync<InvalidOperationException>(async () => await managementClient.ValidateManagementConnection());
95-
}
96-
97-
[Test]
98-
[TestCase("host=localhost", "guest", "guest", "http://guest:guest@localhost:15672")]
99-
[TestCase("host=localhost;useTls=true", "guest", "guest", "https://guest:guest@localhost:15671")]
100-
[TestCase("host=localhost;useTls=false", "guest", "guest", "http://guest:guest@localhost:15672")]
101-
[TestCase("host=localhost;port=12345;useTls=true", "guest", "guest", "https://guest:guest@localhost:15671")]
102-
[TestCase("host=localhost;port=12345;useTls=false", "guest", "guest", "http://guest:guest@localhost:15672")]
103-
[TestCase("host=localhost;username=copa;password=abc123xyz;useTls=true", "guest", "guest", "https://copa:abc123xyz@localhost:15671")]
104-
[TestCase("host=localhost;username=copa;password=abc123xyz;useTls=false", "guest", "guest", "http://copa:abc123xyz@localhost:15672")]
105-
[TestCase("host=localhost;username=copa;password=abc123xyz;useTls=true", "copa", "abc123xyz", "https://copa:abc123xyz@localhost:15671")]
106-
[TestCase("host=localhost;username=copa;password=abc123xyz;useTls=false", "copa", "abc123xyz", "http://copa:abc123xyz@localhost:15672")]
107-
[TestCase("host=localhost;username=copa;password=abc123xyz;port=12345;useTls=true", "guest", "guest", "https://copa:abc123xyz@localhost:15671")]
108-
[TestCase("host=localhost;username=copa;password=abc123xyz;port=12345;useTls=false", "guest", "guest", "http://copa:abc123xyz@localhost:15672")]
109-
[TestCase("host=localhost;username=copa;password=abc123xyz;port=12345;useTls=true", "copa", "abc123xyz", "https://copa:abc123xyz@localhost:15671")]
110-
[TestCase("host=localhost;username=copa;password=abc123xyz;port=12345;useTls=false", "copa", "abc123xyz", "http://copa:abc123xyz@localhost:15672")]
111-
public void ValidateManagementConnection_Should_Not_Throw_With_Valid_Or_Default_Broker_ConnectionConfiguration(
112-
string brokerConnectionString,
113-
string expectedUserName,
114-
string expectedPassword,
115-
string expectedUrl)
116-
{
117-
var httpClient = CreateFakeHttpClient(request => FakeResponses.CheckAuthentication(request, expectedUserName, expectedPassword));
118-
var connectionConfiguration = ConnectionConfiguration.Create(brokerConnectionString);
119-
var managementApiUrl = ManagementClient.CreateManagementConnectionString(connectionConfiguration);
120-
Assert.That(string.Equals(managementApiUrl, expectedUrl), Is.True);
121-
122-
var managementClient = CreateManagementClient(managementApiUrl, httpClient);
123-
124-
Assert.DoesNotThrowAsync(async () => await managementClient.ValidateManagementConnection());
125-
}
126-
127-
[Test]
128-
[TestCase("host=localhost", "admin", "admin", "http://guest:guest@localhost:15672")]
129-
[TestCase("host=localhost;useTls=true", "admin", "admin", "https://guest:guest@localhost:15671")]
130-
[TestCase("host=localhost;useTls=false", "admin", "admin", "http://guest:guest@localhost:15672")]
131-
[TestCase("host=localhost;port=12345;useTls=true", "admin", "admin", "https://guest:guest@localhost:15671")]
132-
[TestCase("host=localhost;port=12345;useTls=false", "admin", "admin", "http://guest:guest@localhost:15672")]
133-
[TestCase("host=localhost;username=copa;password=abc123xyz;useTls=true", "admin", "admin", "https://copa:abc123xyz@localhost:15671")]
134-
[TestCase("host=localhost;username=copa;password=abc123xyz;useTls=false", "admin", "admin", "http://copa:abc123xyz@localhost:15672")]
135-
[TestCase("host=localhost;username=copa;password=abc123xyz;useTls=true", "admin", "admin", "https://copa:abc123xyz@localhost:15671")]
136-
[TestCase("host=localhost;username=copa;password=abc123xyz;useTls=false", "admin", "admin", "http://copa:abc123xyz@localhost:15672")]
137-
[TestCase("host=localhost;username=copa;password=abc123xyz;port=12345;useTls=true", "admin", "admin", "https://copa:abc123xyz@localhost:15671")]
138-
[TestCase("host=localhost;username=copa;password=abc123xyz;port=12345;useTls=false", "admin", "admin", "http://copa:abc123xyz@localhost:15672")]
139-
[TestCase("host=localhost;username=copa;password=abc123xyz;port=12345;useTls=true", "admin", "admin", "https://copa:abc123xyz@localhost:15671")]
140-
[TestCase("host=localhost;username=copa;password=abc123xyz;port=12345;useTls=false", "admin", "admin", "http://copa:abc123xyz@localhost:15672")]
141-
public void ValidateManagementConnection_Should_Throw_With_Invalid_Management_Credentials_From_Broker_ConnectionConfiguration(
142-
string brokerConnectionString,
143-
string expectedUserName,
14471
string expectedPassword,
14572
string expectedUrl)
14673
{
147-
var httpClient = CreateFakeHttpClient(request => FakeResponses.CheckAuthentication(request, expectedUserName, expectedPassword));
148-
var connectionConfiguration = ConnectionConfiguration.Create(brokerConnectionString);
149-
var managementApiUrl = ManagementClient.CreateManagementConnectionString(connectionConfiguration);
150-
Assert.That(string.Equals(managementApiUrl, expectedUrl), Is.True);
151-
152-
var managementClient = CreateManagementClient(managementApiUrl, httpClient);
74+
var HttpClient = CreateFakeHttpClient(request => FakeResponses.GetOverview(request, expectedUserName, expectedPassword, expectedUrl));
75+
managementClient = CreateManagementClient(managementApiUrl, HttpClient);
15376

154-
var exception = Assert.ThrowsAsync<InvalidOperationException>(async () => await managementClient.ValidateManagementConnection());
77+
var result = await managementClient.GetOverview();
78+
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.Unauthorized));
15579
}
15680

15781
[Test]
158-
public void Constructor_Should_Throw_With_Invalid_Scheme()
82+
public void Should_Throw_With_Invalid_Scheme()
15983
{
16084
var managementApiUrl = "amqp:guest:guest@localhost:15672";
16185

@@ -166,6 +90,7 @@ public void Constructor_Should_Throw_With_Invalid_Scheme()
16690
public async Task GetQueue_Should_Return_Queue_Information_When_Exists()
16791
{
16892
// Arrange
93+
managementClient = new(defaultManagementUrl, defaultVirtualHost);
16994
var queueName = nameof(GetQueue_Should_Return_Queue_Information_When_Exists);
17095
await CreateQuorumQueue(queueName).ConfigureAwait(false);
17196

@@ -185,6 +110,7 @@ public async Task GetQueue_Should_Return_Queue_Information_When_Exists()
185110
public async Task GetOverview_Should_Return_Broker_Information()
186111
{
187112
// Act
113+
managementClient = new(defaultManagementUrl, defaultVirtualHost);
188114
var response = await managementClient.GetOverview();
189115

190116
// Assert
@@ -203,6 +129,7 @@ public async Task GetOverview_Should_Return_Broker_Information()
203129
public async Task GetFeatureFlags_Should_Return_FeatureFlag_Information()
204130
{
205131
// Act
132+
managementClient = new(defaultManagementUrl, defaultVirtualHost);
206133
var response = await managementClient.GetFeatureFlags();
207134

208135
// Assert
@@ -221,8 +148,9 @@ public async Task GetFeatureFlags_Should_Return_FeatureFlag_Information()
221148
public async Task CreatePolicy_With_DeliveryLimit_Should_Be_Applied_To_Quorum_Queues(int deliveryLimit)
222149
{
223150
// Arrange
151+
managementClient = new(defaultManagementUrl, defaultVirtualHost);
224152
var queueName = nameof(CreatePolicy_With_DeliveryLimit_Should_Be_Applied_To_Quorum_Queues);
225-
var policyName = $"{queueName} policy";
153+
var policyName = $"nsb.{queueName}";
226154
await CreateQuorumQueue(queueName);
227155

228156
// Act
@@ -242,23 +170,31 @@ public async Task CreatePolicy_With_DeliveryLimit_Should_Be_Applied_To_Quorum_Qu
242170
// Assert
243171

244172
// It can take some time for updated policies to be applied, so we need to wait.
245-
// If this test is randomly failing, consider increasing the delay
246-
await Task.Delay(10000);
247-
var response = await managementClient.GetQueue(queueName);
248-
Assert.Multiple(() =>
173+
// If this test is randomly failing, consider increasing the maxWaitTime
174+
var maxWaitTime = TimeSpan.FromSeconds(30);
175+
var pollingInterval = TimeSpan.FromSeconds(2);
176+
var stopwatch = Stopwatch.StartNew();
177+
while (stopwatch.Elapsed < maxWaitTime)
249178
{
250-
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
251-
Assert.That(response.Value, Is.Not.Null);
252-
Assert.That(response.Value?.AppliedPolicyName, Is.EqualTo(policyName));
253-
Assert.That(response.Value?.EffectivePolicyDefinition?.DeliveryLimit, Is.EqualTo(deliveryLimit));
254-
});
179+
var response = await managementClient.GetQueue(queueName);
180+
if (response.StatusCode == HttpStatusCode.OK
181+
&& response.Value != null
182+
&& response.Value.AppliedPolicyName == policyName
183+
&& response.Value.EffectivePolicyDefinition?.DeliveryLimit == deliveryLimit)
184+
{
185+
// Policy applied successfully
186+
return;
187+
}
188+
await Task.Delay(pollingInterval);
189+
}
190+
Assert.Fail($"Policy '{policyName}' was not applied to queue '{queueName}' within {maxWaitTime.TotalSeconds} seconds.");
255191
}
256192

257193
static async Task CreateQuorumQueue(string queueName)
258194
{
259195
using var connection = await connectionFactory.CreateConnection($"{queueName} connection").ConfigureAwait(false);
260196
using var channel = await connection.CreateChannelAsync().ConfigureAwait(false);
261-
var arguments = new Dictionary<string, object?> { { "x-queue-type", "quorum" } };
197+
var arguments = new Dictionary<string, object?> { { "x-queue-type", "quorum" }, { "delivery_limit", 5 } };
262198

263199
_ = await channel.QueueDeclareAsync(queue: queueName, durable: true, exclusive: false, autoDelete: false, arguments: arguments);
264200
}

0 commit comments

Comments
 (0)