FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
netutils/tests/unit/test_ping.py at develop · networktocode/netutils · GitHub
networktocode
/
netutils
Public
Notifications
You must be signed in to change notification settings
Fork
69
Star
256
Code
Issues
32
Pull requests
21
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
netutils
/
tests
/
unit
/
test_ping.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
43 lines (33 loc) · 1.41 KB
Breadcrumbs
netutils
/
tests
/
unit
/
test_ping.py
Copy path
File metadata and controls
43 lines (33 loc) · 1.41 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"""Test for the ping based functions."""
import
socket
from
unittest
import
mock
import
pytest
from
netutils
import
ping
ping_data
=
[
{
"sent"
: {
"ip"
:
"1.1.1.1"
,
"port"
:
443
},
"received"
: {
"retval"
:
True
}},
{
"sent"
: {
"ip"
:
"192.0.2.0"
,
"port"
:
443
},
"received"
: {
"retval"
:
False
},
"raises"
:
socket
.
timeout
},
{
"sent"
: {
"ip"
:
"1.1.1.1"
,
"port"
:
443
,
"timeout"
:
3
},
"received"
: {
"retval"
:
True
}},
{
"sent"
: {
"ip"
:
"nevergonnagiveyouup.pizza"
,
"port"
:
443
},
"received"
: {
"retval"
:
None
,
"raised"
:
socket
.
gaierror
},
"raises"
:
socket
.
gaierror
,
},
]
@
pytest
.
mark
.
parametrize
(
"data"
,
ping_data
)
def
test_tcp_ping
(
data
):
with
mock
.
patch
(
"netutils.ping.socket.socket"
)
as
socket_mock
:
instance
=
socket_mock
.
return_value
instance
.
connect
.
side_effect
=
data
.
get
(
"raises"
)
raised
=
data
[
"received"
].
get
(
"raised"
)
if
raised
:
pytest
.
raises
(
raised
,
ping
.
tcp_ping
,
**
data
[
"sent"
])
else
:
assert
ping
.
tcp_ping
(
**
data
[
"sent"
])
==
data
[
"received"
][
"retval"
]
if
not
data
.
get
(
"raises"
):
instance
.
shutdown
.
assert_called_with
(
socket
.
SHUT_RDWR
)
timeout
=
data
[
"sent"
].
get
(
"timeout"
)
or
1
instance
.
settimeout
.
assert_called_with
(
timeout
)
ip
=
data
[
"sent"
].
get
(
"ip"
)
port
=
data
[
"sent"
].
get
(
"port"
)
instance
.
connect
.
assert_called_with
((
ip
,
port
))
instance
.
close
.
assert_called
()
Back
|
FazBrowse Home
|
New Git URL