Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
slapos.core
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Romain Courteaud
slapos.core
Commits
b6aa843a
Commit
b6aa843a
authored
Jul 12, 2019
by
Rafael Monnerat
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
slapos: Add tests for slapos cache lookup command
parent
28cb4d8d
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
7 deletions
+48
-7
slapos/cli/cache.py
slapos/cli/cache.py
+8
-6
slapos/tests/test_cli.py
slapos/tests/test_cli.py
+40
-1
No files found.
slapos/cli/cache.py
View file @
b6aa843a
...
...
@@ -40,6 +40,7 @@ from slapos.grid import networkcache
from
slapos.grid.distribution
import
distribution_tuple
from
slapos.cli.config
import
ConfigCommand
FAILURE_EXIT_CODE
=
10
class
CacheLookupCommand
(
ConfigCommand
):
"""
...
...
@@ -61,8 +62,8 @@ class CacheLookupCommand(ConfigCommand):
def
take_action
(
self
,
args
):
configp
=
self
.
fetch_config
(
args
)
cache_dir
=
configp
.
get
(
'networkcache'
,
'download-binary-dir-url'
)
do_lookup
(
self
.
app
.
log
,
cache_dir
,
args
.
software_url
)
sys
.
exit
(
do_lookup
(
self
.
app
.
log
,
cache_dir
,
args
.
software_url
))
def
looks_like_md5
(
s
):
"""
...
...
@@ -77,27 +78,26 @@ def do_lookup(logger, cache_dir, software_url):
md5
=
software_url
else
:
md5
=
hashlib
.
md5
(
software_url
).
hexdigest
()
try
:
url
=
'%s/%s'
%
(
cache_dir
,
md5
)
logger
.
debug
(
'Connecting to %s'
,
url
)
req
=
requests
.
get
(
url
,
timeout
=
5
)
except
(
requests
.
Timeout
,
requests
.
ConnectionError
):
logger
.
critical
(
'Cannot connect to cache server at %s'
,
url
)
sys
.
exit
(
10
)
return
FAILURE_EXIT_CODE
if
not
req
.
ok
:
if
req
.
status_code
==
404
:
logger
.
critical
(
'Object not in cache: %s'
,
software_url
)
else
:
logger
.
critical
(
'Error while looking object %s: %s'
,
software_url
,
req
.
reason
)
sys
.
exit
(
10
)
return
FAILURE_EXIT_CODE
entries
=
req
.
json
()
if
not
entries
:
logger
.
info
(
'Object found in cache, but has no binary entries.'
)
return
return
0
ostable
=
sorted
(
ast
.
literal_eval
(
json
.
loads
(
entry
[
0
])[
'os'
])
for
entry
in
entries
)
...
...
@@ -115,3 +115,5 @@ def do_lookup(logger, cache_dir, software_url):
for
line
in
pt
.
get_string
(
border
=
True
,
padding_width
=
0
,
vrules
=
prettytable
.
NONE
).
split
(
'
\
n
'
):
logger
.
info
(
line
)
return
0
slapos/tests/test_cli.py
View file @
b6aa843a
...
...
@@ -44,6 +44,7 @@ import slapos.cli.info
import
slapos.cli.list
import
slapos.cli.supervisorctl
from
slapos.cli.proxy_show
import
do_show
,
StringIO
from
slapos.cli.cache
import
do_lookup
as
cache_do_lookup
from
slapos.client
import
ClientConfig
import
slapos.grid.svcbackend
import
slapos.proxy
...
...
@@ -61,6 +62,45 @@ class CliMixin(unittest.TestCase):
self
.
logger
=
create_autospec
(
logging
.
Logger
)
self
.
conf
=
create_autospec
(
ClientConfig
)
class
TestCliCache
(
CliMixin
):
test_url
=
"https://lab.nexedi.com/nexedi/slapos/raw/1.0.102/software/slaprunner/software.cfg"
def
test_cached_binary
(
self
):
self
.
assertEquals
(
0
,
cache_do_lookup
(
self
.
logger
,
cache_dir
=
"http://dir.shacache.org"
,
software_url
=
self
.
test_url
))
self
.
logger
.
info
.
assert_any_call
(
'Software URL: %s'
,
u'https://lab.nexedi.com/nexedi/slapos/raw/1.0.102/software/slaprunner/software.cfg'
)
self
.
logger
.
info
.
assert_any_call
(
'MD5: %s'
,
'cccdc51a07e8c575c880f2d70dd4d458'
)
self
.
logger
.
info
.
assert_any_call
(
u'------------------------------------------'
)
self
.
logger
.
info
.
assert_any_call
(
u' distribution version id compatible? '
)
self
.
logger
.
info
.
assert_any_call
(
u'------------------------------------------'
)
self
.
logger
.
info
.
assert_any_call
(
u' CentOS Linux 7.5.1804 Core no '
)
self
.
logger
.
info
.
assert_any_call
(
u' Ubuntu 18.04 bionic no '
)
# Omit some lines as it may fail depending of the OS
self
.
logger
.
info
.
assert_any_call
(
u'------------------------------------------'
)
def
test_uncached_binary
(
self
):
self
.
assertEquals
(
10
,
cache_do_lookup
(
self
.
logger
,
cache_dir
=
"http://dir.shacache.org"
,
software_url
=
"this_is_uncached_url"
))
self
.
logger
.
critical
.
assert_any_call
(
'Object not in cache: %s'
,
'this_is_uncached_url'
)
def
test_bad_cache_dir
(
self
):
self
.
assertEquals
(
10
,
cache_do_lookup
(
self
.
logger
,
cache_dir
=
"http://xxx.shacache.org"
,
software_url
=
self
.
test_url
))
self
.
logger
.
critical
.
assert_any_call
(
'Cannot connect to cache server at %s'
,
'http://xxx.shacache.org/cccdc51a07e8c575c880f2d70dd4d458'
)
class
TestCliProxy
(
CliMixin
):
def
test_generateSoftwareProductListFromString
(
self
):
...
...
@@ -87,7 +127,6 @@ product2 url2"""
{}
)
class
TestCliProxyShow
(
CliMixin
):
def
setUp
(
self
):
super
(
TestCliProxyShow
,
self
).
setUp
()
...
...
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