Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

258 lignes
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(
  169. <<"test">>, <<"1.0.0">>, <<"goodpkg">>, <<"1.0.0">>, package_index, State),
  170. ?assertEqual(<<"1.0.1">>, Vsn),
  171. {ok, Vsn1} = rebar_packages:find_highest_matching(
  172. <<"test">>, <<"1.0.0">>, <<"goodpkg">>, <<"1.0">>, package_index, State),
  173. ?assertEqual(<<"1.1.1">>, Vsn1),
  174. {ok, Vsn2} = rebar_packages:find_highest_matching(
  175. <<"test">>, <<"1.0.0">>, <<"goodpkg">>, <<"2.0">>, package_index, State),
  176. ?assertEqual(<<"2.0.0">>, Vsn2).
  177. %%%%%%%%%%%%%%%
  178. %%% Helpers %%%
  179. %%%%%%%%%%%%%%%
  180. mock_config(Name, Config) ->
  181. Priv = ?config(priv_dir, Config),
  182. CacheRoot = filename:join([Priv, "cache", atom_to_list(Name)]),
  183. TmpDir = filename:join([Priv, "tmp", atom_to_list(Name)]),
  184. Tid = ets:new(registry_table, [public]),
  185. ets:insert_new(Tid, [
  186. {<<"badindexchk">>,[[<<"1.0.0">>]]},
  187. {<<"goodpkg">>,[[<<"1.0.0">>, <<"1.0.1">>, <<"1.1.1">>, <<"2.0.0">>]]},
  188. {<<"badpkg">>,[[<<"1.0.0">>]]},
  189. {{<<"badindexchk">>,<<"1.0.0">>}, [[], ?bad_checksum, [<<"rebar3">>]]},
  190. {{<<"goodpkg">>,<<"1.0.0">>}, [[], ?good_checksum, [<<"rebar3">>]]},
  191. {{<<"goodpkg">>,<<"1.0.1">>}, [[], ?good_checksum, [<<"rebar3">>]]},
  192. {{<<"goodpkg">>,<<"1.1.1">>}, [[], ?good_checksum, [<<"rebar3">>]]},
  193. {{<<"goodpkg">>,<<"2.0.0">>}, [[], ?good_checksum, [<<"rebar3">>]]},
  194. {{<<"badpkg">>,<<"1.0.0">>}, [[], ?good_checksum, [<<"rebar3">>]]}
  195. ]),
  196. CacheDir = filename:join([CacheRoot, "hex", "com", "test", "packages"]),
  197. filelib:ensure_dir(filename:join([CacheDir, "registry"])),
  198. ok = ets:tab2file(Tid, filename:join([CacheDir, "registry"])),
  199. %% The state returns us a fake registry
  200. meck:new(rebar_state, [passthrough]),
  201. meck:expect(rebar_state, get,
  202. fun(_State, rebar_packages_cdn, _Default) ->
  203. "http://test.com/"
  204. end),
  205. meck:new(rebar_dir, [passthrough]),
  206. meck:expect(rebar_dir, global_cache_dir, fun(_) -> CacheRoot end),
  207. meck:new(rebar_packages, [passthrough]),
  208. meck:expect(rebar_packages, registry_dir, fun(_) -> {ok, CacheDir} end),
  209. meck:expect(rebar_packages, package_dir, fun(_) -> {ok, CacheDir} end),
  210. rebar_prv_update:hex_to_index(rebar_state:new()),
  211. %% Cache fetches are mocked -- we assume the server and clients are
  212. %% correctly used.
  213. GoodCache = ?config(good_cache, Config),
  214. {Pkg,Vsn} = ?config(pkg, Config),
  215. PkgFile = <<Pkg/binary, "-", Vsn/binary, ".tar">>,
  216. {ok, PkgContents} = file:read_file(filename:join(?config(data_dir, Config), PkgFile)),
  217. meck:new(httpc, [passthrough, unsticky]),
  218. meck:expect(httpc, request,
  219. fun(get, {_Url, _Opts}, _, _, _) when GoodCache ->
  220. {ok, {{Vsn, 304, <<"Not Modified">>}, [{"etag", ?good_etag}], <<>>}};
  221. (get, {_Url, _Opts}, _, _, _) ->
  222. {ok, {{Vsn, 200, <<"OK">>}, [{"etag", ?good_etag}], PkgContents}}
  223. end),
  224. [{cache_root, CacheRoot},
  225. {cache_dir, CacheDir},
  226. {tmp_dir, TmpDir},
  227. {mock_table, Tid} | Config].
  228. unmock_config(Config) ->
  229. meck:unload(),
  230. ets:delete(?config(mock_table, Config)).
  231. copy_to_cache({Pkg,Vsn}, Config) ->
  232. Name = <<Pkg/binary, "-", Vsn/binary, ".tar">>,
  233. Source = filename:join(?config(data_dir, Config), Name),
  234. Dest = filename:join(?config(cache_dir, Config), Name),
  235. ec_file:copy(Source, Dest).