You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 regels
10 KiB

  1. %% Test suite for the rebar pkg index caching and decompression
  2. %% mechanisms.
  3. -module(rebar_pkg_SUITE).
  4. -compile(export_all).
  5. -include_lib("common_test/include/ct.hrl").
  6. -include_lib("eunit/include/eunit.hrl").
  7. -define(bad_etag, "abcdef").
  8. -define(good_etag, "22e1d7387c9085a462340088a2a8ba67").
  9. -define(bad_checksum, <<"D576B442A68C7B92BACDE1EFE9C6E54D8D6C74BDB71D8175B9D3C6EC8C7B62A7">>).
  10. -define(good_checksum, <<"1C6CE379D191FBAB41B7905075E0BF87CBBE23C77CECE775C5A0B786B2244C35">>).
  11. all() -> [good_uncached, good_cached, badindexchk, badpkg,
  12. bad_to_good, good_disconnect, bad_disconnect, pkgs_provider,
  13. find_highest_matching].
  14. init_per_suite(Config) ->
  15. application:start(meck),
  16. Config.
  17. end_per_suite(_Config) ->
  18. application:stop(meck).
  19. init_per_testcase(pkgs_provider=Name, Config) ->
  20. %% Need to mock out a registry for this test now because it will try to update it automatically
  21. Priv = ?config(priv_dir, Config),
  22. Tid = ets:new(registry_table, [public]),
  23. ets:insert_new(Tid, []),
  24. CacheRoot = filename:join([Priv, "cache", atom_to_list(Name)]),
  25. CacheDir = filename:join([CacheRoot, "hex", "com", "test", "packages"]),
  26. filelib:ensure_dir(filename:join([CacheDir, "registry"])),
  27. ok = ets:tab2file(Tid, filename:join([CacheDir, "registry"])),
  28. meck:new(rebar_packages, [passthrough]),
  29. meck:expect(rebar_packages, registry_dir, fun(_) -> {ok, CacheDir} end),
  30. meck:expect(rebar_packages, package_dir, fun(_) -> {ok, CacheDir} end),
  31. rebar_prv_update:hex_to_index(rebar_state:new()),
  32. Config;
  33. init_per_testcase(good_uncached=Name, Config0) ->
  34. Config = [{good_cache, false},
  35. {pkg, {<<"goodpkg">>, <<"1.0.0">>}}
  36. | Config0],
  37. mock_config(Name, Config);
  38. init_per_testcase(good_cached=Name, Config0) ->
  39. Pkg = {<<"goodpkg">>, <<"1.0.0">>},
  40. Config1 = [{good_cache, true},
  41. {pkg, Pkg}
  42. | Config0],
  43. Config = mock_config(Name, Config1),
  44. copy_to_cache(Pkg, Config),
  45. Config;
  46. init_per_testcase(badindexchk=Name, Config0) ->
  47. Config = [{good_cache, false},
  48. {pkg, {<<"badindexchk">>, <<"1.0.0">>}}
  49. | Config0],
  50. mock_config(Name, Config);
  51. init_per_testcase(badpkg=Name, Config0) ->
  52. Config = [{good_cache, false},
  53. {pkg, {<<"badpkg">>, <<"1.0.0">>}}
  54. | Config0],
  55. mock_config(Name, Config);
  56. init_per_testcase(bad_to_good=Name, Config0) ->
  57. Config1 = [{good_cache, false},
  58. {pkg, {<<"goodpkg">>, <<"1.0.0">>}}
  59. | Config0],
  60. Config = mock_config(Name, Config1),
  61. Source = filename:join(?config(data_dir, Config), <<"badpkg-1.0.0.tar">>),
  62. Dest = filename:join(?config(cache_dir, Config), <<"goodpkg-1.0.0.tar">>),
  63. ec_file:copy(Source, Dest),
  64. Config;
  65. init_per_testcase(good_disconnect=Name, Config0) ->
  66. Pkg = {<<"goodpkg">>, <<"1.0.0">>},
  67. Config1 = [{good_cache, true},
  68. {pkg, Pkg}
  69. | Config0],
  70. Config = mock_config(Name, Config1),
  71. copy_to_cache(Pkg, Config),
  72. meck:unload(httpc),
  73. meck:new(httpc, [passthrough, unsticky]),
  74. meck:expect(httpc, request, fun(_, _, _, _, _) -> {error, econnrefused} end),
  75. Config;
  76. init_per_testcase(bad_disconnect=Name, Config0) ->
  77. Pkg = {<<"goodpkg">>, <<"1.0.0">>},
  78. Config1 = [{good_cache, false},
  79. {pkg, Pkg}
  80. | Config0],
  81. Config = mock_config(Name, Config1),
  82. meck:unload(httpc),
  83. meck:new(httpc, [passthrough, unsticky]),
  84. meck:expect(httpc, request, fun(_, _, _, _, _) -> {error, econnrefused} end),
  85. Config;
  86. init_per_testcase(Name, Config0) ->
  87. Config = [{good_cache, false},
  88. {pkg, {<<"goodpkg">>, <<"1.0.0">>}}
  89. | Config0],
  90. mock_config(Name, Config).
  91. end_per_testcase(_, Config) ->
  92. unmock_config(Config),
  93. Config.
  94. good_uncached(Config) ->
  95. Tmp = ?config(tmp_dir, Config),
  96. {Pkg,Vsn} = ?config(pkg, Config),
  97. State = ?config(state, Config),
  98. ?assertEqual({ok, true},
  99. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)),
  100. Cache = ?config(cache_dir, Config),
  101. ?assert(filelib:is_regular(filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>))).
  102. good_cached(Config) ->
  103. Tmp = ?config(tmp_dir, Config),
  104. {Pkg,Vsn} = ?config(pkg, Config),
  105. State = ?config(state, Config),
  106. Cache = ?config(cache_dir, Config),
  107. CachedFile = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>),
  108. ?assert(filelib:is_regular(CachedFile)),
  109. {ok, Content} = file:read_file(CachedFile),
  110. ?assertEqual({ok, true},
  111. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)),
  112. {ok, Content} = file:read_file(CachedFile).
  113. badindexchk(Config) ->
  114. Tmp = ?config(tmp_dir, Config),
  115. {Pkg,Vsn} = ?config(pkg, Config),
  116. State = ?config(state, Config),
  117. ?assertMatch({bad_registry_checksum, _Path},
  118. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)),
  119. %% The cached file is there for forensic purposes
  120. Cache = ?config(cache_dir, Config),
  121. ?assert(filelib:is_regular(filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>))).
  122. badpkg(Config) ->
  123. Tmp = ?config(tmp_dir, Config),
  124. {Pkg,Vsn} = ?config(pkg, Config),
  125. State = ?config(state, Config),
  126. ?assertMatch({bad_download, _Path},
  127. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)),
  128. %% The cached file is there for forensic purposes
  129. Cache = ?config(cache_dir, Config),
  130. ?assert(filelib:is_regular(filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>))).
  131. bad_to_good(Config) ->
  132. Tmp = ?config(tmp_dir, Config),
  133. {Pkg,Vsn} = ?config(pkg, Config),
  134. State = ?config(state, Config),
  135. Cache = ?config(cache_dir, Config),
  136. CachedFile = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>),
  137. ?assert(filelib:is_regular(CachedFile)),
  138. {ok, Contents} = file:read_file(CachedFile),
  139. ?assertEqual({ok, true},
  140. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)),
  141. %% Cache has refreshed
  142. ?assert({ok, Contents} =/= file:read_file(CachedFile)).
  143. good_disconnect(Config) ->
  144. Tmp = ?config(tmp_dir, Config),
  145. {Pkg,Vsn} = ?config(pkg, Config),
  146. State = ?config(state, Config),
  147. Cache = ?config(cache_dir, Config),
  148. CachedFile = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>),
  149. ?assert(filelib:is_regular(CachedFile)),
  150. {ok, Content} = file:read_file(CachedFile),
  151. ?assertEqual({ok, true},
  152. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)),
  153. {ok, Content} = file:read_file(CachedFile).
  154. bad_disconnect(Config) ->
  155. Tmp = ?config(tmp_dir, Config),
  156. {Pkg,Vsn} = ?config(pkg, Config),
  157. State = ?config(state, Config),
  158. ?assertEqual({fetch_fail, Pkg, Vsn},
  159. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)).
  160. pkgs_provider(Config) ->
  161. Config1 = rebar_test_utils:init_rebar_state(Config),
  162. rebar_test_utils:run_and_check(
  163. Config1, [], ["pkgs"],
  164. {ok, []}
  165. ).
  166. find_highest_matching(_Config) ->
  167. State = rebar_state:new(),
  168. {ok, Vsn} = rebar_packages:find_highest_matching(<<"goodpkg">>, <<"1.0.0">>, package_index, State),
  169. ?assertEqual(<<"1.0.1">>, Vsn),
  170. {ok, Vsn1} = rebar_packages:find_highest_matching(<<"goodpkg">>, <<"1.0">>, package_index, State),
  171. ?assertEqual(<<"1.1.1">>, Vsn1),
  172. {ok, Vsn2} = rebar_packages:find_highest_matching(<<"goodpkg">>, <<"2.0">>, package_index, State),
  173. ?assertEqual(<<"2.0.0">>, Vsn2).
  174. %%%%%%%%%%%%%%%
  175. %%% Helpers %%%
  176. %%%%%%%%%%%%%%%
  177. mock_config(Name, Config) ->
  178. Priv = ?config(priv_dir, Config),
  179. CacheRoot = filename:join([Priv, "cache", atom_to_list(Name)]),
  180. TmpDir = filename:join([Priv, "tmp", atom_to_list(Name)]),
  181. Tid = ets:new(registry_table, [public]),
  182. ets:insert_new(Tid, [
  183. {<<"badindexchk">>,[[<<"1.0.0">>]]},
  184. {<<"goodpkg">>,[[<<"1.0.0">>, <<"1.0.1">>, <<"1.1.1">>, <<"2.0.0">>]]},
  185. {<<"badpkg">>,[[<<"1.0.0">>]]},
  186. {{<<"badindexchk">>,<<"1.0.0">>}, [[], ?bad_checksum, [<<"rebar3">>]]},
  187. {{<<"goodpkg">>,<<"1.0.0">>}, [[], ?good_checksum, [<<"rebar3">>]]},
  188. {{<<"goodpkg">>,<<"1.0.1">>}, [[], ?good_checksum, [<<"rebar3">>]]},
  189. {{<<"goodpkg">>,<<"1.1.1">>}, [[], ?good_checksum, [<<"rebar3">>]]},
  190. {{<<"goodpkg">>,<<"2.0.0">>}, [[], ?good_checksum, [<<"rebar3">>]]},
  191. {{<<"badpkg">>,<<"1.0.0">>}, [[], ?good_checksum, [<<"rebar3">>]]}
  192. ]),
  193. CacheDir = filename:join([CacheRoot, "hex", "com", "test", "packages"]),
  194. filelib:ensure_dir(filename:join([CacheDir, "registry"])),
  195. ok = ets:tab2file(Tid, filename:join([CacheDir, "registry"])),
  196. %% The state returns us a fake registry
  197. meck:new(rebar_state, [passthrough]),
  198. meck:expect(rebar_state, get,
  199. fun(_State, rebar_packages_cdn, _Default) ->
  200. "http://test.com/"
  201. end),
  202. meck:new(rebar_dir, [passthrough]),
  203. meck:expect(rebar_dir, global_cache_dir, fun(_) -> CacheRoot end),
  204. meck:new(rebar_packages, [passthrough]),
  205. meck:expect(rebar_packages, registry_dir, fun(_) -> {ok, CacheDir} end),
  206. meck:expect(rebar_packages, package_dir, fun(_) -> {ok, CacheDir} end),
  207. rebar_prv_update:hex_to_index(rebar_state:new()),
  208. %% Cache fetches are mocked -- we assume the server and clients are
  209. %% correctly used.
  210. GoodCache = ?config(good_cache, Config),
  211. {Pkg,Vsn} = ?config(pkg, Config),
  212. PkgFile = <<Pkg/binary, "-", Vsn/binary, ".tar">>,
  213. {ok, PkgContents} = file:read_file(filename:join(?config(data_dir, Config), PkgFile)),
  214. meck:new(httpc, [passthrough, unsticky]),
  215. meck:expect(httpc, request,
  216. fun(get, {_Url, _Opts}, _, _, _) when GoodCache ->
  217. {ok, {{Vsn, 304, <<"Not Modified">>}, [{"etag", ?good_etag}], <<>>}};
  218. (get, {_Url, _Opts}, _, _, _) ->
  219. {ok, {{Vsn, 200, <<"OK">>}, [{"etag", ?good_etag}], PkgContents}}
  220. end),
  221. [{cache_root, CacheRoot},
  222. {cache_dir, CacheDir},
  223. {tmp_dir, TmpDir},
  224. {mock_table, Tid} | Config].
  225. unmock_config(Config) ->
  226. meck:unload(),
  227. ets:delete(?config(mock_table, Config)).
  228. copy_to_cache({Pkg,Vsn}, Config) ->
  229. Name = <<Pkg/binary, "-", Vsn/binary, ".tar">>,
  230. Source = filename:join(?config(data_dir, Config), Name),
  231. Dest = filename:join(?config(cache_dir, Config), Name),
  232. ec_file:copy(Source, Dest).