From dde443393349862bfbe07f1edb0b7171b4192754 Mon Sep 17 00:00:00 2001
From: Dominik Richter <dominik.richter@gmail.com>
Date: Fri, 13 May 2016 11:16:45 +0200
Subject: [PATCH] use struct for processes list

we know all the fields + struct is fully compatible to the curren hash implementation
---
 lib/resources/processes.rb            | 49 +++++++--------------------
 test/unit/resources/processes_test.rb | 21 ++++++++----
 2 files changed, 27 insertions(+), 43 deletions(-)

diff --git a/lib/resources/processes.rb b/lib/resources/processes.rb
index 46e796f7f..3013000de 100644
--- a/lib/resources/processes.rb
+++ b/lib/resources/processes.rb
@@ -26,7 +26,6 @@ module Inspec::Resources
         grep = '(/[^/]*)*'+grep if grep[0] != '/'
         grep = Regexp.new('^' + grep + '(\s|$)')
       end
-
       all_cmds = ps_aux
       @list = all_cmds.find_all do |hm|
         hm[:command] =~ grep
@@ -57,7 +56,12 @@ module Inspec::Resources
       build_process_list(command, regex, os)
     end
 
-    def build_process_list(command, regex, os) # rubocop:disable MethodLength, Metrics/AbcSize
+    Process = Struct.new(:label, :user, :pid,
+                         :cpu, :mem, :vsz,
+                         :rss, :tty, :stat,
+                         :start, :time, :command)
+
+    def build_process_list(command, regex, os)
       cmd = inspec.command(command)
       all = cmd.stdout.split("\n")[1..-1]
       return [] if all.nil?
@@ -66,40 +70,13 @@ module Inspec::Resources
         line.match(regex)
       end.compact
 
-      if os.linux?
-        lines.map do |m|
-          {
-            label: m[1],
-            user: m[2],
-            pid: m[3].to_i,
-            cpu: m[4],
-            mem: m[5],
-            vsz: m[6].to_i,
-            rss: m[7].to_i,
-            tty: m[8],
-            stat: m[9],
-            start: m[10],
-            time: m[11],
-            command: m[12],
-          }
-        end
-      else
-        lines.map do |m|
-          {
-            label: nil,
-            user: m[1],
-            pid: m[2].to_i,
-            cpu: m[3],
-            mem: m[4],
-            vsz: m[5].to_i,
-            rss: m[6].to_i,
-            tty: m[7],
-            stat: m[8],
-            start: m[9],
-            time: m[10],
-            command: m[11],
-          }
-        end
+      lines.map do |m|
+        a = m.to_a[1..-1] # grab all matching groups
+        a.unshift(nil) unless os.linux?
+        a[2] = a[2].to_i
+        a[5] = a[5].to_i
+        a[6] = a[6].to_i
+        Process.new(*a)
       end
     end
   end
diff --git a/test/unit/resources/processes_test.rb b/test/unit/resources/processes_test.rb
index 1e1476866..45dc6be48 100644
--- a/test/unit/resources/processes_test.rb
+++ b/test/unit/resources/processes_test.rb
@@ -13,7 +13,8 @@ describe 'Inspec::Resources::Processes' do
 
   it 'verify processes resource' do
     resource = MockLoader.new(:freebsd10).load_resource('processes', '/bin/bash')
-    _(resource.list).must_equal [{
+    _(resource.list.length).must_equal 1
+    _(resource.list[0].to_h).must_equal({
       label: nil,
       user: 'root',
       pid: 1,
@@ -26,14 +27,13 @@ describe 'Inspec::Resources::Processes' do
       start: '14:15',
       time: '0:00',
       command: '/bin/bash',
-    }]
-
-    _(resource.list.length).must_equal 1
+    })
   end
 
   it 'verify processes resource on linux os' do
     resource = MockLoader.new(:centos6).load_resource('processes', '/sbin/init')
-    _(resource.list).must_equal [{
+    _(resource.list.length).must_equal 1
+    _(resource.list[0].to_h).must_equal({
       label: 'system_u:system_r:kernel_t:s0',
       user: 'root',
       pid: 1,
@@ -46,9 +46,16 @@ describe 'Inspec::Resources::Processes' do
       start: 'May04',
       time: '0:01',
       command: '/sbin/init',
-    }]
+    })
+  end
 
-    _(resource.list.length).must_equal 1
+  it 'access attributes of a process' do
+    resource = MockLoader.new(:centos6).load_resource('processes', '/sbin/init')
+    process = resource.list[0]
+    process.user.must_equal 'root'
+    process[:user].must_equal 'root'
+    process['user'].must_equal 'root'
+    process[1].must_equal 'root'
   end
 
   it 'retrieves the users and states as arrays' do