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
|
## 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-2020 VMware, Inc. or its affiliates. All rights reserved.
defmodule ExportDefinitionsCommandTest do
use ExUnit.Case, async: false
import TestHelper
@command RabbitMQ.CLI.Ctl.Commands.ExportDefinitionsCommand
setup_all do
RabbitMQ.CLI.Core.Distribution.start()
:ok
end
setup context do
{:ok, opts: %{
node: get_rabbit_hostname(),
timeout: context[:test_timeout] || 30000,
format: context[:format] || "json"
}}
end
test "merge_defaults: defaults to JSON for format" do
assert @command.merge_defaults([valid_file_path()], %{}) ==
{[valid_file_path()], %{format: "json"}}
end
test "merge_defaults: defaults to --silent if target is stdout" do
assert @command.merge_defaults(["-"], %{}) == {["-"], %{format: "json", silent: true}}
end
test "merge_defaults: format is case insensitive" do
assert @command.merge_defaults([valid_file_path()], %{format: "JSON"}) ==
{[valid_file_path()], %{format: "json"}}
assert @command.merge_defaults([valid_file_path()], %{format: "Erlang"}) ==
{[valid_file_path()], %{format: "erlang"}}
end
test "merge_defaults: format can be overridden" do
assert @command.merge_defaults([valid_file_path()], %{format: "erlang"}) ==
{[valid_file_path()], %{format: "erlang"}}
end
test "validate: accepts a file path argument", context do
assert @command.validate([valid_file_path()], context[:opts]) == :ok
end
test "validate: accepts a dash for stdout", context do
assert @command.validate(["-"], context[:opts]) == :ok
end
test "validate: unsupported format fails validation", context do
assert match?({:validation_failure, {:bad_argument, _}},
@command.validate([valid_file_path()], Map.merge(context[:opts], %{format: "yolo"})))
end
test "validate: no positional arguments fails validation", context do
assert @command.validate([], context[:opts]) ==
{:validation_failure, :not_enough_args}
end
test "validate: more than one positional argument fails validation", context do
assert @command.validate([valid_file_path(), "extra-arg"], context[:opts]) ==
{:validation_failure, :too_many_args}
end
test "validate: supports JSON and Erlang formats", context do
assert @command.validate([valid_file_path()], Map.merge(context[:opts], %{format: "json"})) == :ok
assert @command.validate([valid_file_path()], Map.merge(context[:opts], %{format: "erlang"})) == :ok
end
@tag test_timeout: 3000
test "run: targeting an unreachable node throws a badrpc", context do
result = @command.run([valid_file_path()],
%{node: :jake@thedog,
timeout: context[:test_timeout],
format: "json"})
assert match?({:badrpc, _}, result)
end
@tag format: "json"
test "run: returns a list of definitions when target is stdout and format is JSON", context do
{:ok, map} = @command.run(["-"], context[:opts])
assert Map.has_key?(map, :rabbitmq_version)
end
@tag format: "erlang"
test "run: returns a list of definitions when target is stdout and format is Erlang Terms", context do
{:ok, map} = @command.run(["-"], context[:opts])
assert Map.has_key?(map, :rabbitmq_version)
end
@tag format: "json"
test "run: writes to a file and returns nil when target is a file and format is JSON", context do
File.rm(valid_file_path())
{:ok, nil} = @command.run([valid_file_path()], context[:opts])
{:ok, bin} = File.read(valid_file_path())
{:ok, map} = JSON.decode(bin)
assert Map.has_key?(map, "rabbitmq_version")
end
@tag format: "json"
test "run: correctly formats runtime parameter values", context do
File.rm(valid_file_path())
imported_file_path = Path.join([File.cwd!(), "test", "fixtures", "files", "definitions.json"])
# prepopulate some runtime parameters
RabbitMQ.CLI.Ctl.Commands.ImportDefinitionsCommand.run([imported_file_path], context[:opts])
{:ok, nil} = @command.run([valid_file_path()], context[:opts])
# clean up the state we've modified
clear_parameter("/", "federation-upstream", "up-1")
{:ok, bin} = File.read(valid_file_path())
{:ok, map} = JSON.decode(bin)
assert Map.has_key?(map, "rabbitmq_version")
params = map["parameters"]
assert is_map(hd(params)["value"])
end
@tag format: "erlang"
test "run: writes to a file and returns nil when target is a file and format is Erlang Terms", context do
File.rm(valid_file_path())
{:ok, nil} = @command.run([valid_file_path()], context[:opts])
{:ok, bin} = File.read(valid_file_path())
map = :erlang.binary_to_term(bin)
assert Map.has_key?(map, :rabbitmq_version)
end
defp valid_file_path(), do: "#{System.tmp_dir()}/definitions"
end
|