-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3709 from bgartzi/2211405-proxies_setuper
test_setup: Refactor network proxies setup
- Loading branch information
Showing
3 changed files
with
134 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import unittest | ||
from unittest.mock import Mock, patch | ||
|
||
from virttest.test_setup.networking import NetworkProxies | ||
|
||
|
||
class TestProxySetuper(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self._test_mock = Mock() | ||
self._env_mock = Mock() | ||
|
||
self._handler_mock = Mock() | ||
self._proxy_handler_mock = Mock() | ||
self._proxy_handler_mock.return_value = self._handler_mock | ||
|
||
self._opener_mock = Mock() | ||
self._build_opener_mock = Mock() | ||
self._build_opener_mock.return_value = self._opener_mock | ||
|
||
self._installer_mock = Mock() | ||
self._patchers = [] | ||
self._patchers.append(patch('urllib.request.ProxyHandler', | ||
self._proxy_handler_mock)) | ||
self._patchers.append(patch('urllib.request.build_opener', | ||
self._build_opener_mock)) | ||
self._patchers.append(patch('urllib.request.install_opener', | ||
self._installer_mock)) | ||
for patcher in self._patchers: | ||
patcher.start() | ||
|
||
def tearDown(self): | ||
for patcher in self._patchers: | ||
patcher.stop() | ||
|
||
def test_no_config(self): | ||
params = {'other_key': 'yes', | ||
} | ||
np = NetworkProxies(self._test_mock, params, self._env_mock) | ||
np.setup() | ||
self._proxy_handler_mock.assert_not_called() | ||
self._build_opener_mock.assert_not_called() | ||
self._installer_mock.assert_not_called() | ||
|
||
def test_empty_config(self): | ||
params = {'network_proxies': '', | ||
'other_key': 'yes', | ||
} | ||
np = NetworkProxies(self._test_mock, params, self._env_mock) | ||
np.setup() | ||
self._proxy_handler_mock.assert_not_called() | ||
self._build_opener_mock.assert_not_called() | ||
self._installer_mock.assert_not_called() | ||
|
||
def test_invalid_config(self): | ||
params = {'network_proxies': '', | ||
'other_key': 'yes', | ||
} | ||
np = NetworkProxies(self._test_mock, params, self._env_mock) | ||
np.setup() | ||
self._proxy_handler_mock.assert_not_called() | ||
self._build_opener_mock.assert_not_called() | ||
self._installer_mock.assert_not_called() | ||
|
||
def test_config_unique_proxy(self): | ||
params = {'network_proxies': 'https_proxy: https://proxy.com:8080/', | ||
'other_key': 'no', | ||
} | ||
proxies_dict = {'https': 'https://proxy.com:8080/'} | ||
np = NetworkProxies(self._test_mock, params, self._env_mock) | ||
np.setup() | ||
self._proxy_handler_mock.assert_called_once_with(proxies_dict) | ||
self._build_opener_mock.assert_called_once_with(self._handler_mock) | ||
self._installer_mock.assert_called_once_with(self._opener_mock) | ||
|
||
def test_config_multiple_proxies(self): | ||
params = {'network_proxies': | ||
'https_proxy: https://proxy.com:8080/; ' | ||
'ftp_proxy: ftp://proxy.com:3128/', | ||
'other_key': 'no', | ||
} | ||
proxies_dict = {'https': 'https://proxy.com:8080/', | ||
'ftp': 'ftp://proxy.com:3128/'} | ||
np = NetworkProxies(self._test_mock, params, self._env_mock) | ||
np.setup() | ||
self._proxy_handler_mock.assert_called_once_with(proxies_dict) | ||
self._build_opener_mock.assert_called_once_with(self._handler_mock) | ||
self._installer_mock.assert_called_once_with(self._opener_mock) | ||
|
||
def test_config_half_valid_config(self): | ||
params = {'network_proxies': | ||
'https_proxy: https://proxy.com:8080/; ' | ||
'nonsense: nonsense://bar.foo:0000; ', | ||
'other_key': 'no', | ||
} | ||
proxies_dict = {'https': 'https://proxy.com:8080/'} | ||
np = NetworkProxies(self._test_mock, params, self._env_mock) | ||
with self.assertRaises(ValueError): | ||
np.setup() | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
""" Define set/clean up procedures for network proxies | ||
""" | ||
import re | ||
import urllib.request | ||
|
||
from virttest.test_setup.core import Setuper | ||
|
||
|
||
class NetworkProxies(Setuper): | ||
|
||
def setup(self): | ||
# enable network proxies setting in urllib2 | ||
if self.params.get("network_proxies"): | ||
proxies = {} | ||
for proxy in re.split(r"[,;]\s*", self.params["network_proxies"]): | ||
proxy = dict([re.split(r"_proxy:\s*", proxy)]) | ||
proxies.update(proxy) | ||
handler = urllib.request.ProxyHandler(proxies) | ||
opener = urllib.request.build_opener(handler) | ||
urllib.request.install_opener(opener) | ||
|
||
def cleanup(self): | ||
""" There is not any cleanup going on as the changes applied by | ||
installing the ProxyHandler are only applied to a the default | ||
global opener of urllib. That is, changes are only applied in the | ||
process in which such installation has taken place. | ||
""" | ||
pass |