Skip to content

Commit

Permalink
Remove six usage (2/2)
Browse files Browse the repository at this point in the history
This repo does not support Python 2 anymore, so we don't need
six for compatibility between Python2 and 3,
convert six usage to Python 3 code.

This changes urllib usage.

mock.patch usage in heat_dashboard/test/tests/api/test_heat.py
is modified to cope with the mix usage of urllib from python3 (in
heat-dashboard) and six.moves.urllib (in heatclient).
In the case of the mix usage, patching urllib.request.urlopen() only
does not work as urllib.request.urlopen() is not called after
resolving a lazy loading in six and the resolved object is
six.moves.urllib.request is called. The previous code depends on
the behavior in heatclient read_url_content() and the method should
be mocked instead. Considering this, mocking in api/test_heat.py
is modified to mock direct methods called in the heat-dashboard code.

Co-Authored-By: Akihiro Motoki <amotoki@gmail.com>
Change-Id: Icf3f889770242b02023fe22c405cfa2d823581a5
Needed-By: https://review.opendev.org/701743
  • Loading branch information
ajaeger and amotoki committed Jan 11, 2020
1 parent 7103caa commit 79ef24a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion heat_dashboard/api/heat.py
Expand Up @@ -12,7 +12,7 @@

import contextlib

from six.moves.urllib import request
from urllib import request

from django.conf import settings
from oslo_serialization import jsonutils
Expand Down
22 changes: 13 additions & 9 deletions heat_dashboard/test/tests/api/test_heat.py
Expand Up @@ -13,7 +13,6 @@
import io

import mock
import six

from django.conf import settings
from django.test.utils import override_settings
Expand Down Expand Up @@ -257,8 +256,8 @@ def test_get_template_files_with_template_data(self):
files = api.heat.get_template_files(template_data=tmpl)[0]
self.assertEqual(files, expected_files)

@mock.patch.object(six.moves.urllib.request, 'urlopen')
def test_get_template_files(self, mock_request):
@mock.patch('heatclient.common.utils.read_url_content')
def test_get_template_files(self, mock_read_url_content):
tmpl = '''
# comment
Expand All @@ -276,14 +275,16 @@ def test_get_template_files(self, mock_request):
expected_files = {u'http://test.example/example': b'echo "test"'}
url = 'http://test.example/example'
data = b'echo "test"'
mock_request.return_value = io.BytesIO(data)
mock_read_url_content.return_value = data

files = api.heat.get_template_files(template_data=tmpl)[0]
self.assertEqual(files, expected_files)
mock_request.assert_called_once_with(url)
mock_read_url_content.assert_called_once_with(url)

@mock.patch.object(six.moves.urllib.request, 'urlopen')
def test_get_template_files_with_template_url(self, mock_request):
@mock.patch('urllib.request.urlopen')
@mock.patch('heatclient.common.utils.read_url_content')
def test_get_template_files_with_template_url(self, mock_read_url_content,
mock_request):
url = 'https://test.example/example.yaml'
data = b'''
# comment
Expand All @@ -301,11 +302,14 @@ def test_get_template_files_with_template_url(self, mock_request):
'''
data2 = b'echo "test"'
expected_files = {'http://test.example/example': b'echo "test"'}
mock_request.side_effect = \
[io.BytesIO(data), io.BytesIO(data2)]
mock_request.return_value = io.BytesIO(data)
mock_read_url_content.return_value = data2

files = api.heat.get_template_files(template_url=url)[0]
self.assertEqual(files, expected_files)
mock_request.assert_called_once_with(url)
mock_read_url_content.assert_called_once_with(
'http://test.example/example')

def test_get_template_files_invalid(self):
tmpl = '''
Expand Down

0 comments on commit 79ef24a

Please sign in to comment.