summaryrefslogtreecommitdiff
path: root/deps/rabbitmq_cli/test/ctl/list_vhosts_command_test.exs
blob: 76f46af422d67a9e802565b5282a07744533750b (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
## 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 ListVhostsCommandTest do
  use ExUnit.Case, async: false
  import TestHelper

  @command RabbitMQ.CLI.Ctl.Commands.ListVhostsCommand

  @vhost1 "test1"
  @vhost2 "test2"
  @root   "/"

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

    add_vhost @vhost1
    add_vhost @vhost2
    trace_off @root

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

    name_result = [
      [{:name, @vhost1}],
      [{:name, @vhost2}],
      [{:name, @root}]
    ]

    tracing_result = [
      [{:tracing, false}],
      [{:tracing, false}],
      [{:tracing, false}]
    ]

    full_result = [
      [{:name, @vhost1}, {:tracing, false}],
      [{:name, @vhost2}, {:tracing, false}],
      [{:name, @root}, {:tracing, false}]
    ]

    transposed_result = [
      [{:tracing, false}, {:name, @vhost1}],
      [{:tracing, false}, {:name, @vhost2}],
      [{:tracing, false}, {:name, @root}]
    ]

    {
      :ok,
      name_result: name_result,
      tracing_result: tracing_result,
      full_result: full_result,
      transposed_result: transposed_result
    }
  end

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

  test "merge_defaults with no command, print just use the names" do
    assert match?({["name"], %{}}, @command.merge_defaults([], %{}))
  end

  test "validate: return bad_info_key on a single bad arg", context do
    assert @command.validate(["quack"], context[:opts]) ==
      {:validation_failure, {:bad_info_key, [:quack]}}
  end

  test "validate: multiple bad args return a list of bad info key values", context do
    assert @command.validate(["quack", "oink"], context[:opts]) ==
      {:validation_failure, {:bad_info_key, [:oink, :quack]}}
  end

  test "validate: return bad_info_key on mix of good and bad args", context do
    assert @command.validate(["quack", "tracing"], context[:opts]) ==
      {:validation_failure, {:bad_info_key, [:quack]}}
    assert @command.validate(["name", "oink"], context[:opts]) ==
      {:validation_failure, {:bad_info_key, [:oink]}}
    assert @command.validate(["name", "oink", "tracing"], context[:opts]) ==
      {:validation_failure, {:bad_info_key, [:oink]}}
  end

  test "run: on a bad RabbitMQ node, return a badrpc" do
    opts = %{node: :jake@thedog, timeout: 200}

    assert match?({:badrpc, _}, @command.run(["name"], opts))
  end

  @tag test_timeout: :infinity
  test "run: with the name tag, print just the names", context do
    # checks to ensure that all expected vhosts are in the results
    matches_found = @command.run(["name"], context[:opts])
    assert Enum.all?(context[:name_result], fn(vhost) ->
      Enum.find(matches_found, fn(found) -> found == vhost end)
    end)
  end

  @tag test_timeout: :infinity
  test "run: with the tracing tag, print just say if tracing is on", context do
    # checks to ensure that all expected vhosts are in the results
    matches_found = @command.run(["tracing"], context[:opts])
    assert Enum.all?(context[:tracing_result], fn(vhost) ->
      Enum.find(matches_found, fn(found) -> found == vhost end)
    end)
  end

  @tag test_timeout: :infinity
  test "run: with name and tracing keys, print both", context do
    # checks to ensure that all expected vhosts are in the results
    matches_found = @command.run(["name", "tracing"], context[:opts])
    assert Enum.all?(context[:full_result], fn(vhost) ->
      Enum.find(matches_found, fn(found) -> found == vhost end)
    end)

    # checks to ensure that all expected vhosts are in the results
    matches_found = @command.run(["tracing", "name"], context[:opts])
    assert Enum.all?(context[:transposed_result], fn(vhost) ->
      Enum.find(matches_found, fn(found) -> found == vhost end)
    end)
  end

  @tag test_timeout: :infinity
  test "run: duplicate args do not produce duplicate entries", context do
    # checks to ensure that all expected vhosts are in the results
    matches_found = @command.run(["name", "name"], context[:opts])
    assert Enum.all?(context[:name_result], fn(vhost) ->
      Enum.find(matches_found, fn(found) -> found == vhost end)
    end)
  end

  @tag test_timeout: 30000
  test "run: sufficiently long timeouts don't interfere with results", context do
    # checks to ensure that all expected vhosts are in the results
    matches_found = @command.run(["name", "tracing"], context[:opts])
    assert Enum.all?(context[:full_result], fn(vhost) ->
      Enum.find(matches_found, fn(found) -> found == vhost end)
    end)
  end

  @tag test_timeout: 0, username: "guest"
  test "run: timeout causes command to return a bad RPC", context do
    assert @command.run(["name", "tracing"], context[:opts]) ==
      {:badrpc, :timeout}
  end

  @tag test_timeout: :infinity
  test "banner", context do
    assert @command.banner([], context[:opts]) =~ ~r/Listing vhosts \.\.\./
  end
end