From 8ef6eeea5ff52fc8de290b1c0d135ad350a9a3e1 Mon Sep 17 00:00:00 2001 From: Jacob Vorreuter Date: Sun, 19 Dec 2010 20:45:39 -0800 Subject: [PATCH] add more tests --- src/logplex_api.erl | 10 +++++----- src/logplex_drain.erl | 2 +- test/logplex_api_tests.erl | 36 ++++++++++++++++++++++++++++++++---- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/logplex_api.erl b/src/logplex_api.erl index 146a051e..43f56847 100644 --- a/src/logplex_api.erl +++ b/src/logplex_api.erl @@ -137,7 +137,7 @@ handlers() -> case logplex_channel:update_addon(list_to_integer(ChannelId), Addon) of ok -> {200, <<"OK">>}; - {error, not_found} -> {404, <<"not found">>} + {error, not_found} -> {404, <<"Not found">>} end end}, @@ -145,7 +145,7 @@ handlers() -> authorize(Req), case logplex_channel:delete(list_to_integer(ChannelId)) of ok -> {200, <<"OK">>}; - {error, not_found} -> {404, <<"not found">>} + {error, not_found} -> {404, <<"Not found">>} end end}, @@ -172,7 +172,7 @@ handlers() -> {['GET', "/sessions/([\\w-]+)$"], fun(Req, [Session]) -> Body = logplex_session:lookup(list_to_binary("/sessions/" ++ Session)), - not is_binary(Body) andalso error_resp(404, <<"not found">>), + not is_binary(Body) andalso error_resp(404, <<"Not found">>), {struct, Data} = mochijson2:decode(Body), ChannelId0 = proplists:get_value(<<"channel_id">>, Data), @@ -259,12 +259,12 @@ handlers() -> case logplex_drain:delete(list_to_integer(ChannelId), list_to_binary(Host), Port) of ok -> {200, <<"OK">>}; - {error, not_found} -> {404, <<"not found">>} + {error, not_found} -> {404, <<"Not found">>} end end}]. serve([], _Method, _Path, _Req) -> - {404, <<"Not Found.">>}; + {404, <<"Not found">>}; serve([{[HMethod, Regexp], Fun}|Tail], Method, Path, Req) -> case re:run(Path, Regexp, [{capture, all_but_first, list}]) of diff --git a/src/logplex_drain.erl b/src/logplex_drain.erl index 5d92f9f5..a63b315e 100644 --- a/src/logplex_drain.erl +++ b/src/logplex_drain.erl @@ -36,7 +36,7 @@ start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). create(ChannelId, Host, Port) when is_integer(ChannelId), is_binary(Host), (is_integer(Port) orelse Port == undefined) -> - case ets:match_object(?MODULE, #drain{id='_', channel_id=ChannelId, host=Host, port=Port}) of + case ets:match_object(?MODULE, #drain{id='_', channel_id=ChannelId, resolved_host='_', host=Host, port=Port}) of [_] -> {error, already_exists}; [] -> diff --git a/test/logplex_api_tests.erl b/test/logplex_api_tests.erl index 630fb733..48535e7d 100644 --- a/test/logplex_api_tests.erl +++ b/test/logplex_api_tests.erl @@ -72,6 +72,12 @@ logplex_api_test_() -> {"http://localhost:8002/channels/" ++ get(channel_id) ++ "/addon", headers(), content_type(), iolist_to_binary(mochijson2:encode({struct, [ {"addon", <<"advanced">>} ]}))}, [], []))}, + {"Upgrade addon for non-existent channel", + ?_assertMatch({ok,{{_,404,_},_,"Not found"}}, + http:request(post, + {"http://localhost:8002/channels/0/addon", headers(), content_type(), iolist_to_binary(mochijson2:encode({struct, [ + {"addon", <<"advanced">>} + ]}))}, [], []))}, {"Create session", fun() -> Ret = http:request(post, @@ -90,6 +96,20 @@ logplex_api_test_() -> {"host", <<"10.0.0.1">>}, {"port", 514} ]}))}, [], []))}, + {"Create duplicate drain", + ?_assertMatch({ok,{{_,400,_},_,"Drain already exists"}}, + http:request(post, + {"http://localhost:8002/channels/" ++ get(channel_id) ++ "/drains", headers(), content_type(), iolist_to_binary(mochijson2:encode({struct, [ + {"host", <<"10.0.0.1">>}, + {"port", 514} + ]}))}, [], []))}, + {"Create invalid drain", + ?_assertMatch({ok,{{_,400,_},_,"Invalid drain"}}, + http:request(post, + {"http://localhost:8002/channels/" ++ get(channel_id) ++ "/drains", headers(), content_type(), iolist_to_binary(mochijson2:encode({struct, [ + {"host", <<"donkey">>}, + {"port", 514} + ]}))}, [], []))}, {"Get info", fun() -> Ret = http:request(get, {"http://localhost:8002/channels/" ++ get(channel_id) ++ "/info", headers()}, [], []), @@ -221,12 +241,20 @@ logplex_api_test_() -> {"Delete drain", ?_assertMatch({ok,{{_,200,_},_,_}}, http:request(delete, {"http://localhost:8002/channels/" ++ get(channel_id) ++ "/drains?host=10.0.0.1&port=514", headers()}, [], []))}, + {"Delete non-existent drain", + ?_assertMatch({ok,{{_,404,_},_,"Not found"}}, + http:request(delete, {"http://localhost:8002/channels/" ++ get(channel_id) ++ "/drains?host=10.0.0.1&port=514", headers()}, [], []))}, {"Delete channel", ?_assertMatch({ok,{{_,200,_},_,_}}, - http:request(delete, {"http://localhost:8002/channels/" ++ get(channel_id), headers()}, [], []))} - - %% test healthcheck 'Zero child processes running' - %% test healthcheck 'Process dead' + http:request(delete, {"http://localhost:8002/channels/" ++ get(channel_id), headers()}, [], []))}, + {"Delete non-existent channel", + ?_assertMatch({ok,{{_,404,_},_,"Not found"}}, + http:request(delete, {"http://localhost:8002/channels/" ++ get(channel_id), headers()}, [], []))}, + {"Verify healthcheck 'Zero child processes running'", + fun() -> + [logplex_queue:stop(Pid) || {_,Pid,_,_} <- supervisor:which_children(logplex_read_queue_sup)], + ?assertMatch({ok,{{_,500,_},_,"Zero logplex_read_queue_sup child processes running"}}, http:request(get, {"http://localhost:8002/healthcheck", headers()}, [], [])) + end} ] }.