Remove quack, ensure adapter is finch
[akkoma] / test / support / cluster.ex
1 defmodule Pleroma.Cluster do
2 @moduledoc """
3 Facilities for managing a cluster of slave VM's for federated testing.
4
5 ## Spawning the federated cluster
6
7 `spawn_cluster/1` spawns a map of slave nodes that are started
8 within the running VM. During startup, the slave node is sent all configuration
9 from the parent node, as well as all code. After receiving configuration and
10 code, the slave then starts all applications currently running on the parent.
11 The configuration passed to `spawn_cluster/1` overrides any parent application
12 configuration for the provided OTP app and key. This is useful for customizing
13 the Ecto database, Phoenix webserver ports, etc.
14
15 For example, to start a single federated VM named ":federated1", with the
16 Pleroma Endpoint running on port 4123, and with a database named
17 "pleroma_test1", you would run:
18
19 endpoint_conf = Application.fetch_env!(:pleroma, Pleroma.Web.Endpoint)
20 repo_conf = Application.fetch_env!(:pleroma, Pleroma.Repo)
21
22 Pleroma.Cluster.spawn_cluster(%{
23 :"federated1@127.0.0.1" => [
24 {:pleroma, Pleroma.Repo, Keyword.merge(repo_conf, database: "pleroma_test1")},
25 {:pleroma, Pleroma.Web.Endpoint,
26 Keyword.merge(endpoint_conf, http: [port: 4011], url: [port: 4011], server: true)}
27 ]
28 })
29
30 *Note*: application configuration for a given key is not merged,
31 so any customization requires first fetching the existing values
32 and merging yourself by providing the merged configuration,
33 such as above with the endpoint config and repo config.
34
35 ## Executing code within a remote node
36
37 Use the `within/2` macro to execute code within the context of a remote
38 federated node. The code block captures all local variable bindings from
39 the parent's context and returns the result of the expression after executing
40 it on the remote node. For example:
41
42 import Pleroma.Cluster
43
44 parent_value = 123
45
46 result =
47 within :"federated1@127.0.0.1" do
48 {node(), parent_value}
49 end
50
51 assert result == {:"federated1@127.0.0.1, 123}
52
53 *Note*: while local bindings are captured and available within the block,
54 other parent contexts like required, aliased, or imported modules are not
55 in scope. Those will need to be reimported/aliases/required within the block
56 as `within/2` is a remote procedure call.
57 """
58
59 @extra_apps Pleroma.Mixfile.application()[:extra_applications]
60
61 @doc """
62 Spawns the default Pleroma federated cluster.
63
64 Values before may be customized as needed for the test suite.
65 """
66 def spawn_default_cluster do
67 endpoint_conf = Application.fetch_env!(:pleroma, Pleroma.Web.Endpoint)
68 repo_conf = Application.fetch_env!(:pleroma, Pleroma.Repo)
69
70 spawn_cluster(%{
71 :"federated1@127.0.0.1" => [
72 {:pleroma, Pleroma.Repo, Keyword.merge(repo_conf, database: "pleroma_test_federated1")},
73 {:pleroma, Pleroma.Web.Endpoint,
74 Keyword.merge(endpoint_conf, http: [port: 4011], url: [port: 4011], server: true)}
75 ],
76 :"federated2@127.0.0.1" => [
77 {:pleroma, Pleroma.Repo, Keyword.merge(repo_conf, database: "pleroma_test_federated2")},
78 {:pleroma, Pleroma.Web.Endpoint,
79 Keyword.merge(endpoint_conf, http: [port: 4012], url: [port: 4012], server: true)}
80 ]
81 })
82 end
83
84 @doc """
85 Spawns a configured map of federated nodes.
86
87 See `Pleroma.Cluster` module documentation for details.
88 """
89 def spawn_cluster(node_configs) do
90 # Turn node into a distributed node with the given long name
91 :net_kernel.start([:"primary@127.0.0.1"])
92
93 # Allow spawned nodes to fetch all code from this node
94 {:ok, _} = :erl_boot_server.start([])
95 allow_boot("127.0.0.1")
96
97 silence_logger_warnings(fn ->
98 node_configs
99 |> Enum.map(&Task.async(fn -> start_peer(&1) end))
100 |> Enum.map(&Task.await(&1, 90_000))
101 end)
102 end
103
104 @doc """
105 Executes block of code again remote node.
106
107 See `Pleroma.Cluster` module documentation for details.
108 """
109 defmacro within(node, do: block) do
110 quote do
111 rpc(unquote(node), unquote(__MODULE__), :eval_quoted, [
112 unquote(Macro.escape(block)),
113 binding()
114 ])
115 end
116 end
117
118 @doc false
119 def eval_quoted(block, binding) do
120 {result, _binding} = Code.eval_quoted(block, binding, __ENV__)
121 result
122 end
123
124 defp start_peer({node_host, override_configs}) do
125 log(node_host, "booting federated VM")
126 {:ok, node} = :peer.start(%{host: ~c"127.0.0.1", name: node_name(node_host), args: vm_args()})
127 add_code_paths(node)
128 load_apps_and_transfer_configuration(node, override_configs)
129 ensure_apps_started(node)
130 {:ok, node}
131 end
132
133 def rpc(node, module, function, args) do
134 :rpc.block_call(node, module, function, args)
135 end
136
137 defp vm_args do
138 ~c"-loader inet -hosts 127.0.0.1 -setcookie #{:erlang.get_cookie()}"
139 end
140
141 defp allow_boot(host) do
142 {:ok, ipv4} = :inet.parse_ipv4_address(~c"#{host}")
143 :ok = :erl_boot_server.add_slave(ipv4)
144 end
145
146 defp add_code_paths(node) do
147 rpc(node, :code, :add_paths, [:code.get_path()])
148 end
149
150 defp load_apps_and_transfer_configuration(node, override_configs) do
151 Enum.each(Application.loaded_applications(), fn {app_name, _, _} ->
152 app_name
153 |> Application.get_all_env()
154 |> Enum.each(fn {key, primary_config} ->
155 rpc(node, Application, :put_env, [app_name, key, primary_config, [persistent: true]])
156 end)
157 end)
158
159 Enum.each(override_configs, fn {app_name, key, val} ->
160 rpc(node, Application, :put_env, [app_name, key, val, [persistent: true]])
161 end)
162 end
163
164 defp log(node, msg), do: IO.puts("[#{node}] #{msg}")
165
166 defp ensure_apps_started(node) do
167 loaded_names = Enum.map(Application.loaded_applications(), fn {name, _, _} -> name end)
168 app_names = @extra_apps ++ (loaded_names -- @extra_apps)
169
170 rpc(node, Application, :ensure_all_started, [:mix])
171 rpc(node, Mix, :env, [Mix.env()])
172 rpc(node, __MODULE__, :prepare_database, [])
173
174 log(node, "starting application")
175
176 Enum.reduce(app_names, MapSet.new(), fn app, loaded ->
177 if Enum.member?(loaded, app) do
178 loaded
179 else
180 {:ok, started} = rpc(node, Application, :ensure_all_started, [app])
181 MapSet.union(loaded, MapSet.new(started))
182 end
183 end)
184 end
185
186 @doc false
187 def prepare_database do
188 log(node(), "preparing database")
189 repo_config = Application.get_env(:pleroma, Pleroma.Repo)
190 repo_config[:adapter].storage_down(repo_config)
191 repo_config[:adapter].storage_up(repo_config)
192
193 {:ok, _, _} =
194 Ecto.Migrator.with_repo(Pleroma.Repo, fn repo ->
195 Ecto.Migrator.run(repo, :up, log: false, all: true)
196 end)
197
198 Ecto.Adapters.SQL.Sandbox.mode(Pleroma.Repo, :manual)
199 {:ok, _} = Application.ensure_all_started(:ex_machina)
200 end
201
202 defp silence_logger_warnings(func) do
203 prev_level = Logger.level()
204 Logger.configure(level: :error)
205 res = func.()
206 Logger.configure(level: prev_level)
207
208 res
209 end
210
211 defp node_name(node_host) do
212 node_host
213 |> to_string()
214 |> String.split("@")
215 |> Enum.at(0)
216 |> String.to_atom()
217 end
218 end