Skip to content

Commit c89b1e7

Browse files
committed
build: run saucelabs tests with bazel
1 parent d952a22 commit c89b1e7

File tree

8 files changed

+118
-8
lines changed

8 files changed

+118
-8
lines changed

BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,11 @@ genrule(
2727
outs = ["entry_points_manifest.json"],
2828
cmd = "echo '%s' > $@" % entryPoints,
2929
)
30+
31+
genrule(
32+
name = "saucelabs_tunnel_identifier",
33+
outs = ["saucelabs_tunnel_identifier.txt"],
34+
cmd = "$(execpath //scripts/saucelabs:extract-tunnel-identifier) > $@",
35+
stamp = True,
36+
tools = ["//scripts/saucelabs:extract-tunnel-identifier"],
37+
)

scripts/circleci/run-saucelabs-tests.sh

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@ cd $(dirname ${0})/../..
99

1010
# Decode access token and make it accessible for child processes.
1111
export SAUCE_ACCESS_KEY=`echo ${SAUCE_ACCESS_KEY} | rev`
12+
export SAUCE_TUNNEL_IDENTIFIER="angular-material-${CIRCLE_BUILD_NUM}-${CIRCLE_NODE_INDEX}"
1213

1314
# Start tunnel and wait for it being ready.
1415
./scripts/saucelabs/start-tunnel.sh &
1516
./scripts/saucelabs/wait-tunnel.sh
1617

17-
# Setup the test platform environment variable that will be read
18-
# by the Karma configuration script.
19-
export TEST_PLATFORM="saucelabs"
18+
TEST_TARGETS=$(yarn -s bazel query --output label 'attr("tags", "saucelabs", //src/...)')
2019

21-
# Run the unit tests on Saucelabs with Karma.
22-
yarn gulp ci:test
20+
# Run all Saucelabs test targets.
21+
yarn bazel test ${TEST_TARGETS} \
22+
--workspace_status_command="echo SAUCE_TUNNEL_IDENTIFIER ${SAUCE_TUNNEL_IDENTIFIER}" \
23+
--stamp \
24+
--test_env=SAUCE_USERNAME \
25+
--test_env=SAUCE_ACCESS_KEY
2326

2427
# Kill the Saucelabs tunnel. This is necessary in order to avoid rate-limit
2528
# errors that cause the unit tests to be flaky.

scripts/saucelabs/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
sh_binary(
4+
name = "extract-tunnel-identifier",
5+
srcs = ["extract-tunnel-identifier.sh"],
6+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
set -e -o pipefail
4+
5+
volatile_status_key="bazel-out/volatile-status.txt"
6+
tunnel_key=$(cat ${volatile_status_key} | { grep SAUCE_TUNNEL_IDENTIFIER || echo ""; })
7+
8+
if [[ -z "${tunnel_key}" ]]; then
9+
echo "No tunnel identifier set as volatile status key."
10+
exit 1
11+
fi
12+
13+
echo ${tunnel_key} | cut -f2 -d ' '

scripts/saucelabs/start-tunnel.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ rm ${tunnelFileName}
2929
# Command arguments that will be passed to sauce-connect.
3030
sauceArgs="--readyfile ${tunnelReadyFile} --pidfile ${tunnelPidFile}"
3131

32-
if [ ! -z "${CIRCLE_BUILD_NUM}" ]; then
33-
sauceArgs="${sauceArgs} --tunnel-identifier angular-material-${CIRCLE_BUILD_NUM}-${CIRCLE_NODE_INDEX}"
32+
if [ ! -z "${SAUCE_TUNNEL_IDENTIFIER}" ]; then
33+
sauceArgs="${sauceArgs} --tunnel-identifier ${SAUCE_TUNNEL_IDENTIFIER}"
3434
fi
3535

3636
echo "Starting Sauce Connect in the background. Passed arguments: ${sauceArgs}"

test/BUILD.bazel

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ load("//tools:defaults.bzl", "ts_library")
33

44
package(default_visibility = ["//visibility:public"])
55

6-
exports_files(["bazel-karma-local-config.js"])
6+
exports_files([
7+
"bazel-karma-local-config.js",
8+
"karma-saucelabs.conf.js",
9+
"browser-providers.js",
10+
"karma-browsers.json",
11+
])
712

813
# Common set-up for all Angular Material and CDK tests.
914
ts_library(

test/karma-saucelabs.conf.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const {readFileSync} = require('fs');
2+
const {customLaunchers, platformMap} = require('./browser-providers');
3+
4+
module.exports = karmaConfig => {
5+
const config = {
6+
plugins: [
7+
require('karma-sauce-launcher')
8+
],
9+
customLaunchers: customLaunchers,
10+
sauceLabs: {
11+
testName: 'Angular Material Unit Tests',
12+
startConnect: false,
13+
recordVideo: false,
14+
recordScreenshots: false,
15+
idleTimeout: 600,
16+
commandTimeout: 600,
17+
maxDuration: 5400,
18+
},
19+
browserNoActivityTimeout: 300000,
20+
browserDisconnectTimeout: 180000,
21+
browserDisconnectTolerance: 3,
22+
captureTimeout: 180000,
23+
browsers: platformMap.saucelabs,
24+
25+
// Try Websocket for a faster transmission first. Fallback to polling if necessary.
26+
transports: ['websocket', 'polling'],
27+
};
28+
29+
// The tunnel identifier has been written to a stamped genrule that access the tunnel
30+
// identifier through a volatile status key. This ensures that test and build results
31+
// are not invalidated whenever the tunnel identifier changes (which it does for each PR).
32+
const tunnelIdentifier = readFileSync(
33+
require.resolve('../saucelabs_tunnel_identifier.txt'), 'utf8');
34+
35+
config.sauceLabs.build = tunnelIdentifier;
36+
config.sauceLabs.tunnelIdentifier = tunnelIdentifier;
37+
38+
karmaConfig.set(config);
39+
};

tools/defaults.bzl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,42 @@ def karma_web_test_suite(name, **kwargs):
182182
testonly = True,
183183
)
184184

185+
data = kwargs.get("data", [])
186+
tags = kwargs.get("tags", [])
187+
188+
sauce_web_test_args = dict(**web_test_args)
189+
sauce_web_test_args["data"] = sauce_web_test_args.get("data", []) + [
190+
"//:saucelabs_tunnel_identifier",
191+
"//test:browser-providers.js",
192+
"//test:karma-browsers.json"
193+
]
194+
sauce_web_test_args["deps"] = sauce_web_test_args.get("deps", []) + [
195+
"@npm//karma-sauce-launcher"
196+
]
197+
198+
# Add a saucelabs target for these karma tests
199+
_karma_web_test(
200+
name = "%s_saucelabs_bin" % name,
201+
timeout = "long",
202+
config_file = "//test:karma-saucelabs.conf.js",
203+
tags = ["manual"],
204+
**sauce_web_test_args
205+
)
206+
207+
# Workaround for: https://github.com/bazelbuild/rules_nodejs/issues/1429
208+
native.sh_test(
209+
name = "%s_saucelabs" % name,
210+
srcs = ["%s_saucelabs_bin" % name],
211+
tags = tags + [
212+
"ibazel_notify_changes",
213+
"exclusive",
214+
"manual",
215+
"no-remote-exec",
216+
"saucelabs",
217+
],
218+
testonly = True,
219+
)
220+
185221
# Default test suite with all configured browsers.
186222
_karma_web_test_suite(
187223
name = name,

0 commit comments

Comments
 (0)