examples_SUITE.erl 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. %% Copyright (c) 2016, Loïc Hoguin <essen@ninenines.eu>
  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(examples_SUITE).
  15. -compile(export_all).
  16. -import(ct_helper, [config/2]).
  17. -import(ct_helper, [doc/1]).
  18. -import(cowboy_test, [gun_open/1]).
  19. %% ct.
  20. all() ->
  21. ct_helper:all(?MODULE).
  22. %% Remove environment variables inherited from Erlang.mk.
  23. init_per_suite(Config) ->
  24. os:unsetenv("ERLANG_MK_TMP"),
  25. os:unsetenv("APPS_DIR"),
  26. os:unsetenv("DEPS_DIR"),
  27. os:unsetenv("ERL_LIBS"),
  28. Config.
  29. end_per_suite(_) ->
  30. ok.
  31. %% Compile, start and stop releases.
  32. do_get_paths(Example0) ->
  33. Example = atom_to_list(Example0),
  34. {ok, CWD} = file:get_cwd(),
  35. Dir = CWD ++ "/../../examples/" ++ Example,
  36. Rel = Dir ++ "/_rel/" ++ Example ++ "_example/bin/" ++ Example ++ "_example",
  37. {Dir, Rel}.
  38. do_compile_and_start(Example) ->
  39. {Dir, Rel} = do_get_paths(Example),
  40. ct:comment("~s~n", [os:cmd("cd " ++ Dir ++ " && make distclean && make all")]),
  41. ct:comment("~s~n", [os:cmd(Rel ++ " stop")]),
  42. ct:comment("~s~n", [os:cmd(Rel ++ " start")]),
  43. timer:sleep(2000),
  44. ok.
  45. do_stop(Example) ->
  46. {_, Rel} = do_get_paths(Example),
  47. ct:comment("~s~n", [os:cmd(Rel ++ " stop")]),
  48. ok.
  49. %% TCP and SSL Hello World.
  50. hello_world(Config) ->
  51. doc("Hello World example."),
  52. try
  53. do_compile_and_start(hello_world),
  54. do_hello_world(tcp, http, Config),
  55. do_hello_world(tcp, http2, Config)
  56. after
  57. do_stop(hello_world)
  58. end.
  59. ssl_hello_world(Config) ->
  60. doc("SSL Hello World example."),
  61. try
  62. do_compile_and_start(ssl_hello_world),
  63. do_hello_world(ssl, http, Config),
  64. do_hello_world(ssl, http2, Config)
  65. after
  66. do_stop(ssl_hello_world)
  67. end.
  68. do_hello_world(Transport, Protocol, Config) ->
  69. Port = case Transport of
  70. tcp -> 8080;
  71. ssl -> 8443
  72. end,
  73. ConnPid = gun_open([{port, Port}, {type, Transport}, {protocol, Protocol}|Config]),
  74. Ref = gun:get(ConnPid, "/"),
  75. {response, nofin, 200, _} = gun:await(ConnPid, Ref),
  76. {ok, <<"Hello world!">>} = gun:await_body(ConnPid, Ref),
  77. ok.