Skip to content

Commit 8adb92e

Browse files
committed
Remove apiPort from class state in order to avoid concurrency issues
1 parent 8ed70b7 commit 8adb92e

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
lines changed

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

+10-26
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, apiPort, options);
252234
args = addExtraInfoLegacy(args);
253235
} else {
254-
args = generateSauceConnectArgs(args, username, accessKey, options);
236+
args = generateSauceConnectArgs(args, username, accessKey, apiPort, options);
255237
args = addExtraInfo(args);
256238
}
257239

@@ -342,13 +324,14 @@ public static String getLatestSauceConnectVersion() {
342324
* @param args the initial Sauce Connect command line args
343325
* @param username name of the user which launched Sauce Connect
344326
* @param accessKey the access key for the Sauce user
327+
* @param apiPort specify the port the Sauce Connect API will listen on
345328
* @param options command line args specified by the user
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, int apiPort, String options) {
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
}
@@ -362,15 +345,16 @@ protected String[] addExtraInfo(String[] args) {
362345
* @param args the initial Sauce Connect command line args
363346
* @param username name of the user which launched Sauce Connect
364347
* @param accessKey the access key for the Sauce user
348+
* @param apiPort specify the port the Sauce Connect API will listen on
365349
* @param options command line args specified by the user
366350
* @return String array representing the command line args to be used to launch Sauce Connect
367351
*/
368352
@Deprecated
369353
protected String[] generateSauceConnectArgsLegacy(
370-
String[] args, String username, String accessKey, String options) {
354+
String[] args, String username, String accessKey, int apiPort, String options) {
371355
String[] result;
372356

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

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

+11
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ void testSauceConnectSecretsCoveredWithStars() {
244244
args,
245245
"username",
246246
"apikey",
247+
9000,
247248
"--access-key apiKey");
248249
String result = manager.hideSauceConnectCommandlineSecrets(args);
249250

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

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)