From de8d2cc3c443dda7197ef2c898549a2c24b5c4bc Mon Sep 17 00:00:00 2001 From: Fabio Salvini Date: Mon, 3 Jul 2017 17:33:30 +0200 Subject: [PATCH] EUnit tests --- Makefile | 9 ++++++++ apps/log_monitor/src/gatherer.erl | 34 +++++++++++++++++++++++-------- apps/log_monitor/src/mailer.erl | 27 +++++++++++++++++++++++- apps/log_monitor/src/utils.erl | 16 +++++++++++++++ 4 files changed, 76 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 94ced5d..e246b01 100644 --- a/Makefile +++ b/Makefile @@ -2,12 +2,21 @@ REBAR=rebar3 all: release +prod: + ${REBAR} as prod tar + release: ${REBAR} release compile: ${REBAR} compile +code: + ${REBAR} dialyzer + +test: + ${REBAR} eunit + doc: ${REBAR} edoc diff --git a/apps/log_monitor/src/gatherer.erl b/apps/log_monitor/src/gatherer.erl index 5cb8bbe..afd91ff 100644 --- a/apps/log_monitor/src/gatherer.erl +++ b/apps/log_monitor/src/gatherer.erl @@ -10,6 +10,10 @@ -behaviour(gen_fsm). +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). +-endif. + %% API -export([start_link/2]). @@ -19,8 +23,6 @@ -export([state_off/2, state_on/2]). -define(SERVER, ?MODULE). --define(DEFAULT_TIMER_TIME, 1000). --define(DEFAULT_SAFE_TIMER_TIME, 30000). -record(log, {file, error_regex}). -record(state_off, {log}). @@ -80,7 +82,7 @@ init([File, ErrorRegex]) -> %% @end %%-------------------------------------------------------------------- state_off({log_line, Text}, State = #state_off{log = Log}) -> - case isError(Text, Log#log.error_regex) of + case is_error(Text, Log#log.error_regex) of true -> Timeout = gathering_time(), MaxTimeout = max_gathering_time(), @@ -110,7 +112,7 @@ state_off({log_line, Text}, State = #state_off{log = Log}) -> %% @end %%-------------------------------------------------------------------- state_on({log_line, Text}, #state_on{log = Log, error = Error, until = Until, max_until = MaxUntil}) -> - case isError(Text, Log#log.error_regex) of + case is_error(Text, Log#log.error_regex) of true -> Timeout = gathering_time(), Now = to_milliseconds(os:timestamp()), @@ -212,7 +214,7 @@ code_change(_OldVsn, StateName, State, _Extra) -> %%%=================================================================== %%% Internal functions %%%=================================================================== -isError(Text, ErrorRegex) -> +is_error(Text, ErrorRegex) -> case re:run(Text, ErrorRegex) of {match, _} -> true; @@ -221,12 +223,26 @@ isError(Text, ErrorRegex) -> end. gathering_time() -> - {ok, ProcessingConfig} = application:get_env(log_monitor, general), - proplists:get_value(gathering_time, ProcessingConfig, ?DEFAULT_TIMER_TIME). + {ok, GeneralConfig} = application:get_env(log_monitor, general), + proplists:get_value(gathering_time, GeneralConfig). max_gathering_time() -> - {ok, ProcessingConfig} = application:get_env(log_monitor, general), - proplists:get_value(max_gathering_time, ProcessingConfig, ?DEFAULT_TIMER_TIME). + {ok, GeneralConfig} = application:get_env(log_monitor, general), + proplists:get_value(max_gathering_time, GeneralConfig). to_milliseconds({Me, S, Mu}) -> (Me * 1000 * 1000 * 1000) + (S * 1000) + (Mu div 1000). + +%%%=================================================================== +%%% Tests +%%%=================================================================== +-ifdef(TEST). + +start_test() -> + {ok, _Pid} = start_link("/tmp/test.log", "ERROR"). + +is_error_test() -> + ?assert(not is_error("2017-01-01 MyApp - WARN: abc", "ERROR")), + ?assert(is_error("2017-01-01 MyApp - ERROR: abc", "ERROR")). + +-endif. diff --git a/apps/log_monitor/src/mailer.erl b/apps/log_monitor/src/mailer.erl index 39f7a40..bf843e3 100644 --- a/apps/log_monitor/src/mailer.erl +++ b/apps/log_monitor/src/mailer.erl @@ -11,6 +11,9 @@ -behaviour(gen_server). -include_lib("mnesia_tables.hrl"). +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). +-endif. %% API -export([start_link/0]). @@ -219,5 +222,27 @@ email_subject(Text, File) -> re:replace(Text1, "%F", file_name_from_path(File), [global, {return, list}]). file_name_from_path(File) -> - {match, [_, Basename]} = re:run(File, "^.*\/(.*)\\..*$", [{capture, all, list}]), + {match, [_, _, Basename]} = re:run(File, "^(.*\/)?(.*)\\..*$", [{capture, all, list}]), Basename. + +%%%=================================================================== +%%% Tests +%%%=================================================================== +-ifdef(TEST). + +start_test() -> + {ok, _Pid} = start_link(). + +email_subject_test() -> + ?assertEqual("Error", email_subject("Error", "/var/log/myApp.log")), + ?assertEqual( + "[myApp] Error: /var/log/myApp.log", + email_subject("[%F] Error: %f", "/var/log/myApp.log") + ). + +file_name_from_path_test() -> + ?assertEqual("myApp", file_name_from_path("myApp.log")), + ?assertEqual("myApp", file_name_from_path("./myApp.log")), + ?assertEqual("myApp", file_name_from_path("/var/log/myApp.log")). + +-endif. diff --git a/apps/log_monitor/src/utils.erl b/apps/log_monitor/src/utils.erl index 3daa7ac..8fe114a 100644 --- a/apps/log_monitor/src/utils.erl +++ b/apps/log_monitor/src/utils.erl @@ -8,6 +8,10 @@ %%%------------------------------------------------------------------- -module(utils). +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). +-endif. + %% API -export([flatten/1]). @@ -31,3 +35,15 @@ flatten([],Acc) -> Acc; flatten([[]|T],Acc) -> flatten(T, Acc); flatten([[_|_]=H|T],Acc) -> flatten(T, flatten(H,Acc)); flatten([H|T],Acc) -> flatten(T,Acc++[H]) . + +%%%=================================================================== +%%% Tests +%%%=================================================================== +-ifdef(TEST). + +flatten_test() -> + ?assertEqual([], flatten([])), + ?assertEqual([1,2,3], flatten([1,2,3])), + ?assertEqual([1,2,3], flatten([[1],[2,3]])). + +-endif.