autobahn_SUITE.erl 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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(inets),
  32. application:start(cowboy),
  33. %% /tmp must be used as the parent directory for the virtualenv because
  34. %% the directory names used in CT are so long that the interpreter path
  35. %% in the scripts generated by virtualenv get so long that the system
  36. %% refuses to execute them.
  37. EnvPath = "/tmp/cowboy_autobahn_env",
  38. os:putenv("AB_TESTS_ENV", EnvPath),
  39. os:putenv("AB_TESTS_PRIV", ?config(priv_dir, Config)),
  40. BinPath = filename:join(?config(data_dir, Config), "test.py"),
  41. Stdout = os:cmd(BinPath ++ " setup"),
  42. ct:log("~s~n", [Stdout]),
  43. case string:str(Stdout, "AB-TESTS-SETUP-OK") of
  44. 0 -> erlang:error(failed);
  45. _ -> [{env_path, EnvPath},{bin_path,BinPath}|Config]
  46. end.
  47. end_per_suite(_Config) ->
  48. os:cmd("deactivate"),
  49. application:stop(cowboy),
  50. application:stop(inets),
  51. ok.
  52. init_per_group(autobahn, Config) ->
  53. Port = 33080,
  54. cowboy:start_listener(autobahn, 100,
  55. cowboy_tcp_transport, [{port, Port}],
  56. cowboy_http_protocol, [{dispatch, init_dispatch()}]
  57. ),
  58. [{port, Port}|Config].
  59. end_per_group(Listener, _Config) ->
  60. cowboy:stop_listener(Listener),
  61. ok.
  62. %% Dispatch configuration.
  63. init_dispatch() ->
  64. [{[<<"localhost">>], [
  65. {[<<"echo">>], websocket_echo_handler, []}]}].
  66. %% autobahn cases
  67. run_tests(Config) ->
  68. PrivDir = ?config(priv_dir, Config),
  69. IndexFile = filename:join([PrivDir, "reports", "servers", "index.html"]),
  70. ct:log("<h2><a href=\"~s\">Full Test Results Report</a></h2>~n", [IndexFile]),
  71. BinPath = ?config(bin_path, Config),
  72. Stdout = os:cmd(BinPath ++ " test"),
  73. ct:log("~s~n", [Stdout]),
  74. case string:str(Stdout, "AB-TESTS-TEST-OK") of
  75. 0 -> erlang:error(failed);
  76. _ -> ok
  77. end,
  78. {ok, IndexHTML} = file:read_file(IndexFile),
  79. case binary:match(IndexHTML, <<"Fail">>) of
  80. {_, _} -> erlang:error(failed);
  81. nomatch -> ok
  82. end.