autobahn_SUITE.erl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. %% Copyright (c) 2011, Magnus Klaar <magnus.klaar@gmail.com>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. -module(autobahn_SUITE).
  15. %% This CT suite reuses the websocket server test suite from the Autobahn
  16. %% project. The Autobahn project is a websocket implementation for Python.
  17. %% Given that we don't expect to find the packages and tools to properly
  18. %% set up and run such a test on a system used primarily for Erlang devlopment
  19. %% this test suite is not included in the default 'ct' target in the makefile.
  20. -include_lib("common_test/include/ct.hrl").
  21. -export([all/0, groups/0, init_per_suite/1, end_per_suite/1,
  22. init_per_group/2, end_per_group/2]). %% ct.
  23. -export([run_tests/1]). %% autobahn.
  24. %% ct.
  25. all() ->
  26. [{group, autobahn}].
  27. groups() ->
  28. BaseTests = [run_tests],
  29. [{autobahn, [], BaseTests}].
  30. init_per_suite(Config) ->
  31. application:start(crypto),
  32. application:start(cowlib),
  33. application:start(ranch),
  34. application:start(cowboy),
  35. %% /tmp must be used as the parent directory for the virtualenv because
  36. %% the directory names used in CT are so long that the interpreter path
  37. %% in the scripts generated by virtualenv get so long that the system
  38. %% refuses to execute them.
  39. EnvPath = "/tmp/cowboy_autobahn_env",
  40. os:putenv("AB_TESTS_ENV", EnvPath),
  41. os:putenv("AB_TESTS_PRIV", ?config(priv_dir, Config)),
  42. BinPath = filename:join(?config(data_dir, Config), "test.py"),
  43. Stdout = os:cmd(BinPath ++ " setup"),
  44. ct:log("~s~n", [Stdout]),
  45. case string:str(Stdout, "AB-TESTS-SETUP-OK") of
  46. 0 -> erlang:error(failed);
  47. _ -> [{env_path, EnvPath},{bin_path,BinPath}|Config]
  48. end.
  49. end_per_suite(_Config) ->
  50. os:cmd("deactivate"),
  51. application:stop(cowboy),
  52. application:stop(ranch),
  53. application:stop(cowlib),
  54. application:stop(crypto),
  55. ok.
  56. init_per_group(autobahn, Config) ->
  57. Port = 33080,
  58. cowboy:start_http(autobahn, 100, [{port, Port}], [
  59. {env, [{dispatch, init_dispatch()}]}
  60. ]),
  61. [{port, Port}|Config].
  62. end_per_group(Listener, _Config) ->
  63. cowboy:stop_listener(Listener),
  64. ok.
  65. %% Dispatch configuration.
  66. init_dispatch() ->
  67. cowboy_router:compile([{"localhost", [
  68. {"/echo", autobahn_echo, []}]}]).
  69. %% autobahn cases
  70. run_tests(Config) ->
  71. PrivDir = ?config(priv_dir, Config),
  72. IndexFile = filename:join([PrivDir, "reports", "servers", "index.html"]),
  73. ct:log("<h2><a href=\"~s\">Full Test Results Report</a></h2>~n", [IndexFile]),
  74. BinPath = ?config(bin_path, Config),
  75. Stdout = os:cmd(BinPath ++ " test"),
  76. ct:log("~s~n", [Stdout]),
  77. case string:str(Stdout, "AB-TESTS-TEST-OK") of
  78. 0 -> erlang:error(failed);
  79. _ -> ok
  80. end,
  81. {ok, IndexHTML} = file:read_file(IndexFile),
  82. case length(binary:matches(IndexHTML, <<"case_failed">>)) > 2 of
  83. true -> erlang:error(failed);
  84. false -> ok
  85. end.