hawatel_ps 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +4 -0
  5. data/.yardopts +1 -0
  6. data/CONTRIBUTING.md +74 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +184 -0
  10. data/Rakefile +12 -0
  11. data/hawatel_ps.gemspec +29 -0
  12. data/lib/hawatel_ps/linux/proc_control.rb +75 -0
  13. data/lib/hawatel_ps/linux/proc_fetch.rb +405 -0
  14. data/lib/hawatel_ps/linux/proc_info.rb +53 -0
  15. data/lib/hawatel_ps/linux/proc_table.rb +124 -0
  16. data/lib/hawatel_ps/linux.rb +4 -0
  17. data/lib/hawatel_ps/shared/hawatelps_exception.rb +21 -0
  18. data/lib/hawatel_ps/version.rb +3 -0
  19. data/lib/hawatel_ps/windows/proc_control.rb +67 -0
  20. data/lib/hawatel_ps/windows/proc_fetch.rb +217 -0
  21. data/lib/hawatel_ps/windows/proc_info.rb +51 -0
  22. data/lib/hawatel_ps/windows/proc_table.rb +138 -0
  23. data/lib/hawatel_ps/windows/wmi/wmi_cli.rb +65 -0
  24. data/lib/hawatel_ps/windows/wmi/wmi_exception.rb +23 -0
  25. data/lib/hawatel_ps/windows/wmi/wmi_instance.rb +56 -0
  26. data/lib/hawatel_ps/windows.rb +5 -0
  27. data/lib/hawatel_ps.rb +34 -0
  28. data/spec/hawatel_ps_spec.rb +11 -0
  29. data/spec/linux/bdd/proc_spec.rb +54 -0
  30. data/spec/linux/factories/etc/passwd +32 -0
  31. data/spec/linux/factories/proc/1761/cmdline +0 -0
  32. data/spec/linux/factories/proc/1761/environ +0 -0
  33. data/spec/linux/factories/proc/1761/io +7 -0
  34. data/spec/linux/factories/proc/1761/limits +17 -0
  35. data/spec/linux/factories/proc/1761/stat +1 -0
  36. data/spec/linux/factories/proc/1761/status +46 -0
  37. data/spec/linux/factories/proc/meminfo +45 -0
  38. data/spec/linux/factories/proc/net/tcp +7 -0
  39. data/spec/linux/factories/proc/net/udp +8 -0
  40. data/spec/linux/factories/proc/uptime +1 -0
  41. data/spec/linux/support/stub_dir.rb +33 -0
  42. data/spec/linux/support/stub_file.rb +107 -0
  43. data/spec/linux/tdd/proc_fetch_spec.rb +107 -0
  44. data/spec/linux/tdd/proc_table_spec.rb +85 -0
  45. data/spec/shared/hawatelps_exception_spec.rb +13 -0
  46. data/spec/spec_helper.rb +16 -0
  47. data/spec/windows/bdd/proc_spec.rb +119 -0
  48. data/spec/windows/factories/proc_fetch_factorie.rb +76 -0
  49. data/spec/windows/tdd/proc_control_spec.rb +36 -0
  50. data/spec/windows/tdd/proc_fetch_spec.rb +73 -0
  51. data/spec/windows/tdd/proc_table_spec.rb +71 -0
  52. data/spec/windows/tdd/wmi_spec.rb +59 -0
  53. metadata +181 -0
@@ -0,0 +1,32 @@
1
+ root:x:0:0:root:/root:/bin/bash
2
+ daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
3
+ bin:x:2:2:bin:/bin:/usr/sbin/nologin
4
+ sys:x:3:3:sys:/dev:/usr/sbin/nologin
5
+ sync:x:4:65534:sync:/bin:/bin/sync
6
+ games:x:5:60:games:/usr/games:/usr/sbin/nologin
7
+ man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
8
+ lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
9
+ mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
10
+ news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
11
+ uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
12
+ proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
13
+ www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
14
+ backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
15
+ list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
16
+ irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
17
+ gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
18
+ nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
19
+ systemd-timesync:x:100:103:systemd Time Synchronization,,,:/run/systemd:/bin/false
20
+ systemd-network:x:101:104:systemd Network Management,,,:/run/systemd/netif:/bin/false
21
+ systemd-resolve:x:102:105:systemd Resolver,,,:/run/systemd/resolve:/bin/false
22
+ systemd-bus-proxy:x:103:106:systemd Bus Proxy,,,:/run/systemd:/bin/false
23
+ syslog:x:104:109::/home/syslog:/bin/false
24
+ messagebus:x:105:110::/var/run/dbus:/bin/false
25
+ uuidd:x:106:111::/run/uuidd:/bin/false
26
+ ntp:x:107:114::/home/ntp:/bin/false
27
+ whoopsie:x:108:115::/nonexistent:/bin/false
28
+ dnsmasq:x:109:65534:dnsmasq,,,:/var/lib/misc:/bin/false
29
+ lightdm:x:110:120:Light Display Manager:/var/lib/lightdm:/bin/false
30
+ daniel:x:1000:1000:daniel,,,:/home/daniel:/bin/bash
31
+ sshd:x:111:65534::/var/run/sshd:/usr/sbin/nologin
32
+ postgres:x:112:123:PostgreSQL administrator,,,:/var/lib/postgresql:/bin/bash
@@ -0,0 +1,7 @@
1
+ rchar: 4806898281
2
+ wchar: 16777736610
3
+ syscr: 2877443
4
+ syscw: 6704827
5
+ read_bytes: 285220864
6
+ write_bytes: 6392266752
7
+ cancelled_write_bytes: 46899200
@@ -0,0 +1,17 @@
1
+ Limit Soft Limit Hard Limit Units
2
+ Max cpu time unlimited unlimited seconds
3
+ Max file size unlimited unlimited bytes
4
+ Max data size unlimited unlimited bytes
5
+ Max stack size 8388608 unlimited bytes
6
+ Max core file size 0 unlimited bytes
7
+ Max resident set unlimited unlimited bytes
8
+ Max processes 15603 15603 processes
9
+ Max open files 65536 65536 files
10
+ Max locked memory 65536 65536 bytes
11
+ Max address space unlimited unlimited bytes
12
+ Max file locks unlimited unlimited locks
13
+ Max pending signals 15603 15603 signals
14
+ Max msgqueue size 819200 819200 bytes
15
+ Max nice priority 0 0
16
+ Max realtime priority 0 0
17
+ Max realtime timeout unlimited unlimited us
@@ -0,0 +1 @@
1
+ 1761 (java_fake) S 1524 1190 1190 0 -1 1077936128 774701 8158330 224 70 1050454 301513 42670 11229 20 0 50 0 4745 3354886144 240521 18446744073709551615 4194304 4196628 140731327677536 140731327660160 139662310664429 0 0 4096 16796879 18446744073709551615 0 0 17 0 0 0 0 0 0 6294960 6295616 12038144 140731327684673 140731327685740 140731327685740 140731327688653 0
@@ -0,0 +1,46 @@
1
+ Name: java_fake
2
+ State: S (sleeping)
3
+ Tgid: 1761
4
+ Ngid: 0
5
+ Pid: 1761
6
+ PPid: 1524
7
+ TracerPid: 0
8
+ Uid: 1000 1000 1000 1000
9
+ Gid: 1000 1000 1000 1000
10
+ FDSize: 512
11
+ Groups: 4 24 27 30 46 113 122 124 1000
12
+ NStgid: 1761
13
+ NSpid: 1761
14
+ NSpgid: 1190
15
+ NSsid: 1190
16
+ VmPeak: 3373940 kB
17
+ VmSize: 3276256 kB
18
+ VmLck: 0 kB
19
+ VmPin: 0 kB
20
+ VmHWM: 965220 kB
21
+ VmRSS: 962084 kB
22
+ VmData: 3120080 kB
23
+ VmStk: 136 kB
24
+ VmExe: 4 kB
25
+ VmLib: 26232 kB
26
+ VmPTE: 2344 kB
27
+ VmPMD: 24 kB
28
+ VmSwap: 0 kB
29
+ Threads: 50
30
+ SigQ: 0/15603
31
+ SigPnd: 0000000000000000
32
+ ShdPnd: 0000000000000000
33
+ SigBlk: 0000000000000000
34
+ SigIgn: 0000000000001000
35
+ SigCgt: 2000000181004ccf
36
+ CapInh: 0000000000000000
37
+ CapPrm: 0000000000000000
38
+ CapEff: 0000000000000000
39
+ CapBnd: 0000003fffffffff
40
+ Seccomp: 0
41
+ Cpus_allowed: 00000000,00000000,00000000,00000003
42
+ Cpus_allowed_list: 0-1
43
+ Mems_allowed: 00000000,00000001
44
+ Mems_allowed_list: 0
45
+ voluntary_ctxt_switches: 1
46
+ nonvoluntary_ctxt_switches: 5
@@ -0,0 +1,45 @@
1
+ MemTotal: 4029312 kB
2
+ MemFree: 864336 kB
3
+ MemAvailable: 2349072 kB
4
+ Buffers: 417988 kB
5
+ Cached: 915584 kB
6
+ SwapCached: 0 kB
7
+ Active: 2277556 kB
8
+ Inactive: 508876 kB
9
+ Active(anon): 1456700 kB
10
+ Inactive(anon): 25000 kB
11
+ Active(file): 820856 kB
12
+ Inactive(file): 483876 kB
13
+ Unevictable: 32 kB
14
+ Mlocked: 32 kB
15
+ SwapTotal: 0 kB
16
+ SwapFree: 0 kB
17
+ Dirty: 3320 kB
18
+ Writeback: 0 kB
19
+ AnonPages: 1454908 kB
20
+ Mapped: 258608 kB
21
+ Shmem: 28844 kB
22
+ Slab: 249188 kB
23
+ SReclaimable: 209668 kB
24
+ SUnreclaim: 39520 kB
25
+ KernelStack: 8944 kB
26
+ PageTables: 18304 kB
27
+ NFS_Unstable: 0 kB
28
+ Bounce: 0 kB
29
+ WritebackTmp: 0 kB
30
+ CommitLimit: 2014656 kB
31
+ Committed_AS: 3347872 kB
32
+ VmallocTotal: 34359738367 kB
33
+ VmallocUsed: 191528 kB
34
+ VmallocChunk: 34359310332 kB
35
+ HardwareCorrupted: 0 kB
36
+ AnonHugePages: 1046528 kB
37
+ CmaTotal: 0 kB
38
+ CmaFree: 0 kB
39
+ HugePages_Total: 0
40
+ HugePages_Free: 0
41
+ HugePages_Rsvd: 0
42
+ HugePages_Surp: 0
43
+ Hugepagesize: 2048 kB
44
+ DirectMap4k: 141120 kB
45
+ DirectMap2M: 4052992 kB
@@ -0,0 +1,7 @@
1
+ sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
2
+ 0: 0100007F:F76E 00000000:0000 0A 00000000:00000000 00:00000000 00000000 1000 0 21181 1 0000000000000000 100 0 0 10 0
3
+ 1: 0101007F:0035 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 15456 1 0000000000000000 100 0 0 10 0
4
+ 2: 00000000:0016 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 15812 1 0000000000000000 100 0 0 10 0
5
+ 3: 0100007F:0277 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 13766 1 0000000000000000 100 0 0 10 0
6
+ 4: 0100007F:1538 00000000:0000 0A 00000000:00000000 00:00000000 00000000 112 0 13299 1 0000000000000000 100 0 0 10 0
7
+ 5: 0100007F:1B1E 00000000:0000 0A 00000000:00000000 00:00000000 00000000 1000 0 19099 1 0000000000000000 100 0 0 10 0
@@ -0,0 +1,8 @@
1
+ sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode ref pointer drops
2
+ 775: 0101007F:0035 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 15455 2 0000000000000000 0
3
+ 790: 00000000:0044 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 414513 2 0000000000000000 0
4
+ 845: 9834A8C0:007B 00000000:0000 07 00000000:00000000 00:00000000 00000000 107 0 116100 2 0000000000000000 0
5
+ 845: 0100007F:007B 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 13194 2 0000000000000000 0
6
+ 845: 00000000:007B 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 13188 2 0000000000000000 0
7
+ 1823: 0100007F:B44D 0100007F:B44D 01 00000000:00000000 00:00000000 00000000 112 0 13306 2 0000000000000000 0
8
+ 1932: 00000000:14BA 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 414837 2 0000000000000000 0
@@ -0,0 +1 @@
1
+ 137023.81 253979.88
@@ -0,0 +1,33 @@
1
+ module ProcFetch
2
+ module StubDir
3
+ # Stub Dir.foreach method for fake /proc directory
4
+ #
5
+ # @example
6
+ # dir_foreach({:pid => '1761', :process_list => ['1768', '123']'})
7
+ #
8
+ # @return
9
+ def dir_foreach(args)
10
+ pid = args[:pid] || 1
11
+ procs_list = args[:process_list]
12
+ allow(Dir).to receive(:foreach) do |dir|
13
+ if dir == '/proc'
14
+ procs_list
15
+ elsif dir == "/proc/#{pid}/fd"
16
+ ['0','1','2','3']
17
+ end
18
+ end
19
+ end
20
+
21
+
22
+ # Stub Dir.exist? method
23
+ #
24
+ # @example
25
+ # dir_exist()
26
+ #
27
+ # @return [Boolen]
28
+ def dir_exists
29
+ allow(Dir).to receive(:exist?).and_return(true)
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,107 @@
1
+ module ProcFetch
2
+ module StubFile
3
+ # Stub File.stat method
4
+ #
5
+ # @example
6
+ # file_stat({:pid => '1761'})
7
+ #
8
+ # @return
9
+ def file_stat(args)
10
+ pid = args[:pid] || 1
11
+ allow(File).to receive(:stat) do |file|
12
+ if file == "/proc/#{pid}"
13
+ OpenStruct.new({:uid => 1000})
14
+ end
15
+ end
16
+ end
17
+
18
+
19
+ # Stub File.foreach method
20
+ #
21
+ # @example
22
+ # dir_foreach({:pid => '1761', :factories => 'spec/linux/factories'})
23
+ #
24
+ # @return
25
+ def file_foreach(args)
26
+ pid = args[:pid] || 1
27
+ factor = args[:factories] || 'spec/linux/factories/'
28
+ allow(File).to receive(:foreach) do |file|
29
+ if file == '/proc/meminfo'
30
+ File.readlines("#{factor}/proc/meminfo")
31
+ elsif file == "/proc/#{pid}/environ"
32
+ File.readlines("#{factor}/proc/#{pid}/environ")
33
+ elsif file == "/proc/#{pid}/status"
34
+ File.readlines("#{factor}/proc/#{pid}/status")
35
+ elsif file == "/proc/#{pid}/stat"
36
+ File.readlines("#{factor}/proc/#{pid}/stat")
37
+ elsif file == "/proc/#{pid}/io"
38
+ File.readlines("#{factor}/proc/#{pid}/io")
39
+ elsif file == "/proc/#{pid}/limits"
40
+ File.readlines("#{factor}/proc/#{pid}/limits")
41
+ elsif file == "/proc/#{pid}/cmdline"
42
+ File.readlines("#{factor}/proc/#{pid}/cmdline")
43
+ elsif file == "/etc/passwd"
44
+ File.readlines("#{factor}/etc/passwd")
45
+ elsif file == "/proc/net/tcp"
46
+ File.readlines("#{factor}/proc/net/tcp")
47
+ elsif file == "/proc/net/udp"
48
+ File.readlines("#{factor}/proc/net/udp")
49
+ elsif file == "/proc/uptime"
50
+ File.readlines("#{factor}/proc/uptime")
51
+ end
52
+ end
53
+ end
54
+
55
+
56
+ # Stub File.ctime method
57
+ #
58
+ # @example
59
+ # file_ctime({:pid => '1761'})
60
+ #
61
+ # @return
62
+ def file_ctime(args)
63
+ pid = args[:pid] || 1
64
+ allow(File).to receive(:ctime) do |file|
65
+ if file == "/proc/#{pid}"
66
+ Time.at(1457266080)
67
+ end
68
+ end
69
+ end
70
+
71
+
72
+ # Stub File.readable? method
73
+ #
74
+ # @example
75
+ # file_readable()
76
+ #
77
+ # @return
78
+ def file_readable
79
+ allow(File).to receive(:readable?).and_return(true)
80
+ end
81
+
82
+
83
+ # Stub File.readlink method
84
+ #
85
+ # @example
86
+ # file_readlink({:pid => '1761'})
87
+ #
88
+ # @return
89
+ def file_readlink(args)
90
+ pid = args[:pid] || 1
91
+ allow(File).to receive(:readlink) do |file|
92
+ if file == "/proc/#{pid}/cwd"
93
+ '/home/daniel'
94
+ elsif file == "/proc/#{pid}/fd/0"
95
+ '/dev/pts/3'
96
+ elsif file == "/proc/#{pid}/fd/1"
97
+ '/home/daniel/.cache/lxsession/Lubuntu/run.log'
98
+ elsif file == "/proc/#{pid}/fd/2"
99
+ 'socket:[21181]'
100
+ elsif file == "/proc/#{pid}/fd/3"
101
+ 'anon_inode:[eventfd]'
102
+ end
103
+ end
104
+ end
105
+
106
+ end
107
+ end
@@ -0,0 +1,107 @@
1
+ require 'spec_helper'
2
+ require 'ostruct'
3
+ require './spec/linux/support/stub_dir'
4
+ require './spec/linux/support/stub_file'
5
+
6
+ RSpec.configure do |config|
7
+ config.include ProcFetch::StubDir
8
+ config.include ProcFetch::StubFile
9
+ end
10
+
11
+
12
+ describe HawatelPS::Linux::ProcFetch do
13
+
14
+ before(:each) do
15
+ process_list = ['1761']
16
+ dir_exists
17
+ file_readable
18
+ process_list.each do |pid|
19
+ dir_foreach({:pid => pid, :process_list => process_list})
20
+ file_stat({:pid => pid})
21
+ file_foreach({:pid => pid, :factories => 'spec/linux/factories'})
22
+ file_ctime({:pid => pid})
23
+ file_readlink({:pid => pid})
24
+ end
25
+ end
26
+
27
+ let(:process) { HawatelPS::Linux::ProcFetch.get_process[0] }
28
+
29
+ it "check pid attribute for process" do
30
+ expect(process[:pid]).to be_a_kind_of(Integer)
31
+ end
32
+
33
+ it 'check attributes from /proc/<pid>/cwd' do
34
+ expect(process[:cwd]).to match(/^\//)
35
+ end
36
+
37
+ it 'check attributes from /etc/passwd' do
38
+ expect(process[:username]).to match(/\A/)
39
+ end
40
+
41
+ it 'check attributes from /proc/cmdline' do
42
+ expect(process[:cmdline]).to_not be_nil
43
+ end
44
+
45
+ it 'check attributes from /proc/<pid>' do
46
+ expect(process[:ctime]).to_not be_nil
47
+ end
48
+
49
+ it 'check attributes from /proc/<pid>/limits' do
50
+ process[:limits].each do |limit|
51
+ expect(limit[:name]).to match(/\A/)
52
+ expect(limit[:soft]).to_not be_nil
53
+ expect(limit[:hard]).to_not be_nil
54
+ end
55
+ end
56
+
57
+ it 'check attributes from /proc/<pid>/environ' do
58
+ expect(process[:environ][0]).to_not be_nil
59
+ end
60
+
61
+ it 'check attributes from /proc/<pid>/io' do
62
+ expect(process[:wchar]).to be_a_kind_of(Integer)
63
+ expect(process[:rchar]).to be_a_kind_of(Integer)
64
+ expect(process[:syscr]).to be_a_kind_of(Integer)
65
+ expect(process[:syscw]).to be_a_kind_of(Integer)
66
+ expect(process[:read_bytes]).to be_a_kind_of(Integer)
67
+ expect(process[:write_bytes]).to be_a_kind_of(Integer)
68
+ expect(process[:cancelled_write_bytes]).to be_a_kind_of(Integer)
69
+ end
70
+
71
+ it 'check attributes from /proc/<pid>/fd/' do
72
+ expect(process[:tty]).to_not be_nil
73
+ expect(process[:open_files][0]).to_not be_nil
74
+ expect(process[:listen_ports][0]).to_not be_nil
75
+ end
76
+
77
+ it 'check attributes from /proc/<pid>/status' do
78
+ expect(process[:name]).to_not be_nil
79
+ expect(process[:state]).to match(/^[a-z]*$/)
80
+ expect(process[:ppid]).to be_a_kind_of(Integer)
81
+ expect(process[:ruid]).to be_a_kind_of(Integer)
82
+ expect(process[:euid]).to be_a_kind_of(Integer)
83
+ expect(process[:suid]).to be_a_kind_of(Integer)
84
+ expect(process[:fsuid]).to be_a_kind_of(Integer)
85
+ expect(process[:gid]).to be_a_kind_of(Integer)
86
+ expect(process[:egid]).to be_a_kind_of(Integer)
87
+ expect(process[:sgid]).to be_a_kind_of(Integer)
88
+ expect(process[:fsgid]).to be_a_kind_of(Integer)
89
+ expect(process[:threads]).to be_a_kind_of(Integer)
90
+ expect(process[:vmsize]).to be_a_kind_of(Integer)
91
+ expect(process[:vmrss]).to be_a_kind_of(Integer)
92
+ expect(process[:vmdata]).to be_a_kind_of(Integer)
93
+ expect(process[:vmswap]).to be_a_kind_of(Integer)
94
+ expect(process[:vmlib]).to be_a_kind_of(Integer)
95
+ expect(process[:memory_percent]).to be_a_kind_of(Float)
96
+ end
97
+
98
+ it 'check attributes from /proc/<pid>/stat' do
99
+ expect(process[:utime]).to be_a_kind_of(Integer)
100
+ expect(process[:stime]).to be_a_kind_of(Integer)
101
+ expect(process[:cpu_time]).to be_a_kind_of(Integer)
102
+ expect(process[:cpu_percent]).to be_a_kind_of(Float)
103
+ end
104
+
105
+
106
+
107
+ end
@@ -0,0 +1,85 @@
1
+ require 'spec_helper'
2
+ require 'ostruct'
3
+ require './spec/linux/support/stub_dir'
4
+ require './spec/linux/support/stub_file'
5
+
6
+ RSpec.configure do |config|
7
+ config.include ProcFetch::StubDir
8
+ config.include ProcFetch::StubFile
9
+ end
10
+
11
+
12
+ describe HawatelPS::Linux::ProcTable do
13
+
14
+ before(:each) do
15
+ process_list = ['1761']
16
+ process_list.each do |pid|
17
+ dir_exists
18
+ dir_foreach({:pid => pid, :process_list => process_list})
19
+ file_readable
20
+ file_stat({:pid => pid})
21
+ file_foreach({:pid => pid, :factories => 'spec/linux/factories'})
22
+ file_ctime({:pid => pid})
23
+ file_readlink({:pid => pid})
24
+ end
25
+ end
26
+
27
+
28
+ it "return all process instances" do
29
+ process = HawatelPS::Linux::ProcTable.proc_table
30
+ expect(process[0].pid).to be_a_kind_of(Integer)
31
+ end
32
+
33
+ it "search process by name" do
34
+ process = HawatelPS::Linux::ProcTable.search_by_name('java_fake')
35
+ expect(process[0].pid).to be_a_kind_of(Integer)
36
+ end
37
+
38
+ it "search process by name using regex" do
39
+ process = HawatelPS::Linux::ProcTable.search_by_name('/^java/')
40
+ expect(process[0].pid).to be_a_kind_of(Integer)
41
+ end
42
+
43
+ it "search process by name using regex" do
44
+ process = HawatelPS::Linux::ProcTable.search_by_name('/^jasdfsdfva/')
45
+ expect { process[0].pid }.to raise_error(NoMethodError)
46
+ end
47
+
48
+ it "search process by pid" do
49
+ process = HawatelPS::Linux::ProcTable.search_by_pid(1761)
50
+ expect(process.pid).to eq(1761)
51
+ end
52
+
53
+ it "search process by condition == " do
54
+ process = HawatelPS::Linux::ProcTable.search_by_condition(:attr => 'pid', :oper => '==', :value => '1761')
55
+ expect(process[0].pid).to eq(1761)
56
+ end
57
+
58
+ it "search process by condition <= " do
59
+ process = HawatelPS::Linux::ProcTable.search_by_condition(:attr => 'vmsize', :oper => '<=', :value => '3276257')
60
+ expect(process[0].pid).to be_a_kind_of(Integer)
61
+ end
62
+
63
+ it "search process by condition >= " do
64
+ process = HawatelPS::Linux::ProcTable.search_by_condition(:attr => 'vmsize', :oper => '>=', :value => '200000')
65
+ expect(process[0].pid).to be_a_kind_of(Integer)
66
+ end
67
+
68
+ it "search process by condition != " do
69
+ process = HawatelPS::Linux::ProcTable.search_by_condition(:attr => 'vmsize', :oper => '!=', :value => '200000')
70
+ expect(process[0].pid).to be_a_kind_of(Integer)
71
+ end
72
+
73
+ it "search process by condition > " do
74
+ process = HawatelPS::Linux::ProcTable.search_by_condition(:attr => 'vmsize', :oper => '>', :value => '200000')
75
+ expect(process[0].pid).to be_a_kind_of(Integer)
76
+ end
77
+
78
+ it "search process by condition < " do
79
+ process = HawatelPS::Linux::ProcTable.search_by_condition(:attr => 'vmsize', :oper => '<', :value => '6000000')
80
+ expect(process[0].pid).to be_a_kind_of(Integer)
81
+ end
82
+
83
+
84
+
85
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe HawatelPS::HawatelPSException do
4
+ it 'catch exception' do
5
+ expect {
6
+ begin
7
+ 100 / 0
8
+ rescue => ex
9
+ raise HawatelPS::HawatelPSException.new({:exception => ex})
10
+ end
11
+ }.to raise_error(HawatelPS::HawatelPSException)
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'hawatel_ps'
3
+ require 'factory_girl'
4
+
5
+ RSpec.configure do |config|
6
+ config.include FactoryGirl::Syntax::Methods
7
+
8
+ config.mock_with :rspec do |mocks|
9
+ mocks.allow_message_expectations_on_nil = true
10
+ end
11
+
12
+ config.before(:suite) do
13
+ FactoryGirl.definition_file_paths = %W(spec/linux/factories spec/windows/factories)
14
+ FactoryGirl.find_definitions
15
+ end
16
+ end
@@ -0,0 +1,119 @@
1
+ require 'spec_helper'
2
+
3
+ describe HawatelPS do
4
+ context '#search_by_pid' do
5
+ it 'process found' do
6
+ name = 'PING.EXE'
7
+ pid = child(20)
8
+ proc = HawatelPS.search_by_pid(pid)
9
+ expect(proc).not_to be_nil
10
+ expect_proc_attrs(proc, {:pid => pid, :name => name})
11
+ end
12
+
13
+ it 'process not found' do
14
+ proc = HawatelPS.search_by_pid(-1)
15
+ expect(proc).to be_nil
16
+ end
17
+ end
18
+
19
+ context '#search_by_name' do
20
+ it 'process found' do
21
+ name = 'PING.EXE'
22
+ pid = child(20)
23
+ procs = HawatelPS.search_by_name(name)
24
+
25
+ expect(procs).not_to be_nil
26
+ expect(find_proc(procs, pid, name)).to eq(true)
27
+
28
+ end
29
+
30
+ it 'process not found' do
31
+ proc = HawatelPS.search_by_name("_____________procss_not_exists_____________")
32
+ expect(proc).to be_nil
33
+ end
34
+ end
35
+
36
+ context '#search_by_condition' do
37
+ it 'process found' do
38
+ pid = child(20)
39
+ processes = HawatelPS.search_by_condition(:attr => 'processid', :oper => '>', :value => "#{pid}" )
40
+ expect(processes.size).to be >= 2
41
+ end
42
+
43
+ it 'process not found' do
44
+ processes = HawatelPS.search_by_condition(:attr => 'processid', :oper => '=', :value => '-1' )
45
+ expect(processes).to be_nil
46
+ end
47
+ end
48
+
49
+ context '#proc_table' do
50
+ let(:procs) { HawatelPS.proc_table }
51
+ let(:name) { 'PING.EXE' }
52
+
53
+ it 'process found' do
54
+ pid = child(20)
55
+ expect(find_proc(procs, pid, name)).to eq(true)
56
+
57
+ end
58
+
59
+ it 'processes not found' do
60
+ expect(find_proc(procs, -1, name)).to eq(false)
61
+ end
62
+ end
63
+
64
+
65
+ context HawatelPS::Windows::ProcControl do
66
+ it '#terminate successful' do
67
+ pid = child(20)
68
+ proc = HawatelPS.search_by_pid(pid)
69
+ status = proc.terminate
70
+ expect(status).to eq(0)
71
+ end
72
+
73
+ it '#terminate no successful' do
74
+ pid = child(20)
75
+ proc = HawatelPS.search_by_pid(pid)
76
+ proc.terminate
77
+ sleep 1
78
+ status = proc.terminate
79
+ expect(status).not_to eq(0)
80
+ end
81
+
82
+ it '#state running' do
83
+ pid = child(20)
84
+ proc = HawatelPS.search_by_pid(pid)
85
+ status = proc.status
86
+ expect(status).to eq("running")
87
+ end
88
+ end
89
+
90
+ end
91
+
92
+ private
93
+
94
+ def expect_proc_attrs(proc, args)
95
+ expect(proc.processid.to_i).to eq(args[:pid])
96
+ expect(proc.name.downcase).to eq(args[:name].downcase)
97
+ expect(proc.sid).to be_a(String)
98
+ expect(proc.wmi_object).to be_a(WIN32OLE)
99
+ expect(proc.user).not_to be_nil
100
+ expect(proc.domain).not_to be_nil
101
+ expect(proc.availablevirtualsize.to_i).to be >= 0
102
+ expect(proc.cpupercent.to_f).to be >= 0
103
+ expect(proc.memorypercent.to_f).to be >= 0
104
+ end
105
+
106
+ def find_proc(procs, pid, name)
107
+ proc_found = false
108
+ procs.each do |proc|
109
+ if proc.processid.to_i == pid
110
+ proc_found = true
111
+ expect_proc_attrs(proc, {:pid => pid, :name => name})
112
+ end
113
+ end
114
+ return proc_found
115
+ end
116
+
117
+ def child(timeout)
118
+ pid = spawn('PING.EXE', "127.0.0.1", "-n", timeout.to_s, "-w", "10000", :out => "NUL")
119
+ end