Skip to content

Commit

Permalink
Merge pull request #3709 from bgartzi/2211405-proxies_setuper
Browse files Browse the repository at this point in the history
test_setup: Refactor network proxies setup
  • Loading branch information
YongxueHong authored Jun 28, 2023
2 parents fdcfdde + ffbcbcb commit b663dd1
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 14 deletions.
103 changes: 103 additions & 0 deletions selftests/unit/test_setup_networking.py
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()
17 changes: 3 additions & 14 deletions virttest/env_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
import sys
import copy
import multiprocessing
try:
from urllib.request import ProxyHandler, build_opener, install_opener
except ImportError:
from urllib2 import ProxyHandler, build_opener, install_opener

import aexpect
from aexpect import remote
Expand Down Expand Up @@ -56,6 +52,8 @@
from virttest.staging import service
from virttest.test_setup.core import SetupManager
from virttest.test_setup.os_posix import UlimitConfig
from virttest.test_setup.networking import NetworkProxies


try:
import PIL.Image
Expand Down Expand Up @@ -999,18 +997,9 @@ def preprocess(test, params, env):

_setup_manager.initialize(test, params, env)
_setup_manager.register(UlimitConfig)
_setup_manager.register(NetworkProxies)
_setup_manager.do_setup()

# enable network proxies setting in urllib2
if params.get("network_proxies"):
proxies = {}
for proxy in re.split(r"[,;]\s*", params["network_proxies"]):
proxy = dict([re.split(r"_proxy:\s*", proxy)])
proxies.update(proxy)
handler = ProxyHandler(proxies)
opener = build_opener(handler)
install_opener(opener)

vm_type = params.get('vm_type')

if vm_type == 'libvirt':
Expand Down
28 changes: 28 additions & 0 deletions virttest/test_setup/networking.py
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

0 comments on commit b663dd1

Please sign in to comment.