Skip to content

Commit

Permalink
Refactor service status and fix missing non-running services (#18)
Browse files Browse the repository at this point in the history
Resolves #15
  • Loading branch information
taylorthurlow authored Oct 28, 2018
1 parent 0524384 commit 4b31db5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 40 deletions.
27 changes: 11 additions & 16 deletions lib/panda_motd/components/service_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def to_s
Services:
#{@results.map do |(name, status)|
name_part = name.to_s.ljust(longest_name_size, ' ') + ':'
status_part = status.to_s.colorize(service_colors[status])
status_part = status.to_s.colorize(service_colors[status.to_sym])
" #{name_part} #{status_part}"
end.join("\n")}
HEREDOC
Expand All @@ -32,28 +32,23 @@ def to_s
private

def parse_service(service)
columns = service.split
[columns[0].sub('.service', ''), columns[3]]
cmd_result = `systemctl is-active #{service[0]}`.strip
@errors << ComponentError.new(self, 'Unable to parse systemctl output') unless valid_responses.include? cmd_result
return cmd_result
end

def parse_services(services)
cmd_result = `systemctl | grep '\.service'`

@errors << ComponentError.new(self, 'Unable to parse systemctl output') if cmd_result.empty?

cmd_result.lines
.map { |line| parse_service(line) }
.select { |name, _status| services.key?(name) }
.map { |service| service.map(&:to_sym) }
.to_h
services.map { |service| [service[1].to_sym, parse_service(service).to_sym] }.to_h
end

def service_colors
return {
running: :green,
exited: :white,
failed: :red,
not_found: :yellow
active: :green,
inactive: :red
}
end

def valid_responses
return ['active', 'inactive']
end
end
36 changes: 14 additions & 22 deletions spec/components/service_status_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

describe ServiceStatus do
before do
stub_system_call(described_class_instance)
stub_system_call(described_class_instance, 'active')
described_class_instance.process
end

Expand All @@ -14,43 +14,35 @@
}

it 'returns the list of statuses' do
expect(described_class_instance.results).to eq(plexmediaserver: :running, sonarr: :running)
expect(described_class_instance.results).to eq(Plex: :active, Sonarr: :active)
end

it 'prints the list of statuses' do
# require 'byebug'
# debugger
results = described_class_instance.to_s.delete(' ') # handle variable whitespace
expect(results).to include 'plexmediaserver:' + 'running'.green
expect(results).to include 'sonarr:' + 'running'.green
expect(results).to include 'Plex:' + 'active'.green
expect(results).to include 'Sonarr:' + 'active'.green
end

context 'when printing different statuses' do
it 'prints running in green' do
described_class_instance.instance_variable_set(:@results, servicename: :running)
expect(described_class_instance.to_s).to include 'running'.green
it 'prints active in green' do
described_class_instance.instance_variable_set(:@results, servicename: :active)
expect(described_class_instance.to_s).to include 'active'.green
end

it 'prints exited in white' do
described_class_instance.instance_variable_set(:@results, servicename: :exited)
expect(described_class_instance.to_s).to include 'exited'.white
end

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

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

context 'when system call output is empty' do
it 'adds an error to the component' do
allow(described_class_instance).to receive(:`).and_return('')
stub_system_call(described_class_instance, '')
described_class_instance.process

expect(described_class_instance.errors.count).to eq 1
expect(described_class_instance.errors.count).to eq 2
expect(described_class_instance.errors.first.message).to eq 'Unable to parse systemctl output'
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def instance_with_configuration(described_class, config_hash)
return described_class.new(motd)
end

def stub_system_call(described_class_instance)
allow(described_class_instance).to receive(:`).and_return(command_output(described_class_instance.class))
def stub_system_call(described_class_instance, returns = command_output(described_class_instance.class))
allow(described_class_instance).to receive(:`).and_return(returns)
end

def command_output(component_class, file_name = 'output')
Expand Down

0 comments on commit 4b31db5

Please sign in to comment.