-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_dnslambda_client.py
61 lines (52 loc) · 1.7 KB
/
test_dnslambda_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import unittest
from unittest.mock import patch
import socket
import sys
#import os
from threading import Thread
import errno
import time
import dnslambda_client
class TestProxyClient(unittest.TestCase):
def call_main(test_args):
with patch.object(sys, 'argv', test_args):
dnslambda_client.main()
# cmd = "python3 " + " ".join(test_args)
# os.system("(" + cmd + ") & sleep 5; kill $!")
def call_main_thread(self, test_args):
try:
thread = Thread(target=TestProxyClient.call_main, args=[test_args])
thread.daemon = True
thread.start()
return True
except Exception as e:
if e:
return False
def check_port_proto(self, port, proto):
socket_type = proto
sock = socket.socket(socket.AF_INET, socket_type)
port_in_use = False
try:
sock.bind(("", port))
except socket.error as e:
if e.errno == errno.EADDRINUSE:
port_in_use = True
else:
print(e)
finally:
sock.close()
return port_in_use
def check_port(self, port):
time.sleep(1) # Waiting for the thread to start
port = int(port)
tcp_in_use = self.check_port_proto(port, socket.SOCK_STREAM)
udp_in_use = self.check_port_proto(port, socket.SOCK_DGRAM)
if tcp_in_use and udp_in_use:
return True
else:
return False
def test_main(self):
test_args = ["dnslambda_client.py", "-p", "9999"]
self.assertEqual(self.call_main_thread(test_args), self.check_port(test_args[-1]))
if __name__ == '__main__':
unittest.main()