summaryrefslogtreecommitdiff
path: root/deps/rabbitmq_cli/test/ctl/clear_operator_policy_command_test.exs
blob: 834caf89f7ec7a9dca1f8d598e14217fdf740cb2 (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
## 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 ClearOperatorPolicyCommandTest do
  use ExUnit.Case, async: false
  import TestHelper

  @command RabbitMQ.CLI.Ctl.Commands.ClearOperatorPolicyCommand
  @vhost "test1"
  @key "message-expiry"
  @pattern "^queue\."
  @value "{\"message-ttl\":10}"

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

    add_vhost @vhost

    on_exit([], fn ->
      delete_vhost @vhost
    end)

    :ok
  end

  setup context do
    on_exit(context, fn ->
      clear_operator_policy(context[:vhost], context[:key])
    end)

    {
      :ok,
      opts: %{
        node: get_rabbit_hostname()
      }
    }
  end

  test "merge_defaults: adds default vhost if missing" do
    assert @command.merge_defaults([], %{}) == {[], %{vhost: "/"}}
  end

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

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

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

  test "validate: providing one argument and no options passes validation" do
    assert @command.validate(["a-policy"], %{}) == :ok
  end

  @tag pattern: @pattern, key: @key, vhost: @vhost
  test "run: if policy does not exist, returns an error", context do
    vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]})

    assert @command.run(
      [context[:key]],
      vhost_opts
    ) == {:error_string, 'Parameter does not exist'}
  end

  test "run: an unreachable node throws a badrpc" do
    opts = %{node: :jake@thedog, vhost: "/", timeout: 200}
    assert match?({:badrpc, _}, @command.run([@key], opts))
  end


  @tag pattern: @pattern, key: @key, vhost: @vhost
  test "run: if policy exists, returns ok and removes it", context do
    vhost_opts = Map.merge(context[:opts], %{vhost: context[:vhost]})

    set_operator_policy(context[:vhost], context[:key], context[:pattern], @value)

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

    assert_operator_policy_does_not_exist(context)
  end

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

    assert @command.run(
      [context[:key]],
      vhost_opts
    ) == {:error_string, 'Parameter does not exist'}
  end

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

    s = @command.banner(
      [context[:key]],
      vhost_opts
    )

    assert s =~ ~r/Clearing operator policy/
    assert s =~ ~r/"#{context[:key]}"/
  end

  defp assert_operator_policy_does_not_exist(context) do
    policy = context[:vhost]
                |> list_operator_policies
                |> Enum.filter(fn(param) ->
                    param[:pattern] == context[:pattern] and
                    param[:key] == context[:key]
                    end)
    assert policy === []
  end
end