Skip to content
This repository was archived by the owner on Nov 18, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
PROJECT = rabbitmq_cli

dep_observer_cli = git https://github.com/zhongwencool/observer_cli 1.4.4

BUILD_DEPS = rabbit_common
DEPS = observer_cli
TEST_DEPS = amqp_client rabbit
Expand Down
58 changes: 58 additions & 0 deletions lib/rabbitmq/cli/diagnostics/commands/observer_command.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
## 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 https://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 RabbitMQ.CLI.Diagnostics.Commands.ObserverCommand do
@behaviour RabbitMQ.CLI.CommandBehaviour
use RabbitMQ.CLI.DefaultOutput

def switches(), do: [interval: :integer]
def aliases(), do: [i: :interval]

def merge_defaults(args, opts) do
{args, Map.merge(%{interval: 5}, opts)}
end


use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments
use RabbitMQ.CLI.Core.RequiresRabbitAppRunning

def run([], %{node: node_name, interval: interval}) do
case :observer_cli.start(node_name, [{:interval, interval * 1000}]) do
# See zhongwencool/observer_cli#54
{:badrpc, _} = err -> err
{:error, _} = err -> err
{:error, _, _} = err -> err
:ok -> {:ok, "Disconnected from #{node_name}."}
:quit -> {:ok, "Disconnected from #{node_name}."}
other -> other
end
end

def help_section(), do: :observability_and_health_checks

def description(), do: "Starts a CLI observer interface on the target node"

def usage, do: "observer [--interval <seconds>]"

def usage_additional() do
[
["--interval <seconds>", "Update interval to use, in seconds"]
]
end

def banner(_, %{node: node_name}) do
"Starting a CLI observer interface on node #{node_name}..."
end
end
5 changes: 3 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,19 @@ defmodule RabbitMQCtl.MixfileBase do
{:json, "~> 1.2.0"},
{:csv, "~> 2.0.0"},
{:stdout_formatter, "~> 0.2.3"},
{:observer_cli, "~> 1.4.4"},

{:amqp, "~> 1.2.0", only: :test},
{:dialyxir, "~> 0.5", only: :test, runtime: false},
{:temp, "~> 0.4", only: :test},
{:temp, "~> 0.4", only: :test}
]

rabbitmq_deps = case System.get_env("DEPS_DIR") do
nil ->
# rabbitmq_cli is built as a standalone Elixir application.
[
{:rabbit_common, "~> 3.7.0"},
{:amqp_client, "~> 3.7.0", only: :test},
{:amqp_client, "~> 3.7.0", only: :test}
]
deps_dir ->
# rabbitmq_cli is built as part of RabbitMQ.
Expand Down
53 changes: 53 additions & 0 deletions test/diagnostics/observer_command_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
## 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 https://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 ObserverCommandTest do
use ExUnit.Case
import TestHelper

@command RabbitMQ.CLI.Diagnostics.Commands.ObserverCommand

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

:ok
end

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

test "merge_defaults: injects a default interval of 5s" do
assert @command.merge_defaults([], %{}) == {[], %{interval: 5}}
end

test "validate: treats positional arguments as a failure" do
assert @command.validate(["extra-arg"], %{}) == {:validation_failure, :too_many_args}
end

test "validate: treats empty positional arguments and default switches as a success" do
assert @command.validate([], %{}) == :ok
end

@tag test_timeout: 3000
test "run: targeting an unreachable node throws a badrpc", context do
assert match?({:badrpc, _}, @command.run([], %{node: :jake@thedog, interval: 5}))
end
end
1 change: 1 addition & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
## Copyright (c) 2007-2019 Pivotal Software, Inc. All rights reserved.


ExUnit.configure(exclude: [disabled: true])
ExUnit.start()

defmodule TestHelper do
Expand Down