summaryrefslogtreecommitdiff
path: root/test/elixir/lib/step/start.ex
blob: ea7c70f5a8312c167083addcb6e39df2783b2d2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
defmodule Couch.Test.Setup.Step.Start do
  @moduledoc """
  Step to start a set of couchdb applications. By default it starts
  list of applications from DEFAULT_APPS macro defined in `test_util.erl`.
  At the time of writing this list included:
    - inets
    - ibrowse
    - ssl
    - config
    - couch_epi
    - couch_event
    - couch

  It is possible to specify additional list of applications to start.

  This setup is also maintains `clustered_url` and `backdoor_url` for future use.
  The value for `clustered_url` could be nil if :chttpd app is not included in extra_apps.

  Example
    setup
      |> Setup.Step.Start.new(:start, extra_apps: [:fabric, :chttpd])
    ...
      |> Setup.run
    ...

    started_apps = Setup.Step.Start.apps
    clustered_url = setup |> Setup.get(:start) |> Setup.Step.Start.clustered_url
    backdoor_url = setup |> Setup.get(:start) |> Setup.Step.Start.backdoor_url
  """
  alias Couch.Test.Setup
  alias Couch.Test.Setup.Step

  defstruct [:test_ctx, :extra_apps, :clustered_url, :backdoor_url]

  def new(setup, id, extra_apps: extra_apps) do
    setup |> Setup.step(id, %__MODULE__{extra_apps: extra_apps || []})
  end

  def setup(setup, %__MODULE__{extra_apps: extra_apps} = step) do
    test_config = setup |> Setup.get(:test_config) |> Step.Config.get()
    protocol = test_config[:backdoor][:protocol] || "http"
    test_ctx = :test_util.start_couch(extra_apps)
    addr = :config.get('couch_httpd', 'bind_address', '127.0.0.1')
    port = :mochiweb_socket_server.get(:couch_httpd, :port)
    backdoor_url = "#{protocol}://#{addr}:#{port}"
    clustered_url =
      if :chttpd in extra_apps do
        protocol = test_config[:clustered][:protocol] || "http"
        addr = :config.get('chttpd', 'bind_address', '127.0.0.1')
        port = :mochiweb_socket_server.get(:chttpd, :port)
        "#{protocol}://#{addr}:#{port}"
      else
        nil
      end
    %{step |
      test_ctx: test_ctx,
      clustered_url: clustered_url,
      backdoor_url: backdoor_url
    }
  end

  def teardown(_setup, %___MODULE__{test_ctx: test_ctx}) do
    :test_util.stop_couch(test_ctx)
  end

  def backdoor_url(%__MODULE__{backdoor_url: url}) do
    url
  end

  def clustered_url(%__MODULE__{clustered_url: url}) do
    url
  end

  def extra_apps(%__MODULE__{extra_apps: apps}) do
    apps
  end

  @doc """
  Returns list of currently running applications
  """
  def apps() do
    for {x, _, _} <- Application.started_applications, do: x
  end

end