Skip to content

Commit 99292aa

Browse files
committed
Support code coverage
Previously it was not possible to see code coverage for the majority of test cases: integration tests that create RabbitMQ nodes. It was only possible to see code coverage for unit tests. This commit allows to see code coverage for tests that create RabbitMQ nodes. The only thing you need to do is setting the `COVER` variable, for example ``` make -C deps/rabbitmq_mqtt ct COVER=1 ``` will show you coverage across all tests in the MQTT plugin. This commit fixes the erlang.mk file that did not append the `-cover <sepc_path>` flag to the `ct_run` command. Whenever a RabbitMQ node is started `ct_cover:add_nodes/1` is called. Contrary to the documentation which states > To have effect, this function is to be called from init_per_suite/1 (see common_test) before any tests are performed. I found that it also works in init_per_group/1 or even within the test cases themselves. Whenever a RabbitMQ node is stopped or killed `ct_cover:remove_nodes/1` is called to transfer results from the RabbitMQ node to the CT node. Since the erlang.mk file writes a file called `test/ct.cover.spec` including the line: ``` {export,".../rabbitmq-server/deps/rabbitmq_mqtt/cover/ct.coverdata"}. ``` results across all test suites will be accumulated in that file. The accumulated result can be seen through the link `Coverage log` on the test suite result pages.
1 parent dddef45 commit 99292aa

5 files changed

Lines changed: 34 additions & 8 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
/plugins.lock
6262
/sbin/
6363
/sbin.lock
64+
ct.cover.spec
6465
erl_crash.dump
6566
rebar3.crashdump
6667
.envrc

deps/rabbit/test/direct_exchange_routing_v2_SUITE.erl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,7 @@ recover_bindings(Config) ->
321321
assert_index_table_empty(Config),
322322
rabbit_ct_broker_helpers:rabbitmqctl(Config, Server, ["import_definitions", Path], 10_000),
323323
?assertEqual(?NUM_BINDINGS_TO_DIRECT_ECHANGE, table_size(Config, ?INDEX_TABLE_NAME)),
324-
ok = rabbit_ct_broker_helpers:stop_node(Config, 0),
325-
ok = rabbit_ct_broker_helpers:start_node(Config, 0),
324+
ok = rabbit_ct_broker_helpers:restart_node(Config, 0),
326325
?assertEqual(?NUM_BINDINGS_TO_DIRECT_ECHANGE_DURABLE, table_size(Config, ?INDEX_TABLE_NAME)),
327326

328327
%% cleanup

deps/rabbitmq_ct_helpers/src/rabbit_ct_broker_helpers.erl

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,7 @@ query_node(Config, NodeConfig) ->
805805
ct:pal("NO RABBITMQ_FEATURE_FLAGS_FILE"),
806806
Vars0
807807
end,
808+
cover_add_node(Nodename),
808809
rabbit_ct_helpers:set_config(NodeConfig, Vars).
809810

810811
maybe_cluster_nodes(Config) ->
@@ -1002,9 +1003,10 @@ stop_rabbitmq_nodes(Config) ->
10021003
proplists:delete(rmq_nodes, Config).
10031004

10041005
stop_rabbitmq_node(Config, NodeConfig) ->
1006+
Nodename = ?config(nodename, NodeConfig),
1007+
cover_remove_node(Nodename),
10051008
SrcDir = ?config(effective_srcdir, NodeConfig),
10061009
InitialMakeVars = ?config(make_vars_for_node_startup, NodeConfig),
1007-
Nodename = ?config(nodename, NodeConfig),
10081010
InitialNodename = ?config(initial_nodename, NodeConfig),
10091011
MakeVars = InitialMakeVars ++ [
10101012
{"RABBITMQ_NODENAME=~ts", [Nodename]},
@@ -1626,7 +1628,7 @@ stop_node_after(Config, Node, Sleep) ->
16261628

16271629
kill_node(Config, Node) ->
16281630
Pid = rpc(Config, Node, os, getpid, []),
1629-
%% FIXME maybe_flush_cover(Cfg),
1631+
cover_remove_node(Node),
16301632
Cmd = case os:type() of
16311633
{win32, _} ->
16321634
case os:find_executable("taskkill.exe") of
@@ -1917,3 +1919,23 @@ user(Username) ->
19171919
#user{username = Username,
19181920
tags = [administrator],
19191921
authz_backends = [{rabbit_auth_backend_internal, none}]}.
1922+
1923+
cover_add_node(Node) ->
1924+
if_cover(
1925+
fun() ->
1926+
{ok, [Node]} = ct_cover:add_nodes([Node])
1927+
end).
1928+
1929+
cover_remove_node(Node) ->
1930+
if_cover(
1931+
fun() ->
1932+
ok = ct_cover:remove_nodes([Node])
1933+
end).
1934+
1935+
if_cover(F) ->
1936+
case os:getenv("COVER") of
1937+
false ->
1938+
ok;
1939+
_ ->
1940+
F()
1941+
end.

deps/rabbitmq_mqtt/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.sw?
22
.*.sw?
33
*.beam
4+
*.coverdata
45
.idea/*
56
/.erlang.mk/
67
/cover/
@@ -15,6 +16,7 @@
1516
/plugins.lock
1617
/sbin/
1718
/sbin.lock
19+
/test/ct.cover.spec
1820
/xrefr
1921
debug/*
2022
*.plt

erlang.mk

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6669,6 +6669,12 @@ CT_RUN = ct_run \
66696669
-dir $(TEST_DIR) \
66706670
-logdir $(CT_LOGS_DIR)
66716671

6672+
ifdef COVER
6673+
ifneq ($(wildcard $(TEST_DIR)),)
6674+
CT_RUN += -cover $(TEST_DIR)/ct.cover.spec
6675+
endif
6676+
endif
6677+
66726678
ifeq ($(CT_SUITES),)
66736679
ct: $(if $(IS_APP)$(ROOT_DIR),,apps-ct)
66746680
else
@@ -8011,7 +8017,6 @@ endif
80118017
# Code coverage for Common Test.
80128018

80138019
ifdef COVER
8014-
ifdef CT_RUN
80158020
ifneq ($(wildcard $(TEST_DIR)),)
80168021
test-build:: $(TEST_DIR)/ct.cover.spec
80178022

@@ -8022,9 +8027,6 @@ $(TEST_DIR)/ct.cover.spec: cover-data-dir
80228027
$(foreach a,$(COVER_APPS),$(comma) \"$(call core_native_path,$(APPS_DIR)/$a/ebin)\") \
80238028
$(foreach d,$(COVER_DEPS),$(comma) \"$(call core_native_path,$(DEPS_DIR)/$d/ebin)\")]}." \
80248029
'{export,"$(call core_native_path,$(abspath $(COVER_DATA_DIR))/ct.coverdata)"}.' > $@
8025-
8026-
CT_RUN += -cover $(TEST_DIR)/ct.cover.spec
8027-
endif
80288030
endif
80298031
endif
80308032

0 commit comments

Comments
 (0)