Skip to content

Commit

Permalink
Change service status component to handle all statuses
Browse files Browse the repository at this point in the history
Closes #27
  • Loading branch information
taylorthurlow committed Dec 31, 2018
1 parent e1eb2d9 commit 254632c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
16 changes: 7 additions & 9 deletions lib/panda_motd/components/service_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def to_s

def parse_service(service)
cmd_result = `systemctl is-active #{service[0]}`.strip
unless valid_responses.include? cmd_result
@errors << ComponentError.new(self, 'Unable to parse systemctl output')
if cmd_result.empty?
@errors << ComponentError.new(self, 'systemctl output was blank.')
end
cmd_result
end
Expand All @@ -41,13 +41,11 @@ def parse_services(services)
end

def service_colors
{
active: :green,
inactive: :red
}
end
colors = Hash.new(:red)
colors[:active] = :green
colors[:inactive] = :yellow
colors[:failed] = :red

def valid_responses
['active', 'inactive']
colors
end
end
22 changes: 13 additions & 9 deletions spec/components/service_status_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,23 @@

context 'when printing different statuses' do
it 'prints active in green' do
stub_system_call(component, returns: 'active')
component.process

component.instance_variable_set(:@results, servicename: :active)
expect(component.to_s).to include 'active'.green
end

it 'prints inactive in red' do
stub_system_call(component, returns: 'active')
component.process

it 'prints inactive in yellow' do
component.instance_variable_set(:@results, servicename: :inactive)
expect(component.to_s).to include 'inactive'.red
expect(component.to_s).to include 'inactive'.yellow
end

it 'prints failed in red' do
component.instance_variable_set(:@results, servicename: :failed)
expect(component.to_s).to include 'failed'.red
end

it 'prints unknown status in red' do
component.instance_variable_set(:@results, servicename: :asdf123)
expect(component.to_s).to include 'asdf123'.red
end
end

Expand All @@ -46,7 +50,7 @@
errors = component.errors

expect(errors.count).to eq 2
expect(errors.first.message).to eq 'Unable to parse systemctl output'
expect(errors.first.message).to eq 'systemctl output was blank.'
end
end
end
Expand Down

0 comments on commit 254632c

Please sign in to comment.