summaryrefslogtreecommitdiff
path: root/deps/rabbitmq_cli/test/core/command_modules_test.exs
blob: f8257c7e6aa6b820c1406498379cd4bedabadb9b (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
## 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 CommandModulesTest do
  use ExUnit.Case, async: false
  import TestHelper

  @subject RabbitMQ.CLI.Core.CommandModules

  setup_all do
    on_exit(fn ->
      set_scope(:none)
      Application.put_env(:rabbitmqctl, :commands, nil)
    end)
    :ok
  end

  test "command modules has existing commands" do
    assert @subject.load_commands(:all, %{})["duck"] ==
      RabbitMQ.CLI.Ctl.Commands.DuckCommand
  end

  test "command with multiple underscores shows up in map" do
    assert @subject.load_commands(:all, %{})["gray_goose"] ==
      RabbitMQ.CLI.Ctl.Commands.GrayGooseCommand
  end

  test "command modules does not have non-existent commands" do
    assert @subject.load_commands(:all, %{})["usurper"] == nil
  end

  test "non command modules do not show in command map" do
    assert @subject.load_commands(:all, %{})["ugly_duckling"] == nil
  end

  test "loaded commands are saved in env variable" do
    set_scope(:ctl)
    commands = @subject.module_map
    assert commands == @subject.module_map
    assert commands == Application.get_env(:rabbitmqctl, :commands)
  end

  test "load commands for current scope" do
    set_scope(:ctl)
    commands = @subject.load(%{})
    assert commands == @subject.load_commands(:ctl, %{})

    assert commands["duck"] == RabbitMQ.CLI.Ctl.Commands.DuckCommand
    assert commands["gray_goose"] == RabbitMQ.CLI.Ctl.Commands.GrayGooseCommand

    assert commands["stork"] == nil
    assert commands["heron"] == nil

    assert commands["crow"] == nil
    assert commands["raven"] == nil

    set_scope(:plugins)
    commands = @subject.load(%{})
    assert commands == @subject.load_commands(:plugins, %{})
    assert commands["duck"] == nil
    assert commands["gray_goose"] == nil

    assert commands["stork"] == RabbitMQ.CLI.Plugins.Commands.StorkCommand
    assert commands["heron"] == RabbitMQ.CLI.Plugins.Commands.HeronCommand

    assert commands["crow"] == nil
    assert commands["raven"] == nil
  end

  test "can set scopes inside command" do
    plugin_commands = @subject.load_commands(:plugins, %{})

    assert plugin_commands["duck"] == nil
    assert plugin_commands["gray_goose"] == nil

    assert plugin_commands["stork"] == RabbitMQ.CLI.Plugins.Commands.StorkCommand
    assert plugin_commands["heron"] == RabbitMQ.CLI.Plugins.Commands.HeronCommand

    assert plugin_commands["crow"] == nil
    assert plugin_commands["raven"] == nil

    # SeagullCommand has scopes() defined as [:plugins, :custom]
    assert plugin_commands["seagull"] == RabbitMQ.CLI.Seagull.Commands.SeagullCommand

    custom_commands = @subject.load_commands(:custom, %{})

    assert custom_commands["duck"] == nil
    assert custom_commands["gray_goose"] == nil

    assert custom_commands["stork"] == nil
    assert custom_commands["heron"] == nil

    assert custom_commands["crow"] == RabbitMQ.CLI.Custom.Commands.CrowCommand
    assert custom_commands["raven"] == RabbitMQ.CLI.Custom.Commands.RavenCommand

    # SeagullCommand has scopes() defined as [:plugins, :custom]
    assert custom_commands["seagull"] == RabbitMQ.CLI.Seagull.Commands.SeagullCommand

  end

  ## ------------------- commands/0 tests --------------------

  test "command_modules has existing commands" do
    set_scope(:ctl)
    @subject.load(%{})
    assert @subject.module_map["status"] == RabbitMQ.CLI.Ctl.Commands.StatusCommand
    assert @subject.module_map["environment"] == RabbitMQ.CLI.Ctl.Commands.EnvironmentCommand
  end

  test "command_modules does not have non-existent commands" do
    set_scope(:ctl)
    @subject.load(%{})
    assert @subject.module_map[:p_equals_np_proof] == nil
  end
end

# Mock command modules for Ctl

defmodule RabbitMQ.CLI.Ctl.Commands.DuckCommand do
  @behaviour RabbitMQ.CLI.CommandBehaviour
  use RabbitMQ.CLI.DefaultOutput
  def usage(), do: ["duck"]
  def validate(_,_), do: :ok
  def merge_defaults(_,_), do: {[], %{}}
  def banner(_,_), do: ""
  def run(_,_), do: :ok
end

defmodule RabbitMQ.CLI.Ctl.Commands.GrayGooseCommand do
  @behaviour RabbitMQ.CLI.CommandBehaviour
  use RabbitMQ.CLI.DefaultOutput
  def usage(), do: ["gray_goose"]
  def validate(_,_), do: :ok
  def merge_defaults(_,_), do: {[], %{}}
  def banner(_,_), do: ""
  def run(_,_), do: :ok
end

defmodule RabbitMQ.CLI.Ctl.Commands.UglyDucklingCommand do
end


# Mock command modules for Plugins

defmodule RabbitMQ.CLI.Plugins.Commands.StorkCommand do
  @behaviour RabbitMQ.CLI.CommandBehaviour
  use RabbitMQ.CLI.DefaultOutput
  def usage(), do: ["stork"]
  def validate(_,_), do: :ok
  def merge_defaults(_,_), do: {[], %{}}
  def banner(_,_), do: ""
  def run(_,_), do: :ok
end

defmodule RabbitMQ.CLI.Plugins.Commands.HeronCommand do
  @behaviour RabbitMQ.CLI.CommandBehaviour
  use RabbitMQ.CLI.DefaultOutput
  def usage(), do: ["heron"]
  def validate(_,_), do: :ok
  def merge_defaults(_,_), do: {[], %{}}
  def banner(_,_), do: ""
  def run(_,_), do: :ok
end

# Mock command modules for Custom

defmodule RabbitMQ.CLI.Custom.Commands.CrowCommand do
  @behaviour RabbitMQ.CLI.CommandBehaviour
  use RabbitMQ.CLI.DefaultOutput
  def usage(), do: ["crow"]
  def validate(_,_), do: :ok
  def merge_defaults(_,_), do: {[], %{}}
  def banner(_,_), do: ""
  def run(_,_), do: :ok
  def scopes(), do: [:custom, ]
end

defmodule RabbitMQ.CLI.Custom.Commands.RavenCommand do
  @behaviour RabbitMQ.CLI.CommandBehaviour
  use RabbitMQ.CLI.DefaultOutput
  def usage(), do: ["raven"]
  def validate(_,_), do: :ok
  def merge_defaults(_,_), do: {[], %{}}
  def banner(_,_), do: ""
  def run(_,_), do: :ok
end

defmodule RabbitMQ.CLI.Seagull.Commands.SeagullCommand do
  @behaviour RabbitMQ.CLI.CommandBehaviour
  use RabbitMQ.CLI.DefaultOutput
  def usage(), do: ["seagull"]
  def validate(_,_), do: :ok
  def merge_defaults(_,_), do: {[], %{}}
  def banner(_,_), do: ""
  def run(_,_), do: :ok
  def scopes(), do: [:plugins, :custom]
end