forked from rabbitmq/rabbitmq-server
/
list_plugins_command_test.exs
499 lines (394 loc) · 15.1 KB
/
list_plugins_command_test.exs
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
## This Source Code Form is subject to the terms of the Mozilla Public
## License, v. 2.0. If a copy of the MPL was not distributed with this
## file, You can obtain one at https://mozilla.org/MPL/2.0/.
##
## Copyright (c) 2007-2023 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
defmodule ListPluginsCommandTest do
use ExUnit.Case, async: false
import TestHelper
import ExUnit.CaptureIO
@command RabbitMQ.CLI.Plugins.Commands.ListCommand
def reset_enabled_plugins_to_preconfigured_defaults(context) do
set_enabled_plugins(
[:rabbitmq_stomp, :rabbitmq_federation],
:online,
get_rabbit_hostname(),
context[:opts]
)
end
setup_all do
RabbitMQ.CLI.Core.Distribution.start()
node = get_rabbit_hostname()
{:ok, plugins_file} =
:rabbit_misc.rpc_call(node, :application, :get_env, [:rabbit, :enabled_plugins_file])
{:ok, plugins_dir} =
:rabbit_misc.rpc_call(node, :application, :get_env, [:rabbit, :plugins_dir])
rabbitmq_home = :rabbit_misc.rpc_call(node, :code, :lib_dir, [:rabbit])
IO.puts(
"plugins list tests default env: enabled plugins = #{plugins_file}, plugins dir = #{plugins_dir}, RabbitMQ home directory = #{rabbitmq_home}"
)
{:ok, [enabled_plugins]} = :file.consult(plugins_file)
IO.puts(
"plugins list tests will assume that #{Enum.join(enabled_plugins, ",")} is the list of enabled plugins to revert to"
)
opts = %{
enabled_plugins_file: plugins_file,
plugins_dir: plugins_dir,
rabbitmq_home: rabbitmq_home,
minimal: false,
verbose: false,
enabled: false,
implicitly_enabled: false,
ignore_warnings: false
}
on_exit(fn ->
set_enabled_plugins(enabled_plugins, :online, get_rabbit_hostname(), opts)
end)
{:ok, opts: opts}
end
setup context do
reset_enabled_plugins_to_preconfigured_defaults(context)
{
:ok,
opts:
Map.merge(context[:opts], %{
node: get_rabbit_hostname(),
timeout: 1000
})
}
end
test "validate: specifying both --minimal and --verbose is reported as invalid", context do
assert match?(
{:validation_failure, {:bad_argument, _}},
@command.validate([], Map.merge(context[:opts], %{minimal: true, verbose: true}))
)
end
test "validate: specifying multiple patterns is reported as an error", context do
assert @command.validate(["a", "b", "c"], context[:opts]) ==
{:validation_failure, :too_many_args}
end
test "validate: succeeds when multiple plugins directories are used, and one of them does not exist",
context do
opts = get_opts_with_non_existing_plugins_directory(context)
assert @command.validate([], opts) == :ok
end
test "validate: succeeds when multiple plugins directories are used and all of them exist",
context do
opts = get_opts_with_existing_plugins_directory(context)
assert @command.validate([], opts) == :ok
end
test "validate_execution_environment: specifying a non-existent enabled_plugins_file is fine",
context do
assert @command.validate_execution_environment(
["a"],
Map.merge(context[:opts], %{enabled_plugins_file: "none"})
) == :ok
end
test "validate_execution_environment: specifying non existent plugins_dir is reported as an error",
context do
assert @command.validate_execution_environment(
["a"],
Map.merge(context[:opts], %{plugins_dir: "none"})
) ==
{:validation_failure, :plugins_dir_does_not_exist}
end
test "run: reports list of plugins from file for stopped node", context do
reset_enabled_plugins_to_preconfigured_defaults(context)
node = context[:opts][:node]
:ok = :rabbit_misc.rpc_call(node, :application, :stop, [:rabbitmq_stomp])
on_exit(fn ->
:rabbit_misc.rpc_call(node, :application, :start, [:rabbitmq_stomp])
end)
expected_plugins = [
%{name: :rabbitmq_federation, enabled: :enabled, running: false},
%{name: :rabbitmq_stomp, enabled: :enabled, running: false}
]
%{
plugins: actual_plugins
} = @command.run([".*"], Map.merge(context[:opts], %{node: :nonode}))
assert_plugin_states(actual_plugins, expected_plugins)
end
test "run: reports list of started plugins for started node", context do
reset_enabled_plugins_to_preconfigured_defaults(context)
node = context[:opts][:node]
:ok = :rabbit_misc.rpc_call(node, :application, :stop, [:rabbitmq_stomp])
on_exit(fn ->
:rabbit_misc.rpc_call(node, :application, :start, [:rabbitmq_stomp])
end)
expected_plugins = [
%{name: :rabbitmq_federation, enabled: :enabled, running: true},
%{name: :rabbitmq_stomp, enabled: :enabled, running: false}
]
%{
status: :running,
plugins: actual_plugins
} = @command.run([".*"], context[:opts])
assert_plugin_states(actual_plugins, expected_plugins)
end
test "run: in verbose mode, reports plugin description and dependencies", context do
reset_enabled_plugins_to_preconfigured_defaults(context)
expected_plugins = [
%{
name: :rabbitmq_federation,
enabled: :enabled,
running: true,
dependencies: [:amqp_client]
},
%{
name: :rabbitmq_stomp,
enabled: :enabled,
running: true,
dependencies: [:amqp_client]
}
]
%{
plugins: actual_plugins
} = @command.run([".*"], Map.merge(context[:opts], %{verbose: true}))
assert_plugin_states(actual_plugins, expected_plugins)
end
test "run: reports plugin names in minimal mode", context do
reset_enabled_plugins_to_preconfigured_defaults(context)
expected_plugins = [%{name: :rabbitmq_federation}, %{name: :rabbitmq_stomp}]
%{status: :running, plugins: actual_plugins} =
@command.run([".*"], Map.merge(context[:opts], %{minimal: true}))
assert_plugin_states(actual_plugins, expected_plugins)
end
test "run: by default lists all plugins", context do
reset_enabled_plugins_to_preconfigured_defaults(context)
set_enabled_plugins([:rabbitmq_federation], :online, context[:opts][:node], context[:opts])
on_exit(fn ->
set_enabled_plugins(
[:rabbitmq_stomp, :rabbitmq_federation],
:online,
context[:opts][:node],
context[:opts]
)
end)
expected_plugins = [
%{name: :rabbitmq_federation, enabled: :enabled, running: true},
%{name: :rabbitmq_stomp, enabled: :not_enabled, running: false}
]
%{
plugins: actual_plugins
} = @command.run([".*"], context[:opts])
assert_plugin_states(actual_plugins, expected_plugins)
end
test "run: with --enabled flag, lists only explicitly enabled plugins", context do
reset_enabled_plugins_to_preconfigured_defaults(context)
set_enabled_plugins([:rabbitmq_federation], :online, context[:opts][:node], context[:opts])
on_exit(fn ->
set_enabled_plugins(
[:rabbitmq_stomp, :rabbitmq_federation],
:online,
context[:opts][:node],
context[:opts]
)
end)
expected_plugins = [%{name: :rabbitmq_federation, enabled: :enabled, running: true}]
%{
status: :running,
plugins: actual_plugins
} = @command.run([".*"], Map.merge(context[:opts], %{enabled: true}))
assert_plugin_states(actual_plugins, expected_plugins)
end
test "run: with --implicitly_enabled flag lists explicitly and implicitly enabled plugins",
context do
reset_enabled_plugins_to_preconfigured_defaults(context)
set_enabled_plugins([:rabbitmq_federation], :online, context[:opts][:node], context[:opts])
on_exit(fn ->
set_enabled_plugins(
[:rabbitmq_stomp, :rabbitmq_federation],
:online,
context[:opts][:node],
context[:opts]
)
end)
expected_plugins = [%{name: :rabbitmq_federation, enabled: :enabled, running: true}]
%{
status: :running,
plugins: actual_plugins
} = @command.run([".*"], Map.merge(context[:opts], %{implicitly_enabled: true}))
assert_plugin_states(actual_plugins, expected_plugins)
end
test "run: filters plugins by name with pattern provided", context do
reset_enabled_plugins_to_preconfigured_defaults(context)
set_enabled_plugins([:rabbitmq_federation], :online, context[:opts][:node], context[:opts])
on_exit(fn ->
set_enabled_plugins(
[:rabbitmq_stomp, :rabbitmq_federation],
:online,
context[:opts][:node],
context[:opts]
)
end)
%{status: :running, plugins: actual_plugins} =
@command.run(["fede"], Map.merge(context[:opts], %{minimal: true}))
assert_plugin_states(actual_plugins, [%{name: :rabbitmq_federation}])
%{status: :running, plugins: actual_plugins2} =
@command.run(["stomp$"], Map.merge(context[:opts], %{minimal: true}))
assert_plugin_states(actual_plugins2, [%{name: :rabbitmq_stomp}])
end
test "should succeed when using multiple plugins directories, one of them does not exist",
context do
opts = get_opts_with_non_existing_plugins_directory(context)
expected_plugins = [
%{name: :rabbitmq_federation},
%{name: :rabbitmq_stomp}
]
%{status: :running, plugins: actual_plugins} =
@command.run([".*"], Map.merge(opts, %{minimal: true}))
assert_plugin_states(actual_plugins, expected_plugins)
end
test "run: succeeds when using multiple plugins directories, directories do exist and do contain plugins",
context do
reset_enabled_plugins_to_preconfigured_defaults(context)
opts = get_opts_with_existing_plugins_directory(context)
expected_plugins = [%{name: :rabbitmq_federation}, %{name: :rabbitmq_stomp}]
%{status: :running, plugins: actual_plugins} =
@command.run([".*"], Map.merge(opts, %{minimal: true}))
assert_plugin_states(actual_plugins, expected_plugins)
end
test "run: lists plugins when using multiple plugins directories", context do
reset_enabled_plugins_to_preconfigured_defaults(context)
plugins_directory = fixture_plugins_path("plugins-subdirectory-01")
opts = get_opts_with_plugins_directories(context, [plugins_directory])
switch_plugins_directories(context[:opts][:plugins_dir], opts[:plugins_dir])
reset_enabled_plugins_to_preconfigured_defaults(context)
expected_plugins = [
%{name: :mock_rabbitmq_plugins_01},
%{name: :mock_rabbitmq_plugins_02},
%{name: :rabbitmq_federation},
%{name: :rabbitmq_stomp}
]
%{
plugins: actual_plugins
} = @command.run([".*"], Map.merge(opts, %{minimal: true}))
assert_plugin_states(actual_plugins, expected_plugins)
end
test "run: lists the most recent version of every plugin", context do
reset_enabled_plugins_to_preconfigured_defaults(context)
plugins_directory_01 = fixture_plugins_path("plugins-subdirectory-01")
plugins_directory_02 = fixture_plugins_path("plugins-subdirectory-02")
opts =
get_opts_with_plugins_directories(context, [plugins_directory_01, plugins_directory_02])
switch_plugins_directories(context[:opts][:plugins_dir], opts[:plugins_dir])
reset_enabled_plugins_to_preconfigured_defaults(context)
expected_plugins = [
%{
name: :mock_rabbitmq_plugins_01,
enabled: :not_enabled,
running: false,
version: ~c"0.2.0"
},
%{
name: :mock_rabbitmq_plugins_02,
enabled: :not_enabled,
running: false,
version: ~c"0.2.0"
},
%{name: :rabbitmq_federation, enabled: :enabled, running: true},
%{name: :rabbitmq_stomp, enabled: :enabled, running: true}
]
%{
plugins: actual_plugins
} = @command.run([".*"], opts)
assert_plugin_states(actual_plugins, expected_plugins)
end
test "run: reports both running and pending upgrade versions", context do
reset_enabled_plugins_to_preconfigured_defaults(context)
plugins_directory_01 = fixture_plugins_path("plugins-subdirectory-01")
plugins_directory_02 = fixture_plugins_path("plugins-subdirectory-02")
opts = get_opts_with_plugins_directories(context, [plugins_directory_01])
switch_plugins_directories(context[:opts][:plugins_dir], opts[:plugins_dir])
set_enabled_plugins(
[:mock_rabbitmq_plugins_02, :rabbitmq_federation, :rabbitmq_stomp],
:online,
get_rabbit_hostname(),
opts
)
expected_plugins = [
%{
name: :mock_rabbitmq_plugins_01,
enabled: :not_enabled,
running: false,
version: ~c"0.2.0"
},
%{
name: :mock_rabbitmq_plugins_02,
enabled: :enabled,
running: true,
version: ~c"0.1.0",
running_version: ~c"0.1.0"
},
%{name: :rabbitmq_federation, enabled: :enabled, running: true},
%{name: :rabbitmq_stomp, enabled: :enabled, running: true}
]
%{
status: :running,
plugins: actual_plugins
} = @command.run([".*"], opts)
assert_plugin_states(actual_plugins, expected_plugins)
opts =
get_opts_with_plugins_directories(context, [plugins_directory_01, plugins_directory_02])
switch_plugins_directories(context[:opts][:plugins_dir], opts[:plugins_dir])
expected_plugins2 = [
%{
name: :mock_rabbitmq_plugins_01,
enabled: :not_enabled,
running: false,
version: ~c"0.2.0"
},
%{
name: :mock_rabbitmq_plugins_02,
enabled: :enabled,
running: true,
version: ~c"0.2.0",
running_version: ~c"0.1.0"
},
%{name: :rabbitmq_federation, enabled: :enabled, running: true},
%{name: :rabbitmq_stomp, enabled: :enabled, running: true}
]
%{
plugins: actual_plugins2
} = @command.run([".*"], opts)
assert_plugin_states(actual_plugins2, expected_plugins2)
end
test "run: lists all plugins with missing plugins warning control", context do
opts = Map.replace(context[:opts], :ignore_warnings, true)
context = Map.replace(context, :opts, opts)
missing_plugin = :rabbitmq_non_existent
reset_enabled_plugins_to_preconfigured_defaults(context)
set_enabled_plugins(
[:rabbitmq_federation, missing_plugin],
:online,
context[:opts][:node],
context[:opts]
)
on_exit(fn ->
set_enabled_plugins(
[:rabbitmq_stomp, :rabbitmq_federation],
:online,
context[:opts][:node],
context[:opts]
)
end)
expected_plugins = [
%{name: :rabbitmq_federation, enabled: :enabled, running: true},
%{name: :rabbitmq_stomp, enabled: :not_enabled, running: false}
]
opts = context[:opts]
assert capture_io(fn ->
%{
plugins: actual_plugins
} = @command.run([".*"], opts)
assert_plugin_states(actual_plugins, expected_plugins)
end) =~ ~s//
opts = Map.replace(opts, :ignore_warnings, false)
assert capture_io(fn ->
%{
plugins: actual_plugins
} = @command.run([".*"], opts)
assert_plugin_states(actual_plugins, expected_plugins)
end) =~ ~s/WARNING - plugins currently enabled but missing: #{missing_plugin}\n/
end
end