Test if a firewall is blocking a port

541 Views Asked by At

I am writing a Chef cookbook which will, among other things, add some firewall rules. But before I do that I need to write a test for it. That's where I'm stuck!

What I need is Serverspec/Rspec code that verifies that it is not possible to send data over port 1234, even if something on the server is listening to that port.

How should I write that test?

1

There are 1 best solutions below

0
On

Found the answer in comment from @Tensibai

describe host('localhost') do
  before do
    @server = TCPServer.open 1234
  end

  it { should_not be_reachable.with(port: 1234, proto: :tcp) }

  after do
    @server.close
  end
end