Skip to content

Commit dc45a0a

Browse files
author
Dave Parfitt
committed
fix ct
1 parent 8d7b340 commit dc45a0a

File tree

2 files changed

+174
-52
lines changed

2 files changed

+174
-52
lines changed

src/oc_erchef/apps/oc_chef_wm/itest/chef_wm_actions_queue_monitoring_SUITE.erl

Lines changed: 168 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -34,43 +34,58 @@
3434
-define(CLIENT_AUTHZ_ID, <<"00000000000000000000000000000003">>).
3535
-define(ACTIONS_EXCHANGE, <<"actions">>).
3636

37-
38-
37+
-define(Q_SETTING(K, V),
38+
chef_wm_rabbitmq_management:set_rabbit_queue_monitor_setting(K,V)).
3939

4040
init_per_suite(InitialConfig) ->
41-
[
42-
{test_vhost, "/testing_vhost"},
43-
{test_password, "chef123"},
44-
{max_length, 10}]
45-
++ InitialConfig.
41+
UseFakeRabbit =
42+
case os:getenv("TRAVIS") of
43+
false -> false;
44+
_ -> true
45+
end,
46+
[{test_vhost, "/testing_vhost"},
47+
{test_password, "chef123"},
48+
{max_length, 10},
49+
{use_fake_rabbit, UseFakeRabbit}]
50+
++ InitialConfig.
4651

4752
end_per_suite(_Config) ->
4853
ok.
4954

5055

5156
init_per_testcase(_, Config) ->
5257
% ensure rabbit starts from a clean slate
53-
teardown_rabbit(Config),
58+
case ?config(use_fake_rabbit, Config) of
59+
false -> teardown_rabbit(Config);
60+
true -> ok
61+
end,
62+
5463
Config.
5564

5665
end_per_testcase(_, Config) ->
66+
case ?config(use_fake_rabbit, Config) of
67+
true ->
68+
catch(exit(whereis(fake_rabbit), kill)),
69+
meck:unload(bunnyc),
70+
meck:unload(chef_wm_rabbitmq_management);
71+
false -> ok
72+
end,
73+
5774
% leave rabbit up for troubleshooting failed tests
58-
%teardown_rabbit(Config),
5975
setup_helper:base_end_per_suite(Config),
6076
Config.
6177

78+
all() -> [basic_queue_monitor, queue_full_dont_start].
79+
80+
81+
6282

63-
all() ->
64-
% don't run tests in Travis yet
65-
case os:getenv("TRAVIS") of
66-
false -> [basic_queue_monitor,
67-
queue_full_dont_start];
68-
true -> []
69-
end.
7083

7184
basic_queue_monitor(Config) ->
7285
default_env_with_queue_monitor(Config),
86+
7387
setup_rabbit(Config),
88+
7489
_FinalConfig = setup_chef(Config),
7590
lager:start(),
7691
lager:set_loglevel(lager_console_backend, debug),
@@ -139,7 +154,6 @@ basic_queue_monitor(Config) ->
139154
get_alaska_msg(Config),
140155
get_alaska_msg(Config),
141156

142-
143157
wait_until_not_at_capacity(),
144158
Status6 = chef_wm_actions_queue_monitoring:status(),
145159
?assert(proplists:get_value(check_count, Status6) > 0),
@@ -152,8 +166,20 @@ basic_queue_monitor(Config) ->
152166

153167
queue_full_dont_start(Config) ->
154168
default_env_with_queue_monitor(Config),
169+
MaxLength = ?config(max_length, Config),
170+
155171
setup_rabbit(Config),
156172

173+
%% one more meck for this test
174+
case ?config(use_fake_rabbit, Config) of
175+
true ->
176+
meck:expect(chef_wm_rabbitmq_management, sync_check_queue_at_capacity,
177+
fun(_Vhost, _Queue) ->
178+
{MaxLength, MaxLength, true}
179+
end);
180+
false -> ok
181+
end,
182+
157183
ibrowse:start(),
158184
[ begin
159185
ct:pal("Publishing msg ~p to queue~n", [X]),
@@ -168,7 +194,8 @@ queue_full_dont_start(Config) ->
168194
catch Error:Reason ->
169195
ct:pal("Erchef failed to start (this is expected): ~p~n~p~n", [Error, Reason])
170196
end,
171-
ok.
197+
198+
ok.
172199

173200
wait_until_not_at_capacity() ->
174201
wait_until(fun () ->
@@ -233,22 +260,22 @@ default_rabbit_config(Config) ->
233260
{queue_length_monitor_queue, alaska },
234261
{queue_length_monitor_millis, 30000 },
235262
{queue_length_monitor_timeout_millis, 5000 },
236-
{drop_on_full_capacity, true }
263+
{drop_on_full_capacity, true },
264+
{prevent_startup_on_full_capacity, true}
237265
]}
238266
].
239267

240268

241-
%-define(Q_SETTING(K, V),
242-
% chef_wm_rabbitmq_management:set_rabbit_queue_monitor_setting(K,V)).
243269

244270
default_env_with_queue_monitor(Config) ->
245271
application:set_env(oc_chef_wm, rabbitmq, default_rabbit_config(Config)),
246272

247-
chef_wm_rabbitmq_management:set_rabbit_queue_monitor_setting(queue_length_monitor_enabled, true),
248-
chef_wm_rabbitmq_management:set_rabbit_queue_monitor_setting(queue_length_monitor_vhost, ?config(test_vhost, Config)),
249-
chef_wm_rabbitmq_management:set_rabbit_queue_monitor_setting(queue_length_monitor_queue, "alaska"),
250-
chef_wm_rabbitmq_management:set_rabbit_queue_monitor_setting(queue_length_monitor_millis, 2000),
251-
chef_wm_rabbitmq_management:set_rabbit_queue_monitor_setting(drop_on_full_capacity, true),
273+
?Q_SETTING(queue_length_monitor_enabled, true),
274+
?Q_SETTING(queue_length_monitor_vhost, ?config(test_vhost, Config)),
275+
?Q_SETTING(queue_length_monitor_queue, "alaska"),
276+
?Q_SETTING(queue_length_monitor_millis, 2000),
277+
?Q_SETTING(drop_on_full_capacity, true),
278+
?Q_SETTING(prevent_startup_on_full_capacity, true),
252279

253280
application:set_env(oc_chef_wm, enable_actions, true),
254281
application:set_env(oc_chef_wm, actions_host, "127.0.0.1"),
@@ -279,6 +306,56 @@ setup_chef(InitialConfig) ->
279306

280307

281308
setup_rabbit(Config) ->
309+
UseFakeRabbit = ?config(use_fake_rabbit, Config),
310+
case UseFakeRabbit of
311+
true -> setup_rabbit_fake(Config);
312+
false -> setup_rabbit_real(Config)
313+
end.
314+
315+
teardown_rabbit(Config) ->
316+
UseFakeRabbit = ?config(use_fake_rabbit, Config),
317+
case UseFakeRabbit of
318+
true -> ok;
319+
false ->
320+
Vhost = ?config(test_vhost, Config),
321+
rabbitmqctl(["delete_vhost", to_str("\"~p\"", [Vhost])])
322+
end.
323+
324+
325+
setup_rabbit_fake(Config) ->
326+
start_fake_rabbit(?config(max_length, Config)),
327+
328+
meck:new(bunnyc),
329+
meck:expect(bunnyc, start_link, fun(_, _, _, _) ->
330+
{ok, self()}
331+
end),
332+
333+
meck:expect(bunnyc, publish,
334+
fun(_Server, _RoutingKey, _PersistentData) ->
335+
%io:format(user, "CAUGHT BUNNYC:PUBLISH ~p ~p ~p~n",
336+
% [Server, RoutingKey, PersistentData]),
337+
fake_rabbit_enqueue(),
338+
ok
339+
end),
340+
341+
342+
meck:new(chef_wm_rabbitmq_management, [passthrough]),
343+
344+
345+
meck:expect(chef_wm_rabbitmq_management, get_max_length,
346+
fun(_Vhost) ->
347+
?config(max_length, Config)
348+
end),
349+
350+
meck:expect(chef_wm_rabbitmq_management, get_current_length,
351+
fun(_Vhost, _Queue) ->
352+
fake_rabbit_current_length()
353+
end),
354+
ok.
355+
356+
357+
358+
setup_rabbit_real(Config) ->
282359
ibrowse:start(),
283360
Vhost = ?config(test_vhost, Config),
284361
Password = ?config(test_password, Config),
@@ -295,7 +372,10 @@ setup_rabbit(Config) ->
295372
all_permissions("/", "actions"),
296373
all_permissions(Vhost, "actions"),
297374

375+
%% This is a lie, as rabbitmq needs to be restarted after the
376+
%% plugin is installed
298377
cmd(["rabbitmq-plugins", "enable", "rabbitmq_management"]),
378+
299379
rabbitmqctl(["set_user_tags", "rabbitmgmt", "administrator"]),
300380

301381
MaxLength = ?config(max_length, Config),
@@ -321,28 +401,39 @@ setup_rabbit(Config) ->
321401

322402

323403
put_alaska_msg(Config) ->
324-
Password = ?config(test_password, Config),
325-
User = "rabbitmgmt",
326-
Vhost = ?config(test_vhost, Config),
327-
Path = escape_to_str("/api/exchanges/~s/actions/publish", Vhost),
328-
Body = "{\"properties\":{},\"routing_key\":\"my key\",\"payload\":\"my body\",\"payload_encoding\":\"string\"}",
329-
rabbitmq_mgmt(Path, post, Body, User, Password).
404+
UseFakeRabbit = ?config(use_fake_rabbit, Config),
405+
case UseFakeRabbit of
406+
true ->
407+
fake_rabbit_enqueue();
408+
false ->
409+
Password = ?config(test_password, Config),
410+
User = "rabbitmgmt",
411+
Vhost = ?config(test_vhost, Config),
412+
Path = escape_to_str("/api/exchanges/~s/actions/publish", Vhost),
413+
Body = "{\"properties\":{},\"routing_key\":\"my key\",\"payload\":\"my body\",\"payload_encoding\":\"string\"}",
414+
rabbitmq_mgmt(Path, post, Body, User, Password)
415+
end.
330416

331417

332418
get_alaska_msg(Config) ->
333419
Password = ?config(test_password, Config),
334-
User = "rabbitmgmt",
335-
Vhost = ?config(test_vhost, Config),
336-
Path = escape_to_str("/api/queues/~s/alaska/get", Vhost),
337-
Body = "{\"count\":1,\"requeue\":false,\"encoding\":\"auto\",\"truncate\":50000}",
338-
Url = "http://localhost:15672" ++ Path,
339-
% this request must use accept */*
340-
ibrowse:send_req(Url,
341-
[{"accept", "*/*"},
342-
{"content-type", "application/json"}],
343-
post,
344-
Body,
345-
[{basic_auth, {User, Password}}]).
420+
UseFakeRabbit = ?config(use_fake_rabbit, Config),
421+
case UseFakeRabbit of
422+
true -> fake_rabbit_dequeue();
423+
false ->
424+
User = "rabbitmgmt",
425+
Vhost = ?config(test_vhost, Config),
426+
Path = escape_to_str("/api/queues/~s/alaska/get", Vhost),
427+
Body = "{\"count\":1,\"requeue\":false,\"encoding\":\"auto\",\"truncate\":50000}",
428+
Url = "http://localhost:15672" ++ Path,
429+
% this request must use accept */*
430+
ibrowse:send_req(Url,
431+
[{"accept", "*/*"},
432+
{"content-type", "application/json"}],
433+
post,
434+
Body,
435+
[{basic_auth, {User, Password}}])
436+
end.
346437

347438
% make an http call to the rabbitmq mgmt console
348439
rabbitmq_mgmt(Path, Method, Body, User, Password) ->
@@ -354,9 +445,7 @@ rabbitmq_mgmt(Path, Method, Body, User, Password) ->
354445
Body,
355446
[{basic_auth, {User, Password}}]).
356447

357-
teardown_rabbit(Config) ->
358-
Vhost = ?config(test_vhost, Config),
359-
rabbitmqctl(["delete_vhost", to_str("\"~p\"", [Vhost])]).
448+
360449

361450
cmd(List) ->
362451
Command = space_join(List),
@@ -384,7 +473,6 @@ space_join(L) ->
384473

385474

386475
make_data_bag(Requestor, Id) ->
387-
%io:format(user, "Create data bag foobar~p~n", [Id]),
388476
DataBag = to_str("{\"name\":\"foobar~p\"}",[Id]),
389477
Url = "http://localhost:8000/organizations/testorg/data",
390478
ibrowse:send_req(Url, [{"x-ops-userid", binary_to_list(Requestor)},
@@ -414,7 +502,6 @@ context() ->
414502
chef_db:make_context(?API_MIN_VER, <<"AB">>, no_header).
415503

416504

417-
%% TODO: move to chef_test lib
418505
rabbitmqctl(Subcmds) ->
419506
cmd(["rabbitmqctl"] ++ Subcmds).
420507

@@ -429,3 +516,35 @@ to_str(P, Q) ->
429516
escape_to_str(Str, Val) ->
430517
lists:flatten(io_lib:format(Str, [http_uri:encode(Val)])).
431518

519+
520+
521+
% spawn a process that keeps a count of enqueue/dequeues
522+
fake_rabbit(Max, Count) ->
523+
receive
524+
inc -> case Count == Max of
525+
true -> fake_rabbit(Max, Count);
526+
false -> fake_rabbit(Max, Count + 1)
527+
end;
528+
dec -> fake_rabbit(Max, Count -1);
529+
{current_count, Pid} -> Pid ! {current_count, Count},
530+
fake_rabbit(Max, Count);
531+
die -> ok
532+
end.
533+
534+
535+
start_fake_rabbit(MaxLength) ->
536+
Pid = spawn(?MODULE, fake_rabbit, [MaxLength, 0]),
537+
register(fake_rabbit, Pid).
538+
539+
fake_rabbit_enqueue() ->
540+
whereis(fake_rabbit) ! inc.
541+
542+
fake_rabbit_dequeue() ->
543+
whereis(fake_rabbit) ! dec.
544+
545+
fake_rabbit_current_length() ->
546+
whereis(fake_rabbit) ! {current_count, self()},
547+
receive
548+
{current_count, N} -> N
549+
end.
550+

src/oc_erchef/apps/oc_chef_wm/src/chef_wm_rabbitmq_management.erl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ set_rabbit_management_setting(Key, NewVal) ->
9797
set_rabbit_queue_monitor_setting(Key, NewVal) ->
9898
set_app_value(oc_chef_wm, rabbitmq, monitoring, Key, NewVal).
9999

100+
%% value MUST already exist in the dict
100101
set_app_value(App, ConfigKey, SubSectionKey, Prop, NewValue) ->
101102
Cfg = envy:get(App, ConfigKey, [], any),
102103
NewCfg =
@@ -147,7 +148,7 @@ get_max_length(Vhost) ->
147148
{ok, "200", _, MaxLengthJson} ->
148149
parse_max_length_response(MaxLengthJson);
149150
{error, {conn_failed,_}} ->
150-
lager:info("Can't connect to RabbitMQ management console"),
151+
lager:info("Can't connect to RabbitMQ management console to fetch max-length"),
151152
undefined;
152153
{ok, "404", _, _} ->
153154
lager:info("RabbitMQ max-length policy not set"),
@@ -165,7 +166,7 @@ get_current_length(Vhost, Queue) ->
165166
CurrentResult = rabbit_mgmt_server_request(mk_current_length_path(Vhost)),
166167
case CurrentResult of
167168
{error, {conn_failed,_}} ->
168-
lager:info("Can't connect to RabbitMQ management console"),
169+
lager:info("Can't connect to RabbitMQ management console to fetch current length"),
169170
undefined;
170171
{ok, "200", _, CurrentStatusJson} ->
171172
parse_current_length_response(CurrentStatusJson, Queue);
@@ -252,7 +253,9 @@ check_current_queue_state(Vhost, Queue, DroppedSinceLastCheck) ->
252253
{max_length(), reset_dropped_since_last_check} |
253254
{max_length(), current_length(), queue_at_capacity()}.
254255
check_current_queue_length(Vhost, Queue, MaxLength, DroppedSinceLastCheck) ->
255-
CurrentLength = get_current_length(Vhost, Queue),
256+
% use ?MODULE here so I can use meck in integrations testing
257+
% https://github.com/eproxus/meck/issues/142
258+
CurrentLength = ?MODULE:get_current_length(Vhost, Queue),
256259
case CurrentLength of
257260
undefined ->
258261
% a queue doesn't appear to be bound to the

0 commit comments

Comments
 (0)