Rename parse_node to normalise_node

This commit is contained in:
Luke Bakken 2018-11-30 12:14:21 -08:00
parent c8e766dec7
commit 35ffd53be1
8 changed files with 40 additions and 26 deletions

View File

@ -21,14 +21,14 @@ defmodule RabbitMQ.CLI.Core.Helpers do
require Record
def get_rabbit_hostname() do
parse_node(Config.get_option(:node))
normalise_node(Config.get_option(:node))
end
def parse_node(nil), do: get_rabbit_hostname()
def parse_node(name) when is_atom(name) do
parse_node(to_string(name))
def normalise_node(nil), do: get_rabbit_hostname()
def normalise_node(name) when is_atom(name) do
normalise_node(to_string(name))
end
def parse_node(name) do
def normalise_node(name) do
case String.split(name, "@", parts: 2) do
[_,""] -> name <> "#{hostname()}" |> String.to_atom
[_,_] -> name |> String.to_atom

View File

@ -50,7 +50,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.JoinClusterCommand do
:rabbit_misc.rpc_call(node_name,
:rabbit_mnesia,
:join_cluster,
[Helpers.parse_node(target_node), node_type]
[Helpers.normalise_node(target_node), node_type]
)
end

View File

@ -33,7 +33,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.UpdateClusterNodesCommand do
:rabbit_misc.rpc_call(node_name,
:rabbit_mnesia,
:update_cluster_nodes,
[Helpers.parse_node(seed_node)]
[Helpers.normalise_node(seed_node)]
)
end

View File

@ -59,7 +59,7 @@ defmodule CloseAllConnectionsCommandTest do
test "run: a close connections request in an existing vhost with all defaults closes all connections", context do
with_connection(@vhost, fn(_) ->
Process.sleep(500)
node = @helpers.parse_node(context[:node])
node = @helpers.normalise_node(context[:node])
nodes = @helpers.nodes_in_cluster(node)
[[vhost: @vhost]] = fetch_connection_vhosts(node, nodes)
opts = %{node: node, vhost: @vhost, global: false, per_connection_delay: 0, limit: 0}
@ -72,7 +72,7 @@ defmodule CloseAllConnectionsCommandTest do
test "run: close a limited number of connections in an existing vhost closes a subset of connections", context do
with_connections([@vhost, @vhost, @vhost], fn(_) ->
Process.sleep(500)
node = @helpers.parse_node(context[:node])
node = @helpers.normalise_node(context[:node])
nodes = @helpers.nodes_in_cluster(node)
[[vhost: @vhost], [vhost: @vhost], [vhost: @vhost]] = fetch_connection_vhosts(node, nodes)
opts = %{node: node, vhost: @vhost, global: false, per_connection_delay: 0, limit: 2}
@ -85,7 +85,7 @@ defmodule CloseAllConnectionsCommandTest do
test "run: a close connections request for a non-existing vhost does nothing", context do
close_all_connections(get_rabbit_hostname())
with_connection(@vhost, fn(_) ->
node = @helpers.parse_node(context[:node])
node = @helpers.normalise_node(context[:node])
nodes = @helpers.nodes_in_cluster(node)
[[vhost: @vhost]] = fetch_connection_vhosts(node, nodes)
opts = %{node: node, vhost: "burrow", global: false, per_connection_delay: 0, limit: 0}
@ -97,7 +97,7 @@ defmodule CloseAllConnectionsCommandTest do
test "run: a close connections request to an existing node with --global (all vhosts)", context do
with_connection(@vhost, fn(_) ->
Process.sleep(500)
node = @helpers.parse_node(context[:node])
node = @helpers.normalise_node(context[:node])
nodes = @helpers.nodes_in_cluster(node)
[[vhost: @vhost]] = fetch_connection_vhosts(node, nodes)
opts = %{node: node, vhost: "fakeit", global: true, per_connection_delay: 0, limit: 0}
@ -115,7 +115,7 @@ defmodule CloseAllConnectionsCommandTest do
end
test "banner for vhost option", context do
node = @helpers.parse_node(context[:node])
node = @helpers.normalise_node(context[:node])
opts = %{node: node, vhost: "burrow", global: false, per_connection_delay: 0, limit: 0}
s = @command.banner(["some reason"], opts)
assert s =~ ~r/Closing all connections in vhost burrow/
@ -123,7 +123,7 @@ defmodule CloseAllConnectionsCommandTest do
end
test "banner for vhost option with limit", context do
node = @helpers.parse_node(context[:node])
node = @helpers.normalise_node(context[:node])
opts = %{node: node, vhost: "burrow", global: false, per_connection_delay: 0, limit: 2}
s = @command.banner(["some reason"], opts)
assert s =~ ~r/Closing 2 connections in vhost burrow/

View File

@ -54,7 +54,7 @@ defmodule CloseConnectionCommandTest do
test "run: a close connection request on an existing connection", context do
with_connection("/", fn(_) ->
Process.sleep(500)
node = @helpers.parse_node(context[:node])
node = @helpers.normalise_node(context[:node])
nodes = @helpers.nodes_in_cluster(node)
[[pid: pid]] = fetch_connection_pids(node, nodes)
assert :ok == @command.run([:rabbit_misc.pid_to_string(pid), "test"], %{node: node})
@ -65,7 +65,7 @@ defmodule CloseConnectionCommandTest do
test "run: a close connection request on for a non existing connection returns successfully", context do
assert match?(:ok,
@command.run(["<#{node()}.2.121.12>", "test"], %{node: @helpers.parse_node(context[:node])}))
@command.run(["<#{node()}.2.121.12>", "test"], %{node: @helpers.normalise_node(context[:node])}))
end
test "run: a close_connection request on nonexistent RabbitMQ node returns nodedown" do

View File

@ -67,36 +67,50 @@ defmodule HelpersTest do
RabbitMQ.CLI.Core.Distribution.stop()
end
## ------------------- parse_node* tests --------------------
## ------------------- normalise_node tests (:shortnames) --------------------
test "if nil input, retrieve standard rabbit hostname" do
assert @subject.parse_node(nil) == get_rabbit_hostname()
assert @subject.normalise_node(nil) == get_rabbit_hostname()
end
test "if input is an atom short name, return the atom with hostname" do
assert @subject.parse_node(:rabbit_test) == "rabbit_test@#{hostname()}" |> String.to_atom
want = String.to_atom("rabbit_test@#{hostname()}")
got = @subject.normalise_node(:rabbit_test)
assert want == got
end
test "if input is a string fully qualified node name, return an atom" do
assert @subject.parse_node("rabbit_test@#{hostname()}") == "rabbit_test@#{hostname()}" |> String.to_atom
want = String.to_atom("rabbit_test@#{hostname()}")
got = @subject.normalise_node("rabbit_test@#{hostname()}")
assert want == got
end
test "if input is a short node name, host name is added" do
assert @subject.parse_node("rabbit_test") == "rabbit_test@#{hostname()}" |> String.to_atom
want = String.to_atom("rabbit_test@#{hostname()}")
got = @subject.normalise_node("rabbit_test")
assert want == got
end
test "if input is a hostname without a node name, return an atom" do
assert @subject.parse_node("@#{hostname()}") == "@#{hostname()}" |> String.to_atom
test "if input is a hostname without a node name, default node name is added" do
want = String.to_atom("rabbit_test@#{hostname()}")
got = @subject.normalise_node("@#{hostname()}")
assert want == got
end
test "if input is a short node name with an @ and no hostname, local host name is added" do
assert @subject.parse_node("rabbit_test@") == "rabbit_test@#{hostname()}" |> String.to_atom
want = String.to_atom("rabbit_test@#{hostname()}")
got = @subject.normalise_node("rabbit_test@")
assert want == got
end
test "if input contains more than one @, return an atom" do
assert @subject.parse_node("rabbit@rabbit_test@#{hostname()}") == "rabbit@rabbit_test@#{hostname()}" |>String.to_atom
want = String.to_atom("rabbit@rabbit_test@#{hostname()}")
got = @subject.normalise_node("rabbit@rabbit_test@#{hostname()}")
assert want == got
end
## ------------------- normalise_node tests (:longnames) --------------------
## ------------------- require_rabbit/1 tests --------------------
test "locate plugin with version number in filename" do

View File

@ -44,7 +44,7 @@ defmodule StartAppCommandTest do
end
test "run: request to an active node succeeds", context do
node = RabbitMQ.CLI.Core.Helpers.parse_node context[:node]
node = RabbitMQ.CLI.Core.Helpers.normalise_node(context[:node])
stop_rabbitmq_app()
refute :rabbit_misc.rpc_call(node, :rabbit, :is_running, [])
assert @command.run([], context[:opts])

View File

@ -44,7 +44,7 @@ defmodule StopAppCommandTest do
end
test "run: request to an active node succeeds", context do
node = RabbitMQ.CLI.Core.Helpers.parse_node context[:node]
node = RabbitMQ.CLI.Core.Helpers.normalise_node(context[:node])
assert :rabbit_misc.rpc_call(node, :rabbit, :is_running, [])
assert @command.run([], context[:opts])
refute :rabbit_misc.rpc_call(node, :rabbit, :is_running, [])