summaryrefslogtreecommitdiff
path: root/deps/rabbitmq_cli/test/ctl/set_policy_command_test.exs
blob: f9067db05544b9c8781a8b4236d706d91b030d2b (plain)
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
## 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 http://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 SetPolicyCommandTest do
  use ExUnit.Case, async: false
  import TestHelper

  @command RabbitMQ.CLI.Ctl.Commands.SetPolicyCommand

  @vhost "test1"
  @root   "/"
  @key "federate"
  @pattern "^fed\."
  @value "{\"federation-upstream-set\":\"all\"}"
  @apply_to "all"
  @priority 0

  setup_all do
    RabbitMQ.CLI.Core.Distribution.start()


    add_vhost @vhost

    enable_federation_plugin()

    on_exit([], fn ->
      delete_vhost @vhost


    end)

    :ok
  end

  setup context do

    on_exit(context, fn ->
      clear_policy context[:vhost], context[:key]
    end)

    {
      :ok,
      opts: %{
        node: get_rabbit_hostname(),
        vhost: "/",
        apply_to: @apply_to,
        priority: @priority
      }
    }
  end

  @tag pattern: @pattern, key: @key, value: @value, vhost: @root
  test "merge_defaults: a well-formed command with no vhost runs against the default" do
    assert match?({_, %{vhost: "/"}}, @command.merge_defaults([], %{}))
  end

  test "merge_defaults: does not change defined vhost" do
    assert match?({[], %{vhost: "test_vhost"}}, @command.merge_defaults([], %{vhost: "test_vhost"}))
  end

  test "merge_defaults: default apply_to is \"all\"" do
    assert match?({_, %{apply_to: "all"}}, @command.merge_defaults([], %{}))
    assert match?({_, %{apply_to: "custom"}}, @command.merge_defaults([], %{apply_to: "custom"}))
  end

  test "merge_defaults: default priority is 0" do
    assert match?({_, %{priority: 0}}, @command.merge_defaults([], %{}))
    assert match?({_, %{priority: 3}}, @command.merge_defaults([], %{priority: 3}))
  end

  test "validate: providing too few arguments fails validation" do
    assert @command.validate([], %{}) == {:validation_failure, :not_enough_args}
    assert @command.validate(["insufficient"], %{}) == {:validation_failure, :not_enough_args}
    assert @command.validate(["not", "enough"], %{}) == {:validation_failure, :not_enough_args}
  end

  test "validate: providing too many arguments fails validation" do
    assert @command.validate(["this", "is", "too", "many"], %{}) == {:validation_failure, :too_many_args}
  end

  @tag pattern: @pattern, key: @key, value: @value, vhost: @vhost
  test "run: a well-formed, host-specific command returns okay", context do
    vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]})

    assert @command.run(
      [context[:key], context[:pattern], context[:value]],
      vhost_opts
    ) == :ok

    assert_policy_fields(context)
  end

  test "run: an unreachable node throws a badrpc" do
    target = :jake@thedog

    opts = %{node: target, vhost: "/", priority: 0, apply_to: "all"}

    assert @command.run([@key, @pattern, @value], opts) == {:badrpc, :nodedown}
  end

  @tag pattern: @pattern, key: @key, value: @value, vhost: "bad-vhost"
  test "run: providing a non-existent vhost reports an error", context do
    vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]})

    assert @command.run(
      [context[:key], context[:pattern], context[:value]],
      vhost_opts
    ) == {:error, {:no_such_vhost, context[:vhost]}}
  end

  @tag pattern: @pattern, key: @key, value: "bad-value", vhost: @root
  test "run: an invalid value returns a JSON decoding error", context do
    assert match?({:error_string, _},
      @command.run([context[:key], context[:pattern], context[:value]],
        context[:opts]))

    assert list_policies(context[:vhost]) == []
  end

  @tag pattern: @pattern, key: @key, value: "{\"foo\":\"bar\"}", vhost: @root
  test "run: invalid policy returns an error", context do
    assert @command.run(
      [context[:key], context[:pattern], context[:value]],
      context[:opts]
    ) == {:error_string, 'Validation failed\n\n[{<<"foo">>,<<"bar">>}] are not recognised policy settings\n'}

    assert list_policies(context[:vhost]) == []
  end

  @tag pattern: @pattern, key: @key, value: "{}", vhost: @root
  test "run: an empty JSON object value returns an error", context do
    assert @command.run(
      [context[:key], context[:pattern], context[:value]],
      context[:opts]
    ) == {:error_string, 'Validation failed\n\nno policy provided\n'}

    assert list_policies(context[:vhost]) == []
  end

  @tag pattern: @pattern, key: @key, value: @value, vhost: @vhost
  test "banner", context do
    vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]})

    assert @command.banner([context[:key], context[:pattern], context[:value]], vhost_opts)
      == "Setting policy \"#{context[:key]}\" for pattern \"#{context[:pattern]}\" to \"#{context[:value]}\" with priority \"#{context[:opts][:priority]}\" for vhost \"#{context[:vhost]}\" \.\.\."
  end

  @tag pattern: "ha_", key: "ha_policy_test", vhost: @vhost
  test "ha policy validation", context do
    vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]})
    context = Map.put(context, :opts, vhost_opts)
    pass_validation(context, "{\"ha-mode\":\"all\"}")
    fail_validation(context, "{\"ha-mode\":\"made_up\"}")

    fail_validation(context, "{\"ha-mode\":\"nodes\"}")
    fail_validation(context, "{\"ha-mode\":\"nodes\",\"ha-params\":2}")
    fail_validation(context, "{\"ha-mode\":\"nodes\",\"ha-params\":[\"a\",2]}")
    pass_validation(context, "{\"ha-mode\":\"nodes\",\"ha-params\":[\"a\",\"b\"]}")
    fail_validation(context, "{\"ha-params\":[\"a\",\"b\"]}")

    fail_validation(context, "{\"ha-mode\":\"exactly\"}")
    fail_validation(context, "{\"ha-mode\":\"exactly\",\"ha-params\":[\"a\",\"b\"]}")
    pass_validation(context, "{\"ha-mode\":\"exactly\",\"ha-params\":2}")
    fail_validation(context, "{\"ha-params\":2}")

    pass_validation(context, "{\"ha-mode\":\"all\",\"ha-sync-mode\":\"manual\"}")
    pass_validation(context, "{\"ha-mode\":\"all\",\"ha-sync-mode\":\"automatic\"}")
    fail_validation(context, "{\"ha-mode\":\"all\",\"ha-sync-mode\":\"made_up\"}")
    fail_validation(context, "{\"ha-sync-mode\":\"manual\"}")
    fail_validation(context, "{\"ha-sync-mode\":\"automatic\"}")
  end

  @tag pattern: "ha_", key: "ha_policy_test", vhost: @vhost
  test "queue master locator policy validation", context do
    vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]})
    context = Map.put(context, :opts, vhost_opts)
    pass_validation(context, "{\"queue-master-locator\":\"min-masters\"}")
    pass_validation(context, "{\"queue-master-locator\":\"client-local\"}")
    pass_validation(context, "{\"queue-master-locator\":\"random\"}")
    fail_validation(context, "{\"queue-master-locator\":\"made_up\"}")
  end

  @tag pattern: "ha_", key: "ha_policy_test", vhost: @vhost
  test "queue modes policy validation", context do
    vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]})
    context = Map.put(context, :opts, vhost_opts)
    pass_validation(context, "{\"queue-mode\":\"lazy\"}")
    pass_validation(context, "{\"queue-mode\":\"default\"}")
    fail_validation(context, "{\"queue-mode\":\"wrong\"}")
  end

  def pass_validation(context, value) do
    assert @command.run(
      [context[:key], context[:pattern], value],
      context[:opts]
    ) == :ok
    assert_policy_fields(Map.merge(context, %{value: value}))
  end

  def fail_validation(context, value) do
    result = @command.run(
      [context[:key], context[:pattern], value],
      context[:opts]
    )
    assert {:error_string, _} = result
    {:error_string, msg} = result
    assert "Validation failed"<>_ = to_string(msg)
  end

  # Checks each element of the first policy against the expected context values
  defp assert_policy_fields(context) do
    result_policy = context[:vhost] |> list_policies |> List.first
    assert result_policy[:definition] == context[:value]
    assert result_policy[:vhost] == context[:vhost]
    assert result_policy[:pattern] == context[:pattern]
    assert result_policy[:name] == context[:key]
  end
end