summaryrefslogtreecommitdiff
path: root/deps/rabbitmq_cli/test/ctl/list_connections_command_test.exs
blob: 9cfcb8787febc75e149cd34cf28fa50917a7a3e3 (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
defmodule ListConnectionsCommandTest do
  use ExUnit.Case, async: false
  import TestHelper

  @command RabbitMQ.CLI.Ctl.Commands.ListConnectionsCommand
  @user "guest"
  @default_timeout 15000
  @default_options %{table_headers: true}

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

    close_all_connections(get_rabbit_hostname())

    on_exit([], fn ->
      close_all_connections(get_rabbit_hostname())
    end)

    :ok
  end

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

  test "merge_defaults: user, peer_host, peer_port and state by default" do
    assert @command.merge_defaults([], %{}) == {~w(user peer_host peer_port state), @default_options}
  end

  test "merge_defaults: includes table headers by default", _context do
    {_, opts} = @command.merge_defaults([], %{})
    assert opts[:table_headers]
  end

  test "validate: returns 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", "peer_host"], context[:opts]) ==
      {:validation_failure, {:bad_info_key, [:quack]}}
    assert @command.validate(["user", "oink"], context[:opts]) ==
      {:validation_failure, {:bad_info_key, [:oink]}}
    assert @command.validate(["user", "oink", "peer_host"], context[:opts]) ==
      {:validation_failure, {:bad_info_key, [:oink]}}
  end

  @tag test_timeout: 0
  test "run: timeout causes command to return badrpc", context do
    assert run_command_to_list(@command, [["name"], context[:opts]]) ==
      [{:badrpc, {:timeout, 0.0}}]
  end

  test "run: filter single key", context do
    vhost = "/"
    with_connection(vhost, fn(_conn) ->
      conns = run_command_to_list(@command, [["name"], context[:opts]])
      assert (Enum.map(conns, &Keyword.keys/1) |> Enum.uniq) == [[:name]]
      assert Enum.any?(conns, fn(conn) -> conn[:name] != nil end)
    end)
  end

  test "run: show connection vhost", context do
    vhost = "custom_vhost"
    add_vhost vhost
    set_permissions @user, vhost, [".*", ".*", ".*"]
    on_exit(fn ->
      delete_vhost vhost
    end)
    with_connection(vhost, fn(_conn) ->
      conns = run_command_to_list(@command, [["vhost"], context[:opts]])
      assert (Enum.map(conns, &Keyword.keys/1) |> Enum.uniq) == [[:vhost]]
      assert Enum.any?(conns, fn(conn) -> conn[:vhost] == vhost end)
    end)
  end


end