-
Notifications
You must be signed in to change notification settings - Fork 16
Invoke StopAll() in terminal exit pathways when service startup fails #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,32 @@ | ||
| load("@rules_itest//:itest.bzl", "itest_service", "service_test") | ||
| load("@rules_shell//shell:sh_binary.bzl", "sh_binary") | ||
| load("//:must_fail.bzl", "must_fail") | ||
| load(":cleanup_failure_test.bzl", "cleanup_failure_test") | ||
|
|
||
| package(default_visibility = ["//visibility:public"]) | ||
|
|
||
| NOT_WINDOWS = select({ | ||
| "@platforms//os:windows": ["@platforms//:incompatible"], | ||
| "//conditions:default": [], | ||
| }) | ||
|
|
||
| sh_binary( | ||
| name = "crash_immediately", | ||
| srcs = ["crash_immediately.sh"], | ||
| ) | ||
|
|
||
| sh_binary( | ||
| name = "cleanup_on_sigterm", | ||
| srcs = ["cleanup_on_sigterm.sh"], | ||
| ) | ||
|
|
||
| cleanup_failure_test( | ||
| name = "cleanup_on_startup_failure_test", | ||
| target_compatible_with = NOT_WINDOWS, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. marked this test as
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah i think that's ok, windows is very best-effort |
||
| test = ":dependent_startup_failure_test", | ||
| timeout = "short", | ||
| ) | ||
|
|
||
| itest_service( | ||
| name = "immediate_exit_service", | ||
| autoassign_port = True, | ||
|
|
@@ -26,3 +44,31 @@ must_fail( | |
| test = "immediate_exit_service_hygiene_test", | ||
| timeout = "short", | ||
| ) | ||
|
|
||
| itest_service( | ||
| name = "cleanup_service", | ||
| exe = ":cleanup_on_sigterm", | ||
| shutdown_timeout = "5s", | ||
| tags = ["manual"], | ||
| ) | ||
|
|
||
| itest_service( | ||
| name = "dependent_immediate_exit_service", | ||
| autoassign_port = True, | ||
| deps = [":cleanup_service"], | ||
| env = { | ||
| "PORT": "$${PORT}", | ||
| }, | ||
| exe = ":crash_immediately", | ||
| health_check_interval = "100ms", | ||
| http_health_check_address = "http://127.0.0.1:$${PORT}/health", | ||
| tags = ["manual"], | ||
| ) | ||
|
|
||
| service_test( | ||
| name = "dependent_startup_failure_test", | ||
| services = [":dependent_immediate_exit_service"], | ||
| tags = ["manual"], | ||
| test = "@rules_itest//:exit0", | ||
| timeout = "short", | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| def _shell_quote(value): | ||
| return "'" + value.replace("'", "'\"'\"'") + "'" | ||
|
|
||
| def _cleanup_failure_test_impl(ctx): | ||
| test_env = ctx.attr.test[RunEnvironmentInfo].environment | ||
| test_path = ctx.executable.test.short_path | ||
|
|
||
| env_lines = [ | ||
| "export {}={}".format(key, _shell_quote(value)) | ||
| for key, value in sorted(test_env.items()) | ||
| ] | ||
|
|
||
| script = ctx.actions.declare_file(ctx.label.name + ".sh") | ||
| ctx.actions.write( | ||
| output = script, | ||
| is_executable = True, | ||
| content = """#!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| cleanup_marker="${{TEST_TMPDIR}}/startup_failure_cleanup_marker" | ||
| log_file="${{TEST_TMPDIR}}/dependent_startup_failure.log" | ||
| test_path="${{TEST_SRCDIR}}/${{TEST_WORKSPACE}}/{test_path}" | ||
|
|
||
| rm -f "${{cleanup_marker}}" | ||
|
|
||
| {env} | ||
|
|
||
| set +e | ||
| "${{test_path}}" >"${{log_file}}" 2>&1 | ||
| status=$? | ||
| set -e | ||
|
|
||
| cat "${{log_file}}" | ||
|
|
||
| if [[ "${{status}}" -eq 0 ]]; then | ||
| echo "expected dependent startup failure test to fail" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| if [[ ! -f "${{cleanup_marker}}" ]]; then | ||
| echo "expected already-started service to receive shutdown before svcinit exited" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "cleanup marker found: $(cat "${{cleanup_marker}}")" | ||
| """.format( | ||
| env = "\n".join(env_lines), | ||
| test_path = test_path, | ||
| ), | ||
| ) | ||
|
|
||
| runfiles = ctx.runfiles(files = [ctx.executable.test]) | ||
| runfiles = runfiles.merge(ctx.attr.test.default_runfiles) | ||
|
|
||
| return [ | ||
| DefaultInfo( | ||
| executable = script, | ||
| runfiles = runfiles, | ||
| ), | ||
| ] | ||
|
|
||
| cleanup_failure_test = rule( | ||
| implementation = _cleanup_failure_test_impl, | ||
| attrs = { | ||
| "test": attr.label( | ||
| executable = True, | ||
| cfg = "target", | ||
| providers = [RunEnvironmentInfo], | ||
| ), | ||
| }, | ||
| test = True, | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| cleanup_marker="${TEST_TMPDIR}/startup_failure_cleanup_marker" | ||
|
|
||
| cleanup() { | ||
| echo "cleanup service received shutdown" | ||
| echo "cleanup ran" >"${cleanup_marker}" | ||
| exit 0 | ||
| } | ||
|
|
||
| trap cleanup TERM INT | ||
|
|
||
| while true; do | ||
| sleep 1 | ||
| done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: combine with the above? like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated! much cleaner