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
|
## The contents of this file are subject to the Mozilla Public License
## Version 1.1 (the "License"); you may not use this file except in
## compliance with the License. You may obtain a copy of the License
## at https://www.mozilla.org/MPL/
##
## Software distributed under the License is distributed on an "AS IS"
## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
## the License for the specific language governing rights and
## limitations under the License.
##
## The Original Code is RabbitMQ.
##
## The Initial Developer of the Original Code is GoPivotal, Inc.
## Copyright (c) 2007-2019 Pivotal Software, Inc. All rights reserved.
defmodule EvalCommandTest do
use ExUnit.Case, async: false
import TestHelper
import ExUnit.CaptureIO
@command RabbitMQ.CLI.Ctl.Commands.EvalCommand
setup_all do
RabbitMQ.CLI.Core.Distribution.start()
:ok
end
setup _ do
{:ok, opts: %{node: get_rabbit_hostname()}}
end
test "validate: providing too few arguments fails validation" do
assert @command.validate([], %{}) == {:validation_failure, :not_enough_args}
end
test "validate: empty expression to eval fails validation" do
assert @command.validate([""], %{}) == {:validation_failure, "Expression must not be blank"}
assert @command.validate(["", "foo"], %{}) == {:validation_failure, "Expression must not be blank"}
end
test "validate: syntax error in expression to eval fails validation" do
assert @command.validate(["foo bar"], %{}) == {:validation_failure, "syntax error before: bar"}
assert @command.validate(["foo bar", "foo"], %{}) == {:validation_failure, "syntax error before: bar"}
end
test "run: request to a non-existent node returns nodedown", _context do
target = :jake@thedog
opts = %{node: target}
assert @command.run(["ok."], opts) == {:badrpc, :nodedown}
end
test "run: evaluates provided Erlang expression", context do
assert @command.run(["foo."], context[:opts]) == {:ok, :foo}
assert @command.run(["length([1,2,3])."], context[:opts]) == {:ok, 3}
assert @command.run(["lists:sum([1,2,3])."], context[:opts]) == {:ok, 6}
{:ok, apps} = @command.run(["application:loaded_applications()."], context[:opts])
assert is_list(apps)
end
test "run: evaluates provided expression on the target server node", context do
{:ok, apps} = @command.run(["application:loaded_applications()."], context[:opts])
assert is_list(apps)
assert List.keymember?(apps, :rabbit, 0)
end
test "run: returns stdout output", context do
assert capture_io(fn ->
assert @command.run(["io:format(\"output\")."], context[:opts]) == {:ok, :ok}
end) == "output"
end
test "run: passes parameters to the expression as positional/numerical variables", context do
assert @command.run(["binary_to_atom(_1, utf8).", "foo"], context[:opts]) == {:ok, :foo}
assert @command.run(["{_1, _2}.", "foo", "bar"], context[:opts]) == {:ok, {"foo", "bar"}}
end
test "run: passes globally recognised options as named variables", context do
assert @command.run(["{_vhost, _node}."], Map.put(context[:opts], :vhost, "a-node")) ==
{:ok, {"a-node", context[:opts][:node]}}
end
end
|