diff options
-rw-r--r-- | test/rebar_pkg_SUITE.erl | 137 |
1 files changed, 125 insertions, 12 deletions
diff --git a/test/rebar_pkg_SUITE.erl b/test/rebar_pkg_SUITE.erl index 19c4bd0..270946c 100644 --- a/test/rebar_pkg_SUITE.erl +++ b/test/rebar_pkg_SUITE.erl @@ -10,8 +10,8 @@ -define(bad_checksum, <<"D576B442A68C7B92BACDE1EFE9C6E54D8D6C74BDB71D8175B9D3C6EC8C7B62A7">>). -define(good_checksum, <<"1C6CE379D191FBAB41B7905075E0BF87CBBE23C77CECE775C5A0B786B2244C35">>). -all() -> [good_uncached]. - +all() -> [good_uncached, good_cached, badindexchk, badpkg, + bad_to_good, good_disconnect, bad_disconnect]. init_per_suite(Config) -> application:start(meck), @@ -32,13 +32,135 @@ init_per_testcase(good_cached=Name, Config0) -> | Config0], Config = mock_config(Name, Config1), copy_to_cache(Pkg, Config), + Config; +init_per_testcase(badindexchk=Name, Config0) -> + Config = [{good_cache, false}, + {pkg, {<<"badindexchk">>, <<"1.0.0">>}} + | Config0], + mock_config(Name, Config); +init_per_testcase(badpkg=Name, Config0) -> + Config = [{good_cache, false}, + {pkg, {<<"badpkg">>, <<"1.0.0">>}} + | Config0], + mock_config(Name, Config); +init_per_testcase(bad_to_good=Name, Config0) -> + Config1 = [{good_cache, false}, + {pkg, {<<"goodpkg">>, <<"1.0.0">>}} + | Config0], + Config = mock_config(Name, Config1), + Source = filename:join(?config(data_dir, Config), <<"badpkg-1.0.0.tar">>), + Dest = filename:join(?config(cache_dir, Config), <<"goodpkg-1.0.0.tar">>), + ec_file:copy(Source, Dest), + Config; +init_per_testcase(good_disconnect=Name, Config0) -> + Pkg = {<<"goodpkg">>, <<"1.0.0">>}, + Config1 = [{good_cache, true}, + {pkg, Pkg} + | Config0], + Config = mock_config(Name, Config1), + copy_to_cache(Pkg, Config), + meck:unload(httpc), + meck:new(httpc, [passthrough, unsticky]), + meck:expect(httpc, request, fun(_, _, _, _) -> {error, econnrefused} end), + Config; +init_per_testcase(bad_disconnect=Name, Config0) -> + Pkg = {<<"goodpkg">>, <<"1.0.0">>}, + Config1 = [{good_cache, false}, + {pkg, Pkg} + | Config0], + Config = mock_config(Name, Config1), + meck:unload(httpc), + meck:new(httpc, [passthrough, unsticky]), + meck:expect(httpc, request, fun(_, _, _, _) -> {error, econnrefused} end), Config. - end_per_testcase(_, Config) -> unmock_config(Config), Config. +good_uncached(Config) -> + Tmp = ?config(tmp_dir, Config), + {Pkg,Vsn} = ?config(pkg, Config), + State = ?config(state, Config), + ?assertEqual({ok, true}, + rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)), + Cache = ?config(cache_dir, Config), + ?assert(filelib:is_regular(filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>))). + +good_cached(Config) -> + Tmp = ?config(tmp_dir, Config), + {Pkg,Vsn} = ?config(pkg, Config), + State = ?config(state, Config), + Cache = ?config(cache_dir, Config), + CachedFile = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>), + ?assert(filelib:is_regular(CachedFile)), + FInfo = file:read_file_info(CachedFile), + {ok, Content} = file:read_file(CachedFile), + ?assertEqual({ok, true}, + rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)), + ?assertEqual(FInfo, + file:read_file_info(CachedFile)), + {ok, Content} = file:read_file(CachedFile). + +badindexchk(Config) -> + Tmp = ?config(tmp_dir, Config), + {Pkg,Vsn} = ?config(pkg, Config), + State = ?config(state, Config), + ?assertMatch({bad_registry_checksum, _Path}, + rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)), + %% The cached file is there for forensic purposes + Cache = ?config(cache_dir, Config), + ?assert(filelib:is_regular(filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>))). + +badpkg(Config) -> + Tmp = ?config(tmp_dir, Config), + {Pkg,Vsn} = ?config(pkg, Config), + State = ?config(state, Config), + ?assertMatch({bad_download, _Path}, + rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)), + %% The cached file is there for forensic purposes + Cache = ?config(cache_dir, Config), + ?assert(filelib:is_regular(filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>))). + +bad_to_good(Config) -> + Tmp = ?config(tmp_dir, Config), + {Pkg,Vsn} = ?config(pkg, Config), + State = ?config(state, Config), + Cache = ?config(cache_dir, Config), + CachedFile = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>), + ?assert(filelib:is_regular(CachedFile)), + {ok, Contents} = file:read_file(CachedFile), + ?assertEqual({ok, true}, + rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)), + %% Cache has refreshed + ?assert({ok, Contents} =/= file:read_file(CachedFile)). + +good_disconnect(Config) -> + Tmp = ?config(tmp_dir, Config), + {Pkg,Vsn} = ?config(pkg, Config), + State = ?config(state, Config), + Cache = ?config(cache_dir, Config), + CachedFile = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>), + ?assert(filelib:is_regular(CachedFile)), + FInfo = file:read_file_info(CachedFile), + {ok, Content} = file:read_file(CachedFile), + ?assertEqual({ok, true}, + rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)), + ?assertEqual(FInfo, + file:read_file_info(CachedFile)), + {ok, Content} = file:read_file(CachedFile). + +bad_disconnect(Config) -> + Tmp = ?config(tmp_dir, Config), + {Pkg,Vsn} = ?config(pkg, Config), + State = ?config(state, Config), + ?assertEqual(request_failed, + rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)). + + +%%%%%%%%%%%%%%% +%%% Helpers %%% +%%%%%%%%%%%%%%% mock_config(Name, Config) -> Priv = ?config(priv_dir, Config), CacheRoot = filename:join([Priv, "cache", atom_to_list(Name)]), @@ -89,12 +211,3 @@ copy_to_cache({Pkg,Vsn}, Config) -> Source = filename:join(?config(data_dir, Config), Name), Dest = filename:join(?config(cache_dir, Config), Name), ec_file:copy(Source, Dest). - -good_uncached(Config) -> - Tmp = ?config(tmp_dir, Config), - {Pkg,Vsn} = ?config(pkg, Config), - State = ?config(state, Config), - ?assertEqual({ok, true}, - rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)), - Cache = ?config(cache_dir, Config), - ?assert(filelib:is_regular(filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>))). |