Added ftp urls handling to network checker

This change allows network checker to properly check availability of
urls using ftp protocol. Basically, this change checks if ftp urls are
possible to open with urllib2.urlopen function.

Add requirements.txt to test-requirements.txt

Add running url_access_checker tests to tox

Add six to network_checker requirements.txt, nailgun.spec,
debian/control

Change-Id: I89c3d4635e9f58d19eab8440c720f1900e81aea4
Closes-Bug: #1488001
This commit is contained in:
Maciej Kwiek 2015-08-24 12:51:18 +02:00
parent 10f473c637
commit 6c61f1d1cd
9 changed files with 45 additions and 11 deletions

2
debian/control vendored
View File

@ -20,6 +20,6 @@ Description: <insert up to 60 chars description>
Package: nailgun-net-check
Architecture: all
Depends: ${misc:Depends}, ${python:Depends}, python-pypcap, vlan, python-scapy, cliff-tablib, python-stevedore, python-daemonize, python-yaml, tcpdump, python-requests
Depends: ${misc:Depends}, ${python:Depends}, python-pypcap, vlan, python-scapy, cliff-tablib, python-stevedore, python-daemonize, python-yaml, tcpdump, python-requests, python-six
Description: NailGun client net-check
.

View File

@ -7,3 +7,4 @@ daemonize
pyyaml
requests
netifaces
six

View File

@ -1,3 +1,4 @@
-r requirements.txt
hacking==0.7
mock==1.0.1
pytest

View File

@ -7,9 +7,8 @@ envlist = py26,py27,pep8
usedevelop = True
install_command = pip install --allow-external -U {opts} {packages}
setenv = VIRTUAL_ENV={envdir}
deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
commands =
deps = -r{toxinidir}/test-requirements.txt
commands = py.test {toxinidir}/url_access_checker/tests
[tox:jenkins]
downloadcache = ~/cache/pip

View File

@ -17,13 +17,11 @@ import os
import socket
import requests
from six.moves import urllib
import url_access_checker.errors as errors
FILE_PREFIX = 'file://'
def check_urls(urls, proxies=None, timeout=60):
"""Checks a set of urls to see if they are valid
@ -62,14 +60,19 @@ def _get_response_tuple(url, proxies=None, timeout=60):
result[1] -- unchange url argument
"""
if url.startswith(FILE_PREFIX):
parsed = urllib.parse.urlparse(url)
if parsed.scheme == 'file':
return _get_file_existence_tuple(url)
else:
elif parsed.scheme in ['http', 'https']:
return _get_http_response_tuple(url, proxies, timeout)
elif parsed.scheme == 'ftp':
return _get_ftp_response_tuple(url, timeout)
else:
raise errors.InvalidProtocol(url)
def _get_file_existence_tuple(url):
path = url[len(FILE_PREFIX):]
path = url[len('file://'):]
return (not os.path.exists(path), url)
@ -92,3 +95,18 @@ def _get_http_response_tuple(url, proxies=None, timeout=60):
ValueError,
socket.timeout):
return (True, url)
def _get_ftp_response_tuple(url, timeout=60):
"""Return a tuple which contains a result of ftp url test
It will try to open ftp url as anonymous user and return (True, url) if
any errors occur, or return (False, url) otherwise.
"""
try:
# NOTE(mkwiek): requests don't have tested ftp adapter yet, so
# lower level urllib2 is used here
urllib.request.urlopen(url, timeout=timeout)
return (False, url)
except urllib.error.URLError:
return (True, url)

View File

@ -19,3 +19,7 @@ class UrlNotAvailable(Exception):
class CommandFailed(Exception):
pass
class InvalidProtocol(Exception):
pass

View File

@ -26,6 +26,7 @@ class TestApi(unittest2.TestCase):
def setUp(self):
self.urls = ['http://url{0}'.format(i) for i in range(10)]
self.paths = ['file:///tmp/test_api{0}'.format(i) for i in range(10)]
self.ftps = ['ftp://url{0}'.format(i) for i in range(10)]
@requests_mock.Mocker()
def test_check_urls(self, req_mocker):
@ -56,3 +57,12 @@ class TestApi(unittest2.TestCase):
mock_exists.return_value = False
with self.assertRaises(errors.UrlNotAvailable):
api.check_urls(self.paths)
@mock.patch('urllib2.urlopen')
def test_check_ftp(self, _):
check_result = api.check_urls(self.ftps, timeout=5)
self.assertTrue(check_result)
def test_check_ftp_fail(self):
with self.assertRaises(errors.UrlNotAvailable):
api.check_urls(self.paths)

View File

@ -22,7 +22,7 @@ from url_access_checker import cli
class TestUrlCheckerCommands(unittest.TestCase):
def setUp(self):
self.urls = ['url{0}'.format(i) for i in range(10)]
self.urls = ['http://url{0}'.format(i) for i in range(10)]
@mock.patch('requests.get')
def test_check_urls_success(self, get_mock):

View File

@ -227,6 +227,7 @@ Requires: python-urwid >= 1.1.0
Requires: PyYAML
Requires: python-ordereddict
Requires: screen
Requires: python-six
%description -n fuelmenu
Summary: Console utility for pre-configuration of Fuel server