Просмотр исходного кода

Erlang backward compatible fixes

Kozlov Yakov 8 лет назад
Родитель
Сommit
f9bc828245
4 измененных файлов с 95 добавлено и 46 удалено
  1. 30 18
      test/epgsql_SUITE.erl
  2. 9 2
      test/epgsql_ct.erl
  3. 54 25
      test/epgsql_cth.erl
  4. 2 1
      test/epgsql_perf_SUITE.erl

+ 30 - 18
test/epgsql_SUITE.erl

@@ -6,7 +6,6 @@
 -include("epgsql_tests.hrl").
 -include("epgsql.hrl").
 
-
 -export([
     init_per_suite/1,
     init_per_group/2,
@@ -31,6 +30,14 @@ init_per_suite(Config) ->
 all() ->
     [{group, M} || M <- modules()].
 
+-ifdef(have_maps).
+-define(MAPS_TESTS, [
+    connect_map
+]).
+-else.
+-define(MAPS_TESTS, []).
+-endif.
+
 groups() ->
     Groups = [
         {connect, [parrallel], [
@@ -43,6 +50,7 @@ groups() ->
             connect_with_invalid_password,
             connect_with_ssl,
             connect_with_client_cert
+            | ?MAPS_TESTS
         ]},
         {types, [parallel], [
             numeric_type,
@@ -179,7 +187,7 @@ connect_with_md5(Config) ->
     ]).
 
 connect_with_invalid_user(Config) ->
-    #{pg_port := Port, pg_host := Host} = ?config(pg_config, Config),
+    {Host, Port} = epgsql_ct:connection_data(Config),
     Module = ?config(module, Config),
     {error, Why} = Module:connect(
         Host,
@@ -192,7 +200,7 @@ connect_with_invalid_user(Config) ->
     end.
 
 connect_with_invalid_password(Config) ->
-    #{pg_port := Port, pg_host := Host} = ?config(pg_config, Config),
+    {Host, Port} = epgsql_ct:connection_data(Config),
     Module = ?config(module, Config),
     {error, Why} = Module:connect(
         Host,
@@ -231,19 +239,23 @@ connect_with_client_cert(Config) ->
          "epgsql_test_cert",
         [{ssl, true}, {keyfile, File("epgsql.key")}, {certfile, File("epgsql.crt")}]).
 
-%% -ifdef(have_maps).
-%% connect_map_test(Module) ->
-%%     Opts = #{host => ?host,
-%%              port => ?port,
-%%              database => "epgsql_test_db1",
-%%              username => "epgsql_test_md5",
-%%              password => "epgsql_test_md5"
-%%             },
-%%     {ok, C} = Module:connect(Opts),
-%%     Module:close(C),
-%%     epgsql_ct:flush(),
-%%     ok.
-%% -endif.
+-ifdef(have_maps).
+connect_map(Config) ->
+    {Host, Port} = epgsql_ct:connection_data(Config),
+    Module = ?config(module, Config),
+
+    Opts = #{
+        host => Host,
+        port => Port,
+        database => "epgsql_test_db1",
+        username => "epgsql_test_md5",
+        password => "epgsql_test_md5"
+    },
+    {ok, C} = Module:connect(Opts),
+    Module:close(C),
+    epgsql_ct:flush(),
+    ok.
+-endif.
 
 prepared_query(Config) ->
     Module = ?config(module, Config),
@@ -821,8 +833,8 @@ execute_timeout(Config) ->
     end, []).
 
 connection_closed(Config) ->
+    {Host, Port} = epgsql_ct:connection_data(Config),
     Module = ?config(module, Config),
-    #{pg_host := Host, pg_port := Port} = ?config(pg_config, Config),
     P = self(),
     spawn_link(fun() ->
         process_flag(trap_exit, true),
@@ -867,8 +879,8 @@ connection_closed_by_server(Config) ->
     end).
 
 active_connection_closed(Config) ->
+    {Host, Port} = epgsql_ct:connection_data(Config),
     Module = ?config(module, Config),
-    #{pg_host := Host, pg_port := Port} = ?config(pg_config, Config),
     P = self(),
     F = fun() ->
           process_flag(trap_exit, true),

+ 9 - 2
test/epgsql_ct.erl

@@ -3,6 +3,7 @@
 -include_lib("eunit/include/eunit.hrl").
 
 -export([
+    connection_data/1,
     connect_only/2,
     with_connection/2,
     with_connection/3,
@@ -12,8 +13,14 @@
     flush/0
 ]).
 
+connection_data(Config) ->
+    PgConfig = ?config(pg_config, Config),
+    Host = ?config(host, PgConfig),
+    Port = ?config(port, PgConfig),
+    {Host, Port}.
+
 connect_only(Config, Args) ->
-    #{pg_host := Host, pg_port := Port} = ?config(pg_config, Config),
+    {Host, Port} = connection_data(Config),
     Module = ?config(module, Config),
     TestOpts = [{port, Port}],
     case Args of
@@ -32,7 +39,7 @@ with_connection(Config, F, Args) ->
     with_connection(Config, F, "epgsql_test", Args).
 
 with_connection(Config, F, Username, Args) ->
-    #{pg_host := Host, pg_port := Port} = ?config(pg_config, Config),
+    {Host, Port} = connection_data(Config),
     Module = ?config(module, Config),
     Args2 = [{port, Port}, {database, "epgsql_test_db1"} | Args],
     {ok, C} = Module:connect(Host, Username, Args2),

+ 54 - 25
test/epgsql_cth.erl

@@ -23,8 +23,14 @@ pre_init_per_suite(_SuiteName, Config, State) ->
 terminate(State) ->
     ok = stop_postgres(?config(pg_config, State)).
 
-create_testdbs(#{pg_host := PgHost, pg_port := PgPort, pg_user := PgUser,
-                 utils := #{psql := Psql, createdb := CreateDB}}) ->
+create_testdbs(Config) ->
+    PgHost = ?config(host, Config),
+    PgPort = ?config(port, Config),
+    PgUser = ?config(user, Config),
+    Utils = ?config(utils, Config),
+    Psql = ?config(psql, Utils),
+    CreateDB = ?config(createdb, Utils),
+
     Opts = lists:concat([" -h ", PgHost, " -p ", PgPort, " "]),
     Cmds = [
         [CreateDB, Opts, PgUser],
@@ -41,7 +47,7 @@ create_testdbs(#{pg_host := PgHost, pg_port := PgPort, pg_user := PgUser,
 -define(PG_TIMEOUT, 30000).
 
 start_postgres() ->
-    {ok, _} = application:ensure_all_started(erlexec),
+    ok = application:start(erlexec),
     pipe([
         fun find_utils/1,
         fun init_database/1,
@@ -49,19 +55,21 @@ start_postgres() ->
         fun copy_certs/1,
         fun write_pg_hba_config/1,
         fun start_postgresql/1
-    ], #{}).
+    ], []).
+
+stop_postgres(Config) ->
+    PgProc = ?config(proc, Config),
 
-stop_postgres(#{pg_proc := PgProc}) ->
     PgProc ! stop,
     ok.
 
 find_utils(State) ->
     Utils = [initdb, createdb, postgres, psql],
-    UtilsMap = lists:foldl(fun(U, Map) ->
+    UtilsConfig = lists:foldl(fun(U, Acc) ->
         UList = atom_to_list(U),
         Path = case os:find_executable(UList) of
             false ->
-                case filelib:wildcard("/usr/lib/postgresql/**/" ++ UList) of
+                case filelib:wildcard("/usr/lib/postgresql/**/bin/" ++ UList) of
                     [] ->
                         error_logger:error_msg("~s not found", [U]),
                         throw({util_no_found, U});
@@ -69,11 +77,15 @@ find_utils(State) ->
                 end;
             P -> P
         end,
-        maps:put(U, Path, Map)
-    end, #{}, Utils),
-    State#{utils => UtilsMap}.
+        [{U, Path}|Acc]
+    end, [], Utils),
+    [{utils, UtilsConfig}|State].
+
+start_postgresql(Config) ->
+    PgDataDir = ?config(datadir, Config),
+    Utils = ?config(utils, Config),
+    Postgres = ?config(postgres, Utils),
 
-start_postgresql(#{pg_datadir := PgDataDir, utils := #{postgres := Postgres}} = Config) ->
     PgHost = "localhost",
     PgPort = get_free_port(),
     SocketDir = "/tmp",
@@ -87,17 +99,25 @@ start_postgresql(#{pg_datadir := PgDataDir, utils := #{postgres := Postgres}} =
               fun(_, _, Msg) ->
                   error_logger:info_msg("postgres: ~s", [Msg])
               end}]),
-        (fun Loop() ->
-             receive
-                 stop -> exec:kill(I, 0);
-                 _ -> Loop()
-             end
-        end)()
+        loop(I)
     end),
-    ConfigR = Config#{pg_host => PgHost, pg_port => PgPort, pg_proc => Pid},
+    ConfigR = [
+        {host, PgHost},
+        {port, PgPort},
+        {proc, Pid}
+        | Config
+    ],
     wait_postgresql_ready(SocketDir, ConfigR).
 
-wait_postgresql_ready(SocketDir, #{pg_port := PgPort} = Config) ->
+loop(I) ->
+    receive
+        stop -> exec:kill(I, 0);
+        _ -> loop(I)
+    end.
+
+wait_postgresql_ready(SocketDir, Config) ->
+    PgPort = ?config(port, Config),
+
     PgFile = lists:concat([".s.PGSQL.", PgPort]),
     Path = filename:join(SocketDir, PgFile),
     WaitUntil = ts_add(os:timestamp(), ?PG_TIMEOUT),
@@ -119,13 +139,18 @@ wait_(Path, Until) ->
         _ -> true
     end.
 
-init_database(#{utils := #{initdb := Initdb}}=Config) ->
+init_database(Config) ->
+    Utils = ?config(utils, Config),
+    Initdb = ?config(initdb, Utils),
+
     {ok, Cwd} = file:get_cwd(),
     PgDataDir = filename:append(Cwd, "datadir"),
     {ok, _} = exec:run(Initdb ++ " --locale en_US.UTF8 " ++ PgDataDir, [sync,stdout,stderr]),
-    Config#{pg_datadir => PgDataDir}.
+    [{datadir, PgDataDir}|Config].
+
+write_postgresql_config(Config) ->
+    PgDataDir = ?config(datadir, Config),
 
-write_postgresql_config(#{pg_datadir := PgDataDir}=Config) ->
     PGConfig = [
         "ssl = on\n",
         "ssl_ca_file = 'root.crt'\n",
@@ -138,7 +163,9 @@ write_postgresql_config(#{pg_datadir := PgDataDir}=Config) ->
     ok = file:write_file(FilePath, PGConfig),
     Config.
 
-copy_certs(#{pg_datadir := PgDataDir}=Config) ->
+copy_certs(Config) ->
+    PgDataDir = ?config(datadir, Config),
+
     Files = [
         {"epgsql.crt", "server.crt", 8#00660},
         {"epgsql.key", "server.key", 8#00600},
@@ -153,7 +180,9 @@ copy_certs(#{pg_datadir := PgDataDir}=Config) ->
     end, Files),
     Config.
 
-write_pg_hba_config(#{pg_datadir := PgDataDir}=Config) ->
+write_pg_hba_config(Config) ->
+    PgDataDir = ?config(datadir, Config),
+
     User = os:getenv("USER"),
     PGConfig = [
         "local   all             ", User, "                              trust\n",
@@ -169,7 +198,7 @@ write_pg_hba_config(#{pg_datadir := PgDataDir}=Config) ->
     ],
     FilePath = filename:join(PgDataDir, "pg_hba.conf"),
     ok = file:write_file(FilePath, PGConfig),
-    Config#{pg_user => User}.
+    [{user, User}|Config].
 
 %% =============================================================================
 %% Internal functions

+ 2 - 1
test/epgsql_perf_SUITE.erl

@@ -64,7 +64,8 @@ with_connection(Config, F) ->
     with_connection(Config, F, "epgsql_test", []).
 
 with_connection(Config, F, Username, Args) ->
-    #{pg_host := Host, pg_port := Port} = ?config(pg_config, Config),
+    {Host, Port} = epgsql_ct:connection_data(Config),
+
     Args2 = [{port, Port}, {database, "epgsql_test_db1"} | Args],
     fun () ->
         {ok, C} = epgsql:connect(Host, Username, Args2),