Add tests for normalising name with :longnames

This commit is contained in:
Luke Bakken 2018-11-30 13:58:27 -08:00
parent ff6243bfd2
commit 9cc61e594b
5 changed files with 79 additions and 31 deletions

View File

@ -20,15 +20,40 @@ defmodule RabbitMQ.CLI.Core.Helpers do
alias RabbitMQ.CLI.Core.Config
require Record
def get_rabbit_hostname() do
normalise_node(Config.get_option(:node))
def get_rabbit_hostname(node_name_type \\ :shortnames) do
normalise_node(Config.get_option(:node), node_name_type)
end
def normalise_node(nil), do: get_rabbit_hostname()
def normalise_node(name) when is_atom(name) do
normalise_node(to_string(name))
def normalise_node(name, node_name_type \\ :shortnames)
def normalise_node(nil, node_name_type) do
get_rabbit_hostname(node_name_type)
end
def normalise_node(name) do
def normalise_node(name, :longnames) when is_atom(name) do
priv_normalise_node(name, :longnames)
end
def normalise_node(name, node_name_type) when is_atom(name) do
priv_normalise_node(to_string(name), node_name_type)
end
def normalise_node(name, :longnames) do
priv_normalise_node(String.to_atom(name), :longnames)
end
def normalise_node(name, node_name_type) do
priv_normalise_node(name, node_name_type)
end
defp priv_normalise_node(name, :longnames) when is_atom(name) do
case :net_kernel.get_net_ticktime do
:ignored ->
raise "distributed Erlang must be active to "
"normalise a node with :longnames name type"
_ ->
:rabbit_nodes_common.make(name)
end
end
defp priv_normalise_node(name, :shortnames) when is_atom(name) do
priv_normalise_node(to_string(name), :shortnames)
end
defp priv_normalise_node(name, :shortnames) do
case String.split(name, "@", parts: 2) do
[_,""] ->
name <> "#{hostname()}" |> String.to_atom
@ -42,16 +67,17 @@ defmodule RabbitMQ.CLI.Core.Helpers do
end
end
def normalise_node_option(options) do
node_opt = Config.get_option(:node, options)
longnames_opt = Config.get_option(:longnames, options)
normalised_node_opt = priv_normalise_node(node_opt, longnames_opt)
Map.put(options, :node, normalised_node_opt)
end
def hostname, do: :inet_db.gethostname() |> List.to_string
def domain, do: Keyword.get(:inet.get_rc(), :domain)
def normalise_node_option(options) do
node_opt = Config.get_option(:node, options)
normalised_node_opt = :rabbit_nodes_common.make(node_opt)
Map.put(options, :node, normalised_node_opt)
end
def validate_step(:ok, step) do
case step.() do
{:error, err} -> {:validation_failure, err};

View File

@ -14,8 +14,6 @@
## Copyright (c) 2007-2018 Pivotal Software, Inc. All rights reserved.
defmodule RabbitMQ.CLI.Ctl.Commands.SetLogLevelCommand do
alias RabbitMQ.CLI.Core.Helpers
@behaviour RabbitMQ.CLI.CommandBehaviour
@known_levels ["debug", "info", "notice", "warning", "error", "critical", "alert", "emergency", "none"]

View File

@ -29,7 +29,6 @@ defmodule RabbitMQ.CLI.Plugins.Helpers do
end
def can_set_plugins_with_mode(args, opts) do
mode = mode(opts)
case mode(opts) do
# can always set offline plugins list
:offline -> :ok;

View File

@ -14,6 +14,8 @@
## Copyright (c) 2007-2017 Pivotal Software, Inc. All rights reserved.
defmodule HelpersTest do
alias RabbitMQ.CLI.Core.{Config, Distribution}
use ExUnit.Case, async: false
import TestHelper
@ -51,60 +53,60 @@ defmodule HelpersTest do
## ------------------- normalise_node_option tests --------------------
test "if using longnames and 'rabbit' as node name, correct domain is used" do
test "longnames: 'rabbit' as node name, correct domain is used" do
options = %{node: :rabbit, longnames: true}
RabbitMQ.CLI.Core.Distribution.start(options)
Distribution.start(options)
options = @subject.normalise_node_option(options)
assert options[:node] == :"rabbit@#{hostname()}.#{domain()}"
RabbitMQ.CLI.Core.Distribution.stop()
Distribution.stop
end
test "if using shortnames and 'rabbit' as node name, no domain is used" do
test "shortnames: 'rabbit' as node name, no domain is used" do
options = %{node: :rabbit, longnames: false}
RabbitMQ.CLI.Core.Distribution.start(options)
Distribution.start(options)
options = @subject.normalise_node_option(options)
assert options[:node] == :"rabbit@#{hostname()}"
RabbitMQ.CLI.Core.Distribution.stop()
Distribution.stop
end
## ------------------- normalise_node tests (:shortnames) --------------------
test "if nil input, retrieve standard rabbit hostname" do
test "shortnames: if nil input, retrieve standard rabbit hostname" do
assert @subject.normalise_node(nil) == get_rabbit_hostname()
end
test "if input is an atom short name, return the atom with hostname" do
test "shortnames: if input is an atom short name, return the atom with hostname" do
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
test "shortnames: if input is a string fully qualified node name, return an atom" do
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
test "shortnames: if input is a short node name, host name is added" do
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, default node name is added" do
default_name = RabbitMQ.CLI.Core.Config.get_option(:node)
test "shortnames: if input is a hostname without a node name, default node name is added" do
default_name = Config.get_option(:node)
want = String.to_atom("#{default_name}@#{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
test "shortnames: if input is a short node name with an @ and no hostname, local host name is added" do
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
test "shortnames: if input contains more than one @, return an atom" do
want = String.to_atom("rabbit@rabbit_test@#{hostname()}")
got = @subject.normalise_node("rabbit@rabbit_test@#{hostname()}")
assert want == got
@ -112,6 +114,29 @@ defmodule HelpersTest do
## ------------------- normalise_node tests (:longnames) --------------------
test "longnames: requires distributed Erlang" do
assert_raise(RuntimeError, fn -> @subject.normalise_node(nil, :longnames) end)
end
test "longnames: if nil input, retrieve standard rabbit hostname" do
default_name = Config.get_option(:node)
options = %{node: default_name, longnames: true}
Distribution.start(options)
want = get_rabbit_hostname(:longnames)
got = @subject.normalise_node(nil, :longnames)
assert want == got
Distribution.stop
end
test "longnames: if input is an atom short name, return the atom with full hostname" do
options = %{node: :rabbit_test, longnames: true}
Distribution.start(options)
want = String.to_atom("rabbit_test@#{hostname()}.#{domain()}")
got = @subject.normalise_node(:rabbit_test, :longnames)
assert want == got
Distribution.stop
end
## ------------------- require_rabbit/1 tests --------------------
test "locate plugin with version number in filename" do

View File

@ -21,8 +21,8 @@ defmodule TestHelper do
alias RabbitMQ.CLI.Plugins.Helpers, as: PluginHelpers
alias RabbitMQ.CLI.Core.{CommandModules, Config, Helpers}
def get_rabbit_hostname() do
RabbitMQ.CLI.Core.Helpers.get_rabbit_hostname()
def get_rabbit_hostname(node_name_type \\ :shortnames) do
RabbitMQ.CLI.Core.Helpers.get_rabbit_hostname(node_name_type)
end
def hostname, do: Helpers.hostname