From a27fd22c3fe1f71091dce27be3c71dc5b75ae2f0 Mon Sep 17 00:00:00 2001 From: Fred Hebert Date: Wed, 26 Aug 2015 11:53:23 +0000 Subject: Basic package aliasing tests --- test/rebar_pkg_alias_SUITE.erl | 121 +++++++++++++++++++++++++++++++++++++++++ test/rebar_test_utils.erl | 25 ++++++++- 2 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 test/rebar_pkg_alias_SUITE.erl diff --git a/test/rebar_pkg_alias_SUITE.erl b/test/rebar_pkg_alias_SUITE.erl new file mode 100644 index 0000000..f7fa5d4 --- /dev/null +++ b/test/rebar_pkg_alias_SUITE.erl @@ -0,0 +1,121 @@ +-module(rebar_pkg_alias_SUITE). +-compile(export_all). +-include_lib("common_test/include/ct.hrl"). +-include_lib("eunit/include/eunit.hrl"). +-include_lib("kernel/include/file.hrl"). + +all() -> [same_alias, diff_alias, diff_alias_vsn]. + +%% {uuid, {pkg, uuid}} = uuid +%% {uuid, {pkg, alias}} = uuid on disk +%% another run should yield the same lock file without error +init_per_suite(Config) -> + mock_config(?MODULE, Config). + +end_per_suite(Config) -> + unmock_config(Config). + +init_per_testcase(same_alias, Config0) -> + Config = rebar_test_utils:init_rebar_state(Config0,"same_alias_"), + AppDir = ?config(apps, Config), + rebar_test_utils:create_app(AppDir, "A", "0.0.0", [kernel, stdlib]), + RebarConf = rebar_test_utils:create_config(AppDir, [{deps, [{fakelib, {pkg, fakelib}}]}]), + [{rebarconfig, RebarConf} | Config]; +init_per_testcase(diff_alias, Config0) -> + Config = rebar_test_utils:init_rebar_state(Config0,"diff_alias_"), + AppDir = ?config(apps, Config), + rebar_test_utils:create_app(AppDir, "A", "0.0.0", [kernel, stdlib]), + RebarConf = rebar_test_utils:create_config(AppDir, [{deps, [{fakelib, {pkg, goodpkg}}]}]), + [{rebarconfig, RebarConf} | Config]; +init_per_testcase(diff_alias_vsn, Config0) -> + Config = rebar_test_utils:init_rebar_state(Config0,"diff_alias_vsn_"), + AppDir = ?config(apps, Config), + rebar_test_utils:create_app(AppDir, "A", "0.0.0", [kernel, stdlib]), + RebarConf = rebar_test_utils:create_config(AppDir, [{deps, [{fakelib, "1.0.0", {pkg, goodpkg}}]}]), + [{rebarconfig, RebarConf} | Config]. + +end_per_testcase(_, Config) -> + Config. + +same_alias(Config) -> + {ok, RebarConfig} = file:consult(?config(rebarconfig, Config)), + rebar_test_utils:run_and_check( + Config, RebarConfig, ["lock"], + {ok, [{lock, "fakelib"}, {dep, "fakelib"}]} + ). + +diff_alias(Config) -> + %% even though the dep is 'fakelib' aliased as 'goodpkg' all + %% internal records use 'fakelib' as a value. Just make sure + %% the lock actually maintains the proper source as 'goodpkg' + AppDir = ?config(apps, Config), + Lockfile = filename:join([AppDir, "rebar.lock"]), + {ok, RebarConfig} = file:consult(?config(rebarconfig, Config)), + rebar_test_utils:run_and_check( + Config, RebarConfig, ["lock"], + {ok, [{lock, "fakelib"},{dep, "fakelib"}]} + ), + {ok, [LockData]} = file:consult(Lockfile), + ?assert(lists:any(fun({<<"fakelib">>,{pkg,<<"goodpkg">>,_},_}) -> true + ; (_) -> false end, LockData)), + %% An second run yields the same + rebar_test_utils:run_and_check( + Config, RebarConfig, ["lock"], + {ok, [{lock, "fakelib"},{dep, "fakelib"}]} + ), + {ok, [LockData]} = file:consult(Lockfile), + %% So does an upgrade + rebar_test_utils:run_and_check( + Config, RebarConfig, ["upgrade"], + {ok, [{lock, "fakelib"},{dep, "fakelib"}]} + ), + {ok, [LockData]} = file:consult(Lockfile). + +diff_alias_vsn(Config) -> diff_alias(Config). + +mock_config(Name, Config) -> + Priv = ?config(priv_dir, Config), + AppDir = filename:join([Priv, "fakelib"]), + CacheRoot = filename:join([Priv, "cache", atom_to_list(Name)]), + TmpDir = filename:join([Priv, "tmp", atom_to_list(Name)]), + CacheDir = filename:join([CacheRoot, "hex", "com", "test", "packages"]), + filelib:ensure_dir(filename:join([CacheDir, "registry"])), + rebar_test_utils:create_app(AppDir, "fakelib", "1.0.0", [kernel, stdlib]), + {Chk,Etag} = rebar_test_utils:package_app(AppDir, CacheDir, "fakelib-1.0.0"), + {Chk,Etag} = rebar_test_utils:package_app(AppDir, CacheDir, "goodpkg-1.0.0"), + + Tid = ets:new(registry_table, [public]), + ets:insert_new(Tid, [ + {<<"fakelib">>,[[<<"1.0.0">>]]}, + {<<"goodpkg">>,[[<<"1.0.0">>]]}, + {{<<"fakelib">>,<<"1.0.0">>}, [[], Chk, [<<"rebar3">>]]}, + {{<<"goodpkg">>,<<"1.0.0">>}, [[], Chk, [<<"rebar3">>]]} + ]), + ok = ets:tab2file(Tid, filename:join([CacheDir, "registry"])), + ets:delete(Tid), + %% The state returns us a fake registry + meck:new(rebar_dir, [passthrough, no_link]), + meck:expect(rebar_dir, global_cache_dir, fun(_) -> CacheRoot end), + + meck:new(rebar_packages, [passthrough, no_link]), + meck:expect(rebar_packages, registry_dir, fun(_) -> CacheDir end), + meck:expect(rebar_packages, package_dir, fun(_) -> CacheDir end), + rebar_prv_update:hex_to_index(rebar_state:new()), + + %% Cache fetches are mocked -- we assume the server and clients are + %% correctly used. + meck:new(httpc, [passthrough, unsticky, no_link]), + meck:expect(httpc, request, + fun(get, {_Url, _Opts}, _, _, _) -> + {ok, {{<<"1.0.0">>, 304, <<"Not Modified">>}, [{"etag", Etag}], <<>>}} + end), + %% Move all packages to cache + NewConf = [{cache_root, CacheRoot}, + {cache_dir, CacheDir}, + {tmp_dir, TmpDir}, + {mock_table, Tid} | Config], + NewConf. + +unmock_config(Config) -> + meck:unload(), + Config. diff --git a/test/rebar_test_utils.erl b/test/rebar_test_utils.erl index c07df68..8d1d408 100644 --- a/test/rebar_test_utils.erl +++ b/test/rebar_test_utils.erl @@ -3,7 +3,8 @@ -include_lib("eunit/include/eunit.hrl"). -export([init_rebar_state/1, init_rebar_state/2, run_and_check/4]). -export([expand_deps/2, flat_deps/1, top_level_deps/1]). --export([create_app/4, create_eunit_app/4, create_empty_app/4, create_config/2]). +-export([create_app/4, create_eunit_app/4, create_empty_app/4, create_config/2, + package_app/3]). -export([create_random_name/1, create_random_vsn/0, write_src_file/2]). %%%%%%%%%%%%%% @@ -418,3 +419,25 @@ get_app_metadata(Name, Vsn, Deps) -> {included_applications, []}, {registered, []}, {applications, Deps}]}. + +package_app(AppDir, DestDir, PkgName) -> + Name = PkgName++".tar", + {ok, Fs} = file:list_dir(AppDir), + ok = erl_tar:create(filename:join(DestDir, "contents.tar.gz"), + lists:zip(Fs, [filename:join(AppDir,F) || F <- Fs]), + [compressed]), + ok = file:write_file(filename:join(DestDir, "metadata.config"), "who cares"), + ok = file:write_file(filename:join(DestDir, "VERSION"), "3"), + {ok, Contents} = file:read_file(filename:join(DestDir, "contents.tar.gz")), + Blob = <<"3who cares", Contents/binary>>, + <> = crypto:hash(sha256, Blob), + BinChecksum = list_to_binary(string:to_upper(lists:flatten(io_lib:format("~64.16.0b", [X])))), + ok = file:write_file(filename:join(DestDir, "CHECKSUM"), BinChecksum), + PkgFiles = ["contents.tar.gz", "VERSION", "metadata.config", "CHECKSUM"], + Archive = filename:join(DestDir, Name), + ok = erl_tar:create(Archive, + lists:zip(PkgFiles, [filename:join(DestDir,F) || F <- PkgFiles])), + {ok, BinFull} = file:read_file(Archive), + <> = crypto:hash(md5, BinFull), + Etag = string:to_lower(lists:flatten(io_lib:format("~32.16.0b", [E]))), + {BinChecksum, Etag}. -- cgit v1.1