Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
U
url-checker
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
Romain Courteaud
url-checker
Commits
11840882
Commit
11840882
authored
Nov 29, 2019
by
Romain Courteaud
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More tests
parent
74f2a5c6
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
329 additions
and
8 deletions
+329
-8
test_urlchecker_http.py
test_urlchecker_http.py
+2
-2
test_urlchecker_network.py
test_urlchecker_network.py
+317
-0
urlchecker_db.py
urlchecker_db.py
+5
-5
urlchecker_network.py
urlchecker_network.py
+5
-1
No files found.
test_urlchecker_http.py
View file @
11840882
...
@@ -8,7 +8,7 @@ import mock
...
@@ -8,7 +8,7 @@ import mock
import
peewee
import
peewee
class
UrlChecker
Status
TestCase
(
unittest
.
TestCase
):
class
UrlChecker
Http
TestCase
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
db
=
LogDB
(
":memory:"
)
self
.
db
=
LogDB
(
":memory:"
)
self
.
db
.
createTables
()
self
.
db
.
createTables
()
...
@@ -306,7 +306,7 @@ class UrlCheckerStatusTestCase(unittest.TestCase):
...
@@ -306,7 +306,7 @@ class UrlCheckerStatusTestCase(unittest.TestCase):
def
suite
():
def
suite
():
suite
=
unittest
.
TestSuite
()
suite
=
unittest
.
TestSuite
()
suite
.
addTest
(
unittest
.
makeSuite
(
UrlChecker
Status
TestCase
))
suite
.
addTest
(
unittest
.
makeSuite
(
UrlChecker
Http
TestCase
))
return
suite
return
suite
...
...
test_urlchecker_network.py
0 → 100644
View file @
11840882
import
unittest
from
urlchecker_db
import
LogDB
import
urlchecker_network
from
urlchecker_network
import
logNetwork
,
isTcpPortOpen
from
urlchecker_status
import
logStatus
import
mock
import
peewee
class
UrlCheckerNetworkTestCase
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
db
=
LogDB
(
":memory:"
)
self
.
db
.
createTables
()
################################################
# logNetwork
################################################
def
test_logNetwork_insertFirst
(
self
):
ip
=
"127.0.0.1"
port
=
1234
transport
=
"foobar"
state
=
"bar"
status_id
=
logStatus
(
self
.
db
,
"foo"
)
result
=
logNetwork
(
self
.
db
,
ip
,
transport
,
port
,
state
,
status_id
)
assert
self
.
db
.
NetworkChange
.
select
().
count
()
==
1
assert
self
.
db
.
NetworkChange
.
get
().
ip
==
ip
assert
self
.
db
.
NetworkChange
.
get
().
port
==
port
assert
self
.
db
.
NetworkChange
.
get
().
transport
==
transport
assert
self
.
db
.
NetworkChange
.
get
().
state
==
state
assert
self
.
db
.
NetworkChange
.
get
().
status_id
==
status_id
def
test_logNetwork_insertOnlyOnePerStatusIdIPPortTransport
(
self
):
ip
=
"127.0.0.1"
port
=
1234
transport
=
"foobar"
state
=
"bar"
status_id
=
logStatus
(
self
.
db
,
"foo"
)
result
=
logNetwork
(
self
.
db
,
ip
,
transport
,
port
,
state
,
status_id
)
try
:
logNetwork
(
self
.
db
,
ip
,
transport
,
port
,
state
+
'.'
,
status_id
)
except
peewee
.
IntegrityError
:
assert
self
.
db
.
NetworkChange
.
select
().
count
()
==
1
assert
self
.
db
.
NetworkChange
.
get
().
status_id
==
status_id
else
:
raise
NotImplementedError
(
'Expected IntegrityError'
)
def
test_logNetwork_skipIdenticalPreviousValues
(
self
):
ip
=
"127.0.0.1"
port
=
1234
transport
=
"foobar"
state
=
"bar"
status_id
=
logStatus
(
self
.
db
,
"foo"
)
result
=
logNetwork
(
self
.
db
,
ip
,
transport
,
port
,
state
,
status_id
)
status_id_2
=
logStatus
(
self
.
db
,
"foo"
)
result_2
=
logNetwork
(
self
.
db
,
ip
,
transport
,
port
,
state
,
status_id_2
)
assert
result_2
==
result
assert
self
.
db
.
NetworkChange
.
select
().
count
()
==
1
assert
self
.
db
.
NetworkChange
.
get
().
ip
==
ip
assert
self
.
db
.
NetworkChange
.
get
().
port
==
port
assert
self
.
db
.
NetworkChange
.
get
().
transport
==
transport
assert
self
.
db
.
NetworkChange
.
get
().
state
==
state
assert
self
.
db
.
NetworkChange
.
get
().
status_id
==
status_id
def
test_logNetwork_insertWhenDifferentState
(
self
):
ip
=
"127.0.0.1"
port
=
1234
transport
=
"foobar"
state
=
"bar"
status_id
=
logStatus
(
self
.
db
,
"foo"
)
result
=
logNetwork
(
self
.
db
,
ip
,
transport
,
port
,
state
,
status_id
)
state_2
=
state
+
"."
status_id_2
=
logStatus
(
self
.
db
,
"foo"
)
result_2
=
logNetwork
(
self
.
db
,
ip
,
transport
,
port
,
state_2
,
status_id_2
)
assert
result_2
!=
result
assert
self
.
db
.
NetworkChange
.
select
().
count
()
==
2
assert
self
.
db
.
NetworkChange
.
get
(
self
.
db
.
NetworkChange
.
status
==
status_id
).
ip
==
ip
assert
self
.
db
.
NetworkChange
.
get
(
self
.
db
.
NetworkChange
.
status
==
status_id
).
port
==
port
assert
self
.
db
.
NetworkChange
.
get
(
self
.
db
.
NetworkChange
.
status
==
status_id
).
transport
==
transport
assert
self
.
db
.
NetworkChange
.
get
(
self
.
db
.
NetworkChange
.
status
==
status_id
).
state
==
state
assert
self
.
db
.
NetworkChange
.
get
(
self
.
db
.
NetworkChange
.
status
==
status_id
).
status_id
==
status_id
assert
self
.
db
.
NetworkChange
.
get
(
self
.
db
.
NetworkChange
.
status
==
status_id_2
).
ip
==
ip
assert
self
.
db
.
NetworkChange
.
get
(
self
.
db
.
NetworkChange
.
status
==
status_id_2
).
port
==
port
assert
self
.
db
.
NetworkChange
.
get
(
self
.
db
.
NetworkChange
.
status
==
status_id_2
).
transport
==
transport
assert
self
.
db
.
NetworkChange
.
get
(
self
.
db
.
NetworkChange
.
status
==
status_id_2
).
state
==
state_2
assert
self
.
db
.
NetworkChange
.
get
(
self
.
db
.
NetworkChange
.
status
==
status_id_2
).
status_id
==
status_id_2
def
test_logNetwork_insertDifferentKeys
(
self
):
ip
=
"127.0.0.1"
ip_2
=
ip
+
"2"
port
=
1234
port_2
=
port
+
1
transport
=
"foobar"
transport_2
=
transport
+
'.'
state
=
"bar"
status_id
=
logStatus
(
self
.
db
,
"foo"
)
logNetwork
(
self
.
db
,
ip
,
transport
,
port
,
state
,
status_id
)
logNetwork
(
self
.
db
,
ip_2
,
transport
,
port
,
state
,
status_id
)
logNetwork
(
self
.
db
,
ip
,
transport_2
,
port
,
state
,
status_id
)
logNetwork
(
self
.
db
,
ip
,
transport
,
port_2
,
state
,
status_id
)
logNetwork
(
self
.
db
,
ip_2
,
transport_2
,
port
,
state
,
status_id
)
logNetwork
(
self
.
db
,
ip_2
,
transport
,
port_2
,
state
,
status_id
)
logNetwork
(
self
.
db
,
ip
,
transport_2
,
port_2
,
state
,
status_id
)
logNetwork
(
self
.
db
,
ip_2
,
transport_2
,
port_2
,
state
,
status_id
)
assert
self
.
db
.
NetworkChange
.
select
().
count
()
==
8
################################################
# isTcpPortOpen
################################################
def
test_isTcpPortOpen_open
(
self
):
ip
=
"127.0.0.1"
port
=
1234
status_id
=
logStatus
(
self
.
db
,
"foo"
)
with
mock
.
patch
(
"urlchecker_network.socket.socket"
)
as
mock_socket
:
result
=
isTcpPortOpen
(
self
.
db
,
ip
,
port
,
status_id
)
assert
mock_socket
.
call_count
==
1
assert
mock_socket
.
return_value
.
settimeout
.
call_count
==
1
mock_socket
.
return_value
.
settimeout
.
assert_called_with
(
2
)
assert
mock_socket
.
return_value
.
connect
.
call_count
==
1
mock_socket
.
return_value
.
connect
.
assert_called_with
((
ip
,
port
))
assert
mock_socket
.
return_value
.
close
.
call_count
==
1
assert
self
.
db
.
NetworkChange
.
select
().
count
()
==
1
assert
self
.
db
.
NetworkChange
.
get
().
ip
==
ip
assert
self
.
db
.
NetworkChange
.
get
().
port
==
port
assert
self
.
db
.
NetworkChange
.
get
().
transport
==
'TCP'
assert
self
.
db
.
NetworkChange
.
get
().
state
==
'open'
assert
self
.
db
.
NetworkChange
.
get
().
status_id
==
status_id
assert
result
==
True
def
test_isTcpPortOpen_connectionRefused
(
self
):
ip
=
"127.0.0.1"
port
=
1234
status_id
=
logStatus
(
self
.
db
,
"foo"
)
with
mock
.
patch
(
"urlchecker_network.socket.socket"
)
as
mock_socket
:
def
sideEffect
(
*
args
,
**
kw
):
raise
ConnectionRefusedError
()
mock_socket
.
return_value
.
connect
.
side_effect
=
sideEffect
result
=
isTcpPortOpen
(
self
.
db
,
ip
,
port
,
status_id
)
assert
mock_socket
.
call_count
==
1
assert
mock_socket
.
return_value
.
settimeout
.
call_count
==
1
mock_socket
.
return_value
.
settimeout
.
assert_called_with
(
2
)
assert
mock_socket
.
return_value
.
connect
.
call_count
==
1
mock_socket
.
return_value
.
connect
.
assert_called_with
((
ip
,
port
))
assert
mock_socket
.
return_value
.
close
.
call_count
==
1
assert
self
.
db
.
NetworkChange
.
select
().
count
()
==
1
assert
self
.
db
.
NetworkChange
.
get
().
ip
==
ip
assert
self
.
db
.
NetworkChange
.
get
().
port
==
port
assert
self
.
db
.
NetworkChange
.
get
().
transport
==
'TCP'
assert
self
.
db
.
NetworkChange
.
get
().
state
==
'closed'
assert
self
.
db
.
NetworkChange
.
get
().
status_id
==
status_id
assert
result
==
False
def
test_isTcpPortOpen_timeout
(
self
):
ip
=
"127.0.0.1"
port
=
1234
status_id
=
logStatus
(
self
.
db
,
"foo"
)
with
mock
.
patch
(
"urlchecker_network.socket.socket"
)
as
mock_socket
:
def
sideEffect
(
*
args
,
**
kw
):
raise
urlchecker_network
.
socket
.
timeout
()
mock_socket
.
return_value
.
connect
.
side_effect
=
sideEffect
result
=
isTcpPortOpen
(
self
.
db
,
ip
,
port
,
status_id
)
assert
mock_socket
.
call_count
==
1
assert
mock_socket
.
return_value
.
settimeout
.
call_count
==
1
mock_socket
.
return_value
.
settimeout
.
assert_called_with
(
2
)
assert
mock_socket
.
return_value
.
connect
.
call_count
==
1
mock_socket
.
return_value
.
connect
.
assert_called_with
((
ip
,
port
))
assert
mock_socket
.
return_value
.
close
.
call_count
==
1
assert
self
.
db
.
NetworkChange
.
select
().
count
()
==
1
assert
self
.
db
.
NetworkChange
.
get
().
ip
==
ip
assert
self
.
db
.
NetworkChange
.
get
().
port
==
port
assert
self
.
db
.
NetworkChange
.
get
().
transport
==
'TCP'
assert
self
.
db
.
NetworkChange
.
get
().
state
==
'filtered'
assert
self
.
db
.
NetworkChange
.
get
().
status_id
==
status_id
assert
result
==
False
def
test_isTcpPortOpen_noRoute
(
self
):
ip
=
"127.0.0.1"
port
=
1234
status_id
=
logStatus
(
self
.
db
,
"foo"
)
with
mock
.
patch
(
"urlchecker_network.socket.socket"
)
as
mock_socket
:
def
sideEffect
(
*
args
,
**
kw
):
raise
OSError
(
urlchecker_network
.
errno
.
EHOSTUNREACH
,
'foo'
)
mock_socket
.
return_value
.
connect
.
side_effect
=
sideEffect
result
=
isTcpPortOpen
(
self
.
db
,
ip
,
port
,
status_id
)
assert
mock_socket
.
call_count
==
1
assert
mock_socket
.
return_value
.
settimeout
.
call_count
==
1
mock_socket
.
return_value
.
settimeout
.
assert_called_with
(
2
)
assert
mock_socket
.
return_value
.
connect
.
call_count
==
1
mock_socket
.
return_value
.
connect
.
assert_called_with
((
ip
,
port
))
assert
mock_socket
.
return_value
.
close
.
call_count
==
1
assert
self
.
db
.
NetworkChange
.
select
().
count
()
==
1
assert
self
.
db
.
NetworkChange
.
get
().
ip
==
ip
assert
self
.
db
.
NetworkChange
.
get
().
port
==
port
assert
self
.
db
.
NetworkChange
.
get
().
transport
==
'TCP'
assert
self
.
db
.
NetworkChange
.
get
().
state
==
'filtered'
assert
self
.
db
.
NetworkChange
.
get
().
status_id
==
status_id
assert
result
==
False
def
test_isTcpPortOpen_noNetwork
(
self
):
ip
=
"127.0.0.1"
port
=
1234
status_id
=
logStatus
(
self
.
db
,
"foo"
)
with
mock
.
patch
(
"urlchecker_network.socket.socket"
)
as
mock_socket
:
def
sideEffect
(
*
args
,
**
kw
):
raise
OSError
(
urlchecker_network
.
errno
.
ENETUNREACH
,
'foo'
)
mock_socket
.
return_value
.
connect
.
side_effect
=
sideEffect
result
=
isTcpPortOpen
(
self
.
db
,
ip
,
port
,
status_id
)
assert
mock_socket
.
call_count
==
1
assert
mock_socket
.
return_value
.
settimeout
.
call_count
==
1
mock_socket
.
return_value
.
settimeout
.
assert_called_with
(
2
)
assert
mock_socket
.
return_value
.
connect
.
call_count
==
1
mock_socket
.
return_value
.
connect
.
assert_called_with
((
ip
,
port
))
assert
mock_socket
.
return_value
.
close
.
call_count
==
1
assert
self
.
db
.
NetworkChange
.
select
().
count
()
==
1
assert
self
.
db
.
NetworkChange
.
get
().
ip
==
ip
assert
self
.
db
.
NetworkChange
.
get
().
port
==
port
assert
self
.
db
.
NetworkChange
.
get
().
transport
==
'TCP'
assert
self
.
db
.
NetworkChange
.
get
().
state
==
'unreachable'
assert
self
.
db
.
NetworkChange
.
get
().
status_id
==
status_id
assert
result
==
False
def
test_isTcpPortOpen_OSError
(
self
):
ip
=
"127.0.0.1"
port
=
1234
status_id
=
logStatus
(
self
.
db
,
"foo"
)
with
mock
.
patch
(
"urlchecker_network.socket.socket"
)
as
mock_socket
:
def
sideEffect
(
*
args
,
**
kw
):
raise
OSError
()
mock_socket
.
return_value
.
connect
.
side_effect
=
sideEffect
try
:
isTcpPortOpen
(
self
.
db
,
ip
,
port
,
status_id
)
except
OSError
:
assert
self
.
db
.
NetworkChange
.
select
().
count
()
==
0
else
:
raise
NotImplementedError
(
'Expected OSError'
)
assert
mock_socket
.
call_count
==
1
assert
mock_socket
.
return_value
.
settimeout
.
call_count
==
1
mock_socket
.
return_value
.
settimeout
.
assert_called_with
(
2
)
assert
mock_socket
.
return_value
.
connect
.
call_count
==
1
mock_socket
.
return_value
.
connect
.
assert_called_with
((
ip
,
port
))
assert
mock_socket
.
return_value
.
close
.
call_count
==
1
def
test_isTcpPortOpen_Exception
(
self
):
ip
=
"127.0.0.1"
port
=
1234
status_id
=
logStatus
(
self
.
db
,
"foo"
)
with
mock
.
patch
(
"urlchecker_network.socket.socket"
)
as
mock_socket
:
def
sideEffect
(
*
args
,
**
kw
):
raise
Exception
()
mock_socket
.
return_value
.
connect
.
side_effect
=
sideEffect
try
:
isTcpPortOpen
(
self
.
db
,
ip
,
port
,
status_id
)
except
Exception
:
assert
self
.
db
.
NetworkChange
.
select
().
count
()
==
0
else
:
raise
NotImplementedError
(
'Expected OSError'
)
assert
mock_socket
.
call_count
==
1
assert
mock_socket
.
return_value
.
settimeout
.
call_count
==
1
mock_socket
.
return_value
.
settimeout
.
assert_called_with
(
2
)
assert
mock_socket
.
return_value
.
connect
.
call_count
==
1
mock_socket
.
return_value
.
connect
.
assert_called_with
((
ip
,
port
))
assert
mock_socket
.
return_value
.
close
.
call_count
==
1
def
suite
():
suite
=
unittest
.
TestSuite
()
suite
.
addTest
(
unittest
.
makeSuite
(
UrlCheckerNetworkTestCase
))
return
suite
if
__name__
==
"__main__"
:
unittest
.
main
(
defaultTest
=
"suite"
)
urlchecker_db.py
View file @
11840882
...
@@ -43,12 +43,12 @@ class LogDB:
...
@@ -43,12 +43,12 @@ class LogDB:
# Store remote network status
# Store remote network status
class
NetworkChange
(
BaseModel
):
class
NetworkChange
(
BaseModel
):
status
=
peewee
.
ForeignKeyField
(
Status
)
status
=
peewee
.
ForeignKeyField
(
Status
)
ip
=
peewee
.
TextField
(
index
=
True
)
ip
=
peewee
.
TextField
()
transport
=
peewee
.
TextField
()
transport
=
peewee
.
TextField
()
port
=
peewee
.
IntegerField
()
port
=
peewee
.
IntegerField
()
state
=
peewee
.
TextField
()
state
=
peewee
.
TextField
()
#
class Meta:
class
Meta
:
#
primary_key = peewee.CompositeKey("status", "ip", "transport", "port")
primary_key
=
peewee
.
CompositeKey
(
"status"
,
"ip"
,
"transport"
,
"port"
)
class
DnsChange
(
BaseModel
):
class
DnsChange
(
BaseModel
):
status
=
peewee
.
ForeignKeyField
(
Status
)
status
=
peewee
.
ForeignKeyField
(
Status
)
...
@@ -59,8 +59,8 @@ class LogDB:
...
@@ -59,8 +59,8 @@ class LogDB:
class
HttpCodeChange
(
BaseModel
):
class
HttpCodeChange
(
BaseModel
):
status
=
peewee
.
ForeignKeyField
(
Status
)
status
=
peewee
.
ForeignKeyField
(
Status
)
ip
=
peewee
.
TextField
(
index
=
True
)
ip
=
peewee
.
TextField
()
url
=
peewee
.
TextField
(
index
=
True
)
url
=
peewee
.
TextField
()
status_code
=
peewee
.
IntegerField
()
status_code
=
peewee
.
IntegerField
()
class
Meta
:
class
Meta
:
primary_key
=
peewee
.
CompositeKey
(
"status"
,
"ip"
,
"url"
)
primary_key
=
peewee
.
CompositeKey
(
"status"
,
"ip"
,
"url"
)
...
...
urlchecker_network.py
View file @
11840882
...
@@ -30,7 +30,7 @@ def logNetwork(db, ip, transport, port, state, status_id):
...
@@ -30,7 +30,7 @@ def logNetwork(db, ip, transport, port, state, status_id):
port
=
port
,
port
=
port
,
state
=
state
,
state
=
state
,
)
)
return
previous_entry
.
id
return
previous_entry
.
status
def
isTcpPortOpen
(
db
,
ip
,
port
,
status_id
):
def
isTcpPortOpen
(
db
,
ip
,
port
,
status_id
):
...
@@ -53,6 +53,10 @@ def isTcpPortOpen(db, ip, port, status_id):
...
@@ -53,6 +53,10 @@ def isTcpPortOpen(db, ip, port, status_id):
# OSError: [Errno 101] Network is unreachable
# OSError: [Errno 101] Network is unreachable
state
=
"unreachable"
state
=
"unreachable"
else
:
else
:
sock
.
close
()
raise
except
:
sock
.
close
()
raise
raise
sock
.
close
()
sock
.
close
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment