|
|
@@ -0,0 +1,185 @@
|
|
|
+// The error-handler trigger: which handlers answer a failed run, and what
|
|
|
+// they are told.
|
|
|
+//
|
|
|
+// Pure, so the two things that can go badly wrong - dispatching a handler for
|
|
|
+// a failure it should not see, and dispatching in a loop - are enumerated
|
|
|
+// here rather than discovered in production.
|
|
|
+#include "functions/descriptor.hpp"
|
|
|
+#include "functions/error_hook.hpp"
|
|
|
+#include <gtest/gtest.h>
|
|
|
+
|
|
|
+using namespace svapi::functions;
|
|
|
+
|
|
|
+namespace {
|
|
|
+
|
|
|
+ErrorHandlerCandidate handler(const std::string& id, const std::string& watch) {
|
|
|
+ nlohmann::json on{{"id", id}, {"type", "error"}, {"watch", watch}};
|
|
|
+ ParseResult p = parseDescriptor(on);
|
|
|
+ EXPECT_TRUE(p.ok) << p.error;
|
|
|
+ return {id, p.descriptor};
|
|
|
+}
|
|
|
+
|
|
|
+FailedRun httpFailure(const std::string& id) {
|
|
|
+ FailedRun f;
|
|
|
+ f.functionId = id;
|
|
|
+ f.type = TriggerType::Http;
|
|
|
+ f.runId = "run-1";
|
|
|
+ f.trigger = "http";
|
|
|
+ f.status = 500;
|
|
|
+ f.error = "TypeError: x is not a function";
|
|
|
+ return f;
|
|
|
+}
|
|
|
+
|
|
|
+} // namespace
|
|
|
+
|
|
|
+// --- The descriptor -----------------------------------------------------
|
|
|
+
|
|
|
+TEST(ErrorDescriptor, ParsesAndDefaultsToWatchingEverything) {
|
|
|
+ const ParseResult r = parseDescriptor({{"id", "alert"}, {"type", "error"}});
|
|
|
+ ASSERT_TRUE(r.ok) << r.error;
|
|
|
+ EXPECT_EQ(r.descriptor.type, TriggerType::Error);
|
|
|
+ EXPECT_EQ(r.descriptor.watch, "*");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(ErrorDescriptor, NeedsNoRouteOrSchedule) {
|
|
|
+ // Like a job function: reached only by a failure, so it declares neither.
|
|
|
+ EXPECT_TRUE(parseDescriptor({{"id", "alert"}, {"type", "error"}}).ok);
|
|
|
+}
|
|
|
+
|
|
|
+TEST(ErrorDescriptor, RefusesAnEmptyWatchRatherThanTreatingItAsEverything) {
|
|
|
+ // An empty watch is almost always an unsubstituted template, and reading
|
|
|
+ // it as "*" would wire up a handler the author never asked for.
|
|
|
+ const ParseResult r = parseDescriptor({{"id", "a"}, {"type", "error"}, {"watch", ""}});
|
|
|
+ EXPECT_FALSE(r.ok);
|
|
|
+ EXPECT_NE(r.error.find("watch"), std::string::npos);
|
|
|
+}
|
|
|
+
|
|
|
+TEST(ErrorDescriptor, RoundTripsThroughTheStoredRow) {
|
|
|
+ const ParseResult first =
|
|
|
+ parseDescriptor({{"id", "a"}, {"type", "error"}, {"watch", "importer"}});
|
|
|
+ ASSERT_TRUE(first.ok) << first.error;
|
|
|
+ const ParseResult second = parseDescriptor(first.descriptor.toJson());
|
|
|
+ ASSERT_TRUE(second.ok) << second.error;
|
|
|
+ EXPECT_EQ(second.descriptor.type, TriggerType::Error);
|
|
|
+ EXPECT_EQ(second.descriptor.watch, "importer");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(ErrorDescriptor, TheTypeErrorMessageListsEveryValidType) {
|
|
|
+ const ParseResult r = parseDescriptor({{"id", "a"}, {"type", "nonsense"}});
|
|
|
+ EXPECT_FALSE(r.ok);
|
|
|
+ EXPECT_NE(r.error.find("error"), std::string::npos)
|
|
|
+ << "a script author must be able to discover the new type from the error";
|
|
|
+}
|
|
|
+
|
|
|
+// THE PAIRING for the whole trigger type: an error handler must NOT be
|
|
|
+// reachable over HTTP. Without this, declaring one would publish a public
|
|
|
+// endpoint that anyone could call to fake an alert.
|
|
|
+TEST(ErrorDescriptor, AnErrorHandlerIsNotReachableAsAnHttpRoute) {
|
|
|
+ ParseResult p = parseDescriptor({{"id", "alert"}, {"type", "error"}});
|
|
|
+ ASSERT_TRUE(p.ok) << p.error;
|
|
|
+ // Even if the row somehow carried a route, the type is what decides.
|
|
|
+ p.descriptor.route = "/alert";
|
|
|
+ const std::vector<FunctionDescriptor> all{p.descriptor};
|
|
|
+ const RouteMatch m = matchHttpRoute(all, "GET", "/alert");
|
|
|
+ EXPECT_EQ(m.status, RouteMatchStatus::NotFound)
|
|
|
+ << "an error handler must have no public endpoint";
|
|
|
+}
|
|
|
+
|
|
|
+// --- Selection ----------------------------------------------------------
|
|
|
+
|
|
|
+TEST(ErrorHook, AWildcardHandlerAnswersAnyFailure) {
|
|
|
+ const std::vector<std::string> picked =
|
|
|
+ selectErrorHandlers(httpFailure("importer"), {handler("alert", "*")});
|
|
|
+ ASSERT_EQ(picked.size(), 1u);
|
|
|
+ EXPECT_EQ(picked[0], "alert");
|
|
|
+}
|
|
|
+
|
|
|
+TEST(ErrorHook, AHandlerWatchingThatFunctionAnswers) {
|
|
|
+ const std::vector<std::string> picked =
|
|
|
+ selectErrorHandlers(httpFailure("importer"), {handler("alert", "importer")});
|
|
|
+ ASSERT_EQ(picked.size(), 1u);
|
|
|
+}
|
|
|
+
|
|
|
+TEST(ErrorHook, AHandlerWatchingADifferentFunctionDoesNot) {
|
|
|
+ EXPECT_TRUE(
|
|
|
+ selectErrorHandlers(httpFailure("importer"), {handler("alert", "exporter")}).empty());
|
|
|
+}
|
|
|
+
|
|
|
+TEST(ErrorHook, WatchIsMatchedEXACTLYwithNoPrefixGlobbing) {
|
|
|
+ // A prefix match would quietly start catching functions added later, and a
|
|
|
+ // handler silently widening its own scope is the opposite of what an
|
|
|
+ // operator wants from one.
|
|
|
+ EXPECT_TRUE(
|
|
|
+ selectErrorHandlers(httpFailure("importer-v2"), {handler("alert", "importer")}).empty());
|
|
|
+ EXPECT_TRUE(
|
|
|
+ selectErrorHandlers(httpFailure("importer"), {handler("alert", "import*")}).empty());
|
|
|
+}
|
|
|
+
|
|
|
+TEST(ErrorHook, NonErrorCandidatesAreIgnored) {
|
|
|
+ ParseResult http = parseDescriptor({{"id", "web"}, {"type", "http"}, {"route", "/w"}});
|
|
|
+ ASSERT_TRUE(http.ok) << http.error;
|
|
|
+ EXPECT_TRUE(selectErrorHandlers(httpFailure("importer"), {{"web", http.descriptor}}).empty());
|
|
|
+}
|
|
|
+
|
|
|
+TEST(ErrorHook, SeveralHandlersAllAnswerInTheOrderGiven) {
|
|
|
+ const std::vector<std::string> picked = selectErrorHandlers(
|
|
|
+ httpFailure("importer"),
|
|
|
+ {handler("page-oncall", "*"), handler("ignore-me", "other"), handler("log-it", "importer")});
|
|
|
+ ASSERT_EQ(picked.size(), 2u);
|
|
|
+ EXPECT_EQ(picked[0], "page-oncall");
|
|
|
+ EXPECT_EQ(picked[1], "log-it");
|
|
|
+}
|
|
|
+
|
|
|
+// --- The loop guard, which is the point ---------------------------------
|
|
|
+
|
|
|
+TEST(ErrorHookLoopGuard, AFailingErrorHandlerTriggersNOTHING) {
|
|
|
+ // The classic shape this prevents: a handler that tries to e-mail an
|
|
|
+ // alert, fails because egress is denied, and triggers itself forever on
|
|
|
+ // the tenant's own queue. A depth budget would still allow one wasteful
|
|
|
+ // run per failure and make an infinite loop a question of configuration.
|
|
|
+ FailedRun failed = httpFailure("alert");
|
|
|
+ failed.type = TriggerType::Error;
|
|
|
+ EXPECT_TRUE(selectErrorHandlers(failed, {handler("alert", "*")}).empty());
|
|
|
+ EXPECT_TRUE(selectErrorHandlers(failed, {handler("other-alert", "*")}).empty())
|
|
|
+ << "not even a DIFFERENT handler may answer a handler's failure - two "
|
|
|
+ "handlers watching * would otherwise ping-pong forever";
|
|
|
+}
|
|
|
+
|
|
|
+TEST(ErrorHookLoopGuard, AHandlerIsNeverSelectedForItsOwnFailure) {
|
|
|
+ // Defence in depth. The type guard above already covers this, and both
|
|
|
+ // conditions exist so they cannot be edited away at once.
|
|
|
+ FailedRun failed = httpFailure("alert"); // deliberately typed Http
|
|
|
+ EXPECT_TRUE(selectErrorHandlers(failed, {handler("alert", "*")}).empty());
|
|
|
+}
|
|
|
+
|
|
|
+// --- The payload --------------------------------------------------------
|
|
|
+
|
|
|
+TEST(ErrorHookPayload, NestsEverythingUnderFailure) {
|
|
|
+ // Nested so a handler can tell a failure payload from a cron or db one
|
|
|
+ // without guessing, and so a field added later cannot collide with a key
|
|
|
+ // the trigger shape already uses.
|
|
|
+ const nlohmann::json p = errorHookPayload(httpFailure("importer"));
|
|
|
+ ASSERT_TRUE(p.contains("failure"));
|
|
|
+ EXPECT_EQ(p["failure"]["function_id"], "importer");
|
|
|
+ EXPECT_EQ(p["failure"]["run_id"], "run-1");
|
|
|
+ EXPECT_EQ(p["failure"]["status"], 500);
|
|
|
+ EXPECT_EQ(p["failure"]["trigger"], "http");
|
|
|
+ EXPECT_NE(p["failure"]["error"].get<std::string>().find("TypeError"), std::string::npos)
|
|
|
+ << "the handler is the tenant's own server-side code and does get the message";
|
|
|
+}
|
|
|
+
|
|
|
+TEST(ErrorHookPayload, OmitsFailureKindWhenNobodyClassifiedIt) {
|
|
|
+ // Absent rather than empty, so a handler can tell "not classified" from a
|
|
|
+ // classification it does not recognise. An HTTP run has no queue to
|
|
|
+ // classify it, so this is legitimately absent rather than unknown.
|
|
|
+ const nlohmann::json p = errorHookPayload(httpFailure("importer"));
|
|
|
+ EXPECT_FALSE(p["failure"].contains("failure_kind"));
|
|
|
+}
|
|
|
+
|
|
|
+TEST(ErrorHookPayload, CarriesFailureKindWhenThereIsOne) {
|
|
|
+ FailedRun f = httpFailure("nightly");
|
|
|
+ f.failureKind = "runner_fault";
|
|
|
+ const nlohmann::json p = errorHookPayload(f);
|
|
|
+ ASSERT_TRUE(p["failure"].contains("failure_kind"));
|
|
|
+ EXPECT_EQ(p["failure"]["failure_kind"], "runner_fault");
|
|
|
+}
|