Skip to content

Commit b280b1c

Browse files
committed
Merge branch 'jlukawska-feature/1115-find-available-port-in-acceptance-tests'
2 parents 08512ec + 36064b1 commit b280b1c

39 files changed

+673
-392
lines changed

test/Ocelot.AcceptanceTests/AggregateTests.cs

Lines changed: 52 additions & 38 deletions
Large diffs are not rendered by default.

test/Ocelot.AcceptanceTests/AuthenticationTests.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class AuthenticationTests : IDisposable
2020
{
2121
private readonly Steps _steps;
2222
private IWebHost _identityServerBuilder;
23-
private string _identityServerRootUrl = "http://localhost:51888";
23+
private string _identityServerRootUrl;
2424
private string _downstreamServicePath = "/";
2525
private string _downstreamServiceHost = "localhost";
2626
private string _downstreamServiceScheme = "http";
@@ -31,7 +31,9 @@ public class AuthenticationTests : IDisposable
3131
public AuthenticationTests()
3232
{
3333
_serviceHandler = new ServiceHandler();
34-
_steps = new Steps();
34+
_steps = new Steps();
35+
var identityServerPort = RandomPortFinder.GetRandomPort();
36+
_identityServerRootUrl = $"http://localhost:{identityServerPort}";
3537
_options = o =>
3638
{
3739
o.Authority = _identityServerRootUrl;
@@ -45,7 +47,7 @@ public AuthenticationTests()
4547
[Fact]
4648
public void should_return_401_using_identity_server_access_token()
4749
{
48-
int port = 54329;
50+
int port = RandomPortFinder.GetRandomPort();
4951

5052
var configuration = new FileConfiguration
5153
{
@@ -86,7 +88,7 @@ public void should_return_401_using_identity_server_access_token()
8688
[Fact]
8789
public void should_return_response_200_using_identity_server()
8890
{
89-
int port = 54099;
91+
int port = RandomPortFinder.GetRandomPort();
9092

9193
var configuration = new FileConfiguration
9294
{
@@ -129,7 +131,7 @@ public void should_return_response_200_using_identity_server()
129131
[Fact]
130132
public void should_return_response_401_using_identity_server_with_token_requested_for_other_api()
131133
{
132-
int port = 54196;
134+
int port = RandomPortFinder.GetRandomPort();
133135

134136
var configuration = new FileConfiguration
135137
{
@@ -171,7 +173,7 @@ public void should_return_response_401_using_identity_server_with_token_requeste
171173
[Fact]
172174
public void should_return_201_using_identity_server_access_token()
173175
{
174-
int port = 52226;
176+
int port = RandomPortFinder.GetRandomPort();
175177

176178
var configuration = new FileConfiguration
177179
{
@@ -214,7 +216,7 @@ public void should_return_201_using_identity_server_access_token()
214216
[Fact]
215217
public void should_return_201_using_identity_server_reference_token()
216218
{
217-
int port = 52222;
219+
int port = RandomPortFinder.GetRandomPort();
218220

219221
var configuration = new FileConfiguration
220222
{

test/Ocelot.AcceptanceTests/AuthorisationTests.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ public class AuthorisationTests : IDisposable
2121
private IWebHost _identityServerBuilder;
2222
private readonly Steps _steps;
2323
private readonly Action<IdentityServerAuthenticationOptions> _options;
24-
private string _identityServerRootUrl = "http://localhost:51888";
24+
private string _identityServerRootUrl;
2525
private readonly ServiceHandler _serviceHandler;
2626

2727
public AuthorisationTests()
2828
{
2929
_serviceHandler = new ServiceHandler();
30-
_steps = new Steps();
30+
_steps = new Steps();
31+
var identityServerPort = RandomPortFinder.GetRandomPort();
32+
_identityServerRootUrl = $"http://localhost:{identityServerPort}";
3133
_options = o =>
3234
{
3335
o.Authority = _identityServerRootUrl;
@@ -41,7 +43,7 @@ public AuthorisationTests()
4143
[Fact]
4244
public void should_return_response_200_authorising_route()
4345
{
44-
int port = 52875;
46+
int port = RandomPortFinder.GetRandomPort();
4547

4648
var configuration = new FileConfiguration
4749
{
@@ -86,9 +88,9 @@ public void should_return_response_200_authorising_route()
8688
}
8789
};
8890

89-
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt))
91+
this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", AccessTokenType.Jwt))
9092
.And(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", 200, "Hello from Laura"))
91-
.And(x => _steps.GivenIHaveAToken("http://localhost:51888"))
93+
.And(x => _steps.GivenIHaveAToken(_identityServerRootUrl))
9294
.And(x => _steps.GivenThereIsAConfiguration(configuration))
9395
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
9496
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())
@@ -101,7 +103,7 @@ public void should_return_response_200_authorising_route()
101103
[Fact]
102104
public void should_return_response_403_authorising_route()
103105
{
104-
int port = 59471;
106+
int port = RandomPortFinder.GetRandomPort();
105107

106108
var configuration = new FileConfiguration
107109
{
@@ -145,9 +147,9 @@ public void should_return_response_403_authorising_route()
145147
}
146148
};
147149

148-
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt))
150+
this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", AccessTokenType.Jwt))
149151
.And(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", 200, "Hello from Laura"))
150-
.And(x => _steps.GivenIHaveAToken("http://localhost:51888"))
152+
.And(x => _steps.GivenIHaveAToken(_identityServerRootUrl))
151153
.And(x => _steps.GivenThereIsAConfiguration(configuration))
152154
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
153155
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())
@@ -159,7 +161,7 @@ public void should_return_response_403_authorising_route()
159161
[Fact]
160162
public void should_return_response_200_using_identity_server_with_allowed_scope()
161163
{
162-
int port = 63471;
164+
int port = RandomPortFinder.GetRandomPort();
163165

164166
var configuration = new FileConfiguration
165167
{
@@ -188,9 +190,9 @@ public void should_return_response_200_using_identity_server_with_allowed_scope(
188190
}
189191
};
190192

191-
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt))
193+
this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", AccessTokenType.Jwt))
192194
.And(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", 200, "Hello from Laura"))
193-
.And(x => _steps.GivenIHaveATokenForApiReadOnlyScope("http://localhost:51888"))
195+
.And(x => _steps.GivenIHaveATokenForApiReadOnlyScope(_identityServerRootUrl))
194196
.And(x => _steps.GivenThereIsAConfiguration(configuration))
195197
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
196198
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())
@@ -202,7 +204,7 @@ public void should_return_response_200_using_identity_server_with_allowed_scope(
202204
[Fact]
203205
public void should_return_response_403_using_identity_server_with_scope_not_allowed()
204206
{
205-
int port = 60571;
207+
int port = RandomPortFinder.GetRandomPort();
206208

207209
var configuration = new FileConfiguration
208210
{
@@ -231,9 +233,9 @@ public void should_return_response_403_using_identity_server_with_scope_not_allo
231233
}
232234
};
233235

234-
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt))
236+
this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", AccessTokenType.Jwt))
235237
.And(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", 200, "Hello from Laura"))
236-
.And(x => _steps.GivenIHaveATokenForApiReadOnlyScope("http://localhost:51888"))
238+
.And(x => _steps.GivenIHaveATokenForApiReadOnlyScope(_identityServerRootUrl))
237239
.And(x => _steps.GivenThereIsAConfiguration(configuration))
238240
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
239241
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())
@@ -245,7 +247,7 @@ public void should_return_response_403_using_identity_server_with_scope_not_allo
245247
[Fact]
246248
public void should_fix_issue_240()
247249
{
248-
int port = 61071;
250+
int port = RandomPortFinder.GetRandomPort();
249251

250252
var configuration = new FileConfiguration
251253
{
@@ -292,9 +294,9 @@ public void should_fix_issue_240()
292294
}
293295
};
294296

295-
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt, users))
297+
this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", AccessTokenType.Jwt, users))
296298
.And(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", 200, "Hello from Laura"))
297-
.And(x => _steps.GivenIHaveAToken("http://localhost:51888"))
299+
.And(x => _steps.GivenIHaveAToken(_identityServerRootUrl))
298300
.And(x => _steps.GivenThereIsAConfiguration(configuration))
299301
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
300302
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())

test/Ocelot.AcceptanceTests/ButterflyTracingTests.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public ButterflyTracingTests(ITestOutputHelper output)
3535
[Fact]
3636
public void should_forward_tracing_information_from_ocelot_and_downstream_services()
3737
{
38+
int port1 = RandomPortFinder.GetRandomPort();
39+
int port2 = RandomPortFinder.GetRandomPort();
3840
var configuration = new FileConfiguration
3941
{
4042
ReRoutes = new List<FileReRoute>
@@ -48,7 +50,7 @@ public void should_forward_tracing_information_from_ocelot_and_downstream_servic
4850
new FileHostAndPort
4951
{
5052
Host = "localhost",
51-
Port = 51887,
53+
Port = port1,
5254
}
5355
},
5456
UpstreamPathTemplate = "/api001/values",
@@ -67,7 +69,7 @@ public void should_forward_tracing_information_from_ocelot_and_downstream_servic
6769
new FileHostAndPort
6870
{
6971
Host = "localhost",
70-
Port = 51388,
72+
Port = port2,
7173
}
7274
},
7375
UpstreamPathTemplate = "/api002/values",
@@ -79,12 +81,13 @@ public void should_forward_tracing_information_from_ocelot_and_downstream_servic
7981
}
8082
}
8183
};
82-
83-
var butterflyUrl = "http://localhost:9618";
84+
85+
var butterflyPort = RandomPortFinder.GetRandomPort();
86+
var butterflyUrl = $"http://localhost:{butterflyPort}";
8487

8588
this.Given(x => GivenFakeButterfly(butterflyUrl))
86-
.And(x => GivenServiceOneIsRunning("http://localhost:51887", "/api/values", 200, "Hello from Laura", butterflyUrl))
87-
.And(x => GivenServiceTwoIsRunning("http://localhost:51388", "/api/values", 200, "Hello from Tom", butterflyUrl))
89+
.And(x => GivenServiceOneIsRunning($"http://localhost:{port1}", "/api/values", 200, "Hello from Laura", butterflyUrl))
90+
.And(x => GivenServiceTwoIsRunning($"http://localhost:{port2}", "/api/values", 200, "Hello from Tom", butterflyUrl))
8891
.And(x => _steps.GivenThereIsAConfiguration(configuration))
8992
.And(x => _steps.GivenOcelotIsRunningUsingButterfly(butterflyUrl))
9093
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/api001/values"))
@@ -105,6 +108,7 @@ public void should_forward_tracing_information_from_ocelot_and_downstream_servic
105108
[Fact]
106109
public void should_return_tracing_header()
107110
{
111+
int port = RandomPortFinder.GetRandomPort();
108112
var configuration = new FileConfiguration
109113
{
110114
ReRoutes = new List<FileReRoute>
@@ -118,7 +122,7 @@ public void should_return_tracing_header()
118122
new FileHostAndPort
119123
{
120124
Host = "localhost",
121-
Port = 51387,
125+
Port = port,
122126
}
123127
},
124128
UpstreamPathTemplate = "/api001/values",
@@ -136,10 +140,11 @@ public void should_return_tracing_header()
136140
}
137141
};
138142

139-
var butterflyUrl = "http://localhost:9618";
143+
var butterflyPort = RandomPortFinder.GetRandomPort();
144+
var butterflyUrl = $"http://localhost:{butterflyPort}";
140145

141146
this.Given(x => GivenFakeButterfly(butterflyUrl))
142-
.And(x => GivenServiceOneIsRunning("http://localhost:51387", "/api/values", 200, "Hello from Laura", butterflyUrl))
147+
.And(x => GivenServiceOneIsRunning($"http://localhost:{port}", "/api/values", 200, "Hello from Laura", butterflyUrl))
143148
.And(x => _steps.GivenThereIsAConfiguration(configuration))
144149
.And(x => _steps.GivenOcelotIsRunningUsingButterfly(butterflyUrl))
145150
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/api001/values"))

0 commit comments

Comments
 (0)