@@ -4,22 +4,23 @@ namespace NServiceBus.Transport.RabbitMQ.Tests
4
4
{
5
5
using System ;
6
6
using System . Collections . Generic ;
7
+ using System . Diagnostics ;
7
8
using System . Net ;
8
9
using System . Net . Http ;
9
- using System . Text ;
10
- using System . Threading ;
11
10
using System . Threading . Tasks ;
12
11
using NServiceBus . Transport . RabbitMQ . ManagementClient ;
13
12
using NUnit . Framework ;
14
13
using NUnit . Framework . Internal ;
14
+ using static NServiceBus . Transport . RabbitMQ . Tests . FakeHttpClient ;
15
15
16
16
[ TestFixture ]
17
17
class ManagementClientTests
18
18
{
19
19
static readonly string connectionString = Environment . GetEnvironmentVariable ( "RabbitMQTransport_ConnectionString" ) ?? "host=localhost" ;
20
20
static readonly ConnectionConfiguration connectionConfiguration = ConnectionConfiguration . Create ( connectionString ) ;
21
21
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 ;
23
24
24
25
const int defaultBrokerPort = 5672 ;
25
26
const int defaultBrokerTlsPort = 5671 ;
@@ -30,132 +31,55 @@ class ManagementClientTests
30
31
const string defaultVirtualHost = "/" ;
31
32
32
33
[ 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 ) ;
38
35
39
36
[ Test ]
40
37
[ TestCase ( "http://localhost" , "guest" , "guest" , "http://localhost:15672" ) ]
38
+ [ TestCase ( "https://localhost" , "guest" , "guest" , "https://localhost:15671" ) ]
41
39
[ 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" ) ]
45
41
[ TestCase ( "http://guest:guest@localhost" , "guest" , "guest" , "http://localhost:15672" ) ]
42
+ [ TestCase ( "https://guest:guest@localhost" , "guest" , "guest" , "https://localhost:15671" ) ]
46
43
[ 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 (
48
46
string managementApiUrl ,
49
47
string expectedUserName ,
50
48
string expectedPassword ,
51
49
string expectedUrl )
52
50
{
53
- var HttpClient = CreateFakeHttpClient ( request => FakeResponses . CheckRequestMessageConnection ( request , expectedUserName , expectedPassword , expectedUrl ) ) ;
51
+ var HttpClient = CreateFakeHttpClient ( request => FakeResponses . GetOverview ( request , expectedUserName , expectedPassword , expectedUrl ) ) ;
54
52
managementClient = CreateManagementClient ( managementApiUrl , HttpClient ) ;
55
53
56
- Assert . DoesNotThrowAsync ( async ( ) => await managementClient . ValidateManagementConnection ( ) ) ;
57
- }
54
+ var result = await managementClient . GetOverview ( ) ;
58
55
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 ) ) ;
76
57
}
77
58
78
59
[ 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 (
87
69
string managementApiUrl ,
88
70
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 ,
144
71
string expectedPassword ,
145
72
string expectedUrl )
146
73
{
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 ) ;
153
76
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 ) ) ;
155
79
}
156
80
157
81
[ Test ]
158
- public void Constructor_Should_Throw_With_Invalid_Scheme ( )
82
+ public void Should_Throw_With_Invalid_Scheme ( )
159
83
{
160
84
var managementApiUrl = "amqp:guest:guest@localhost:15672" ;
161
85
@@ -166,6 +90,7 @@ public void Constructor_Should_Throw_With_Invalid_Scheme()
166
90
public async Task GetQueue_Should_Return_Queue_Information_When_Exists ( )
167
91
{
168
92
// Arrange
93
+ managementClient = new ( defaultManagementUrl , defaultVirtualHost ) ;
169
94
var queueName = nameof ( GetQueue_Should_Return_Queue_Information_When_Exists ) ;
170
95
await CreateQuorumQueue ( queueName ) . ConfigureAwait ( false ) ;
171
96
@@ -185,6 +110,7 @@ public async Task GetQueue_Should_Return_Queue_Information_When_Exists()
185
110
public async Task GetOverview_Should_Return_Broker_Information ( )
186
111
{
187
112
// Act
113
+ managementClient = new ( defaultManagementUrl , defaultVirtualHost ) ;
188
114
var response = await managementClient . GetOverview ( ) ;
189
115
190
116
// Assert
@@ -203,6 +129,7 @@ public async Task GetOverview_Should_Return_Broker_Information()
203
129
public async Task GetFeatureFlags_Should_Return_FeatureFlag_Information ( )
204
130
{
205
131
// Act
132
+ managementClient = new ( defaultManagementUrl , defaultVirtualHost ) ;
206
133
var response = await managementClient . GetFeatureFlags ( ) ;
207
134
208
135
// Assert
@@ -221,8 +148,9 @@ public async Task GetFeatureFlags_Should_Return_FeatureFlag_Information()
221
148
public async Task CreatePolicy_With_DeliveryLimit_Should_Be_Applied_To_Quorum_Queues ( int deliveryLimit )
222
149
{
223
150
// Arrange
151
+ managementClient = new ( defaultManagementUrl , defaultVirtualHost ) ;
224
152
var queueName = nameof ( CreatePolicy_With_DeliveryLimit_Should_Be_Applied_To_Quorum_Queues ) ;
225
- var policyName = $ "{ queueName } policy ";
153
+ var policyName = $ "nsb. { queueName } ";
226
154
await CreateQuorumQueue ( queueName ) ;
227
155
228
156
// Act
@@ -242,23 +170,31 @@ public async Task CreatePolicy_With_DeliveryLimit_Should_Be_Applied_To_Quorum_Qu
242
170
// Assert
243
171
244
172
// 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 )
249
178
{
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.") ;
255
191
}
256
192
257
193
static async Task CreateQuorumQueue ( string queueName )
258
194
{
259
195
using var connection = await connectionFactory . CreateConnection ( $ "{ queueName } connection") . ConfigureAwait ( false ) ;
260
196
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 } } ;
262
198
263
199
_ = await channel . QueueDeclareAsync ( queue : queueName , durable : true , exclusive : false , autoDelete : false , arguments : arguments ) ;
264
200
}
0 commit comments