Skip to content
GitLab
Next
Menu
Projects
Groups
Snippets
/
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
NTPsec
ntpsec
Compare Revisions
439ab4e7369bc0830a90517b12fe28e2c4746d91...088c359ecebfa154ce37290ff3888185a4d93e70
Commits (2)
Added test for termsize()
· ef184774
Ian Bruene
authored
Aug 30, 2017
ef184774
Fixed test for rfc3339() to handle python3
· 088c359e
Ian Bruene
authored
Aug 30, 2017
088c359e
Hide whitespace changes
Inline
Side-by-side
pylib/util.py
View file @
088c359e
...
...
@@ -13,6 +13,15 @@ import socket
import
sys
import
time
if
"get_terminal_size"
not
in
dir
(
shutil
):
# used by termsize() on python 2.x systems
import
fcntl
import
termios
import
struct
PY3
=
False
else
:
PY3
=
True
import
ntp.ntpc
import
ntp.version
import
ntp.magic
...
...
@@ -532,15 +541,12 @@ def termsize():
# The way this is used makes it not a big deal if the default is wrong.
size
=
(
80
,
24
)
if
os
.
isatty
(
1
):
try
:
if
PY3
is
True
:
(
w
,
h
)
=
shutil
.
get_terminal_size
((
80
,
24
))
size
=
(
w
,
h
)
e
xcept
AttributeError
:
e
lse
:
try
:
# OK, Python version < 3.3, cope
import
fcntl
import
termios
import
struct
h
,
w
,
hp
,
wp
=
struct
.
unpack
(
'HHHH'
,
fcntl
.
ioctl
(
2
,
termios
.
TIOCGWINSZ
,
...
...
tests/pylib/jigs.py
View file @
088c359e
...
...
@@ -190,3 +190,34 @@ class SelectModuleJig:
return
(
ins
,
[],
[])
else
:
return
([],
[],
[])
class
OSModuleJig
:
def
__init__
(
self
):
self
.
isatty_calls
=
[]
self
.
isatty_returns
=
[]
def
isatty
(
self
,
fd
):
self
.
isatty_calls
.
append
(
fd
)
return
self
.
isatty_returns
.
pop
(
0
)
class
FcntlModuleJig
:
def
__init__
(
self
):
self
.
ioctl_calls
=
[]
self
.
ioctl_returns
=
[]
def
ioctl
(
self
,
fd
,
op
,
arg
=
0
,
mutate_flag
=
False
):
self
.
ioctl_calls
.
append
((
fd
,
op
,
arg
,
mutate_flag
))
return
self
.
ioctl_returns
.
pop
(
0
)
class
ShutilModuleJig
:
def
__init__
(
self
):
self
.
gts_calls
=
[]
self
.
gts_returns
=
[]
def
get_terminal_size
(
self
,
default
=
(
80
,
24
)):
self
.
gts_calls
.
append
(
default
)
return
self
.
gts_returns
.
pop
(
0
)
tests/pylib/test_util.py
View file @
088c359e
...
...
@@ -3,6 +3,8 @@
import
unittest
import
ntp.util
import
shutil
import
sys
import
jigs
...
...
@@ -37,14 +39,19 @@ class TestPylibUtilMethods(unittest.TestCase):
self
.
assertEqual
((
jig
.
written
,
jig
.
flushed
),
(
"blah"
,
True
))
def
test_rfc3339
(
self
):
self
.
assertEqual
(
ntp
.
util
.
rfc3339
(
1480999786
),
f
=
ntp
.
util
.
rfc3339
self
.
assertEqual
(
f
(
1480999786
),
'2016-12-06T04:49:46Z'
)
self
.
assertEqual
(
ntp
.
util
.
rfc3339
(
1480999786.5
),
self
.
assertEqual
(
f
(
1480999786.5
),
'2016-12-06T04:49:46.5Z'
)
# RFC 3339, 2 digits of seconds.
# we round, but the spec is silent on rounding
self
.
assertEqual
(
ntp
.
util
.
rfc3339
(
1480999786.025
),
'2016-12-06T04:49:46.03Z'
)
# Python 2 and 3 round differently
if
sys
.
version_info
[
0
]
<
3
:
self
.
assertEqual
(
f
(
1480999786.025
),
"2016-12-06T04:49:46.03Z"
)
else
:
self
.
assertEqual
(
f
(
1480999786.025
),
"2016-12-06T04:49:46.025Z"
)
def
test_slicedata
(
self
):
f
=
ntp
.
util
.
slicedata
...
...
@@ -455,7 +462,45 @@ class TestPylibUtilMethods(unittest.TestCase):
def
test_termsize
(
self
):
f
=
ntp
.
util
.
termsize
# TODO: write this, it needs many jigs
fakeosmod
=
jigs
.
OSModuleJig
()
fakefcntlmod
=
jigs
.
FcntlModuleJig
()
fakeshutilmod
=
jigs
.
ShutilModuleJig
()
try
:
ostemp
=
ntp
.
util
.
os
ntp
.
util
.
os
=
fakeosmod
# Test default
fakeosmod
.
isatty_returns
=
[
False
]
self
.
assertEqual
(
f
(),
(
80
,
24
))
self
.
assertEqual
(
fakeosmod
.
isatty_calls
,
[
1
])
# termsize takes different code paths for different
# versions of Python
if
"get_terminal_size"
in
dir
(
shutil
):
# Python 3 version
try
:
shutiltemp
=
ntp
.
util
.
shutil
ntp
.
util
.
shutil
=
fakeshutilmod
fakeosmod
.
isatty_returns
=
[
True
]
fakeshutilmod
.
gts_returns
=
[(
42
,
23
)]
self
.
assertEqual
(
f
(),
(
42
,
23
))
finally
:
ntp
.
util
.
shutil
=
shutiltemp
else
:
# Python 2.x version
try
:
fcntltemp
=
ntp
.
util
.
fcntl
ntp
.
util
.
fcntl
=
fakefcntlmod
fakeosmod
.
isatty_returns
=
[
True
]
data
=
[
"
\x11\x11\x22\x22\x33\x33\x44\x44
"
]
fakefcntlmod
.
ioctl_returns
=
data
self
.
assertEqual
(
f
(),
(
0x2222
,
0x1111
))
self
.
assertEqual
(
fakefcntlmod
.
ioctl_calls
,
[(
2
,
ntp
.
util
.
termios
.
TIOCGWINSZ
,
"
\0\0\0\0\0\0\0\0
"
,
False
)])
finally
:
ntp
.
util
.
fcntl
=
fcntltemp
finally
:
ntp
.
util
.
os
=
ostemp
def
test_PeerStatusWord
(
self
):
c
=
ntp
.
util
.
PeerStatusWord
...
...