Skip to content

Commit

Permalink
Fixes #3036, #12587, #3956 - Ping command (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofedoren authored and mbacovsky committed Oct 17, 2019
1 parent 30091a4 commit 62ee512
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/hammer_cli_foreman.rb
Expand Up @@ -168,6 +168,13 @@ def self.exception_handler_class
'HammerCLIForeman::ConfigGroup', 'hammer_cli_foreman/config_group'
)

HammerCLI::MainCommand.lazy_subcommand('ping', _("Get the status of the server and/or it's subcomponents"),
'HammerCLIForeman::PingCommand', 'hammer_cli_foreman/ping'
)

HammerCLI::MainCommand.lazy_subcommand('status', _("Get the complete status of the server and/or it's subcomponents"),
'HammerCLIForeman::StatusCommand', 'hammer_cli_foreman/status'
)
rescue => e
handler = HammerCLIForeman::ExceptionHandler.new(:context => {}, :adapter => :base)
handler.handle_exception(e)
Expand Down
2 changes: 2 additions & 0 deletions lib/hammer_cli_foreman/command_extensions.rb
Expand Up @@ -3,3 +3,5 @@
require 'hammer_cli_foreman/command_extensions/puppet_environments'
require 'hammer_cli_foreman/command_extensions/option_sources'
require 'hammer_cli_foreman/command_extensions/hosts'
require 'hammer_cli_foreman/command_extensions/ping'
require 'hammer_cli_foreman/command_extensions/status'
14 changes: 14 additions & 0 deletions lib/hammer_cli_foreman/command_extensions/ping.rb
@@ -0,0 +1,14 @@
module HammerCLIForeman
module CommandExtensions
class Ping < HammerCLI::CommandExtensions
before_print do |data|
unless data['results']['foreman'].nil?
status = data['results']['foreman']['database']['active']
data['results']['foreman']['database']['active'] = status ? 'ok' : 'FAIL'
duration = data['results']['foreman']['database']['duration_ms']
data['results']['foreman']['database']['duration_ms'] = _("Duration: %sms") % duration
end
end
end
end
end
44 changes: 44 additions & 0 deletions lib/hammer_cli_foreman/command_extensions/status.rb
@@ -0,0 +1,44 @@
module HammerCLIForeman
module CommandExtensions
class Status < HammerCLI::CommandExtensions
before_print do |data|
return if data['results'].nil?

normalize_plugins(data['results']['foreman']['plugins'])
data['results']['foreman']['smart_proxies'].each do |proxy|
proxy['features'] = normalize_features(proxy['features'])
proxy['failed_features'] = normalize_failed_features(proxy['failed_features'])
end
end

def self.normalize_plugins(plugins)
plugins.map! do |plugin|
name, version = plugin.split(': ', 2)[1].split(', ', 3)[0..1]
{ name: name, version: version }
end
end

def self.normalize_features(features)
active_features = []
features.each_pair do |name, version|
active_features << {
name: name,
version: version
}
end
active_features
end

def self.normalize_failed_features(features)
failed_features = []
features.each_pair do |name, error|
failed_features << {
name: name,
error: error
}
end
failed_features
end
end
end
end
24 changes: 24 additions & 0 deletions lib/hammer_cli_foreman/ping.rb
@@ -0,0 +1,24 @@
module HammerCLIForeman
class PingCommand < HammerCLIForeman::Command
resource :ping

class ForemanCommand < HammerCLIForeman::Command
action :ping
command_name 'foreman'

output do
from 'foreman' do
field :database, _('database'), Fields::Label do
field :active, _('Status')
field :duration_ms, _('Server Response')
end
end
end

extend_with(HammerCLIForeman::CommandExtensions::Ping.new)
end

self.default_subcommand = 'foreman'
autoload_subcommands
end
end
53 changes: 53 additions & 0 deletions lib/hammer_cli_foreman/status.rb
@@ -0,0 +1,53 @@
module HammerCLIForeman
class StatusCommand < HammerCLIForeman::Command
resource :ping

class ForemanCommand < HammerCLIForeman::Command
action :statuses
command_name 'foreman'

output do
from 'foreman' do
field :version, _('Version')
from 'api' do
field :version, _('API Version')
end
field :database, _('Database'), Fields::Label do
field :active, _('Status')
field :duration_ms, _('Server Response')
end
collection :plugins, _('Plugins') do
field :name, _('Name')
field :version, _('Version')
end
collection :smart_proxies, _('Smart Proxies') do
field :name, _('Name')
field :version, _('Version')
field :status, _('Status')
collection :features, _('Features'), hide_blank: true do
field :name, _('Name')
field :version, _('Version')
end
collection :failed_features, _('Failed features'), hide_blank: true do
field :name, _('Name')
field :error, _('Error')
end
end
collection :compute_resources, _('Compute Resources') do
field :name, _('Name')
field :status, _('Status')
collection :errors, _('Errors'), hide_blank: true do
field nil, nil
end
end
end
end

extend_with(HammerCLIForeman::CommandExtensions::Ping.new(only: :data))
extend_with(HammerCLIForeman::CommandExtensions::Status.new(only: :data))
end

self.default_subcommand = 'foreman'
autoload_subcommands
end
end
1 change: 1 addition & 0 deletions test/data/1.24/foreman_api.json

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions test/functional/ping_test.rb
@@ -0,0 +1,34 @@
require File.join(File.dirname(__FILE__), 'test_helper')

describe 'ping' do
let(:base_cmd) { %w[ping] }

describe 'foreman' do
let(:cmd) { base_cmd << 'foreman' }
let(:ping_results) do
{
'results' => {
'foreman' => {
'database' => { 'active' => true, 'duration_ms' => 0 }
}
}
}
end

it 'pings foreman system' do
api_expects(:ping, :ping, 'Ping').returns(ping_results)

output = OutputMatcher.new(
[
'database:',
' Status: ok',
' Server Response: Duration: 0ms'
]
)

expected_result = success_result(output)
result = run_cmd(cmd)
assert_cmd(expected_result, result)
end
end
end
46 changes: 46 additions & 0 deletions test/functional/status_test.rb
@@ -0,0 +1,46 @@
require File.join(File.dirname(__FILE__), 'test_helper')

describe 'status' do
let(:base_cmd) { %w[status] }

describe 'foreman' do
let(:cmd) { base_cmd << 'foreman' }
let(:status_results) do
{
'results' => {
'foreman' => {
'database' => { 'active' => true, 'duration_ms' => 0 },
'version' => '1.24.0-develop',
'api' => { 'version' => 'v2' },
'plugins' => [],
'smart_proxies' => [],
'compute_resources' => []
}
}
}
end

it 'checks status of the foreman system' do
api_expects(:ping, :statuses, 'Status').returns(status_results)

output = OutputMatcher.new(
[
'Version: 1.24.0-develop',
'API Version: v2',
'Database:',
' Status: ok',
' Server Response: Duration: 0ms',
'Plugins:',
'',
'Smart Proxies:',
'',
'Compute Resources:'
]
)

expected_result = success_result(output)
result = run_cmd(cmd)
assert_cmd(expected_result, result)
end
end
end
2 changes: 1 addition & 1 deletion test/test_helper.rb
Expand Up @@ -17,7 +17,7 @@
require 'hammer_cli'
require 'hammer_cli_foreman/testing/api_expectations'

FOREMAN_VERSION = Gem::Version.new(ENV['TEST_API_VERSION'] || '1.22')
FOREMAN_VERSION = Gem::Version.new(ENV['TEST_API_VERSION'] || '1.24')

include HammerCLIForeman::Testing::APIExpectations
HammerCLI.context[:api_connection].create('foreman') do
Expand Down

0 comments on commit 62ee512

Please sign in to comment.