Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

100 Zeilen
3.7 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].
  12. init_per_suite(Config) ->
  13. application:start(meck),
  14. Config.
  15. end_per_suite(_Config) ->
  16. application:stop(meck).
  17. init_per_testcase(good_uncached=Name, Config0) ->
  18. Config = [{good_cache, false},
  19. {pkg, {<<"goodpkg">>, <<"1.0.0">>}}
  20. | Config0],
  21. mock_config(Name, Config);
  22. init_per_testcase(good_cached=Name, Config0) ->
  23. Pkg = {<<"goodpkg">>, <<"1.0.0">>},
  24. Config1 = [{good_cache, true},
  25. {pkg, Pkg}
  26. | Config0],
  27. Config = mock_config(Name, Config1),
  28. copy_to_cache(Pkg, Config),
  29. Config.
  30. end_per_testcase(_, Config) ->
  31. unmock_config(Config),
  32. Config.
  33. mock_config(Name, Config) ->
  34. Priv = ?config(priv_dir, Config),
  35. CacheRoot = filename:join([Priv, "cache", atom_to_list(Name)]),
  36. TmpDir = filename:join([Priv, "tmp", atom_to_list(Name)]),
  37. T = ets:new(fake_registry, [public]),
  38. ets:insert_new(T, [
  39. {{<<"badindexchk">>,<<"1.0.0">>}, [[], ?bad_checksum]},
  40. {{<<"goodpkg">>,<<"1.0.0">>}, [[], ?good_checksum]},
  41. {{<<"badpkg">>,<<"1.0.0">>}, [[], ?good_checksum]}
  42. ]),
  43. CacheDir = filename:join([CacheRoot, "hex", "com", "test", "packages"]),
  44. filelib:ensure_dir(filename:join([CacheDir, "registry"])),
  45. ok = ets:tab2file(T, filename:join([CacheDir, "registry"])),
  46. %% The state returns us a fake registry
  47. meck:new(rebar_state, [passthrough]),
  48. meck:expect(rebar_state, registry,
  49. fun(_State) -> {ok, fake_registry} end),
  50. meck:expect(rebar_state, get,
  51. fun(_State, rebar_packages_cdn, _Default) ->
  52. "http://test.com/"
  53. end),
  54. meck:new(rebar_dir, [passthrough]),
  55. meck:expect(rebar_dir, global_cache_dir, fun(_) -> CacheRoot end),
  56. %% Cache fetches are mocked -- we assume the server and clients are
  57. %% correctly used.
  58. GoodCache = ?config(good_cache, Config),
  59. {Pkg,Vsn} = ?config(pkg, Config),
  60. PkgFile = <<Pkg/binary, "-", Vsn/binary, ".tar">>,
  61. {ok, PkgContents} = file:read_file(filename:join(?config(data_dir, Config), PkgFile)),
  62. meck:new(httpc, [passthrough, unsticky]),
  63. meck:expect(httpc, request,
  64. fun(get, {_Url, _Opts}, _, _) when GoodCache ->
  65. {ok, {{Vsn, 304, <<"Not Modified">>}, [{"etag", ?good_etag}], <<>>}};
  66. (get, {_Url, _Opts}, _, _) ->
  67. {ok, {{Vsn, 200, <<"OK">>}, [{"etag", ?good_etag}], PkgContents}}
  68. end),
  69. [{cache_root, CacheRoot},
  70. {cache_dir, CacheDir},
  71. {tmp_dir, TmpDir},
  72. {mock_table, T} | Config].
  73. unmock_config(Config) ->
  74. meck:unload(),
  75. ets:delete(?config(mock_table, Config)).
  76. copy_to_cache({Pkg,Vsn}, Config) ->
  77. Name = <<Pkg/binary, "-", Vsn/binary, ".tar">>,
  78. Source = filename:join(?config(data_dir, Config), Name),
  79. Dest = filename:join(?config(cache_dir, Config), Name),
  80. ec_file:copy(Source, Dest).
  81. good_uncached(Config) ->
  82. Tmp = ?config(tmp_dir, Config),
  83. {Pkg,Vsn} = ?config(pkg, Config),
  84. State = ?config(state, Config),
  85. ?assertEqual({ok, true},
  86. rebar_pkg_resource:download(Tmp, {pkg, Pkg, Vsn}, State)),
  87. Cache = ?config(cache_dir, Config),
  88. ?assert(filelib:is_regular(filename:join(Cache, <<Pkg/binary, "-", Vsn/binary, ".tar">>))).