Skip to content

Commit

Permalink
Replace --rest-url option with --region option
Browse files Browse the repository at this point in the history
  • Loading branch information
valfirst committed Jan 5, 2025
1 parent 272e862 commit d009a4d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.saucelabs.saucerest.DataCenter;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.SystemUtils;
import org.vividus.selenium.tunnel.TunnelOptions;
Expand All @@ -47,15 +50,15 @@ public class SauceConnectOptions extends TunnelOptions
private static final String FULL_FILE_PROTOCOL = FILE_PROTOCOL + (SystemUtils.IS_OS_WINDOWS ? "/" : "");

private final boolean useLatestSauceConnect;
private final String restUrl;
private final DataCenter dataCenter;
private final String customArguments;
private final Set<String> skipHostGlobPatterns;

public SauceConnectOptions(boolean useLatestSauceConnect, String restUrl, String customArguments,
public SauceConnectOptions(boolean useLatestSauceConnect, DataCenter dataCenter, String customArguments,
Set<String> skipHostGlobPatterns)
{
this.useLatestSauceConnect = useLatestSauceConnect;
this.restUrl = restUrl;
this.dataCenter = dataCenter;
this.customArguments = customArguments;
this.skipHostGlobPatterns = new TreeSet<>(skipHostGlobPatterns);
this.skipHostGlobPatterns.addAll(List.of(
Expand All @@ -70,6 +73,7 @@ public String build(String tunnelName) throws IOException
{
StringBuilder options = Optional.ofNullable(customArguments).map(args -> new StringBuilder(args).append(' '))
.orElseGet(StringBuilder::new);
appendOption(options, "region", dataCenter.name().toLowerCase(Locale.ROOT).replace('_', '-'));
if (tunnelName != null)
{
appendOption(options, "tunnel-name", tunnelName);
Expand All @@ -84,10 +88,6 @@ public String build(String tunnelName) throws IOException

appendOption(options, "pac", pacFileUrl);
}
if (restUrl != null)
{
appendOption(options, "rest-url", restUrl);
}
appendOption(options, "tunnel-pool");
return options.substring(0, options.length() - 1);
}
Expand Down Expand Up @@ -127,13 +127,13 @@ public boolean equals(Object o)
return false;
}
SauceConnectOptions that = (SauceConnectOptions) o;
return useLatestSauceConnect == that.useLatestSauceConnect && Objects.equals(restUrl, that.restUrl)
return useLatestSauceConnect == that.useLatestSauceConnect && Objects.equals(dataCenter, that.dataCenter)
&& Objects.equals(skipHostGlobPatterns, that.skipHostGlobPatterns);
}

@Override
public int hashCode()
{
return Objects.hash(super.hashCode(), useLatestSauceConnect, restUrl, skipHostGlobPatterns);
return Objects.hash(super.hashCode(), useLatestSauceConnect, dataCenter, skipHostGlobPatterns);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,7 +29,7 @@ public class SauceLabsCapabilitiesConfigurer extends AbstractTunnellingCapabilit
private static final String SAUCE_OPTIONS = "sauce:options";

private final boolean useLatestSauceConnect;
private final String restUrl;
private final DataCenter dataCenter;
private String sauceConnectArguments;
private Set<String> skipHostGlobPatterns;

Expand All @@ -38,7 +38,7 @@ public SauceLabsCapabilitiesConfigurer(boolean useLatestSauceConnect, RunContext
{
super(runContext, sauceConnectManager);
this.useLatestSauceConnect = useLatestSauceConnect;
this.restUrl = dataCenter.apiServer() + "rest/v1";
this.dataCenter = dataCenter;
}

@Override
Expand All @@ -53,7 +53,7 @@ public void configure(DesiredCapabilities desiredCapabilities)
@Override
protected SauceConnectOptions createOptions()
{
return new SauceConnectOptions(useLatestSauceConnect, restUrl, sauceConnectArguments,
return new SauceConnectOptions(useLatestSauceConnect, dataCenter, sauceConnectArguments,
skipHostGlobPatterns == null ? Set.of() : skipHostGlobPatterns);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.nio.file.Path;
import java.util.Set;

import com.saucelabs.saucerest.DataCenter;

import org.apache.commons.lang3.SystemUtils;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
Expand All @@ -43,7 +45,9 @@ class SauceConnectOptionsTests
private static final String TUNNEL_NAME = "test-tunnel";
private static final String TUNNEL_NAME_OPTION = "--tunnel-name" + SPACE + TUNNEL_NAME;
private static final String TUNNEL_POOL = "--tunnel-pool";
private static final String DEFAULT_REST_URL = "https://saucelabs.com/rest/v1/";
private static final DataCenter DATA_CENTER = DataCenter.US_WEST;
private static final String REGION = "us-west";
private static final String REGION_OPTION = "--region" + SPACE + REGION;
private static final String DEFAULT_CUSTOM_ARGS = "--verbose";
private static final Set<String> DEFAULT_SKIP_GLOB_HOST_PATTERNS = Set.of("vividus.dev");
private static final String DEFAULT_MATCH_CHAIN = "shExpMatch(host, \"*.api.testobject.com\") || "
Expand All @@ -68,23 +72,23 @@ void testBuildWithProxy() throws IOException
{
Path pacPath = mockPac(resources, DEFAULT_MATCH_CHAIN);

assertEquals(TUNNEL_NAME_OPTION + SPACE + PAC_FILE + pacPath + SPACE + TUNNEL_POOL,
assertEquals(REGION_OPTION + SPACE + TUNNEL_NAME_OPTION + SPACE + PAC_FILE + pacPath + SPACE + TUNNEL_POOL,
sauceConnectOptions.build(TUNNEL_NAME));
}
}

@Test
void testBuildWithProxyForLatestSauceConnect() throws IOException
{
SauceConnectOptions sauceConnectOptions = new SauceConnectOptions(true, null, null, Set.of());
SauceConnectOptions sauceConnectOptions = new SauceConnectOptions(true, DATA_CENTER, null, Set.of());
sauceConnectOptions.setProxy(PROXY);
try (MockedStatic<ResourceUtils> resources = mockStatic(ResourceUtils.class))
{
Path pacPath = mockPac(resources, DEFAULT_MATCH_CHAIN);

assertEquals(
TUNNEL_NAME_OPTION + SPACE + PAC_FILE + (SystemUtils.IS_OS_WINDOWS ? "/" : "") + pacPath + SPACE
+ TUNNEL_POOL, sauceConnectOptions.build(TUNNEL_NAME));
REGION_OPTION + SPACE + TUNNEL_NAME_OPTION + SPACE + PAC_FILE + (SystemUtils.IS_OS_WINDOWS ? "/"
: "") + pacPath + SPACE + TUNNEL_POOL, sauceConnectOptions.build(TUNNEL_NAME));
}
}

Expand All @@ -98,38 +102,38 @@ void testBuildWithProxyWindowsPathDelimiters() throws IOException
Path pacPath = mockPac(resources, DEFAULT_MATCH_CHAIN);
when(pacPath.toString()).thenReturn("c:\\user\\temp.js");

assertEquals(TUNNEL_NAME_OPTION + SPACE + PAC_FILE + "c:/user/temp.js" + SPACE + TUNNEL_POOL,
sauceConnectOptions.build(TUNNEL_NAME));
assertEquals(REGION_OPTION + SPACE + TUNNEL_NAME_OPTION + SPACE + PAC_FILE + "c:/user/temp.js" + SPACE
+ TUNNEL_POOL, sauceConnectOptions.build(TUNNEL_NAME));
}
}

@Test
void testBuildWithProxyWithAuth() throws IOException
{
String customFlags = "--auth host:9999:user:pass";
SauceConnectOptions sauceConnectOptions = createOptions(null, customFlags, Set.of(), PROXY);
SauceConnectOptions sauceConnectOptions = createOptions(customFlags, Set.of(), PROXY);
try (MockedStatic<ResourceUtils> resources = mockStatic(ResourceUtils.class))
{
Path pacPath = mockPac(resources, DEFAULT_MATCH_CHAIN);

assertEquals(customFlags + SPACE + TUNNEL_NAME_OPTION + SPACE + PAC_FILE + pacPath + SPACE + TUNNEL_POOL,
sauceConnectOptions.build(TUNNEL_NAME));
assertEquals(customFlags + SPACE + REGION_OPTION + SPACE + TUNNEL_NAME_OPTION + SPACE + PAC_FILE + pacPath
+ SPACE + TUNNEL_POOL, sauceConnectOptions.build(TUNNEL_NAME));
}
}

@Test
void testBuildWithProxyWithSkipHostsPattern() throws IOException
{
Set<String> hosts = Set.of("example.com", "*.vividus.dev");
SauceConnectOptions sauceConnectOptions = createOptions(null, null, hosts, PROXY);
SauceConnectOptions sauceConnectOptions = createOptions(null, hosts, PROXY);
try (MockedStatic<ResourceUtils> resources = mockStatic(ResourceUtils.class))
{
String matchCondition = "shExpMatch(host, \"*.api.testobject.com\") || shExpMatch"
+ "(host, \"*.miso.saucelabs.com\") || shExpMatch(host, \"*.saucelabs.com\") || shExpMatch(host, "
+ "\"*.vividus.dev\") || shExpMatch(host, \"example.com\") || shExpMatch(host, \"saucelabs.com\")";
Path pacPath = mockPac(resources, matchCondition);

assertEquals(TUNNEL_NAME_OPTION + SPACE + PAC_FILE + pacPath + SPACE + TUNNEL_POOL,
assertEquals(REGION_OPTION + SPACE + TUNNEL_NAME_OPTION + SPACE + PAC_FILE + pacPath + SPACE + TUNNEL_POOL,
sauceConnectOptions.build(TUNNEL_NAME));
}
}
Expand All @@ -138,21 +142,15 @@ void testBuildWithProxyWithSkipHostsPattern() throws IOException
void testBuildWOProxy() throws IOException
{
SauceConnectOptions sauceConnectOptions = createEmptyOptions();
assertEquals(TUNNEL_NAME_OPTION + SPACE + TUNNEL_POOL, sauceConnectOptions.build(TUNNEL_NAME));
assertEquals(REGION_OPTION + SPACE + TUNNEL_NAME_OPTION + SPACE + TUNNEL_POOL,
sauceConnectOptions.build(TUNNEL_NAME));
}

@Test
void testBuildWOProxyNullOption() throws IOException
{
SauceConnectOptions sauceConnectOptions = createEmptyOptions();
assertEquals(TUNNEL_POOL, sauceConnectOptions.build(null));
}

@Test
void testBuildWithRestUrl() throws IOException
{
SauceConnectOptions sauceConnectOptions = createOptions(DEFAULT_REST_URL, null, Set.of(), null);
assertEquals("--rest-url" + SPACE + DEFAULT_REST_URL + SPACE + TUNNEL_POOL, sauceConnectOptions.build(null));
assertEquals(REGION_OPTION + SPACE + TUNNEL_POOL, sauceConnectOptions.build(null));
}

@Test
Expand Down Expand Up @@ -194,30 +192,36 @@ void testNotEqualsProxy()
@Test
void testNotEqualsSkipProxyHostsPattern()
{
assertNotEquals(createOptions(DEFAULT_REST_URL, DEFAULT_CUSTOM_ARGS, Set.of(), PROXY), createDefaultOptions());
assertNotEquals(createOptions(DEFAULT_CUSTOM_ARGS, Set.of(), PROXY), createDefaultOptions());
}

@Test
void testNotEqualsRestUrl()
void testNotEqualsDataCenter()
{
assertNotEquals(createOptions(null, DEFAULT_CUSTOM_ARGS, DEFAULT_SKIP_GLOB_HOST_PATTERNS, PROXY),
assertNotEquals(
createOptions(DataCenter.EU_CENTRAL, DEFAULT_CUSTOM_ARGS, DEFAULT_SKIP_GLOB_HOST_PATTERNS, PROXY),
createDefaultOptions());
}

private SauceConnectOptions createDefaultOptions()
{
return createOptions(DEFAULT_REST_URL, DEFAULT_CUSTOM_ARGS, DEFAULT_SKIP_GLOB_HOST_PATTERNS, PROXY);
return createOptions(DEFAULT_CUSTOM_ARGS, DEFAULT_SKIP_GLOB_HOST_PATTERNS, PROXY);
}

private SauceConnectOptions createEmptyOptions()
{
return createOptions(null, null, Set.of(), null);
return createOptions(null, Set.of(), null);
}

private SauceConnectOptions createOptions(String customArguments, Set<String> skipHostGlobPatterns, String proxy)
{
return createOptions(DATA_CENTER, customArguments, skipHostGlobPatterns, proxy);
}

private SauceConnectOptions createOptions(String restUrl, String customArguments, Set<String> skipHostGlobPatterns,
String proxy)
private static SauceConnectOptions createOptions(DataCenter dataCenter, String customArguments,
Set<String> skipHostGlobPatterns, String proxy)
{
SauceConnectOptions options = new SauceConnectOptions(false, restUrl, customArguments, skipHostGlobPatterns);
SauceConnectOptions options = new SauceConnectOptions(false, dataCenter, customArguments, skipHostGlobPatterns);
options.setProxy(proxy);
return options;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -60,9 +60,9 @@ class SauceLabsCapabilitiesConfigurerTests

private static final String STORY_PATH = STORY_NAME + ".story";

private static final String REST_URL = "https://api.eu-central-1.saucelabs.com/rest/v1";
private static final String CUSTOM_ARGS = "--verbose";
private static final Set<String> SKIP_HOST_GLOB_PATTERNS = Set.of("example.com");
private static final DataCenter DATA_CENTER = DataCenter.EU_CENTRAL;

@Mock private RunContext runContext;
@Mock private SauceConnectManager sauceConnectManager;
Expand All @@ -71,7 +71,7 @@ class SauceLabsCapabilitiesConfigurerTests
@BeforeEach
void beforeEach()
{
configurer = new SauceLabsCapabilitiesConfigurer(true, runContext, sauceConnectManager, DataCenter.EU_CENTRAL);
configurer = new SauceLabsCapabilitiesConfigurer(true, runContext, sauceConnectManager, DATA_CENTER);
}

@Test
Expand Down Expand Up @@ -99,7 +99,7 @@ void shouldStartSauceConnectWhenSauceConnectIsEnabled()
configurer.setTunnellingEnabled(true);
Map<String, Object> sauceOptions = new HashMap<>();
var desiredCapabilities = mockDesiredCapabilities(null, sauceOptions);
var sauceConnectOptions = new SauceConnectOptions(true, REST_URL, null, Set.of());
var sauceConnectOptions = new SauceConnectOptions(true, DATA_CENTER, null, Set.of());
when(sauceConnectManager.start(sauceConnectOptions)).thenReturn(TUNNEL_NAME);

configurer.configure(desiredCapabilities);
Expand All @@ -118,7 +118,7 @@ void shouldStartSauceConnectWhenSauceConnectIsDisabledButProxyIsStarted()
var httpProxy = "http-proxy:8080";
when(proxy.getHttpProxy()).thenReturn(httpProxy);

var sauceConnectOptions = new SauceConnectOptions(true, REST_URL, CUSTOM_ARGS, SKIP_HOST_GLOB_PATTERNS);
var sauceConnectOptions = new SauceConnectOptions(true, DATA_CENTER, CUSTOM_ARGS, SKIP_HOST_GLOB_PATTERNS);
sauceConnectOptions.setProxy(httpProxy);

Map<String, Object> sauceOptions = new HashMap<>();
Expand Down Expand Up @@ -146,7 +146,8 @@ void shouldCreateOptions(Set<String> setValue, Set<String> expectedValue)
{
configurer.setSauceConnectArguments(CUSTOM_ARGS);
configurer.setSkipHostGlobPatterns(setValue);
assertEquals(new SauceConnectOptions(true, REST_URL, CUSTOM_ARGS, expectedValue), configurer.createOptions());
assertEquals(new SauceConnectOptions(true, DATA_CENTER, CUSTOM_ARGS, expectedValue),
configurer.createOptions());
}

private void mockRunningStory()
Expand Down

0 comments on commit d009a4d

Please sign in to comment.