diff --git a/lib/panda_motd/components/service_status.rb b/lib/panda_motd/components/service_status.rb index cc1ea08..d5e281d 100644 --- a/lib/panda_motd/components/service_status.rb +++ b/lib/panda_motd/components/service_status.rb @@ -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 @@ -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 diff --git a/spec/components/service_status_spec.rb b/spec/components/service_status_spec.rb index 663074b..dfae591 100644 --- a/spec/components/service_status_spec.rb +++ b/spec/components/service_status_spec.rb @@ -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 @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 3f81e6e..29796b8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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')