Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
sbws
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
juga
sbws
Commits
acba8226
Commit
acba8226
authored
6 years ago
by
Matt Traudt
Browse files
Options
Downloads
Patches
Plain Diff
Add some tests, test the stats command
parent
3ef660e7
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
.gitignore
+4
-0
4 additions, 0 deletions
.gitignore
setup.py
+1
-1
1 addition, 1 deletion
setup.py
tests/commands/test_stats.py
+194
-0
194 additions, 0 deletions
tests/commands/test_stats.py
tox.ini
+12
-0
12 additions, 0 deletions
tox.ini
with
211 additions
and
1 deletion
.gitignore
+
4
−
0
View file @
acba8226
...
...
@@ -3,3 +3,7 @@ venv*/
passwords.txt
*.egg-info/
docs/build
.tox
.coverage
htmlcov
.pytest_cache
This diff is collapsed.
Click to expand it.
setup.py
+
1
−
1
View file @
acba8226
...
...
@@ -75,7 +75,7 @@ setup(
],
extras_require
=
{
'
dev
'
:
[],
'
test
'
:
[],
'
test
'
:
[
'
tox
'
,
'
pytest
'
,
'
pytest-cov
'
],
# recommonmark: to make sphinx render markdown
'
doc
'
:
[
'
sphinx
'
,
'
recommonmark
'
],
},
...
...
This diff is collapsed.
Click to expand it.
tests/commands/test_stats.py
0 → 100644
+
194
−
0
View file @
acba8226
from
sbws.util.parser
import
create_parser
from
sbws.util.config
import
get_config
from
sbws.lib.resultdump
import
ResultError
from
sbws.lib.resultdump
import
ResultSuccess
from
sbws.lib.resultdump
import
Result
import
sbws.commands.init
import
sbws.commands.stats
from
datetime
import
date
import
os
import
time
class
MockPastlyLogger
:
def
__init__
(
self
,
*
a
,
**
kw
):
pass
def
debug
(
self
,
*
s
):
print
(
*
s
)
def
info
(
self
,
*
s
):
return
self
.
debug
(
*
s
)
def
notice
(
self
,
*
s
):
return
self
.
info
(
*
s
)
def
warn
(
self
,
*
s
):
return
self
.
notice
(
*
s
)
def
error
(
self
,
*
s
):
return
self
.
warn
(
*
s
)
def
init_directory
(
dname
):
p
=
create_parser
()
args
=
p
.
parse_args
(
'
-d {} -vvvv init
'
.
format
(
dname
).
split
())
conf
=
get_config
(
args
,
log_fn
=
log
.
debug
)
sbws
.
commands
.
init
.
main
(
args
,
conf
,
log
)
def
add_single_stale_result
(
dname
):
r
=
ResultError
(
Result
.
Relay
(
'
DEADBEEF1111
'
,
'
CowSayWhat
'
,
'
127.0.0.1
'
),
[
'
DEADBEEF1111
'
,
'
BEADDEEF2222
'
],
'
127.0.1.1
'
,
'
SBWSclient
'
,
t
=
19950216
)
os
.
makedirs
(
os
.
path
.
join
(
str
(
dname
),
'
datadir
'
))
dt
=
date
.
fromtimestamp
(
r
.
time
)
ext
=
'
.txt
'
fname
=
os
.
path
.
join
(
str
(
dname
),
'
datadir
'
,
'
{}{}
'
.
format
(
dt
,
ext
))
with
open
(
fname
,
'
wt
'
)
as
fd
:
fd
.
write
(
'
{}
\n
'
.
format
(
str
(
r
)))
def
add_single_fresh_result
(
dname
):
r
=
ResultError
(
Result
.
Relay
(
'
DEADBEEF1111
'
,
'
CowSayWhat
'
,
'
127.0.0.1
'
),
[
'
DEADBEEF1111
'
,
'
BEADDEEF2222
'
],
'
127.0.1.1
'
,
'
SBWSclient
'
,
t
=
time
.
time
())
os
.
makedirs
(
os
.
path
.
join
(
str
(
dname
),
'
datadir
'
))
dt
=
date
.
fromtimestamp
(
r
.
time
)
ext
=
'
.txt
'
fname
=
os
.
path
.
join
(
str
(
dname
),
'
datadir
'
,
'
{}{}
'
.
format
(
dt
,
ext
))
with
open
(
fname
,
'
wt
'
)
as
fd
:
fd
.
write
(
'
{}
\n
'
.
format
(
str
(
r
)))
def
add_two_fresh_results
(
dname
):
r1
=
ResultError
(
Result
.
Relay
(
'
DEADBEEF1111
'
,
'
CowSayWhat
'
,
'
127.0.0.1
'
),
[
'
DEADBEEF1111
'
,
'
BEADDEEF2222
'
],
'
127.0.1.1
'
,
'
SBWSclient
'
,
t
=
time
.
time
())
r2
=
ResultSuccess
(
[
1
,
2
,
3
],
[{
'
amount
'
:
100
,
'
duration
'
:
1
}],
Result
.
Relay
(
'
DEADBEEF1111
'
,
'
CowSayWhat
'
,
'
127.0.0.1
'
),
[
'
DEADBEEF1111
'
,
'
BEADDEEF2222
'
],
'
127.0.1.1
'
,
'
SBWSclient
'
,
t
=
time
.
time
())
os
.
makedirs
(
os
.
path
.
join
(
str
(
dname
),
'
datadir
'
))
dt
=
date
.
fromtimestamp
(
r1
.
time
)
ext
=
'
.txt
'
fname
=
os
.
path
.
join
(
str
(
dname
),
'
datadir
'
,
'
{}{}
'
.
format
(
dt
,
ext
))
with
open
(
fname
,
'
wt
'
)
as
fd
:
fd
.
write
(
'
{}
\n
'
.
format
(
str
(
r1
)))
fd
.
write
(
'
{}
\n
'
.
format
(
str
(
r2
)))
log
=
MockPastlyLogger
()
def
test_stats_uninitted
(
tmpdir
,
capsys
):
'''
An un-initialized .sbws directory should fail hard and exit immediately
'''
p
=
create_parser
()
args
=
p
.
parse_args
(
'
-d {} -vvvv stats
'
.
format
(
tmpdir
).
split
())
conf
=
get_config
(
args
,
log_fn
=
log
.
debug
)
try
:
sbws
.
commands
.
stats
.
main
(
args
,
conf
,
log
)
except
SystemExit
as
e
:
assert
e
.
code
==
1
else
:
assert
None
,
'
Should have failed
'
captured
=
capsys
.
readouterr
()
lines
=
captured
.
out
.
strip
().
split
(
'
\n
'
)
assert
'
Sbws isn
\'
t initialized. Try sbws init
'
==
lines
[
-
1
]
def
test_stats_initted
(
tmpdir
,
capsys
):
'''
An initialized but rather empty .sbws directory should fail about missing
~/.sbws/datadir
'''
init_directory
(
tmpdir
)
p
=
create_parser
()
args
=
p
.
parse_args
(
'
-d {} -vvvv stats
'
.
format
(
tmpdir
).
split
())
conf
=
get_config
(
args
,
log_fn
=
log
.
debug
)
try
:
sbws
.
commands
.
stats
.
main
(
args
,
conf
,
log
)
except
SystemExit
as
e
:
assert
e
.
code
==
1
else
:
assert
None
,
'
Should have failed
'
captured
=
capsys
.
readouterr
()
lines
=
captured
.
out
.
strip
().
split
(
'
\n
'
)
assert
'
{}/datadir does not exist
'
.
format
(
tmpdir
)
==
lines
[
-
1
]
def
test_stats_stale_result
(
tmpdir
,
capsys
):
'''
An initialized .sbws directory with no fresh results should say so and
exit cleanly
'''
init_directory
(
tmpdir
)
add_single_stale_result
(
tmpdir
)
p
=
create_parser
()
args
=
p
.
parse_args
(
'
-d {} -vvvv stats
'
.
format
(
tmpdir
).
split
())
conf
=
get_config
(
args
,
log_fn
=
log
.
debug
)
sbws
.
commands
.
stats
.
main
(
args
,
conf
,
log
)
captured
=
capsys
.
readouterr
()
lines
=
captured
.
out
.
strip
().
split
(
'
\n
'
)
assert
'
No fresh results
'
==
lines
[
-
1
]
def
test_stats_fresh_result
(
tmpdir
,
capsys
):
'''
An initialized .sbws directory with a fresh error result should have some
boring stats and exit cleanly
'''
init_directory
(
tmpdir
)
add_single_fresh_result
(
tmpdir
)
p
=
create_parser
()
args
=
p
.
parse_args
(
'
-d {} -vvvv stats --error-types
'
.
format
(
tmpdir
).
split
())
conf
=
get_config
(
args
,
log_fn
=
log
.
debug
)
sbws
.
commands
.
stats
.
main
(
args
,
conf
,
log
)
captured
=
capsys
.
readouterr
()
lines
=
captured
.
out
.
strip
().
split
(
'
\n
'
)
needed_lines
=
[
'
Read 1 lines from {}/{}/{}.txt
'
.
format
(
tmpdir
,
'
datadir
'
,
date
.
fromtimestamp
(
time
.
time
())),
'
Keeping 1/1 results
'
,
'
1 relays have recent results
'
,
'
Average 0.00 successful measurements per relay
'
,
'
0 success results and 1 error results
'
,
]
for
needed_line
in
needed_lines
:
assert
needed_line
in
lines
def
test_stats_fresh_results
(
tmpdir
,
capsys
):
'''
An initialized .sbws directory with a fresh error and fresh success should
have some exciting stats and exit cleanly
'''
init_directory
(
tmpdir
)
add_two_fresh_results
(
tmpdir
)
p
=
create_parser
()
args
=
p
.
parse_args
(
'
-d {} -vvvv stats --error-types
'
.
format
(
tmpdir
).
split
())
conf
=
get_config
(
args
,
log_fn
=
log
.
debug
)
sbws
.
commands
.
stats
.
main
(
args
,
conf
,
log
)
captured
=
capsys
.
readouterr
()
lines
=
captured
.
out
.
strip
().
split
(
'
\n
'
)
needed_lines
=
[
'
Read 2 lines from {}/{}/{}.txt
'
.
format
(
tmpdir
,
'
datadir
'
,
date
.
fromtimestamp
(
time
.
time
())),
'
Keeping 2/2 results
'
,
'
1 relays have recent results
'
,
'
1 success results and 1 error results
'
,
'
Average 1.00 successful measurements per relay
'
,
'
Found a _ResultType.Error for the first time
'
,
'
Found a _ResultType.Success for the first time
'
,
'
1/2 (50.00%) results were error-misc
'
,
]
for
needed_line
in
needed_lines
:
assert
needed_line
in
lines
This diff is collapsed.
Click to expand it.
tox.ini
0 → 100644
+
12
−
0
View file @
acba8226
# tox (https://tox.readthedocs.io/) is a tool for running tests
# in multiple virtualenvs. This configuration file will run the
# test suite on all supported python versions. To use it, "pip install tox"
# and then run "tox" from this directory.
[tox]
envlist
=
py35
[testenv]
commands
=
pytest
deps
=
pytest
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment