summaryrefslogtreecommitdiff
path: root/deps/rabbitmq_cli/test/diagnostics/check_alarms_command_test.exs
blob: f5b64282e3faa9e63b80c3da74fd965ef3faff5c (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
## 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 CheckAlarmsCommandTest do
  use ExUnit.Case, async: false
  import TestHelper
  import RabbitMQ.CLI.Core.Alarms, only: [alarm_types: 1]

  @command RabbitMQ.CLI.Diagnostics.Commands.CheckAlarmsCommand

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

    start_rabbitmq_app()

    on_exit([], fn ->
      start_rabbitmq_app()
    end)

    :ok
  end

  setup context do
    {:ok, opts: %{
        node: get_rabbit_hostname(),
        timeout: context[:test_timeout] || 30000
      }}
  end

  test "merge_defaults: nothing to do" do
    assert @command.merge_defaults([], %{}) == {[], %{}}
  end

  test "validate: treats positional arguments as a failure" do
    assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args}
  end

  test "validate: treats empty positional arguments and default switches as a success" do
    assert @command.validate([], %{}) == :ok
  end

  @tag test_timeout: 3000
  test "run: targeting an unreachable node throws a badrpc", context do
    assert match?({:badrpc, _}, @command.run([], Map.merge(context[:opts], %{node: :jake@thedog})))
  end

  test "run: when target node has no alarms in effect, returns an empty list", context do
    assert [] == status()[:alarms]

    assert @command.run([], context[:opts]) == []
  end

  test "run: when target node has an alarm in effect, returns it", context do
    old_watermark = status()[:vm_memory_high_watermark]
    on_exit(fn() ->
      set_vm_memory_high_watermark(old_watermark)
    end)
    # 2000 bytes will trigger an alarm
    set_vm_memory_high_watermark({:absolute, 2000})

    assert [:memory] == alarm_types(status()[:alarms])
    assert length(@command.run([], context[:opts])) == 1

    set_vm_memory_high_watermark(old_watermark)
  end


  test "output: when target node has no alarms in effect, returns a success", context do
    assert [] == status()[:alarms]

    assert match?({:ok, _}, @command.output([], context[:opts]))
  end

  test "output: when target node has an alarm in effect, returns a failure", context do
    for input <- [
          [
            :file_descriptor_limit
          ],
          [
            :file_descriptor_limit,
            {{:resource_limit, :disk, :hare@warp10}, []}
          ],
          [
            :file_descriptor_limit,
            {{:resource_limit, :disk,   :hare@warp10},   []},
            {{:resource_limit, :memory, :hare@warp10},   []},
            {{:resource_limit, :disk,   :rabbit@warp10}, []},
            {{:resource_limit, :memory, :rabbit@warp10}, []}
          ]
        ] do
        assert match?({:error, _, _}, @command.output(input, context[:opts]))
    end
  end

  test "output: when target node has an alarm in effect and --silent is passed, returns a silent failure", _context do
    for input <- [
          [
            :file_descriptor_limit
          ],
          [
            :file_descriptor_limit,
            {{:resource_limit, :disk, :hare@warp10}, []}
          ],
          [
            :file_descriptor_limit,
            {{:resource_limit, :disk,   :hare@warp10},   []},
            {{:resource_limit, :memory, :hare@warp10},   []},
            {{:resource_limit, :disk,   :rabbit@warp10}, []},
            {{:resource_limit, :memory, :rabbit@warp10}, []}
          ]
        ] do
        assert {:error, :check_failed} == @command.output(input, %{silent: true})
    end
  end
end