Skip to content

Commit 8417297

Browse files
author
Michał Budziak
committed
Remove apiPort from class state in order to avoid concurrency issues
1 parent 8ed70b7 commit 8417297

File tree

2 files changed

+31
-37
lines changed

2 files changed

+31
-37
lines changed

src/main/java/com/saucelabs/ci/sauceconnect/SauceConnectManager.java

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -140,43 +140,29 @@ public String getDefaultSauceConnectLogDirectory() {
140140
private static final String SAUCE_CONNECT_PREFIX = "sauce-connect-";
141141
public static final String SAUCE_CONNECT = SAUCE_CONNECT_PREFIX + CURRENT_SC_VERSION;
142142

143-
private static final int DEFAULT_API_PORT = 9000;
144-
private int apiPort;
145-
146143
/** Constructs a new instance with quiet mode disabled. */
147144
public SauceConnectManager() {
148145
this(false);
149146
}
150147

151-
/**
152-
* Constructs a new instance with quiet mode disabled.
153-
*
154-
* @param runner System which runs SauceConnect, this info is added to '--metadata runner=' argument
155-
*/
156-
public SauceConnectManager(String runner) {
157-
this(false, runner, DEFAULT_API_PORT);
158-
}
159-
160148
/**
161149
* Constructs a new instance.
162150
*
163151
* @param quietMode indicates whether Sauce Connect output should be suppressed
164152
*/
165153
public SauceConnectManager(boolean quietMode) {
166-
this(quietMode, "jenkins", DEFAULT_API_PORT);
154+
this(quietMode, "jenkins");
167155
}
168156

169157
/**
170158
* Constructs a new instance.
171159
*
172160
* @param quietMode indicates whether Sauce Connect output should be suppressed
173161
* @param runner System which runs SauceConnect, this info is added to '--metadata runner=' argument
174-
* @param apiPort Port the Sauce Connect process will listen on
175162
*/
176-
public SauceConnectManager(boolean quietMode, String runner, int apiPort) {
163+
public SauceConnectManager(boolean quietMode, String runner) {
177164
super(quietMode);
178165
this.runner = runner;
179-
this.apiPort = apiPort;
180166
}
181167

182168
/**
@@ -241,17 +227,13 @@ protected Process prepAndCreateProcess(
241227
}
242228
}
243229

244-
if ( apiPort != 0 ) {
245-
this.apiPort = apiPort;
246-
}
247-
248230
// although we are setting the working directory, we need to specify the full path to the exe
249231
String[] args = {sauceConnectBinary.getPath()};
250232
if (legacy) {
251-
args = generateSauceConnectArgsLegacy(args, username, accessKey, options);
233+
args = generateSauceConnectArgsLegacy(args, username, accessKey, options, apiPort);
252234
args = addExtraInfoLegacy(args);
253235
} else {
254-
args = generateSauceConnectArgs(args, username, accessKey, options);
236+
args = generateSauceConnectArgs(args, username, accessKey, options, apiPort);
255237
args = addExtraInfo(args);
256238
}
257239

@@ -343,12 +325,13 @@ public static String getLatestSauceConnectVersion() {
343325
* @param username name of the user which launched Sauce Connect
344326
* @param accessKey the access key for the Sauce user
345327
* @param options command line args specified by the user
328+
* @param apiPort specify the port the Sauce Connect API will listen on
346329
* @return String array representing the command line args to be used to launch Sauce Connect
347330
*/
348331
protected String[] generateSauceConnectArgs(
349-
String[] args, String username, String accessKey, String options) {
332+
String[] args, String username, String accessKey, String options, int apiPort) {
350333
String[] result =
351-
joinArgs(args, "run", "--username", username.trim(), "--access-key", accessKey.trim(), "--api-address", ":" + String.valueOf(this.apiPort));
334+
joinArgs(args, "run", "--username", username.trim(), "--access-key", accessKey.trim(), "--api-address", ":" + String.valueOf(apiPort));
352335
result = addElement(result, options);
353336
return result;
354337
}
@@ -367,10 +350,10 @@ protected String[] addExtraInfo(String[] args) {
367350
*/
368351
@Deprecated
369352
protected String[] generateSauceConnectArgsLegacy(
370-
String[] args, String username, String accessKey, String options) {
353+
String[] args, String username, String accessKey, String options, int apiPort) {
371354
String[] result;
372355

373-
result = joinArgs(args, "legacy", "--user", username.trim(), "--api-key", accessKey.trim(), "--status-address", "0.0.0.0:" + String.valueOf(this.apiPort));
356+
result = joinArgs(args, "legacy", "--user", username.trim(), "--api-key", accessKey.trim(), "--status-address", "0.0.0.0:" + String.valueOf(apiPort));
374357
result = addElement(result, options);
375358
return result;
376359
}

src/test/java/com/saucelabs/ci/sauceconnect/SauceConnectManagerTest.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ void testSauceConnectSecretsCoveredWithStars() {
244244
args,
245245
"username",
246246
"apikey",
247-
"--access-key apiKey");
247+
"--access-key apiKey",
248+
9000);
248249
String result = manager.hideSauceConnectCommandlineSecrets(args);
249250

250251
assertEquals(
@@ -263,7 +264,8 @@ void testSauceConnectLegacySecretsCoveredWithStars() {
263264
args,
264265
"username",
265266
"apikey",
266-
"--api-key apiKey");
267+
"--api-key apiKey",
268+
9000);
267269
String result = manager.hideSauceConnectCommandlineSecrets(args);
268270

269271
assertEquals(
@@ -282,7 +284,8 @@ void testSauceConnectAuthSecretsCoveredWithStars() {
282284
args,
283285
"username",
284286
"apikey",
285-
"--auth foo:bar@host:8080");
287+
"--auth foo:bar@host:8080",
288+
9000);
286289
String result = manager.hideSauceConnectCommandlineSecrets(args);
287290

288291
assertEquals(
@@ -295,7 +298,8 @@ void testSauceConnectAuthSecretsCoveredWithStars() {
295298
args,
296299
"username",
297300
"apikey",
298-
"-a foo:bar@host:8080");
301+
"-a foo:bar@host:8080",
302+
9000);
299303
result = manager.hideSauceConnectCommandlineSecrets(args);
300304

301305
assertEquals(
@@ -308,7 +312,8 @@ void testSauceConnectAuthSecretsCoveredWithStars() {
308312
args,
309313
"username",
310314
"apikey",
311-
"-a foo:bar@host:8080 -a user:pwd@host1:1234 --auth root:pass@host2:9999 --auth uucp:pass@host3:8080");
315+
"-a foo:bar@host:8080 -a user:pwd@host1:1234 --auth root:pass@host2:9999 --auth uucp:pass@host3:8080",
316+
9000);
312317
result = manager.hideSauceConnectCommandlineSecrets(args);
313318

314319
assertEquals(
@@ -327,7 +332,8 @@ void testSauceConnectProxySecretsCoveredWithStars() {
327332
args,
328333
"username",
329334
"apikey",
330-
"--proxy user:pwd@host:8080");
335+
"--proxy user:pwd@host:8080",
336+
9000);
331337
String result = manager.hideSauceConnectCommandlineSecrets(args);
332338

333339
assertEquals(
@@ -340,7 +346,8 @@ void testSauceConnectProxySecretsCoveredWithStars() {
340346
args,
341347
"username",
342348
"apikey",
343-
"--proxy user@host:8080");
349+
"--proxy user@host:8080",
350+
9000);
344351
result = manager.hideSauceConnectCommandlineSecrets(args);
345352

346353
assertEquals(
@@ -353,7 +360,8 @@ void testSauceConnectProxySecretsCoveredWithStars() {
353360
args,
354361
"username",
355362
"apikey",
356-
"-x user@host:8080");
363+
"-x user@host:8080",
364+
9000);
357365
result = manager.hideSauceConnectCommandlineSecrets(args);
358366

359367
assertEquals(
@@ -366,7 +374,8 @@ void testSauceConnectProxySecretsCoveredWithStars() {
366374
args,
367375
"username",
368376
"apikey",
369-
"--proxy-sauce user@host:8080");
377+
"--proxy-sauce user@host:8080",
378+
9000);
370379
result = manager.hideSauceConnectCommandlineSecrets(args);
371380

372381
assertEquals(
@@ -385,7 +394,8 @@ void testSauceConnectAPIBasicAuthSecretsCoveredWithStars() {
385394
args,
386395
"username",
387396
"apiKey",
388-
"--api-basic-auth user:pwd");
397+
"--api-basic-auth user:pwd",
398+
9000);
389399
String result = manager.hideSauceConnectCommandlineSecrets(args);
390400

391401
assertEquals(
@@ -398,7 +408,8 @@ void testSauceConnectAPIBasicAuthSecretsCoveredWithStars() {
398408
args,
399409
"username",
400410
"apiKey",
401-
"--api-basic-auth user");
411+
"--api-basic-auth user",
412+
9000);
402413
result = manager.hideSauceConnectCommandlineSecrets(args);
403414

404415
assertEquals(

0 commit comments

Comments
 (0)