Commit b6aa843a authored by Rafael Monnerat's avatar Rafael Monnerat

slapos: Add tests for slapos cache lookup command

parent 28cb4d8d
...@@ -40,6 +40,7 @@ from slapos.grid import networkcache ...@@ -40,6 +40,7 @@ from slapos.grid import networkcache
from slapos.grid.distribution import distribution_tuple from slapos.grid.distribution import distribution_tuple
from slapos.cli.config import ConfigCommand from slapos.cli.config import ConfigCommand
FAILURE_EXIT_CODE = 10
class CacheLookupCommand(ConfigCommand): class CacheLookupCommand(ConfigCommand):
""" """
...@@ -61,8 +62,8 @@ class CacheLookupCommand(ConfigCommand): ...@@ -61,8 +62,8 @@ class CacheLookupCommand(ConfigCommand):
def take_action(self, args): def take_action(self, args):
configp = self.fetch_config(args) configp = self.fetch_config(args)
cache_dir = configp.get('networkcache', 'download-binary-dir-url') 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): def looks_like_md5(s):
""" """
...@@ -77,27 +78,26 @@ def do_lookup(logger, cache_dir, software_url): ...@@ -77,27 +78,26 @@ def do_lookup(logger, cache_dir, software_url):
md5 = software_url md5 = software_url
else: else:
md5 = hashlib.md5(software_url).hexdigest() md5 = hashlib.md5(software_url).hexdigest()
try: try:
url = '%s/%s' % (cache_dir, md5) url = '%s/%s' % (cache_dir, md5)
logger.debug('Connecting to %s', url) logger.debug('Connecting to %s', url)
req = requests.get(url, timeout=5) req = requests.get(url, timeout=5)
except (requests.Timeout, requests.ConnectionError): except (requests.Timeout, requests.ConnectionError):
logger.critical('Cannot connect to cache server at %s', url) logger.critical('Cannot connect to cache server at %s', url)
sys.exit(10) return FAILURE_EXIT_CODE
if not req.ok: if not req.ok:
if req.status_code == 404: if req.status_code == 404:
logger.critical('Object not in cache: %s', software_url) logger.critical('Object not in cache: %s', software_url)
else: else:
logger.critical('Error while looking object %s: %s', software_url, req.reason) logger.critical('Error while looking object %s: %s', software_url, req.reason)
sys.exit(10) return FAILURE_EXIT_CODE
entries = req.json() entries = req.json()
if not entries: if not entries:
logger.info('Object found in cache, but has no binary 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) 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): ...@@ -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'): for line in pt.get_string(border=True, padding_width=0, vrules=prettytable.NONE).split('\n'):
logger.info(line) logger.info(line)
return 0
...@@ -44,6 +44,7 @@ import slapos.cli.info ...@@ -44,6 +44,7 @@ import slapos.cli.info
import slapos.cli.list import slapos.cli.list
import slapos.cli.supervisorctl import slapos.cli.supervisorctl
from slapos.cli.proxy_show import do_show, StringIO 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 from slapos.client import ClientConfig
import slapos.grid.svcbackend import slapos.grid.svcbackend
import slapos.proxy import slapos.proxy
...@@ -61,6 +62,45 @@ class CliMixin(unittest.TestCase): ...@@ -61,6 +62,45 @@ class CliMixin(unittest.TestCase):
self.logger = create_autospec(logging.Logger) self.logger = create_autospec(logging.Logger)
self.conf = create_autospec(ClientConfig) 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): class TestCliProxy(CliMixin):
def test_generateSoftwareProductListFromString(self): def test_generateSoftwareProductListFromString(self):
...@@ -87,7 +127,6 @@ product2 url2""" ...@@ -87,7 +127,6 @@ product2 url2"""
{} {}
) )
class TestCliProxyShow(CliMixin): class TestCliProxyShow(CliMixin):
def setUp(self): def setUp(self):
super(TestCliProxyShow, self).setUp() super(TestCliProxyShow, self).setUp()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment