autobahn_SUITE.erl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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_http(autobahn, 100, [{port, Port}], [
  55. {dispatch, init_dispatch()}
  56. ]),
  57. [{port, Port}|Config].
  58. end_per_group(Listener, _Config) ->
  59. cowboy:stop_listener(Listener),
  60. ok.
  61. %% Dispatch configuration.
  62. init_dispatch() ->
  63. [{[<<"localhost">>], [
  64. {[<<"echo">>], websocket_echo_handler, []}]}].
  65. %% autobahn cases
  66. run_tests(Config) ->
  67. PrivDir = ?config(priv_dir, Config),
  68. IndexFile = filename:join([PrivDir, "reports", "servers", "index.html"]),
  69. ct:log("<h2><a href=\"~s\">Full Test Results Report</a></h2>~n", [IndexFile]),
  70. BinPath = ?config(bin_path, Config),
  71. Stdout = os:cmd(BinPath ++ " test"),
  72. ct:log("~s~n", [Stdout]),
  73. case string:str(Stdout, "AB-TESTS-TEST-OK") of
  74. 0 -> erlang:error(failed);
  75. _ -> ok
  76. end,
  77. {ok, IndexHTML} = file:read_file(IndexFile),
  78. case binary:match(IndexHTML, <<"Fail">>) of
  79. {_, _} -> erlang:error(failed);
  80. nomatch -> ok
  81. end.