2
0
Fork 0
mirror of https://github.com/inspec/inspec synced 2025-02-19 07:28:41 +00:00

Updated test to mock the newly added command

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
This commit is contained in:
Vasu1105 2021-05-14 17:57:10 +05:30
parent 9786a467f1
commit 7990b31f5f
2 changed files with 13 additions and 9 deletions
test
helpers
unit/resources

View file

@ -591,6 +591,10 @@ class MockLoader
%{sh -c 'type "ss"'} => empty.call, %{sh -c 'type "ss"'} => empty.call,
%{sh -c 'type "netstat"'} => empty.call, %{sh -c 'type "netstat"'} => empty.call,
"ss -tulpen" => cmd.call("ss-tulpen"), "ss -tulpen" => cmd.call("ss-tulpen"),
"ss -tulpen '( dport = 22 or sport = 22 )'" => cmd.call("ss-tulpen"),
"ss -tulpen '( dport = 68 or sport = 68 )'" => cmd.call("ss-tulpen"),
"ss -tulpen '( dport = 9200 or sport = 9200 )'" => cmd.call("ss-tulpen"),
"ss -tulpen '( dport = 80 or sport = 80 )'" => cmd.call("ss-tulpen"),
"netstat -tulpen" => cmd.call("netstat-tulpen") "netstat -tulpen" => cmd.call("netstat-tulpen")
) )
end end

View file

@ -4,7 +4,7 @@ require "inspec/resources/port"
describe "Inspec::Resources::Port" do describe "Inspec::Resources::Port" do
it "verify port on Ubuntu 14.04" do it "verify port on Ubuntu 14.04" do
resource = MockLoader.new(:ubuntu1404).load_resource("port", 22) resource = MockLoader.new(:ubuntu1604).load_resource("port", 22)
_(resource.listening?).must_equal true _(resource.listening?).must_equal true
_(resource.protocols).must_equal %w{ tcp tcp6 } _(resource.protocols).must_equal %w{ tcp tcp6 }
_(resource.pids).must_equal [1222] _(resource.pids).must_equal [1222]
@ -13,7 +13,7 @@ describe "Inspec::Resources::Port" do
end end
it "lists all ports" do it "lists all ports" do
resource = MockLoader.new(:ubuntu1404).load_resource("port") resource = MockLoader.new(:ubuntu1604).load_resource("port")
_(resource.entries.length).must_equal 9 _(resource.entries.length).must_equal 9
_(resource.listening?).must_equal true _(resource.listening?).must_equal true
_(resource.protocols).must_equal %w{ udp tcp tcp6 } _(resource.protocols).must_equal %w{ udp tcp tcp6 }
@ -23,7 +23,7 @@ describe "Inspec::Resources::Port" do
end end
it "filter ports by conditions" do it "filter ports by conditions" do
resource = MockLoader.new(:ubuntu1404).load_resource("port").where { protocol =~ /udp/i } resource = MockLoader.new(:ubuntu1604).load_resource("port").where { protocol =~ /udp/i }
_(resource.entries.length).must_equal 1 _(resource.entries.length).must_equal 1
_(resource.listening?).must_equal true _(resource.listening?).must_equal true
_(resource.protocols).must_equal ["udp"] _(resource.protocols).must_equal ["udp"]
@ -33,7 +33,7 @@ describe "Inspec::Resources::Port" do
end end
it "verify UDP port on Ubuntu 14.04" do it "verify UDP port on Ubuntu 14.04" do
resource = MockLoader.new(:ubuntu1404).load_resource("port", 68) resource = MockLoader.new(:ubuntu1604).load_resource("port", 68)
_(resource.entries.length).must_equal 1 _(resource.entries.length).must_equal 1
_(resource.listening?).must_equal true _(resource.listening?).must_equal true
_(resource.protocols).must_equal ["udp"] _(resource.protocols).must_equal ["udp"]
@ -43,7 +43,7 @@ describe "Inspec::Resources::Port" do
end end
it "accepts the port as a string" do it "accepts the port as a string" do
resource = MockLoader.new(:ubuntu1404).load_resource("port", "68") resource = MockLoader.new(:ubuntu1604).load_resource("port", "68")
_(resource.entries.length).must_equal 1 _(resource.entries.length).must_equal 1
_(resource.listening?).must_equal true _(resource.listening?).must_equal true
_(resource.protocols).must_equal ["udp"] _(resource.protocols).must_equal ["udp"]
@ -53,7 +53,7 @@ describe "Inspec::Resources::Port" do
end end
it "properly handles multiple processes using one fd" do it "properly handles multiple processes using one fd" do
resource = MockLoader.new(:ubuntu1404).load_resource("port", "80") resource = MockLoader.new(:ubuntu1604).load_resource("port", "80")
_(resource.entries.length).must_equal 1 _(resource.entries.length).must_equal 1
_(resource.listening?).must_equal true _(resource.listening?).must_equal true
_(resource.protocols).must_equal ["tcp"] _(resource.protocols).must_equal ["tcp"]
@ -63,7 +63,7 @@ describe "Inspec::Resources::Port" do
end end
it "properly handles a IPv4 address in a v6 listing" do it "properly handles a IPv4 address in a v6 listing" do
resource = MockLoader.new(:ubuntu1404).load_resource("port", 9200) resource = MockLoader.new(:ubuntu1604).load_resource("port", 9200)
_(resource.protocols).must_equal %w{ tcp tcp6 } _(resource.protocols).must_equal %w{ tcp tcp6 }
_(resource.addresses).must_equal ["10.0.2.15", "fe80::a00:27ff:fe32:ed09"] _(resource.addresses).must_equal ["10.0.2.15", "fe80::a00:27ff:fe32:ed09"]
end end
@ -185,7 +185,7 @@ describe "Inspec::Resources::Port" do
end end
it "verify port and interface on Ubuntu 14.04" do it "verify port and interface on Ubuntu 14.04" do
resource = MockLoader.new(:ubuntu1404).load_resource("port", "0.0.0.0", 22) resource = MockLoader.new(:ubuntu1604).load_resource("port", "0.0.0.0", 22)
_(resource.listening?).must_equal true _(resource.listening?).must_equal true
_(resource.protocols).must_equal %w{ tcp } _(resource.protocols).must_equal %w{ tcp }
_(resource.pids).must_equal [1222] _(resource.pids).must_equal [1222]
@ -194,7 +194,7 @@ describe "Inspec::Resources::Port" do
end end
it "verify not listening port on interface on Ubuntu 14.04" do it "verify not listening port on interface on Ubuntu 14.04" do
resource = MockLoader.new(:ubuntu1404).load_resource("port", "127.0.0.1", 22) resource = MockLoader.new(:ubuntu1604).load_resource("port", "127.0.0.1", 22)
_(resource.listening?).must_equal false _(resource.listening?).must_equal false
_(resource.addresses).must_equal [] _(resource.addresses).must_equal []
end end