Skip to content

Commit cd95ac7

Browse files
committed
Add @all-autogates test variant to wd_test
This adds support for running wd_test tests with all autogates enabled, similar to the existing support in the internal repo.
1 parent 409b16a commit cd95ac7

File tree

13 files changed

+148
-18
lines changed

13 files changed

+148
-18
lines changed

build/kj_test.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ def kj_test(
4949
)
5050

5151
sh_test(
52-
name = test_name,
52+
name = test_name + "@",
5353
size = size,
5454
srcs = ["//build/fixtures:kj_test.sh"],
5555
data = [cross_alias] + data,
5656
args = ["$(location " + cross_alias + ")"],
5757
tags = tags,
5858
)
5959
sh_test(
60-
name = test_name + "@all-autogates-enabled",
60+
name = test_name + "@all-autogates",
6161
size = size,
6262
env = {"WORKERD_ALL_AUTOGATES": "1"},
6363
srcs = ["//build/fixtures:kj_test.sh"],

build/wd_test.bzl

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ def wd_test(
77
args = [],
88
ts_deps = [],
99
python_snapshot_test = False,
10+
generate_default_variant = True,
11+
generate_all_autogates_variant = True,
1012
**kwargs):
1113
"""Rule to define tests that run `workerd test` with a particular config.
1214
@@ -17,6 +19,8 @@ def wd_test(
1719
data: Additional files which the .capnp config file may embed. All TypeScript files will be compiled,
1820
their resulting files will be passed to the test as well. Usually TypeScript or Javascript source files.
1921
args: Additional arguments to pass to `workerd`. Typically used to pass `--experimental`.
22+
autogates_variant: If True (default), generate an @all-autogates variant of the test.
23+
default_variant: If True (default), generate the default (non-autogates) variant of the test.
2024
"""
2125

2226
# Add workerd binary to "data" dependencies.
@@ -57,14 +61,25 @@ def wd_test(
5761
"$(location {})".format(src),
5862
] + args
5963

60-
_wd_test(
61-
src = src,
62-
name = name,
63-
data = data,
64-
args = args,
65-
python_snapshot_test = python_snapshot_test,
66-
**kwargs
67-
)
64+
if generate_default_variant:
65+
_wd_test(
66+
src = src,
67+
name = name + "@",
68+
data = data,
69+
args = args,
70+
python_snapshot_test = python_snapshot_test,
71+
**kwargs
72+
)
73+
74+
if generate_all_autogates_variant:
75+
_wd_test(
76+
src = src,
77+
name = name + "@all-autogates",
78+
data = data,
79+
args = args + ["--all-autogates"],
80+
python_snapshot_test = python_snapshot_test,
81+
**kwargs
82+
)
6883

6984
WINDOWS_TEMPLATE = """
7085
@echo off
@@ -192,7 +207,7 @@ def _wd_test_impl(ctx):
192207
# Include all file types that might contain testable code
193208
extensions = ["cc", "c++", "cpp", "cxx", "c", "h", "hh", "hpp", "hxx", "inc", "js", "ts", "mjs", "wd-test", "capnp"],
194209
)
195-
environment = {}
210+
environment = dict(ctx.attr.env)
196211
if ctx.attr.python_snapshot_test:
197212
environment["PYTHON_SAVE_SNAPSHOT_ARGS"] = ""
198213
if ctx.attr.load_snapshot:
@@ -272,6 +287,8 @@ _wd_test = rule(
272287
),
273288
"python_snapshot_test": attr.bool(),
274289
"load_snapshot": attr.label(allow_single_file = True),
290+
# Environment variables to set when running the test
291+
"env": attr.string_dict(),
275292
# A reference to the Windows platform label, needed for the implementation of wd_test
276293
"_platforms_os_windows": attr.label(default = "@platforms//os:windows"),
277294
},

src/workerd/api/node/tests/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ wd_test(
335335
src = "dns-nodejs-test.wd-test",
336336
args = ["--experimental"],
337337
data = ["dns-nodejs-test.js"],
338+
# TODO(jsg-rs): This test breaks when RUST_BACKED_NODE_DNS is enabled.
339+
generate_all_autogates_variant = False,
338340
tags = ["requires-network"],
339341
)
340342

src/workerd/api/tests/BUILD.bazel

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,22 @@ wd_test(
341341
data = ["unsafe-test.js"],
342342
)
343343

344+
# Test that verifies autogates are ENABLED - only runs in @all-autogates variant
345+
wd_test(
346+
src = "autogate-enabled-test.wd-test",
347+
args = ["--experimental"],
348+
data = ["autogate-enabled-test.js"],
349+
generate_default_variant = False,
350+
)
351+
352+
# Test that verifies autogates are DISABLED - only runs in default variant
353+
wd_test(
354+
src = "autogate-disabled-test.wd-test",
355+
args = ["--experimental"],
356+
data = ["autogate-disabled-test.js"],
357+
generate_all_autogates_variant = False,
358+
)
359+
344360
wd_test(
345361
src = "url-test.wd-test",
346362
args = ["--experimental"],
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Test that verifies autogates are DISABLED.
2+
// This test only runs in the default variant (all-autogates variant is disabled).
3+
4+
import { strictEqual } from 'node:assert';
5+
import unsafe from 'workerd:unsafe';
6+
7+
export const autogateDisabledTest = {
8+
test() {
9+
// In the default variant, WORKERD_ALL_AUTOGATES env var is NOT set,
10+
// so isTestAutogateEnabled() should return false.
11+
const isEnabled = unsafe.isTestAutogateEnabled();
12+
strictEqual(
13+
isEnabled,
14+
false,
15+
'TEST_WORKERD autogate should be DISABLED in default variant'
16+
);
17+
},
18+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Workerd = import "/workerd/workerd.capnp";
2+
3+
const unitTests :Workerd.Config = (
4+
services = [
5+
( name = "autogate-disabled-test",
6+
worker = (
7+
modules = [
8+
(name = "worker", esModule = embed "autogate-disabled-test.js")
9+
],
10+
compatibilityDate = "2024-01-01",
11+
compatibilityFlags = ["nodejs_compat", "unsafe_module"],
12+
)
13+
),
14+
],
15+
);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Test that verifies autogates are ENABLED.
2+
// This test only runs in the @all-autogates variant (default variant is disabled).
3+
4+
import { strictEqual } from 'node:assert';
5+
import unsafe from 'workerd:unsafe';
6+
7+
export const autogateEnabledTest = {
8+
test() {
9+
// In the @all-autogates variant, WORKERD_ALL_AUTOGATES env var is set,
10+
// so isTestAutogateEnabled() should return true.
11+
const isEnabled = unsafe.isTestAutogateEnabled();
12+
strictEqual(
13+
isEnabled,
14+
true,
15+
'TEST_WORKERD autogate should be ENABLED in @all-autogates variant'
16+
);
17+
},
18+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Workerd = import "/workerd/workerd.capnp";
2+
3+
const unitTests :Workerd.Config = (
4+
services = [
5+
( name = "autogate-enabled-test",
6+
worker = (
7+
modules = [
8+
(name = "worker", esModule = embed "autogate-enabled-test.js")
9+
],
10+
compatibilityDate = "2024-01-01",
11+
compatibilityFlags = ["nodejs_compat", "unsafe_module"],
12+
)
13+
),
14+
],
15+
);

src/workerd/api/unsafe.c++

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <workerd/io/io-context.h>
44
#include <workerd/jsg/jsg.h>
55
#include <workerd/jsg/script.h>
6+
#include <workerd/util/autogate.h>
67

78
namespace workerd::api {
89

@@ -203,6 +204,10 @@ jsg::Promise<void> UnsafeModule::abortAllDurableObjects(jsg::Lock& js) {
203204
return js.resolvedPromise();
204205
}
205206

207+
bool UnsafeModule::isTestAutogateEnabled() {
208+
return util::Autogate::isEnabled(util::AutogateKey::TEST_WORKERD);
209+
}
210+
206211
#ifdef WORKERD_FUZZILLI
207212
void Fuzzilli::fuzzilli(jsg::Lock& js, jsg::Arguments<jsg::Value> args) {
208213
// Delegate to the fuzzilli_handler in fuzzilli.c++

src/workerd/api/unsafe.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,13 @@ class UnsafeModule: public jsg::Object {
100100
UnsafeModule(jsg::Lock&, const jsg::Url&) {}
101101
jsg::Promise<void> abortAllDurableObjects(jsg::Lock& js);
102102

103+
// Returns true if the TEST_WORKERD autogate is enabled.
104+
// This is used to verify that the all-autogates test variant is working correctly.
105+
bool isTestAutogateEnabled();
106+
103107
JSG_RESOURCE_TYPE(UnsafeModule) {
104108
JSG_METHOD(abortAllDurableObjects);
109+
JSG_METHOD(isTestAutogateEnabled);
105110
}
106111
};
107112

0 commit comments

Comments
 (0)