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.

207 line
8.0 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].
  13. init_per_suite(Config) ->
  14. application:start(meck),
  15. Config.
  16. end_per_suite(_Config) ->
  17. application:stop(meck).
  18. init_per_testcase(good_uncached=Name, Config0) ->
  19. Config = [{good_cache, false},
  20. {pkg, {<<"goodpkg">>, <<"1.0.0">>}}
  21. | Config0],
  22. mock_config(Name, Config);
  23. init_per_testcase(good_cached=Name, Config0) ->
  24. Pkg = {<<"goodpkg">>, <<"1.0.0">>},
  25. Config1 = [{good_cache, true},
  26. {pkg, Pkg}
  27. | Config0],
  28. Config = mock_config(Name, Config1),
  29. copy_to_cache(Pkg, Config),
  30. Config;
  31. init_per_testcase(badindexchk=Name, Config0) ->
  32. Config = [{good_cache, false},
  33. {pkg, {<<"badindexchk">>, <<"1.0.0">>}}
  34. | Config0],
  35. mock_config(Name, Config);
  36. init_per_testcase(badpkg=Name, Config0) ->
  37. Config = [{good_cache, false},
  38. {pkg, {<<"badpkg">>, <<"1.0.0">>}}
  39. | Config0],
  40. mock_config(Name, Config);
  41. init_per_testcase(bad_to_good=Name, Config0) ->
  42. Config1 = [{good_cache, false},
  43. {pkg, {<<"goodpkg">>, <<"1.0.0">>}}
  44. | Config0],
  45. Config = mock_config(Name, Config1),
  46. Source = filename:join(?config(data_dir, Config), <<"badpkg-1.0.0.tar">>),
  47. Dest = filename:join(?config(cache_dir, Config), <<"goodpkg-1.0.0.tar">>),
  48. ec_file:copy(Source, Dest),
  49. Config;
  50. init_per_testcase(good_disconnect=Name, Config0) ->
  51. Pkg = {<<"goodpkg">>, <<"1.0.0">>},
  52. Config1 = [{good_cache, true},
  53. {pkg, Pkg}
  54. | Config0],
  55. Config = mock_config(Name, Config1),
  56. copy_to_cache(Pkg, Config),
  57. meck:unload(httpc),
  58. meck:new(httpc, [passthrough, unsticky]),
  59. meck:expect(httpc, request, fun(_, _, _, _, _) -> {error, econnrefused} end),
  60. Config;
  61. init_per_testcase(bad_disconnect=Name, Config0) ->
  62. Pkg = {<<"goodpkg">>, <<"1.0.0">>},
  63. Config1 = [{good_cache, false},
  64. {pkg, Pkg}
  65. | Config0],
  66. Config = mock_config(Name, Config1),
  67. meck:unload(httpc),
  68. meck:new(httpc, [passthrough, unsticky]),
  69. meck:expect(httpc, request, fun(_, _, _, _, _) -> {error, econnrefused} end),
  70. Config.
  71. end_per_testcase(_, Config) ->
  72. unmock_config(Config),
  73. Config.
  74. good_uncached(Config) ->
  75. Tmp = ?config(tmp_dir, Config),
  76. {Pkg,Vsn} = ?config(pkg, Config),
  77. State = ?config(state, Config),
  78. ?assertEqual({ok, true},
  79. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)),
  80. Cache = ?config(cache_dir, Config),
  81. ?assert(filelib:is_regular(filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>))).
  82. good_cached(Config) ->
  83. Tmp = ?config(tmp_dir, Config),
  84. {Pkg,Vsn} = ?config(pkg, Config),
  85. State = ?config(state, Config),
  86. Cache = ?config(cache_dir, Config),
  87. CachedFile = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>),
  88. ?assert(filelib:is_regular(CachedFile)),
  89. {ok, Content} = file:read_file(CachedFile),
  90. ?assertEqual({ok, true},
  91. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)),
  92. {ok, Content} = file:read_file(CachedFile).
  93. badindexchk(Config) ->
  94. Tmp = ?config(tmp_dir, Config),
  95. {Pkg,Vsn} = ?config(pkg, Config),
  96. State = ?config(state, Config),
  97. ?assertMatch({bad_registry_checksum, _Path},
  98. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)),
  99. %% The cached file is there for forensic purposes
  100. Cache = ?config(cache_dir, Config),
  101. ?assert(filelib:is_regular(filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>))).
  102. badpkg(Config) ->
  103. Tmp = ?config(tmp_dir, Config),
  104. {Pkg,Vsn} = ?config(pkg, Config),
  105. State = ?config(state, Config),
  106. ?assertMatch({bad_download, _Path},
  107. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)),
  108. %% The cached file is there for forensic purposes
  109. Cache = ?config(cache_dir, Config),
  110. ?assert(filelib:is_regular(filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>))).
  111. bad_to_good(Config) ->
  112. Tmp = ?config(tmp_dir, Config),
  113. {Pkg,Vsn} = ?config(pkg, Config),
  114. State = ?config(state, Config),
  115. Cache = ?config(cache_dir, Config),
  116. CachedFile = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>),
  117. ?assert(filelib:is_regular(CachedFile)),
  118. {ok, Contents} = file:read_file(CachedFile),
  119. ?assertEqual({ok, true},
  120. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)),
  121. %% Cache has refreshed
  122. ?assert({ok, Contents} =/= file:read_file(CachedFile)).
  123. good_disconnect(Config) ->
  124. Tmp = ?config(tmp_dir, Config),
  125. {Pkg,Vsn} = ?config(pkg, Config),
  126. State = ?config(state, Config),
  127. Cache = ?config(cache_dir, Config),
  128. CachedFile = filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>),
  129. ?assert(filelib:is_regular(CachedFile)),
  130. {ok, Content} = file:read_file(CachedFile),
  131. ?assertEqual({ok, true},
  132. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)),
  133. {ok, Content} = file:read_file(CachedFile).
  134. bad_disconnect(Config) ->
  135. Tmp = ?config(tmp_dir, Config),
  136. {Pkg,Vsn} = ?config(pkg, Config),
  137. State = ?config(state, Config),
  138. ?assertEqual(request_failed,
  139. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)).
  140. %%%%%%%%%%%%%%%
  141. %%% Helpers %%%
  142. %%%%%%%%%%%%%%%
  143. mock_config(Name, Config) ->
  144. Priv = ?config(priv_dir, Config),
  145. CacheRoot = filename:join([Priv, "cache", atom_to_list(Name)]),
  146. TmpDir = filename:join([Priv, "tmp", atom_to_list(Name)]),
  147. T = ets:new(fake_registry, [public]),
  148. ets:insert_new(T, [
  149. {{<<"badindexchk">>,<<"1.0.0">>}, [[], ?bad_checksum]},
  150. {{<<"goodpkg">>,<<"1.0.0">>}, [[], ?good_checksum]},
  151. {{<<"badpkg">>,<<"1.0.0">>}, [[], ?good_checksum]}
  152. ]),
  153. CacheDir = filename:join([CacheRoot, "hex", "com", "test", "packages"]),
  154. filelib:ensure_dir(filename:join([CacheDir, "registry"])),
  155. ok = ets:tab2file(T, filename:join([CacheDir, "registry"])),
  156. %% The state returns us a fake registry
  157. meck:new(rebar_state, [passthrough]),
  158. meck:expect(rebar_state, registry,
  159. fun(_State) -> {ok, fake_registry} end),
  160. meck:expect(rebar_state, get,
  161. fun(_State, rebar_packages_cdn, _Default) ->
  162. "http://test.com/"
  163. end),
  164. meck:new(rebar_dir, [passthrough]),
  165. meck:expect(rebar_dir, global_cache_dir, fun(_) -> CacheRoot end),
  166. %% Cache fetches are mocked -- we assume the server and clients are
  167. %% correctly used.
  168. GoodCache = ?config(good_cache, Config),
  169. {Pkg,Vsn} = ?config(pkg, Config),
  170. PkgFile = <<Pkg/binary, "-", Vsn/binary, ".tar">>,
  171. {ok, PkgContents} = file:read_file(filename:join(?config(data_dir, Config), PkgFile)),
  172. meck:new(httpc, [passthrough, unsticky]),
  173. meck:expect(httpc, request,
  174. fun(get, {_Url, _Opts}, _, _, _) when GoodCache ->
  175. {ok, {{Vsn, 304, <<"Not Modified">>}, [{"etag", ?good_etag}], <<>>}};
  176. (get, {_Url, _Opts}, _, _, _) ->
  177. {ok, {{Vsn, 200, <<"OK">>}, [{"etag", ?good_etag}], PkgContents}}
  178. end),
  179. [{cache_root, CacheRoot},
  180. {cache_dir, CacheDir},
  181. {tmp_dir, TmpDir},
  182. {mock_table, T} | Config].
  183. unmock_config(Config) ->
  184. meck:unload(),
  185. ets:delete(?config(mock_table, Config)).
  186. copy_to_cache({Pkg,Vsn}, Config) ->
  187. Name = <<Pkg/binary, "-", Vsn/binary, ".tar">>,
  188. Source = filename:join(?config(data_dir, Config), Name),
  189. Dest = filename:join(?config(cache_dir, Config), Name),
  190. ec_file:copy(Source, Dest).