autobahn_SUITE.erl 3.2 KB

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