Keep py3.X compatibility for urllib

Co-Authored-By: Swapnil Kulkarni <me@coolsvap.net>
Partial-Bug:#1280105
Change-Id: I6d8a18d5818707baaeae93a04580c43872279706
This commit is contained in:
zhangguoqing 2016-01-12 08:26:27 +00:00 committed by Swapnil Kulkarni (coolsvap)
parent 2d03adb1f2
commit 0663d08fc1
6 changed files with 22 additions and 22 deletions

View File

@ -13,9 +13,8 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import urllib
from oslo_config import cfg from oslo_config import cfg
from six.moves.urllib import parse as urllib_parse
from webob import exc from webob import exc
from tacker.common import constants from tacker.common import constants
@ -57,7 +56,7 @@ def get_previous_link(request, items, id_key):
marker = items[0][id_key] marker = items[0][id_key]
params['marker'] = marker params['marker'] = marker
params['page_reverse'] = True params['page_reverse'] = True
return "%s?%s" % (request.path_url, urllib.urlencode(params)) return "%s?%s" % (request.path_url, urllib_parse.urlencode(params))
def get_next_link(request, items, id_key): def get_next_link(request, items, id_key):
@ -67,7 +66,7 @@ def get_next_link(request, items, id_key):
marker = items[-1][id_key] marker = items[-1][id_key]
params['marker'] = marker params['marker'] = marker
params.pop('page_reverse', None) params.pop('page_reverse', None)
return "%s?%s" % (request.path_url, urllib.urlencode(params)) return "%s?%s" % (request.path_url, urllib_parse.urlencode(params))
def get_limit_and_marker(request): def get_limit_and_marker(request):

View File

@ -58,10 +58,11 @@ as it allows particular rules to be explicitly disabled.
import abc import abc
import re import re
import urllib
import six import six
import urllib2
from six.moves.urllib import parse as urllib_parse
from six.moves.urllib import request as urlrequest
from tacker.openstack.common.gettextutils import _ from tacker.openstack.common.gettextutils import _
from tacker.openstack.common import jsonutils from tacker.openstack.common import jsonutils
@ -756,8 +757,8 @@ class HttpCheck(Check):
url = ('http:' + self.match) % target url = ('http:' + self.match) % target
data = {'target': jsonutils.dumps(target), data = {'target': jsonutils.dumps(target),
'credentials': jsonutils.dumps(creds)} 'credentials': jsonutils.dumps(creds)}
post_data = urllib.urlencode(data) post_data = urllib_parse.urlencode(data)
f = urllib2.urlopen(url, post_data) f = urlrequest.urlopen(url, post_data)
return f.read() == "True" return f.read() == "True"

View File

@ -15,12 +15,12 @@
"""Test of Policy Engine For Tacker""" """Test of Policy Engine For Tacker"""
import urllib2
import fixtures import fixtures
import mock import mock
import six import six
from six.moves.urllib import request as urlrequest
import tacker import tacker
from tacker.api.v1 import attributes from tacker.api.v1 import attributes
from tacker.common import exceptions from tacker.common import exceptions
@ -126,7 +126,7 @@ class PolicyTestCase(base.BaseTestCase):
def fakeurlopen(url, post_data): def fakeurlopen(url, post_data):
return six.StringIO("True") return six.StringIO("True")
with mock.patch.object(urllib2, 'urlopen', new=fakeurlopen): with mock.patch.object(urlrequest, 'urlopen', new=fakeurlopen):
action = "example:get_http" action = "example:get_http"
target = {} target = {}
result = policy.enforce(self.context, action, target) result = policy.enforce(self.context, action, target)
@ -137,7 +137,7 @@ class PolicyTestCase(base.BaseTestCase):
def fakeurlopen(url, post_data): def fakeurlopen(url, post_data):
return six.StringIO("False") return six.StringIO("False")
with mock.patch.object(urllib2, 'urlopen', new=fakeurlopen): with mock.patch.object(urlrequest, 'urlopen', new=fakeurlopen):
action = "example:get_http" action = "example:get_http"
target = {} target = {}
self.assertRaises(exceptions.PolicyNotAuthorized, policy.enforce, self.assertRaises(exceptions.PolicyNotAuthorized, policy.enforce,

View File

@ -12,9 +12,8 @@
# under the License. # under the License.
# #
import urllib2
import mock import mock
import six.moves.urllib.error as urlerr
import testtools import testtools
from tacker.vm.monitor_drivers.http_ping import http_ping from tacker.vm.monitor_drivers.http_ping import http_ping
@ -26,7 +25,7 @@ class TestVNFMonitorHTTPPing(testtools.TestCase):
super(TestVNFMonitorHTTPPing, self).setUp() super(TestVNFMonitorHTTPPing, self).setUp()
self.monitor_http_ping = http_ping.VNFMonitorHTTPPing() self.monitor_http_ping = http_ping.VNFMonitorHTTPPing()
@mock.patch('urllib2.urlopen') @mock.patch('six.moves.urllib.request.urlopen')
def test_monitor_call_for_success(self, mock_urlopen): def test_monitor_call_for_success(self, mock_urlopen):
test_device = {} test_device = {}
test_kwargs = { test_kwargs = {
@ -36,9 +35,9 @@ class TestVNFMonitorHTTPPing(testtools.TestCase):
test_kwargs) test_kwargs)
mock_urlopen.assert_called_once_with('http://a.b.c.d:80', timeout=5) mock_urlopen.assert_called_once_with('http://a.b.c.d:80', timeout=5)
@mock.patch('urllib2.urlopen') @mock.patch('six.moves.urllib.request.urlopen')
def test_monitor_call_for_failure(self, mock_urlopen): def test_monitor_call_for_failure(self, mock_urlopen):
mock_urlopen.side_effect = urllib2.URLError("MOCK Error") mock_urlopen.side_effect = urlerr.URLError("MOCK Error")
test_device = {} test_device = {}
test_kwargs = { test_kwargs = {
'mgmt_ip': 'a.b.c.d' 'mgmt_ip': 'a.b.c.d'

View File

@ -12,9 +12,9 @@
# under the License. # under the License.
# #
import urllib2
from oslo_config import cfg from oslo_config import cfg
import six.moves.urllib.error as urlerr
import six.moves.urllib.request as urlreq
from tacker.common import log from tacker.common import log
from tacker.i18n import _LW from tacker.i18n import _LW
@ -49,7 +49,7 @@ class VNFMonitorHTTPPing(abstract_driver.VNFMonitorAbstractDriver):
return device.get('monitor_url', '') return device.get('monitor_url', '')
def _is_pingable(self, mgmt_ip='', retry=5, timeout=5, port=80, **kwargs): def _is_pingable(self, mgmt_ip='', retry=5, timeout=5, port=80, **kwargs):
"""Checks whether the server is reachable by using urllib2. """Checks whether the server is reachable by using urllib.
Waits for connectivity for `timeout` seconds, Waits for connectivity for `timeout` seconds,
and if connection refused, it will retry `retry` and if connection refused, it will retry `retry`
@ -63,9 +63,9 @@ class VNFMonitorHTTPPing(abstract_driver.VNFMonitorAbstractDriver):
url = 'http://' + mgmt_ip + ':' + str(port) url = 'http://' + mgmt_ip + ':' + str(port)
for retry_index in range(int(retry)): for retry_index in range(int(retry)):
try: try:
urllib2.urlopen(url, timeout=timeout) urlreq.urlopen(url, timeout=timeout)
return True return True
except urllib2.URLError: except urlerr.URLError:
LOG.warning(_LW('Unable to reach to the url %s'), url) LOG.warning(_LW('Unable to reach to the url %s'), url)
return 'failure' return 'failure'

View File

@ -14,6 +14,7 @@ hacking<0.11,>=0.10.2
mock>=1.2 # BSD mock>=1.2 # BSD
python-subunit>=0.0.18 # Apache-2.0/BSD python-subunit>=0.0.18 # Apache-2.0/BSD
ordereddict # MIT ordereddict # MIT
six>=1.9.0 # MIT
sphinx!=1.2.0,!=1.3b1,<1.3,>=1.1.2 # BSD sphinx!=1.2.0,!=1.3b1,<1.3,>=1.1.2 # BSD
oslotest>=1.10.0 # Apache-2.0 oslotest>=1.10.0 # Apache-2.0
tempest-lib>=0.14.0 # Apache-2.0 tempest-lib>=0.14.0 # Apache-2.0