Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move all path related functions into path_helper in big tests #1760

Merged
merged 5 commits into from
Mar 12, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions test.disabled/ejabberd_tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ PREPARE ?= prepare
ADD_OPTS ?= -sasl sasl_error_logger false -lager handlers []
TLS_DIST ?= no
REPO_DIR ?= ../..
TEST_DIR ?= .

# autogenerated, not intended for overriding
DEPS_PATHS := $(addprefix $(REPO_DIR)/_build/test/lib/,$(addsuffix /ebin,$(DEPS)))
Expand All @@ -20,6 +21,8 @@ endif

COMMON_OPTS := -sname test -setcookie ejabberd -hidden \
$(TLS_DIST_OPTS) \
-env REPO_DIR "$(shell $(REPO_DIR)/tools/abs_dirpath.sh $(REPO_DIR))" \
-env TEST_DIR "$(shell $(REPO_DIR)/tools/abs_dirpath.sh $(TEST_DIR))" \
-pa `pwd`/tests \
`pwd`/ebin \
`pwd`/deps/*/ebin \
Expand Down
17 changes: 12 additions & 5 deletions test.disabled/ejabberd_tests/run_common_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

-define(CT_DIR, filename:join([".", "tests"])).
-define(CT_REPORT, filename:join([".", "ct_report"])).
-define(ROOT_DIR, "../../").

%% DEBUG: compile time settings
-define(PRINT_ERRORS, false).
Expand Down Expand Up @@ -63,6 +62,14 @@ run(#opts{test = full, spec = Spec, preset = Preset, cover = Cover}) ->
%% Helpers
%%

repo_dir() ->
case os:getenv("REPO_DIR") of
false ->
init:stop("Environment variable REPO_DIR is undefined");
Value ->
Value
end.

args_to_opts(Args) ->
{Args, Opts} = lists:foldl(fun set_opt/2, {Args, #opts{}}, opts()),
Opts.
Expand Down Expand Up @@ -186,8 +193,8 @@ backend(Node) ->

enable_preset_on_node(Node, PresetVars, HostVars) ->
{ok, Cwd} = call(Node, file, get_cwd, []),
Cfg = filename:join(["..", "..", "rel", "files", "ejabberd.cfg"]),
Vars = filename:join(["..", "..", "rel", HostVars]),
Cfg = filename:join([repo_dir(), "rel", "files", "ejabberd.cfg"]),
Vars = filename:join([repo_dir(), "rel", HostVars]),
CfgFile = filename:join([Cwd, "etc", "ejabberd.cfg"]),
{ok, Template} = file:read_file(Cfg),
{ok, Default} = file:consult(Vars),
Expand Down Expand Up @@ -248,7 +255,7 @@ analyze(Test, CoverOpts) ->
report_time("Export cover data from MongooseIM nodes", fun() ->
multicall(Nodes, mongoose_cover_helper, analyze, [], cover_timeout())
end),
Files = filelib:wildcard(?ROOT_DIR ++ "/_build/**/cover/*.coverdata"),
Files = filelib:wildcard(repo_dir() ++ "/_build/**/cover/*.coverdata"),
io:format("Files: ~p", [Files]),
report_time("Import cover data into run_common_test node", fun() ->
[cover:import(File) || File <- Files]
Expand Down Expand Up @@ -481,7 +488,7 @@ export_codecov_json() ->
Mod2Data = lists:foldl(fun add_cover_line_into_array/2, #{}, Result),
JSON = maps:fold(fun format_array_to_list/3, [], Mod2Data),
Binary = jiffy:encode(#{<<"coverage">> => {JSON}}),
file:write_file(?ROOT_DIR ++ "/codecov.json", Binary).
file:write_file(repo_dir() ++ "/codecov.json", Binary).

add_cover_line_into_array({{Module, Line}, CallTimes}, Acc) ->
%% Set missing lines to null
Expand Down
5 changes: 2 additions & 3 deletions test.disabled/ejabberd_tests/src/ct_mongoose_log_hook.erl
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,12 @@ make_content(CurrentLineNum, Line) ->

ensure_initialized(Config, State=#state{node=Node, cookie=Cookie, out_file=undefined}) ->
PrivDir = proplists:get_value(priv_dir, Config),
%% Remove: *SUITE.logs/run.*/log_private/
RunDir = filename:absname(filename:join([PrivDir, "..", "..", ".."])),
RunDir = path_helper:ct_run_dir(Config),
File = atom_to_list(Node) ++ ".log.html",
%% On disk
OutFile = filename:join(RunDir, File),
%% In browser
UrlFile = ct_logs:uri(filename:join(["..", "..", File])),
UrlFile = ct_logs:uri(filename:join(path_helper:ct_run_dir_in_browser(Config), File)),
case spawn_log_reader(Node, Cookie) of
{ok, Reader} ->
%% self() process is temporary
Expand Down
20 changes: 13 additions & 7 deletions test.disabled/ejabberd_tests/tests/ejabberd_node_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
-module(ejabberd_node_utils).

-export([init/1, init/2,
node_cwd/2,
restart_application/1, restart_application/2,
call_fun/3, call_fun/4,
call_ctl/2, call_ctl/3,
Expand All @@ -38,11 +39,11 @@ current_config_path(Node, Config) ->
backup_config_path(Node, Config) ->
filename:join([cwd(Node, Config), "etc","ejabberd.cfg.bak"]).

config_template_path() ->
filename:join(["..", "..", "..", "..", "rel", "files", "ejabberd.cfg"]).
config_template_path(Config) ->
filename:join([path_helper:repo_dir(Config), "rel", "files", "ejabberd.cfg"]).

config_vars_path(File) ->
filename:join(["..", "..", "..", "..", "rel", File]).
config_vars_path(File, Config) ->
filename:join([path_helper:repo_dir(Config), "rel", File]).

ctl_path(Node, Config) ->
filename:join([cwd(Node, Config), "bin", "mongooseimctl"]).
Expand All @@ -61,6 +62,11 @@ init(Config) ->
init(Node, Config) ->
set_ejabberd_node_cwd(Node, Config).

node_cwd(Node, Config) ->
CWD = escalus_config:get_config({ejabberd_cwd, Node}, Config),
CWD == undefined andalso error({{ejabberd_cwd, Node}, undefined}, [Node, Config]),
CWD.

-spec restart_application(atom()) -> ok.
restart_application(ApplicationName) ->
Node = ct:get_config({hosts, mim, node}),
Expand Down Expand Up @@ -149,10 +155,10 @@ modify_config_file(CfgVarsToChange, Config) ->
Value :: string().
modify_config_file(Node, VarsFile, CfgVarsToChange, Config) ->
CurrentCfgPath = current_config_path(Node, Config),
{ok, CfgTemplate} = file:read_file(config_template_path()),
CfgVarsPath = config_vars_path("vars.config"),
{ok, CfgTemplate} = file:read_file(config_template_path(Config)),
CfgVarsPath = config_vars_path("vars.config", Config),
{ok, DefaultVars} = file:consult(CfgVarsPath),
{ok, NodeVars} = file:consult(config_vars_path(VarsFile)),
{ok, NodeVars} = file:consult(config_vars_path(VarsFile, Config)),
PresetVars = case proplists:get_value(preset, Config) of
undefined ->
[];
Expand Down
16 changes: 5 additions & 11 deletions test.disabled/ejabberd_tests/tests/mod_global_distrib_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ init_per_suite(Config) ->
{{ok, _}, {ok, _}} ->
ok = rpc(europe_node2, mongoose_cluster, join, [ct:get_config(europe_node1)]),

CertDir = filename:join(?config(data_dir, Config), "../../priv/ssl"),
CertPath = canonicalize_path(filename:join(CertDir, "fake_cert.pem")),
CACertPath = canonicalize_path(filename:join(CertDir, "cacert.pem")),
escalus:init_per_suite([{add_advertised_endpoints, []}, {certfile, CertPath}, {cafile, CACertPath},
CertDir = filename:join(path_helper:test_dir(Config), "priv/ssl"),
CertPath = path_helper:canonicalize_path(filename:join(CertDir, "fake_cert.pem")),
CACertPath = path_helper:canonicalize_path(filename:join(CertDir, "cacert.pem")),
escalus:init_per_suite([{add_advertised_endpoints, []},
{certfile, CertPath}, {cafile, CACertPath},
{extra_config, []}, {redis_extra_config, []} | Config]);
_ ->
{skip, "Cannot connect to Redis server on 127.0.0.1 6379"}
Expand Down Expand Up @@ -837,13 +838,6 @@ has_exactly_one_service(Service, #xmlel{children = [#xmlel{children = Services}]
_ -> false
end.

canonicalize_path(Path) -> canonicalize_path(filename:split(Path), []).

canonicalize_path([], Acc) -> filename:join(lists:reverse(Acc));
canonicalize_path([".." | Path], [_ | Acc]) -> canonicalize_path(Path, Acc);
canonicalize_path(["." | Path], Acc) -> canonicalize_path(Path, Acc);
canonicalize_path([Elem | Path], Acc) -> canonicalize_path(Path, [Elem | Acc]).

send_steps(From, To, Max, ToHost) ->
next_send_step(From, To, 1, Max, Max div 10, true, ToHost).

Expand Down
47 changes: 47 additions & 0 deletions test.disabled/ejabberd_tests/tests/path_helper.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
%% @doc Common filename functions
-module(path_helper).
%% Paths
-export([repo_dir/1]).
-export([test_dir/1]).
-export([ct_run_dir/1]).
-export([ct_run_dir_in_browser/1]).

%% Path transformation
-export([canonicalize_path/1]).

%% @doc Get repository root directory
repo_dir(Config) ->
get_env_var("REPO_DIR").

%% @doc Get `test.disabled/ejabberd_tests/' directory
test_dir(Config) ->
get_env_var("TEST_DIR").

%% @doc Returns`test.disabled/ejabberd_tests/ct_report/ct_run.*' directory
%% Run it from a test case functions only (not group or suite functions)
ct_run_dir(Config) ->
PrivDir = proplists:get_value(priv_dir, Config),
%% Remove: *SUITE.logs/run.*/log_private/
RunDir = path_helper:test_dir(Config),
filename:absname(filename:join([PrivDir, "..", "..", ".."])).

%% @doc Returns path, corresponding to `ct_run_dir' in browser
ct_run_dir_in_browser(Config) ->
"../..".

%% @doc Unsafe version of `filename:safe_relative_path/1'
canonicalize_path(Path) -> canonicalize_path(filename:split(Path), []).

canonicalize_path([], Acc) -> filename:join(lists:reverse(Acc));
canonicalize_path([".." | Path], [_ | Acc]) -> canonicalize_path(Path, Acc);
canonicalize_path(["." | Path], Acc) -> canonicalize_path(Path, Acc);
canonicalize_path([Elem | Path], Acc) -> canonicalize_path(Path, [Elem | Acc]).


get_env_var(VarName) ->
case os:getenv(VarName) of
false ->
ct:fail({undefined_envvar, VarName});
Value ->
Value
end.
24 changes: 11 additions & 13 deletions test.disabled/ejabberd_tests/tests/reload_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,14 @@ update_config_variables(CfgVarsToChange, CfgVars) ->
lists:keystore(Var, 1, Acc,{Var, Val})
end, CfgVars, CfgVarsToChange).

node_cfg(N, current, C) -> flat([node_cwd(N, C), "etc", "ejabberd.cfg"]);
node_cfg(N, backup, C) -> flat([node_cwd(N, C), "etc", "ejabberd.cfg.bak"]);
node_cfg(N, template, C) -> flat([node_cwd(N, C), "..", "..", "..", "..", "rel", "files", "ejabberd.cfg"]);
node_cfg(N, vars, C) -> flat([node_cwd(N, C), "..", "..", "..", "..", "rel", "vars.config"]).

node_ctl(N, C) -> flat([node_cwd(N, C), "bin", "mongooseimctl"]).

flat(PathComponents) -> filename:join(PathComponents).

node_cwd(Node, Config) ->
CWD = escalus_config:get_config({ejabberd_cwd, Node}, Config),
CWD == undefined andalso error({{ejabberd_cwd, Node}, undefined}, [Node, Config]),
CWD.
node_cfg(N, current, C) ->
filename:join(ejabberd_node_utils:node_cwd(N, C), "etc/ejabberd.cfg");
node_cfg(N, backup, C) ->
filename:join(ejabberd_node_utils:node_cwd(N, C), "etc/ejabberd.cfg.bak");
node_cfg(_N, template, C) ->
filename:join(path_helper:repo_dir(C), "rel/files/ejabberd.cfg");
node_cfg(N, vars, C) ->
filename:join(path_helper:repo_dir(C), "rel/vars.config").

node_ctl(N, C) ->
filename:join(ejabberd_node_utils:node_cwd(N, C), "bin/mongooseimctl").
22 changes: 22 additions & 0 deletions tools/abs_dirpath.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash
# Converts path into an absolute path
# Similar to "readlink -f" or realpath.
# Accepts directories as an argument only.
#
# Example 1:
# $ pwd
# /home/user/erlang/esl/2017/MongooseIM
# $ ./tools/abs_dirpath.sh ../
# /home/user/erlang/esl/2017
#
# Example 2:
# $ ./tools/abs_dirpath.sh /home/user/erlang/esl/2017
# /home/user/erlang/esl/2017
set -e

if [ "$#" -ne 1 ]; then
exit "Illegal number of parameters"
fi

DIR=$(echo "${1%/*}")
(cd "$DIR" && echo "$(pwd -P)")