Skip to content

Instantly share code, notes, and snippets.

@kevit
Created January 25, 2021 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevit/054448430c8cbc372d3b3f56ab4f4ad9 to your computer and use it in GitHub Desktop.
Save kevit/054448430c8cbc372d3b3f56ab4f4ad9 to your computer and use it in GitHub Desktop.
import socket
import pytest
import testinfra
local_command = testinfra.get_backend('local://').get_module('Command')
@pytest.fixture()
def TCPConnection():
def connect(address, port, timeout=5):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(timeout)
s.connect((address, port))
s.close()
return 'established'
except socket.timeout:
return 'timed out'
except socket.error:
return 'refused'
return connect
@pytest.mark.parametrize("address,port", [
("goole.com", 443),
("goole.com", 80),
])
def test_connection(host, address, port):
assert host.TcpConnection(address, port) == "established"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment