2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
|
|
|
require "inspec/resource"
|
|
|
|
require "inspec/resources/processes"
|
2015-09-05 15:40:02 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
describe "Inspec::Resources::Processes" do
|
|
|
|
it "handles empty process results" do
|
|
|
|
resource = load_resource("processes", "nothing")
|
2017-02-02 22:10:41 +00:00
|
|
|
_(resource.entries).must_equal []
|
2015-10-26 15:47:54 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "verify processes resource" do
|
|
|
|
resource = MockLoader.new(:freebsd10).load_resource("processes", "login -fp apop")
|
2019-01-12 23:50:20 +00:00
|
|
|
|
2019-04-16 21:28:39 +00:00
|
|
|
expect_deprecation(:property_processes_list) do
|
2019-02-21 17:24:19 +00:00
|
|
|
resource.list.length.must_equal 2
|
|
|
|
end
|
2019-01-12 23:50:20 +00:00
|
|
|
|
2017-02-02 22:10:41 +00:00
|
|
|
_(resource.entries.length).must_equal 2
|
|
|
|
_(resource.entries[0].to_h).must_equal({
|
2016-05-09 19:19:56 +00:00
|
|
|
label: nil,
|
2016-10-17 14:23:50 +00:00
|
|
|
pid: 7115,
|
2019-06-11 22:24:35 +00:00
|
|
|
cpu: "0.3",
|
|
|
|
mem: "0.0",
|
2016-10-17 14:23:50 +00:00
|
|
|
vsz: 2516588,
|
|
|
|
rss: 3052,
|
2019-06-11 22:24:35 +00:00
|
|
|
tty: "ttys008",
|
|
|
|
stat: "U",
|
|
|
|
start: "Fri05PM",
|
|
|
|
time: "0:00.05",
|
|
|
|
user: "root",
|
|
|
|
command: "login -fp apop",
|
2016-05-13 09:16:45 +00:00
|
|
|
})
|
2015-09-05 15:40:02 +00:00
|
|
|
end
|
2015-12-07 08:39:48 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "verify processes resource on linux os" do
|
|
|
|
resource = MockLoader.new(:centos6).load_resource("processes", "postgres: bifrost bifrost")
|
2017-02-02 22:10:41 +00:00
|
|
|
_(resource.entries.length).must_equal 1
|
|
|
|
_(resource.entries[0].to_h).must_equal({
|
2019-06-11 22:24:35 +00:00
|
|
|
label: "system_u:system_r:init_t:s0",
|
2017-02-02 22:10:41 +00:00
|
|
|
pid: 5127,
|
2019-06-11 22:24:35 +00:00
|
|
|
cpu: "0.0",
|
|
|
|
mem: "0.2",
|
2017-02-02 22:10:41 +00:00
|
|
|
vsz: 547208,
|
|
|
|
rss: 5376,
|
2019-06-11 22:24:35 +00:00
|
|
|
tty: "?",
|
|
|
|
stat: "Ss",
|
|
|
|
start: "10:54:22",
|
|
|
|
time: "00:00:00",
|
|
|
|
user: "opscode-pgsql",
|
|
|
|
command: "postgres: bifrost bifrost 127.0.0.1(43699) idle",
|
2017-02-02 22:10:41 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "verify processes resource using where filters on linux os. String match regex" do
|
|
|
|
resource = MockLoader.new(:centos6).load_resource("processes", ".+")
|
2017-09-05 12:36:55 +00:00
|
|
|
_(resource.entries.length).must_equal 8
|
2019-06-11 22:24:35 +00:00
|
|
|
_(resource.where { pid < 11663 && cpu == "0.0" }.users).must_equal(["opscode-pgsql", "opscode", "root", "httpd"])
|
2017-02-02 22:10:41 +00:00
|
|
|
_(resource.where { user =~ /opscode-.*/ }.entries[0].to_h).must_equal({
|
2019-06-11 22:24:35 +00:00
|
|
|
label: "system_u:system_r:init_t:s0",
|
2016-10-17 14:23:50 +00:00
|
|
|
pid: 5127,
|
2019-06-11 22:24:35 +00:00
|
|
|
cpu: "0.0",
|
|
|
|
mem: "0.2",
|
2016-10-17 14:23:50 +00:00
|
|
|
vsz: 547208,
|
|
|
|
rss: 5376,
|
2019-06-11 22:24:35 +00:00
|
|
|
tty: "?",
|
|
|
|
stat: "Ss",
|
|
|
|
start: "10:54:22",
|
|
|
|
time: "00:00:00",
|
|
|
|
user: "opscode-pgsql",
|
|
|
|
command: "postgres: bifrost bifrost 127.0.0.1(43699) idle",
|
2016-05-13 09:16:45 +00:00
|
|
|
})
|
|
|
|
end
|
2016-05-09 19:19:56 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "verify long-run processes resource on linux os" do
|
|
|
|
resource = MockLoader.new(:centos6).load_resource("processes", "httpd")
|
2017-02-02 22:10:41 +00:00
|
|
|
_(resource.entries.length).must_equal 4
|
|
|
|
_(resource.entries[0].to_h).must_equal({
|
2019-06-11 22:24:35 +00:00
|
|
|
label: "-",
|
2016-11-30 00:20:49 +00:00
|
|
|
pid: 4589,
|
2019-06-11 22:24:35 +00:00
|
|
|
cpu: "0.0",
|
|
|
|
mem: "0.0",
|
2016-11-30 00:20:49 +00:00
|
|
|
vsz: 70992,
|
|
|
|
rss: 2864,
|
2019-06-11 22:24:35 +00:00
|
|
|
tty: "?",
|
|
|
|
stat: "Ss",
|
|
|
|
start: "Nov 09",
|
|
|
|
time: "00:01:01",
|
|
|
|
user: "root",
|
|
|
|
command: "/usr/local/apache2/bin/httpd -k start",
|
2016-11-30 00:20:49 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "access information of a process" do
|
|
|
|
resource = MockLoader.new(:centos6).load_resource("processes", "postgres: bifrost bifrost")
|
2017-02-02 22:10:41 +00:00
|
|
|
process = resource.entries[0]
|
2019-06-11 22:24:35 +00:00
|
|
|
process.user.must_equal "opscode-pgsql"
|
|
|
|
process[:user].must_equal "opscode-pgsql"
|
|
|
|
process["user"].must_equal "opscode-pgsql"
|
|
|
|
process[-1].must_equal "postgres: bifrost bifrost 127.0.0.1(43699) idle"
|
2016-10-17 14:23:50 +00:00
|
|
|
process[1].must_equal 5127
|
2016-05-09 19:19:56 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "retrieves the users and states as arrays" do
|
|
|
|
resource = MockLoader.new(:freebsd10).load_resource("processes", "login -fp apop")
|
|
|
|
_(resource.users.sort).must_equal %w{apop root}
|
|
|
|
_(resource.states.sort).must_equal %w{Ss U}
|
2015-12-07 08:39:48 +00:00
|
|
|
end
|
2016-05-09 19:19:56 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "retrieves the users and states as arrays on linux os" do
|
|
|
|
resource = MockLoader.new(:centos6).load_resource("processes", "postgres: bifrost bifrost")
|
|
|
|
_(resource.users.sort).must_equal ["opscode-pgsql"]
|
|
|
|
_(resource.states.sort).must_equal ["Ss"]
|
2017-06-04 20:03:04 +00:00
|
|
|
_(resource.exists?).must_equal true
|
2016-05-09 19:19:56 +00:00
|
|
|
end
|
2016-11-29 11:00:43 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "command name matches with output (string)" do
|
|
|
|
resource = MockLoader.new(:centos6).load_resource("processes", "mysqld")
|
|
|
|
_(resource.to_s).must_equal "Processes mysqld"
|
2016-11-29 11:00:43 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "command name matches with output (regex)" do
|
|
|
|
resource = MockLoader.new(:centos6).load_resource("processes", /mysqld/)
|
|
|
|
_(resource.to_s).must_equal "Processes /mysqld/"
|
2016-11-29 11:00:43 +00:00
|
|
|
end
|
2017-06-04 20:03:04 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "handles labels with spaces" do
|
|
|
|
resource = MockLoader.new(:centos6).load_resource("processes", "ntpd")
|
2017-09-05 12:36:55 +00:00
|
|
|
_(resource.entries.length).must_equal 1
|
|
|
|
_(resource.entries[0].to_h).must_equal({
|
2019-06-11 22:24:35 +00:00
|
|
|
label: "/usr/sbin/ntpd (enforce)",
|
2017-09-05 12:36:55 +00:00
|
|
|
pid: 14415,
|
2019-06-11 22:24:35 +00:00
|
|
|
cpu: "0.0",
|
|
|
|
mem: "0.5",
|
2017-09-05 12:36:55 +00:00
|
|
|
vsz: 110032,
|
|
|
|
rss: 5164,
|
2019-06-11 22:24:35 +00:00
|
|
|
tty: "?",
|
|
|
|
stat: "Ssl",
|
|
|
|
start: "22:39:25",
|
|
|
|
time: "00:00:00",
|
|
|
|
user: "ntp",
|
|
|
|
command: "/usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 112:117",
|
2017-09-05 12:36:55 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "handles regular processes from busybox" do
|
|
|
|
resource = MockLoader.new(:alpine).load_resource("processes", "/some/other/coolprogram")
|
2018-10-04 18:06:17 +00:00
|
|
|
_(resource.entries.length).must_equal 1
|
|
|
|
_(resource.entries[0].to_h).must_equal({
|
|
|
|
label: nil,
|
|
|
|
pid: 5,
|
|
|
|
cpu: nil,
|
|
|
|
mem: nil,
|
|
|
|
vsz: 1528,
|
|
|
|
rss: 4,
|
2019-06-11 22:24:35 +00:00
|
|
|
tty: "136,0",
|
|
|
|
stat: "R",
|
2018-10-04 18:06:17 +00:00
|
|
|
start: nil,
|
2019-06-11 22:24:35 +00:00
|
|
|
time: "0:00",
|
|
|
|
user: "joe",
|
|
|
|
command: "/some/other/coolprogram",
|
2018-10-04 18:06:17 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "handles human readable megabytes from busybox" do
|
|
|
|
resource = MockLoader.new(:alpine).load_resource("processes", "/a/bigger/program")
|
2018-10-04 18:06:17 +00:00
|
|
|
_(resource.entries.length).must_equal 1
|
|
|
|
_(resource.entries[0].to_h).must_equal({
|
|
|
|
label: nil,
|
|
|
|
pid: 82,
|
|
|
|
cpu: nil,
|
|
|
|
mem: nil,
|
|
|
|
vsz: 24576,
|
|
|
|
rss: 2048,
|
2019-06-11 22:24:35 +00:00
|
|
|
tty: "?",
|
|
|
|
stat: "S",
|
2018-10-04 18:06:17 +00:00
|
|
|
start: nil,
|
2019-06-11 22:24:35 +00:00
|
|
|
time: "3:50",
|
|
|
|
user: "frank",
|
|
|
|
command: "/a/bigger/program",
|
2018-10-04 18:06:17 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "handles human readable gigabytes from busybox" do
|
|
|
|
resource = MockLoader.new(:alpine).load_resource("processes", "/the/biggest/program")
|
2018-10-04 18:06:17 +00:00
|
|
|
_(resource.entries.length).must_equal 1
|
|
|
|
_(resource.entries[0].to_h).must_equal({
|
|
|
|
label: nil,
|
|
|
|
pid: 83,
|
|
|
|
cpu: nil,
|
|
|
|
mem: nil,
|
|
|
|
vsz: 2726297,
|
|
|
|
rss: 1048576,
|
2019-06-11 22:24:35 +00:00
|
|
|
tty: "?",
|
|
|
|
stat: "S",
|
2018-10-04 18:06:17 +00:00
|
|
|
start: nil,
|
2019-06-11 22:24:35 +00:00
|
|
|
time: "39:00",
|
|
|
|
user: "tim",
|
|
|
|
command: "/the/biggest/program",
|
2018-10-04 18:06:17 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "command name matches with output (string)" do
|
|
|
|
resource = MockLoader.new(:windows).load_resource("processes", "winlogon.exe")
|
|
|
|
_(resource.to_s).must_equal "Processes winlogon.exe"
|
2017-06-04 20:03:04 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "retrieves the users and states as arrays on windows os" do
|
|
|
|
resource = MockLoader.new(:windows).load_resource("processes", "winlogon.exe")
|
2017-06-04 20:03:04 +00:00
|
|
|
_(resource.users.sort).must_equal ['NT AUTHORITY\\SYSTEM']
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "process should exist" do
|
|
|
|
resource = MockLoader.new(:windows).load_resource("processes", "winlogon.exe")
|
2017-06-04 20:03:04 +00:00
|
|
|
_(resource.exists?).must_equal true
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "process should_not exist" do
|
|
|
|
resource = MockLoader.new(:windows).load_resource("processes", "unicorn.exe")
|
2017-06-04 20:03:04 +00:00
|
|
|
_(resource.exists?).must_equal false
|
|
|
|
end
|
2017-10-06 17:32:39 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns the correct command for busybox ps" do
|
|
|
|
resource = MockLoader.new(:alpine).load_resource("processes")
|
2017-10-06 17:32:39 +00:00
|
|
|
resource.expects(:busybox_ps?).returns(true)
|
2019-06-11 22:24:35 +00:00
|
|
|
resource.send(:ps_configuration_for_linux)[0].must_equal "ps -o pid,vsz,rss,tty,stat,time,ruser,args"
|
2017-10-06 17:32:39 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "returns the correct command for non-busybox linux" do
|
|
|
|
resource = MockLoader.new(:centos7).load_resource("processes")
|
2017-10-06 17:32:39 +00:00
|
|
|
resource.expects(:busybox_ps?).returns(false)
|
2019-06-11 22:24:35 +00:00
|
|
|
resource.send(:ps_configuration_for_linux)[0].must_equal "ps axo label,pid,pcpu,pmem,vsz,rss,tty,stat,start,time,user:32,command"
|
2017-10-06 17:32:39 +00:00
|
|
|
end
|
2015-09-05 15:40:02 +00:00
|
|
|
end
|