Added test module

This commit is contained in:
Fabio Salvini 2017-07-02 20:14:31 +02:00
parent d0e8d3eba6
commit e54539a103
2 changed files with 56 additions and 21 deletions

View File

@ -14,7 +14,6 @@
%% API
-export([start_link/0]).
-export([test/0]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
@ -36,26 +35,6 @@
start_link() ->
gen_server:start_link(?MODULE, [], []).
%%--------------------------------------------------------------------
%% @doc
%% Send an email to test the configuration.
%%
%% @spec test() -> binary() | {error, Type, Message}
%% @end
%%--------------------------------------------------------------------
test() ->
{ok, EmailConfig} = application:get_env(log_monitor, email_config),
Sender = proplists:get_value(sender, EmailConfig),
AdminReceiver = proplists:get_value(admin_receiver, EmailConfig),
Connection = proplists:get_value(connection, EmailConfig),
Subject = "Test",
error_logger:info_msg("Sender: ~s, Receiver: ~s, Subject: ~s~n", [Sender, AdminReceiver, Subject]),
gen_smtp_client:send_blocking(
{Sender, [AdminReceiver],
io_lib:format("Subject: ~s\r\nFrom: ~s\r\n\r\nTest~n", [Subject, Sender])},
Connection
).
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================

View File

@ -0,0 +1,56 @@
%%%-------------------------------------------------------------------
%%% @author Fabio Salvini <fs@fabiosalvini.com>
%%% @copyright (C) 2017, Fabio Salvini
%%% @doc
%%%
%%% @end
%%% Created : 2 Jul 2017 by Fabio Salvini <fs@fabiosalvini.com>
%%%-------------------------------------------------------------------
-module(test).
%% API
-export([email/0]).
-export([regex/2]).
%%%===================================================================
%%% API
%%%===================================================================
%%--------------------------------------------------------------------
%% @doc
%% Send an email to test the configuration.
%%
%% @spec email -> binary() | {error, Type, Message}
%% @end
%%--------------------------------------------------------------------
email() ->
{ok, EmailConfig} = application:get_env(log_monitor, email_config),
Sender = proplists:get_value(sender, EmailConfig),
AdminReceiver = proplists:get_value(admin_receiver, EmailConfig),
Connection = proplists:get_value(connection, EmailConfig),
Subject = "Test",
error_logger:info_msg("Sender: ~s, Receiver: ~s, Subject: ~s~n", [Sender, AdminReceiver, Subject]),
gen_smtp_client:send_blocking(
{Sender, [AdminReceiver],
io_lib:format("Subject: ~s\r\nFrom: ~s\r\n\r\nTest~n", [Subject, Sender])},
Connection
).
%%--------------------------------------------------------------------
%% @doc
%% Check if the regex match the line.
%%
%% @spec regex(Regex, Line) -> match | nomatch
%% @end
%%--------------------------------------------------------------------
regex(Regex, Line) ->
case re:run(Line, Regex) of
{match, _} ->
match;
nomatch ->
nomatch
end.
%%%===================================================================
%%% Internal functions
%%%===================================================================