Skip to content

Commit 4521569

Browse files
committed
Update testcases
1 parent 27c5487 commit 4521569

File tree

2 files changed

+81
-42
lines changed

2 files changed

+81
-42
lines changed

deps/rabbitmq_aws/src/rabbitmq_aws.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
request/5, request/6, request/7,
1616
set_credentials/2,
1717
has_credentials/0,
18+
parse_uri/1,
1819
set_region/1,
1920
ensure_imdsv2_token_valid/0,
2021
api_get_request/2]).
@@ -177,8 +178,8 @@ start_link() ->
177178

178179
-spec init(list()) -> {ok, state()}.
179180
init([]) ->
180-
{ok, #state{}}.
181-
181+
{ok, _} = application:ensure_all_started(gun),
182+
{ok, #state{}}.
182183

183184
terminate(_, State) ->
184185
%% Close all Gun connections
@@ -656,7 +657,6 @@ create_gun_connection(State, Host, Port, Path, HostKey, Options) ->
656657
protocols => Protocols,
657658
connect_timeout => ConnectTimeout
658659
},
659-
application:ensure_all_started(gun),
660660
case gun:open(Host, Port, Opts) of
661661
{ok, ConnPid} ->
662662
case gun:await_up(ConnPid, ConnectTimeout) of

deps/rabbitmq_aws/test/rabbitmq_aws_tests.erl

Lines changed: 78 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ init_test_() ->
2525
ok = gen_server:stop(Pid),
2626
os:unsetenv("AWS_ACCESS_KEY_ID"),
2727
os:unsetenv("AWS_SECRET_ACCESS_KEY"),
28-
Expectation = {state,"Sésame","ouvre-toi",undefined,undefined,"us-west-3", undefined,undefined},
28+
Expectation = {state,"Sésame","ouvre-toi",undefined,undefined,"us-west-3", undefined,undefined, #{}},
2929
?assertEqual(Expectation, State)
3030
end},
3131
{"error", fun() ->
@@ -35,15 +35,15 @@ init_test_() ->
3535
rabbitmq_aws:refresh_credentials(),
3636
{ok, State} = gen_server:call(Pid, get_state),
3737
ok = gen_server:stop(Pid),
38-
Expectation = {state,undefined,undefined,undefined,undefined,"us-west-3",undefined,test_result},
38+
Expectation = {state,undefined,undefined,undefined,undefined,"us-west-3",undefined,test_result, #{}},
3939
?assertEqual(Expectation, State),
4040
meck:validate(rabbitmq_aws_config)
4141
end}
4242
]
4343
}.
4444

4545
terminate_test() ->
46-
?assertEqual(ok, rabbitmq_aws:terminate(foo, bar)).
46+
?assertEqual(ok, rabbitmq_aws:terminate(foo, {state,undefined,undefined,undefined,undefined,"us-west-3",undefined,test_result, #{}})).
4747

4848
code_change_test() ->
4949
?assertEqual({ok, {state, denial}}, rabbitmq_aws:code_change(foo, bar, {state, denial})).
@@ -141,8 +141,8 @@ gen_server_call_test_() ->
141141
os:putenv("AWS_DEFAULT_REGION", "us-west-3"),
142142
os:putenv("AWS_ACCESS_KEY_ID", "Sésame"),
143143
os:putenv("AWS_SECRET_ACCESS_KEY", "ouvre-toi"),
144-
meck:new(httpc, []),
145-
[httpc]
144+
meck:new(gun, []),
145+
[gun]
146146
end,
147147
fun (Mods) ->
148148
meck:unload(Mods),
@@ -164,14 +164,24 @@ gen_server_call_test_() ->
164164
Body = "",
165165
Options = [],
166166
Host = undefined,
167-
meck:expect(httpc, request,
168-
fun(get, {"https://ec2.us-east-1.amazonaws.com/?Action=DescribeTags&Version=2015-10-01", _Headers}, _Options, []) ->
169-
{ok, {{"HTTP/1.0", 200, "OK"}, [{"content-type", "application/json"}], "{\"pass\": true}"}}
170-
end),
171-
Expectation = {reply, {ok, {[{"content-type", "application/json"}], [{"pass", true}]}}, State},
167+
meck:expect(gun, open, fun(_,_,_) -> {ok,pid} end),
168+
meck:expect(gun, close, fun(_) -> ok end),
169+
meck:expect(gun, await_up, fun(_,_) -> {ok,protocol} end),
170+
meck:expect(gun, get,
171+
fun(_Pid, _Path, _Headers) -> nofin end),
172+
%% {ok, {{"HTTP/1.0", 200, "OK"}, [{"content-type", "application/json"}], "{\"pass\": true}"}}
173+
%% end),
174+
meck:expect(gun, await,
175+
fun(_Pid, _, _) -> {response, nofin, 200, [{"content-type", "application/json"}]} end),
176+
meck:expect(gun, await_body,
177+
fun(_Pid, _, _) -> {ok, <<"{\"pass\": true}">>} end),
178+
179+
%% {ok, {{"HTTP/1.0", 200, "OK"}, [{"content-type", "application/json"}], "{\"pass\": true}"}}
180+
%% end),
181+
Expectation = {reply, {ok, {[{"content-type", "application/json"}], [{"pass", true}]}}, State#state{gun_connections = #{"ec2.us-east-1.amazonaws.com:443" => pid}}},
172182
Result = rabbitmq_aws:handle_call({request, Service, Method, Headers, Path, Body, Options, Host}, eunit, State),
173183
?assertEqual(Expectation, Result),
174-
meck:validate(httpc)
184+
meck:validate(gun)
175185
end
176186
},
177187
{
@@ -327,9 +337,9 @@ perform_request_test_() ->
327337
{
328338
foreach,
329339
fun () ->
330-
meck:new(httpc, []),
340+
meck:new(gun, []),
331341
meck:new(rabbitmq_aws_config, []),
332-
[httpc, rabbitmq_aws_config]
342+
[gun, rabbitmq_aws_config]
333343
end,
334344
fun meck:unload/1,
335345
[
@@ -347,19 +357,21 @@ perform_request_test_() ->
347357
Options = [],
348358
Host = undefined,
349359
ExpectURI = "https://ec2.us-east-1.amazonaws.com/?Action=DescribeTags&Version=2015-10-01",
350-
meck:expect(httpc, request,
351-
fun(get, {URI, _Headers}, _Options, []) ->
352-
case URI of
353-
ExpectURI ->
354-
{ok, {{"HTTP/1.0", 200, "OK"}, [{"content-type", "application/json"}], "{\"pass\": true}"}};
355-
_ ->
356-
{ok, {{"HTTP/1.0", 400, "RequestFailure", [{"content-type", "application/json"}], "{\"pass\": false}"}}}
357-
end
358-
end),
359-
Expectation = {{ok, {[{"content-type", "application/json"}], [{"pass", true}]}}, State},
360+
meck:expect(gun, open, fun(_,_,_) -> {ok,pid} end),
361+
meck:expect(gun, close, fun(_) -> ok end),
362+
meck:expect(gun, await_up, fun(_,_) -> {ok,protocol} end),
363+
364+
meck:expect(gun, get,
365+
fun(_Pid, "/?Action=DescribeTags&Version=2015-10-01", _Headers) -> nofin end),
366+
meck:expect(gun, await,
367+
fun(_Pid, _, _) -> {response, nofin, 200, [{"content-type", "application/json"}]} end),
368+
meck:expect(gun, await_body,
369+
fun(_Pid, _, _) -> {ok, <<"{\"pass\": true}">>} end),
370+
371+
Expectation = {{ok, {[{"content-type", "application/json"}], [{"pass", true}]}}, State#state{gun_connections = #{"ec2.us-east-1.amazonaws.com:443" => pid}}},
360372
Result = rabbitmq_aws:perform_request(State, Service, Method, Headers, Path, Body, Options, Host),
361373
?assertEqual(Expectation, Result),
362-
meck:validate(httpc)
374+
meck:validate(gun)
363375
end},
364376
{
365377
"has_credentials false",
@@ -372,11 +384,9 @@ perform_request_test_() ->
372384
Body = "",
373385
Options = [],
374386
Host = undefined,
375-
meck:expect(httpc, request, fun(get, {_URI, _Headers}, _Options, []) -> {ok, {{"HTTP/1.0", 400, "RequestFailure"}, [{"content-type", "application/json"}], "{\"pass\": false}"}} end),
376387
Expectation = {{error, {credentials, State#state.error}}, State},
377388
Result = rabbitmq_aws:perform_request(State, Service, Method, Headers, Path, Body, Options, Host),
378-
?assertEqual(Expectation, Result),
379-
meck:validate(httpc)
389+
?assertEqual(Expectation, Result)
380390
end
381391
},
382392
{
@@ -447,9 +457,9 @@ api_get_request_test_() ->
447457
{
448458
foreach,
449459
fun () ->
450-
meck:new(httpc, []),
460+
meck:new(gun, []),
451461
meck:new(rabbitmq_aws_config, []),
452-
[httpc, rabbitmq_aws_config]
462+
[gun, rabbitmq_aws_config]
453463
end,
454464
fun meck:unload/1,
455465
[
@@ -459,14 +469,25 @@ api_get_request_test_() ->
459469
secret_access_key = "ExpiredAccessKey",
460470
region = "us-east-1",
461471
expiration = {{3016, 4, 1}, {12, 0, 0}}},
462-
meck:expect(httpc, request, 4, {ok, {{"HTTP/1.0", 200, "OK"}, [{"content-type", "application/json"}], "{\"data\": \"value\"}"}}),
472+
473+
meck:expect(gun, open, fun(_,_,_) -> {ok,pid} end),
474+
meck:expect(gun, close, fun(_) -> ok end),
475+
meck:expect(gun, await_up, fun(_,_) -> {ok,protocol} end),
476+
meck:expect(gun, get,
477+
fun(_Pid, _Path, _Headers) -> nofin end),
478+
meck:expect(gun, await,
479+
fun(_Pid, _, _) -> {response, nofin, 200, [{"content-type", "application/json"}]} end),
480+
meck:expect(gun, await_body,
481+
fun(_Pid, _, _) -> {ok, <<"{\"data\": \"value\"}">>} end),
482+
483+
463484
{ok, Pid} = rabbitmq_aws:start_link(),
464485
rabbitmq_aws:set_region("us-east-1"),
465486
rabbitmq_aws:set_credentials(State),
466487
Result = rabbitmq_aws:api_get_request("AWS", "API"),
467488
ok = gen_server:stop(Pid),
468489
?assertEqual({ok, [{"data","value"}]}, Result),
469-
meck:validate(httpc)
490+
meck:validate(gun)
470491
end
471492
},
472493
{"AWS service API request failed - credentials",
@@ -485,14 +506,22 @@ api_get_request_test_() ->
485506
secret_access_key = "ExpiredAccessKey",
486507
region = "us-east-1",
487508
expiration = {{3016, 4, 1}, {12, 0, 0}}},
488-
meck:expect(httpc, request, 4, {error, "network error"}),
509+
510+
meck:expect(gun, open, fun(_,_,_) -> {ok, spawn(fun() -> ok end)} end),
511+
meck:expect(gun, close, fun(_) -> ok end),
512+
meck:expect(gun, await_up, fun(_,_) -> {ok,protocol} end),
513+
meck:expect(gun, get,
514+
fun(_Pid, _Path, _Headers) -> nofin end),
515+
meck:expect(gun, await,
516+
fun(_Pid, _, _) -> {error, "network error"} end),
517+
489518
{ok, Pid} = rabbitmq_aws:start_link(),
490519
rabbitmq_aws:set_region("us-east-1"),
491520
rabbitmq_aws:set_credentials(State),
492521
Result = rabbitmq_aws:api_get_request_with_retries("AWS", "API", 3, 1),
493522
ok = gen_server:stop(Pid),
494523
?assertEqual({error, "AWS service is unavailable"}, Result),
495-
meck:validate(httpc)
524+
meck:validate(gun)
496525
end
497526
},
498527
{"AWS service API request succeeded after a transient error",
@@ -501,18 +530,28 @@ api_get_request_test_() ->
501530
secret_access_key = "ExpiredAccessKey",
502531
region = "us-east-1",
503532
expiration = {{3016, 4, 1}, {12, 0, 0}}},
504-
meck:expect(httpc, request, 4, meck:seq([
505-
{error, "network error"},
506-
{ok, {{"HTTP/1.0", 500, "OK"}, [{"content-type", "application/json"}], "{\"error\": \"server error\"}"}},
507-
{ok, {{"HTTP/1.0", 200, "OK"}, [{"content-type", "application/json"}], "{\"data\": \"value\"}"}}
508-
])),
533+
534+
meck:expect(gun, open, fun(_,_,_) -> {ok, spawn(fun() -> ok end)} end),
535+
meck:expect(gun, close, fun(_) -> ok end),
536+
meck:expect(gun, await_up, fun(_,_) -> {ok,protocol} end),
537+
meck:expect(gun, get,
538+
fun(_Pid, _Path, _Headers) -> nofin end),
539+
540+
%% meck:expect(gun, get, 3, meck:seq(
541+
%% fun(_Pid, _Path, _Headers) -> {error, "network errors"} end),
542+
meck:expect(gun, await, 3, meck:seq([{error, "network error"},
543+
{response, nofin, 500, [{"content-type", "application/json"}]},
544+
{response, nofin, 200, [{"content-type", "application/json"}]}])),
545+
546+
meck:expect(gun, await_body, 3, meck:seq([{ok, <<"{\"error\": \"server error\"}">>},
547+
{ok, <<"{\"data\": \"value\"}">>}])),
509548
{ok, Pid} = rabbitmq_aws:start_link(),
510549
rabbitmq_aws:set_region("us-east-1"),
511550
rabbitmq_aws:set_credentials(State),
512551
Result = rabbitmq_aws:api_get_request_with_retries("AWS", "API", 3, 1),
513552
ok = gen_server:stop(Pid),
514553
?assertEqual({ok, [{"data","value"}]}, Result),
515-
meck:validate(httpc)
554+
meck:validate(gun)
516555
end
517556
}
518557
]

0 commit comments

Comments
 (0)