Created
January 25, 2021 19:19
-
-
Save kevit/054448430c8cbc372d3b3f56ab4f4ad9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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