Skip to content

Commit 363e1ab

Browse files
devversionmhevery
authored andcommitted
ci: ensure saucelabs browsers can load karma test page (angular#35171)
In the past we had connecitivity issues on Saucelabs. Browsers on mobile devices were not able to properly resolve the `localhost` hostname through the tunnel. This is because the device resolves `localhost` or `127.0.0.1` to the actual Saucelabs device, while it should resolve to the tunnel host machine (in our case the CircleCI VM). In the past, we simply disabled the failing devices and re-enabled the devices later. At this point, the Saucelabs team claimed that the connecitivy/proxy issues were fixed. Saucelabs seems to have a process for VMs which ensures that requests to `localhost` / `127.0.0.1` are properly resolved through the tunnel. This process is not very reliable and can cause tests to fail. Related issues have been observed/mentioned in the Saucelabs support docs. e.g. https://support.saucelabs.com/hc/en-us/articles/115002212447-Unable-to-Reach-Application-on-localhost-for-Tests-Run-on-Safari-8-and-9-and-Edge https://support.saucelabs.com/hc/en-us/articles/225106887-Safari-and-Internet-Explorer-Won-t-Load-Website-When-Using-Sauce-Connect-on-Localhost In order to ensure that requests are always resolved through the tunnel, we add our own domain alias in the CircleCI's hosts file, and enforce that it is always resolved through the tunnel (using the `--tunnel-domains` SC flag). Saucelabs devices by default will never resolve this domain/hostname to the actual local Saucelabs device. PR Close angular#35171
1 parent dc5ac88 commit 363e1ab

File tree

5 files changed

+83
-27
lines changed

5 files changed

+83
-27
lines changed

.circleci/config.yml

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,27 @@ commands:
136136
git config --global url."ssh://git@github.com".insteadOf "https://github.com" || true
137137
git config --global gc.auto 0 || true
138138
139+
init_saucelabs_environment:
140+
description: Sets up a domain that resolves to the local host.
141+
steps:
142+
- run:
143+
name: Preparing environment for running tests on Saucelabs.
144+
command: |
145+
# For SauceLabs jobs, we set up a domain which resolves to the machine which launched
146+
# the tunnel. We do this because devices are sometimes not able to properly resolve
147+
# `localhost` or `127.0.0.1` through the SauceLabs tunnel. Using a domain that does not
148+
# resolve to anything on SauceLabs VMs ensures that such requests are always resolved
149+
# through the tunnel, and resolve to the actual tunnel host machine (i.e. the CircleCI VM).
150+
# More context can be found in: https://github.com/angular/angular/pull/35171.
151+
setPublicVar SAUCE_LOCALHOST_ALIAS_DOMAIN "angular-ci.local"
152+
setSecretVar SAUCE_ACCESS_KEY $(echo $SAUCE_ACCESS_KEY | rev)
153+
- run:
154+
# Sets up a local domain in the machine's host file that resolves to the local
155+
# host. This domain is helpful in Saucelabs tests where devices are not able to
156+
# properly resolve `localhost` or `127.0.0.1` through the sauce-connect tunnel.
157+
name: Setting up alias domain for local host.
158+
command: echo "127.0.0.1 $SAUCE_LOCALHOST_ALIAS_DOMAIN" | sudo tee -a /etc/hosts
159+
139160
# Normally this would be an individual job instead of a command.
140161
# But startup and setup time for each invidual windows job are high enough to discourage
141162
# many small jobs, so instead we use a command for setup unless the gain becomes significant.
@@ -295,9 +316,7 @@ jobs:
295316
steps:
296317
- custom_attach_workspace
297318
- init_environment
298-
- run:
299-
name: Preparing environment for running tests on Saucelabs.
300-
command: setSecretVar SAUCE_ACCESS_KEY $(echo $SAUCE_ACCESS_KEY | rev)
319+
- init_saucelabs_environment
301320
- run:
302321
name: Run Bazel tests on Saucelabs
303322
# See /tools/saucelabs/README.md for more info
@@ -319,9 +338,7 @@ jobs:
319338
steps:
320339
- custom_attach_workspace
321340
- init_environment
322-
- run:
323-
name: Preparing environment for running tests on Saucelabs.
324-
command: setSecretVar SAUCE_ACCESS_KEY $(echo $SAUCE_ACCESS_KEY | rev)
341+
- init_saucelabs_environment
325342
- run:
326343
name: Run Bazel tests on Saucelabs
327344
# See /tools/saucelabs/README.md for more info
@@ -639,11 +656,7 @@ jobs:
639656
steps:
640657
- custom_attach_workspace
641658
- init_environment
642-
- run:
643-
name: Preparing environment for running tests on Saucelabs.
644-
command: |
645-
setPublicVar KARMA_JS_BROWSERS $(node -e 'console.log(require("./browser-providers.conf").sauceAliases.CI_REQUIRED.join(","))')
646-
setSecretVar SAUCE_ACCESS_KEY $(echo $SAUCE_ACCESS_KEY | rev)
659+
- init_saucelabs_environment
647660
- run:
648661
name: Starting Saucelabs tunnel service
649662
command: ./tools/saucelabs/sauce-service.sh run
@@ -655,7 +668,11 @@ jobs:
655668
# Waiting on ready ensures that we don't run tests too early without Saucelabs not being ready.
656669
name: Waiting for Saucelabs tunnel to connect
657670
command: ./tools/saucelabs/sauce-service.sh ready-wait
658-
- run: yarn karma start ./karma-js.conf.js --single-run --browsers=${KARMA_JS_BROWSERS}
671+
- run:
672+
name: Running tests on Saucelabs.
673+
command: |
674+
browsers=$(node -e 'console.log(require("./browser-providers.conf").sauceAliases.CI_REQUIRED.join(","))')
675+
yarn karma start ./karma-js.conf.js --single-run --browsers=${browsers}
659676
- run:
660677
name: Stop Saucelabs tunnel service
661678
command: ./tools/saucelabs/sauce-service.sh stop

.circleci/env.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ setPublicVar SAUCE_TUNNEL_IDENTIFIER "angular-framework-${CIRCLE_BUILD_NUM}-${CI
6565
# acquire CircleCI instances for too long if sauceconnect failed, we need a connect timeout.
6666
setPublicVar SAUCE_READY_FILE_TIMEOUT 120
6767

68+
6869
####################################################################################################
6970
# Define environment variables for the `angular/components` repo unit tests job.
7071
####################################################################################################

karma-js.conf.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,16 @@ module.exports = function(config) {
167167
conf.browserStack.tunnelIdentifier = tunnelIdentifier;
168168
}
169169

170+
// For SauceLabs jobs, we set up a domain which resolves to the machine which launched
171+
// the tunnel. We do this because devices are sometimes not able to properly resolve
172+
// `localhost` or `127.0.0.1` through the SauceLabs tunnel. Using a domain that does not
173+
// resolve to anything on SauceLabs VMs ensures that such requests are always resolved through
174+
// the tunnel, and resolve to the actual tunnel host machine (commonly the CircleCI VMs).
175+
// More context can be found in: https://github.com/angular/angular/pull/35171.
176+
if (process.env.SAUCE_LOCALHOST_ALIAS_DOMAIN) {
177+
conf.hostname = process.env.SAUCE_LOCALHOST_ALIAS_DOMAIN;
178+
}
179+
170180
if (process.env.KARMA_WEB_TEST_MODE) {
171181
// KARMA_WEB_TEST_MODE is used to setup karma to run in
172182
// SauceLabs or Browserstack

tools/saucelabs/karma-saucelabs.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,18 @@ try {
2121
// KARMA_WEB_TEST_MODE is set which informs /karma-js.conf.js that it should
2222
// run the test with the karma saucelabs launcher
2323
process.env['KARMA_WEB_TEST_MODE'] = 'SL_REQUIRED';
24+
// Saucelabs parameters read from a temporary file that is created by the `sauce-service`. This
25+
// will be `null` if the test runs locally without the `sauce-service` being started.
26+
const saucelabsParams = readLocalSauceConnectParams();
2427
// Setup required SAUCE_* env if they are not already set
2528
if (!process.env['SAUCE_USERNAME'] || !process.env['SAUCE_ACCESS_KEY'] ||
2629
!process.env['SAUCE_TUNNEL_IDENTIFIER']) {
27-
try {
28-
// The following path comes from /tools/saucelabs/sauce-service.sh.
29-
// We setup the required saucelabs environment variables here for the karma test
30-
// from a json file under /tmp/angular/sauce-service so that we don't break the
31-
// test cache with a changing SAUCE_TUNNEL_IDENTIFIER provided through --test_env
32-
const scParams = require('/tmp/angular/sauce-service/sauce-connect-params.json');
33-
process.env['SAUCE_USERNAME'] = scParams.SAUCE_USERNAME;
34-
process.env['SAUCE_ACCESS_KEY'] = scParams.SAUCE_ACCESS_KEY;
35-
process.env['SAUCE_TUNNEL_IDENTIFIER'] = scParams.SAUCE_TUNNEL_IDENTIFIER;
36-
} catch (e) {
37-
console.error(e.stack || e);
38-
console.error(
39-
`!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
30+
// We print a helpful error message below if the required Saucelabs parameters have not
31+
// been specified in test environment, and the `sauce-service` params file has not been
32+
// created either.
33+
if (saucelabsParams === null) {
34+
console.error(`
35+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4036
!!! Make sure that you have run "yarn bazel run //tools/saucelabs:sauce_service_setup"
4137
!!! (or "./tools/saucelabs/sauce-service.sh setup") before the test target. Alternately
4238
!!! you can provide the required SAUCE_* environment variables (SAUCE_USERNAME, SAUCE_ACCESS_KEY &
@@ -45,6 +41,16 @@ try {
4541
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!`);
4642
process.exit(1);
4743
}
44+
process.env['SAUCE_USERNAME'] = saucelabsParams.SAUCE_USERNAME;
45+
process.env['SAUCE_ACCESS_KEY'] = saucelabsParams.SAUCE_ACCESS_KEY;
46+
process.env['SAUCE_TUNNEL_IDENTIFIER'] = saucelabsParams.SAUCE_TUNNEL_IDENTIFIER;
47+
process.env['SAUCE_LOCALHOST_ALIAS_DOMAIN'] = saucelabsParams.SAUCE_LOCALHOST_ALIAS_DOMAIN;
48+
}
49+
50+
// Pass through the optional `SAUCE_LOCALHOST_ALIAS_DOMAIN` environment variable. The
51+
// variable is usually specified on CI, but is not required for testing with Saucelabs.
52+
if (!process.env['SAUCE_LOCALHOST_ALIAS_DOMAIN'] && saucelabsParams !== null) {
53+
process.env['SAUCE_LOCALHOST_ALIAS_DOMAIN'] = saucelabsParams.SAUCE_LOCALHOST_ALIAS_DOMAIN;
4854
}
4955

5056
const scStart = `${sauceService} start-ready-wait`;
@@ -60,3 +66,15 @@ try {
6066
console.error(e.stack || e);
6167
process.exit(1);
6268
}
69+
70+
function readLocalSauceConnectParams() {
71+
try {
72+
// The following path comes from /tools/saucelabs/sauce-service.sh.
73+
// We setup the required saucelabs environment variables here for the karma test
74+
// from a json file under /tmp/angular/sauce-service so that we don't break the
75+
// test cache with a changing SAUCE_TUNNEL_IDENTIFIER provided through --test_env
76+
return require('/tmp/angular/sauce-service/sauce-connect-params.json');
77+
} catch {
78+
return null;
79+
}
80+
}

tools/saucelabs/sauce-service.sh

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ service-setup-command() {
130130
@fail "sc binary not found at ${SAUCE_CONNECT}"
131131
fi
132132

133-
echo "{ \"SAUCE_USERNAME\": \"${SAUCE_USERNAME}\", \"SAUCE_ACCESS_KEY\": \"${SAUCE_ACCESS_KEY}\", \"SAUCE_TUNNEL_IDENTIFIER\": \"${SAUCE_TUNNEL_IDENTIFIER}\" }" > ${SAUCE_PARAMS_JSON_FILE}
133+
echo "{ \"SAUCE_USERNAME\": \"${SAUCE_USERNAME}\", \"SAUCE_ACCESS_KEY\": \"${SAUCE_ACCESS_KEY}\", \"SAUCE_TUNNEL_IDENTIFIER\": \"${SAUCE_TUNNEL_IDENTIFIER}\", \"SAUCE_LOCALHOST_ALIAS_DOMAIN\": \"${SAUCE_LOCALHOST_ALIAS_DOMAIN}\" }" > ${SAUCE_PARAMS_JSON_FILE}
134134

135135
# Command arguments that will be passed to sauce-connect.
136136
# By default we disable SSL bumping for all requests. This is because SSL bumping is
@@ -147,6 +147,16 @@ service-setup-command() {
147147
"--user ${SAUCE_USERNAME}"
148148
# Don't add the --api-key here so we don't echo it out in service-pre-start
149149
)
150+
151+
if [[ -n "${SAUCE_LOCALHOST_ALIAS_DOMAIN}" ]]; then
152+
# Ensures that requests to the localhost alias domain are always resolved through the tunnel.
153+
# This environment variable is usually configured on CI, and refers to a domain that has been
154+
# locally configured in the current machine's hosts file (e.g. `/etc/hosts`). The domain should
155+
# resolve to the current machine in Saucelabs VMs, so we need to ensure that it is resolved
156+
# through the tunnel we going to create.
157+
sauce_args+=("--tunnel-domains ${SAUCE_LOCALHOST_ALIAS_DOMAIN}")
158+
fi
159+
150160
@echo "Sauce connect will be started with:"
151161
echo " ${SAUCE_CONNECT} ${sauce_args[@]}"
152162
SERVICE_COMMAND="${SAUCE_CONNECT} ${sauce_args[@]} --api-key ${SAUCE_ACCESS_KEY}"
@@ -296,7 +306,7 @@ service-post-stop() {
296306
fi
297307
@wait_for "Waiting for start file" "${SERVICE_START_FILE}"
298308
${SERVICE_COMMAND}
299-
) >>"${SERVICE_LOG_FILE}" 2>&1
309+
) >>"${SERVICE_LOG_FILE}" 2>&1
300310
) &
301311
echo $! >"${SERVICE_PID_FILE}"
302312

0 commit comments

Comments
 (0)