diff options
author | Billy Donahue <billy.donahue@mongodb.com> | 2018-05-14 16:34:53 -0400 |
---|---|---|
committer | Billy Donahue <billy.donahue@mongodb.com> | 2018-05-16 15:12:06 -0400 |
commit | 373aae274364751eec424a3665c50b53b06f6746 (patch) | |
tree | 131d1bed40e861b7b1f84b87f3af9dbf6c10a3f3 /src/mongo | |
parent | def040cc178a7726559e7796557c98384119b16e (diff) | |
download | mongo-373aae274364751eec424a3665c50b53b06f6746.tar.gz |
SERVER-34653 MONGO_REGISTER_TEST_COMMAND
Diffstat (limited to 'src/mongo')
20 files changed, 46 insertions, 136 deletions
diff --git a/src/mongo/db/commands.h b/src/mongo/db/commands.h index 40248943ef0..5e35ff22cc6 100644 --- a/src/mongo/db/commands.h +++ b/src/mongo/db/commands.h @@ -33,12 +33,14 @@ #include <vector> #include "mongo/base/counter.h" +#include "mongo/base/init.h" #include "mongo/base/status.h" #include "mongo/base/status_with.h" #include "mongo/db/auth/privilege.h" #include "mongo/db/auth/resource_pattern.h" #include "mongo/db/client.h" #include "mongo/db/commands/server_status_metric.h" +#include "mongo/db/commands/test_commands_enabled.h" #include "mongo/db/jsobj.h" #include "mongo/db/query/explain.h" #include "mongo/db/repl/read_concern_args.h" @@ -850,4 +852,17 @@ private: */ CommandRegistry* globalCommandRegistry(); +/** + * Creates a test command object of type CmdType if test commands are enabled for this process. + * Prefer this syntax to using MONGO_INITIALIZER directly. + * The created Command object is "leaked" intentionally, since it will register itself. + */ +#define MONGO_REGISTER_TEST_COMMAND(CmdType) \ + MONGO_INITIALIZER(RegisterTestCommand_##CmdType)(InitializerContext*) { \ + if (getTestCommandsEnabled()) { \ + new CmdType(); \ + } \ + return Status::OK(); \ + } + } // namespace mongo diff --git a/src/mongo/db/commands/cpuload.cpp b/src/mongo/db/commands/cpuload.cpp index e713fb4811c..068b6073286 100644 --- a/src/mongo/db/commands/cpuload.cpp +++ b/src/mongo/db/commands/cpuload.cpp @@ -82,12 +82,7 @@ public: } }; -MONGO_INITIALIZER(RegisterCpuLoadCmd)(InitializerContext* context) { - if (getTestCommandsEnabled()) { - new CPULoadCommand(); - } - return Status::OK(); -} +MONGO_REGISTER_TEST_COMMAND(CPULoadCommand); } // namespace mongo diff --git a/src/mongo/db/commands/dbcheck.cpp b/src/mongo/db/commands/dbcheck.cpp index 8bac0ea3b4e..7ab68f7077f 100644 --- a/src/mongo/db/commands/dbcheck.cpp +++ b/src/mongo/db/commands/dbcheck.cpp @@ -549,11 +549,6 @@ public: } }; -MONGO_INITIALIZER(RegisterDbCheckCmd)(InitializerContext* context) { - if (getTestCommandsEnabled()) { - new DbCheckCmd(); - } - return Status::OK(); -} +MONGO_REGISTER_TEST_COMMAND(DbCheckCmd); } // namespace } diff --git a/src/mongo/db/commands/do_txn_cmd.cpp b/src/mongo/db/commands/do_txn_cmd.cpp index d9ab5c0df9e..cacc9aae349 100644 --- a/src/mongo/db/commands/do_txn_cmd.cpp +++ b/src/mongo/db/commands/do_txn_cmd.cpp @@ -164,12 +164,7 @@ public: } }; -MONGO_INITIALIZER(RegisterDoTxnCommand)(InitializerContext* ctx) { - if (getTestCommandsEnabled()) { - new DoTxnCmd(); - } - return Status::OK(); -} +MONGO_REGISTER_TEST_COMMAND(DoTxnCmd); } // namespace } // namespace mongo diff --git a/src/mongo/db/commands/fail_point_cmd.cpp b/src/mongo/db/commands/fail_point_cmd.cpp index 8c1b40f55de..89451c1ca21 100644 --- a/src/mongo/db/commands/fail_point_cmd.cpp +++ b/src/mongo/db/commands/fail_point_cmd.cpp @@ -115,11 +115,5 @@ public: return true; } }; -MONGO_INITIALIZER(RegisterFaultInjectCmd)(InitializerContext* context) { - if (getTestCommandsEnabled()) { - // Leaked intentionally: a Command registers itself when constructed. - new FaultInjectCmd(); - } - return Status::OK(); -} +MONGO_REGISTER_TEST_COMMAND(FaultInjectCmd); } diff --git a/src/mongo/db/commands/generic.cpp b/src/mongo/db/commands/generic.cpp index 9a4bf94a78b..0f5889f655e 100644 --- a/src/mongo/db/commands/generic.cpp +++ b/src/mongo/db/commands/generic.cpp @@ -123,12 +123,7 @@ public: }; constexpr StringData EchoCommand::Request::kCommandName; -MONGO_INITIALIZER(RegisterEcho)(InitializerContext* context) { - if (getTestCommandsEnabled()) { - new EchoCommand(); - } - return Status::OK(); -} +MONGO_REGISTER_TEST_COMMAND(EchoCommand); class ListCommandsCmd : public BasicCommand { public: diff --git a/src/mongo/db/commands/generic_servers.cpp b/src/mongo/db/commands/generic_servers.cpp index 40f47e42465..3fbaea7c2a1 100644 --- a/src/mongo/db/commands/generic_servers.cpp +++ b/src/mongo/db/commands/generic_servers.cpp @@ -316,13 +316,7 @@ public: } }; -MONGO_INITIALIZER(RegisterClearLogCmd)(InitializerContext* context) { - if (getTestCommandsEnabled()) { - // Leaked intentionally: a Command registers itself when constructed. - new ClearLogCmd(); - } - return Status::OK(); -} +MONGO_REGISTER_TEST_COMMAND(ClearLogCmd); } // namespace diff --git a/src/mongo/db/commands/hashcmd.cpp b/src/mongo/db/commands/hashcmd.cpp index 0eb54cd3ec2..1bf80afabe8 100644 --- a/src/mongo/db/commands/hashcmd.cpp +++ b/src/mongo/db/commands/hashcmd.cpp @@ -101,11 +101,5 @@ public: return true; } }; -MONGO_INITIALIZER(RegisterHashEltCmd)(InitializerContext* context) { - if (getTestCommandsEnabled()) { - // Leaked intentionally: a Command registers itself when constructed. - new CmdHashElt(); - } - return Status::OK(); -} +MONGO_REGISTER_TEST_COMMAND(CmdHashElt); } diff --git a/src/mongo/db/commands/reap_logical_session_cache_now.cpp b/src/mongo/db/commands/reap_logical_session_cache_now.cpp index 05b1ad665e1..e0d4d1264d8 100644 --- a/src/mongo/db/commands/reap_logical_session_cache_now.cpp +++ b/src/mongo/db/commands/reap_logical_session_cache_now.cpp @@ -82,13 +82,7 @@ public: } }; -MONGO_INITIALIZER(RegisterReapLogicalSessionCacheNowCommand)(InitializerContext* context) { - if (getTestCommandsEnabled()) { - // Leaked intentionally: a Command registers itself when constructed. - new ReapLogicalSessionCacheNowCommand(); - } - return Status::OK(); -} +MONGO_REGISTER_TEST_COMMAND(ReapLogicalSessionCacheNowCommand); } // namespace diff --git a/src/mongo/db/commands/refresh_logical_session_cache_now.cpp b/src/mongo/db/commands/refresh_logical_session_cache_now.cpp index 4cb54b0c823..9a8124a1e4e 100644 --- a/src/mongo/db/commands/refresh_logical_session_cache_now.cpp +++ b/src/mongo/db/commands/refresh_logical_session_cache_now.cpp @@ -83,13 +83,7 @@ public: } }; -MONGO_INITIALIZER(RegisterRefreshLogicalSessionCacheNowCommand)(InitializerContext* context) { - if (getTestCommandsEnabled()) { - // Leaked intentionally: a Command registers itself when constructed. - new RefreshLogicalSessionCacheNowCommand(); - } - return Status::OK(); -} +MONGO_REGISTER_TEST_COMMAND(RefreshLogicalSessionCacheNowCommand); } // namespace diff --git a/src/mongo/db/commands/restart_catalog_command.cpp b/src/mongo/db/commands/restart_catalog_command.cpp index c1671eaded5..83cc1a1f628 100644 --- a/src/mongo/db/commands/restart_catalog_command.cpp +++ b/src/mongo/db/commands/restart_catalog_command.cpp @@ -125,11 +125,6 @@ public: } }; -MONGO_INITIALIZER(RegisterRestartCatalogCommand)(InitializerContext* ctx) { - if (getTestCommandsEnabled()) { - // Leaked intentionally: a Command registers itself when constructed. - new RestartCatalogCmd(); - } - return Status::OK(); -} +MONGO_REGISTER_TEST_COMMAND(RestartCatalogCmd); + } // namespace mongo diff --git a/src/mongo/db/commands/snapshot_management.cpp b/src/mongo/db/commands/snapshot_management.cpp index b0b9896eb9b..dd412d086d5 100644 --- a/src/mongo/db/commands/snapshot_management.cpp +++ b/src/mongo/db/commands/snapshot_management.cpp @@ -84,6 +84,7 @@ public: return true; } }; +MONGO_REGISTER_TEST_COMMAND(CmdMakeSnapshot); class CmdSetCommittedSnapshot final : public BasicCommand { public: @@ -125,13 +126,5 @@ public: return true; } }; - -MONGO_INITIALIZER(RegisterSnapshotManagementCommands)(InitializerContext* context) { - if (getTestCommandsEnabled()) { - // Leaked intentionally: a Command registers itself when constructed. - new CmdMakeSnapshot(); - new CmdSetCommittedSnapshot(); - } - return Status::OK(); -} +MONGO_REGISTER_TEST_COMMAND(CmdSetCommittedSnapshot); } diff --git a/src/mongo/db/commands/test_commands.cpp b/src/mongo/db/commands/test_commands.cpp index 8d8982f3b20..74e09ab87ae 100644 --- a/src/mongo/db/commands/test_commands.cpp +++ b/src/mongo/db/commands/test_commands.cpp @@ -108,6 +108,8 @@ public: } }; +MONGO_REGISTER_TEST_COMMAND(GodInsert); + /* for diagnostic / testing purposes. Enabled via command line. */ class CmdSleep : public BasicCommand { public: @@ -198,6 +200,8 @@ public: } }; +MONGO_REGISTER_TEST_COMMAND(CmdSleep); + // Testing only, enabled via command-line. class CapTrunc : public BasicCommand { public: @@ -265,6 +269,8 @@ public: } }; +MONGO_REGISTER_TEST_COMMAND(CapTrunc); + // Testing-only, enabled via command line. class EmptyCapped : public BasicCommand { public: @@ -291,16 +297,5 @@ public: } }; -// ---------------------------- - -MONGO_INITIALIZER(RegisterEmptyCappedCmd)(InitializerContext* context) { - if (getTestCommandsEnabled()) { - // Leaked intentionally: a Command registers itself when constructed. - new CapTrunc(); - new CmdSleep(); - new EmptyCapped(); - new GodInsert(); - } - return Status::OK(); -} +MONGO_REGISTER_TEST_COMMAND(EmptyCapped); } diff --git a/src/mongo/db/commands/test_commands_enabled.h b/src/mongo/db/commands/test_commands_enabled.h index 633c18153b8..f8f48a63c95 100644 --- a/src/mongo/db/commands/test_commands_enabled.h +++ b/src/mongo/db/commands/test_commands_enabled.h @@ -33,15 +33,11 @@ namespace mongo { /** * If true, then testing commands are available. Defaults to false. * - * Testing commands should conditionally register themselves by consulting this flag: - * - * MONGO_INITIALIZER(RegisterMyTestCommand)(InitializerContext* context) { - * if (getTestCommandsEnabled()) { - * // Leaked intentionally: a Command registers itself when constructed. - * new MyTestCommand(); - * } - * return Status::OK(); - * } + * Testing commands should conditionally register themselves by using + * MONGO_REGISTER_TEST_COMMAND (in src/mongo/db/commands.h), which consults this flag: + * + * class MyTestCommand ...; + * MONGO_REGISTER_TEST_COMMAND(MyTestCommand); * * To make testing commands available by default, change the value to true before running any * mongo initializers: diff --git a/src/mongo/db/commands/txn_cmds.cpp b/src/mongo/db/commands/txn_cmds.cpp index 48708f2c9ec..9e7a05bba3f 100644 --- a/src/mongo/db/commands/txn_cmds.cpp +++ b/src/mongo/db/commands/txn_cmds.cpp @@ -149,12 +149,7 @@ public: } }; -MONGO_INITIALIZER(RegisterPrepareTxnCmd)(InitializerContext* context) { - if (getTestCommandsEnabled()) { - new CmdPrepareTxn(); - } - return Status::OK(); -} +MONGO_REGISTER_TEST_COMMAND(CmdPrepareTxn); class CmdAbortTxn : public BasicCommand { public: diff --git a/src/mongo/db/exec/stagedebug_cmd.cpp b/src/mongo/db/exec/stagedebug_cmd.cpp index a7e0dac8a2d..d65a20fea15 100644 --- a/src/mongo/db/exec/stagedebug_cmd.cpp +++ b/src/mongo/db/exec/stagedebug_cmd.cpp @@ -516,12 +516,6 @@ public: } }; -MONGO_INITIALIZER(RegisterStageDebugCmd)(InitializerContext* context) { - if (getTestCommandsEnabled()) { - // Leaked intentionally: a Command registers itself when constructed. - new StageDebugCmd(); - } - return Status::OK(); -} +MONGO_REGISTER_TEST_COMMAND(StageDebugCmd); } // namespace mongo diff --git a/src/mongo/db/repl/repl_set_commands.cpp b/src/mongo/db/repl/repl_set_commands.cpp index c315607369d..5a7a8ddaab6 100644 --- a/src/mongo/db/repl/repl_set_commands.cpp +++ b/src/mongo/db/repl/repl_set_commands.cpp @@ -151,13 +151,7 @@ public: } }; -MONGO_INITIALIZER(RegisterReplSetTestCmd)(InitializerContext* context) { - if (getTestCommandsEnabled()) { - // Leaked intentionally: a Command registers itself when constructed. - new CmdReplSetTest(); - } - return Status::OK(); -} +MONGO_REGISTER_TEST_COMMAND(CmdReplSetTest); /** get rollback id. used to check if a rollback happened during some interval of time. as consumed, the rollback id is not in any particular order, it simply changes on each rollback. diff --git a/src/mongo/db/storage/mmap_v1/journal_latency_test_cmd.cpp b/src/mongo/db/storage/mmap_v1/journal_latency_test_cmd.cpp index 318fea0d755..8807dfbb064 100644 --- a/src/mongo/db/storage/mmap_v1/journal_latency_test_cmd.cpp +++ b/src/mongo/db/storage/mmap_v1/journal_latency_test_cmd.cpp @@ -151,11 +151,5 @@ public: return 1; } }; -MONGO_INITIALIZER(RegisterJournalLatencyTestCmd)(InitializerContext* context) { - if (getTestCommandsEnabled()) { - // Leaked intentionally: a Command registers itself when constructed. - new JournalLatencyTestCmd(); - } - return Status::OK(); -} +MONGO_REGISTER_TEST_COMMAND(JournalLatencyTestCmd); } diff --git a/src/mongo/s/commands/cluster_multicast.cpp b/src/mongo/s/commands/cluster_multicast.cpp index 54991980075..d1fcd3cc729 100644 --- a/src/mongo/s/commands/cluster_multicast.cpp +++ b/src/mongo/s/commands/cluster_multicast.cpp @@ -147,12 +147,7 @@ public: } }; -MONGO_INITIALIZER(RegisterMulticast)(InitializerContext* context) { - if (getTestCommandsEnabled()) { - new MulticastCmd(); - } - return Status::OK(); -} +MONGO_REGISTER_TEST_COMMAND(MulticastCmd); } // namespace } // namespace mongo diff --git a/src/mongo/s/commands/cluster_restart_catalog_command.cpp b/src/mongo/s/commands/cluster_restart_catalog_command.cpp index b91259a8de6..814846afdbc 100644 --- a/src/mongo/s/commands/cluster_restart_catalog_command.cpp +++ b/src/mongo/s/commands/cluster_restart_catalog_command.cpp @@ -89,13 +89,7 @@ public: } }; -MONGO_INITIALIZER(RegisterClusterRestartCatalogCommand)(InitializerContext* ctx) { - if (getTestCommandsEnabled()) { - // Leaked intentionally: a Command registers itself when constructed. - new ClusterRestartCatalogCmd(); - } - return Status::OK(); -} +MONGO_REGISTER_TEST_COMMAND(ClusterRestartCatalogCmd); } // namespace } // namespace mongo |