|
| 1 | +defmodule TestServer.SSH do |
| 2 | + @moduledoc false |
| 3 | + |
| 4 | + alias TestServer.{InstanceManager, SSH} |
| 5 | + |
| 6 | + @spec start(keyword()) :: {:ok, pid()} |
| 7 | + def start(options \\ []) do |
| 8 | + case ExUnit.fetch_test_supervisor() do |
| 9 | + {:ok, _sup} -> start_with_ex_unit(options) |
| 10 | + :error -> raise ArgumentError, "can only be called in a test process" |
| 11 | + end |
| 12 | + end |
| 13 | + |
| 14 | + defp start_with_ex_unit(options) do |
| 15 | + [_first_module_entry | stacktrace] = get_stacktrace() |
| 16 | + caller = self() |
| 17 | + |
| 18 | + options = |
| 19 | + options |
| 20 | + |> Keyword.put_new(:caller, caller) |
| 21 | + |> Keyword.put_new(:stacktrace, stacktrace) |
| 22 | + |
| 23 | + case InstanceManager.start_instance(caller, SSH.Instance.child_spec(options)) do |
| 24 | + {:ok, instance} -> |
| 25 | + put_ex_unit_on_exit_callback(instance) |
| 26 | + {:ok, instance} |
| 27 | + |
| 28 | + {:error, error} -> |
| 29 | + raise_start_failure({:error, error}) |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + defp put_ex_unit_on_exit_callback(instance) do |
| 34 | + ExUnit.Callbacks.on_exit(fn -> |
| 35 | + if Process.alive?(instance) do |
| 36 | + verify_handlers!(:exec, instance) |
| 37 | + verify_handlers!(:shell, instance) |
| 38 | + stop(instance) |
| 39 | + end |
| 40 | + end) |
| 41 | + end |
| 42 | + |
| 43 | + defp verify_handlers!(type, instance) do |
| 44 | + handlers_fn = |
| 45 | + if type == :exec, do: &SSH.Instance.exec_handlers/1, else: &SSH.Instance.shell_handlers/1 |
| 46 | + |
| 47 | + instance |
| 48 | + |> handlers_fn.() |
| 49 | + |> Enum.reject(& &1.suspended) |
| 50 | + |> case do |
| 51 | + [] -> |
| 52 | + :ok |
| 53 | + |
| 54 | + active -> |
| 55 | + raise """ |
| 56 | + #{SSH.Instance.format_instance(instance)} did not receive #{type} requests for these handlers before the test ended: |
| 57 | +
|
| 58 | + #{SSH.Instance.format_handlers(active)} |
| 59 | + """ |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + @spec stop(pid()) :: :ok | {:error, term()} |
| 64 | + def stop(instance) do |
| 65 | + instance_alive!(instance) |
| 66 | + InstanceManager.stop_instance(instance) |
| 67 | + end |
| 68 | + |
| 69 | + @spec address() :: {binary(), :inet.port_number()} |
| 70 | + def address, do: address(fetch_instance!()) |
| 71 | + |
| 72 | + @spec address(pid()) :: {binary(), :inet.port_number()} |
| 73 | + def address(instance) do |
| 74 | + instance_alive!(instance) |
| 75 | + options = SSH.Instance.get_options(instance) |
| 76 | + {"localhost", Keyword.fetch!(options, :port)} |
| 77 | + end |
| 78 | + |
| 79 | + @spec exec(keyword()) :: :ok |
| 80 | + def exec(options) when is_list(options) do |
| 81 | + {:ok, instance} = autostart() |
| 82 | + exec(instance, options) |
| 83 | + end |
| 84 | + |
| 85 | + @spec exec(pid(), keyword()) :: :ok |
| 86 | + def exec(instance, options) when is_pid(instance) and is_list(options) do |
| 87 | + instance_alive!(instance) |
| 88 | + [_first_module_entry | stacktrace] = get_stacktrace() |
| 89 | + options = Keyword.put_new(options, :to, &default_exec_handler/2) |
| 90 | + {:ok, _handler} = SSH.Instance.register(instance, {:exec, options, stacktrace}) |
| 91 | + :ok |
| 92 | + end |
| 93 | + |
| 94 | + defp default_exec_handler(_cmd, state), do: {:reply, {0, "", ""}, state} |
| 95 | + |
| 96 | + @spec shell(keyword()) :: :ok |
| 97 | + def shell(options) when is_list(options) do |
| 98 | + {:ok, instance} = autostart() |
| 99 | + shell(instance, options) |
| 100 | + end |
| 101 | + |
| 102 | + @spec shell(pid(), keyword()) :: :ok |
| 103 | + def shell(instance, options) when is_pid(instance) and is_list(options) do |
| 104 | + instance_alive!(instance) |
| 105 | + [_first_module_entry | stacktrace] = get_stacktrace() |
| 106 | + options = Keyword.put_new(options, :to, &default_shell_handler/2) |
| 107 | + {:ok, _handler} = SSH.Instance.register(instance, {:shell, options, stacktrace}) |
| 108 | + :ok |
| 109 | + end |
| 110 | + |
| 111 | + defp default_shell_handler(data, state), do: {:reply, data, state} |
| 112 | + |
| 113 | + defp autostart do |
| 114 | + case fetch_instance() do |
| 115 | + :error -> start() |
| 116 | + {:ok, instance} -> {:ok, instance} |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + defp fetch_instance! do |
| 121 | + case fetch_instance() do |
| 122 | + :error -> raise "No current #{inspect(SSH.Instance)} running" |
| 123 | + {:ok, instance} -> instance |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + defp fetch_instance do |
| 128 | + instances = InstanceManager.get_by_caller(self()) || [] |
| 129 | + ssh_instances = Enum.filter(instances, &ssh_instance?/1) |
| 130 | + |
| 131 | + case ssh_instances do |
| 132 | + [] -> |
| 133 | + :error |
| 134 | + |
| 135 | + [instance] -> |
| 136 | + {:ok, instance} |
| 137 | + |
| 138 | + [_first | _rest] = multiple -> |
| 139 | + [{m, f, a, _} | _] = get_stacktrace() |
| 140 | + |
| 141 | + formatted = |
| 142 | + multiple |
| 143 | + |> Enum.map(&{&1, SSH.Instance.get_options(&1)}) |
| 144 | + |> Enum.with_index() |
| 145 | + |> Enum.map_join("\n\n", fn {{instance, options}, index} -> |
| 146 | + """ |
| 147 | + ##{index + 1}: #{SSH.Instance.format_instance(instance)} |
| 148 | + #{Enum.map_join(options[:stacktrace], "\n ", &Exception.format_stacktrace_entry/1)} |
| 149 | + """ |
| 150 | + end) |
| 151 | + |
| 152 | + raise """ |
| 153 | + Multiple #{inspect(SSH.Instance)}'s running, please pass instance to `#{inspect(m)}.#{f}/#{a}`. |
| 154 | +
|
| 155 | + #{formatted} |
| 156 | + """ |
| 157 | + end |
| 158 | + end |
| 159 | + |
| 160 | + defp ssh_instance?(pid) do |
| 161 | + SSH.Instance.get_options(pid)[:protocol] == :ssh |
| 162 | + rescue |
| 163 | + _ -> false |
| 164 | + end |
| 165 | + |
| 166 | + defp instance_alive!(instance) do |
| 167 | + unless Process.alive?(instance), |
| 168 | + do: raise("#{SSH.Instance.format_instance(instance)} is not running") |
| 169 | + end |
| 170 | + |
| 171 | + defp raise_start_failure({:error, {{:EXIT, reason}, _spec}}) do |
| 172 | + raise_start_failure({:error, reason}) |
| 173 | + end |
| 174 | + |
| 175 | + defp raise_start_failure({:error, error}) do |
| 176 | + raise """ |
| 177 | + EXIT when starting #{inspect(SSH.Instance)}: |
| 178 | +
|
| 179 | + #{Exception.format_exit(error)} |
| 180 | + """ |
| 181 | + end |
| 182 | + |
| 183 | + defp get_stacktrace do |
| 184 | + {:current_stacktrace, [{Process, :info, _, _} | stacktrace]} = |
| 185 | + Process.info(self(), :current_stacktrace) |
| 186 | + |
| 187 | + first_module_entry = |
| 188 | + stacktrace |
| 189 | + |> Enum.reverse() |
| 190 | + |> Enum.find(fn {mod, _, _, _} -> mod == __MODULE__ end) |
| 191 | + |
| 192 | + [first_module_entry] ++ prune_stacktrace(stacktrace) |
| 193 | + end |
| 194 | + |
| 195 | + defp prune_stacktrace([{__MODULE__, _, _, _} | t]), do: prune_stacktrace(t) |
| 196 | + defp prune_stacktrace([{ExUnit.Assertions, _, _, _} | t]), do: prune_stacktrace(t) |
| 197 | + defp prune_stacktrace([{ExUnit.Runner, _, _, _} | _]), do: [] |
| 198 | + defp prune_stacktrace([h | t]), do: [h | prune_stacktrace(t)] |
| 199 | + defp prune_stacktrace([]), do: [] |
| 200 | +end |
0 commit comments