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
4040init_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
4752end_per_suite (_Config ) ->
4853 ok .
4954
5055
5156init_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
5665end_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
7184basic_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
153167queue_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
173200wait_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
244270default_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
281308setup_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
323403put_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
332418get_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
348439rabbitmq_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
361450cmd (List ) ->
362451 Command = space_join (List ),
@@ -384,7 +473,6 @@ space_join(L) ->
384473
385474
386475make_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
418505rabbitmqctl (Subcmds ) ->
419506 cmd ([" rabbitmqctl" ] ++ Subcmds ).
420507
@@ -429,3 +516,35 @@ to_str(P, Q) ->
429516escape_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+
0 commit comments