Use built-in json module

The simplejson library has been generally used in OpenStack in the past
to implement compatibility with Python 2 and Python 3. However now
python 2 is no longer supported, and we can use the built-in json
module.

Note that the built-in json module does not accept byte values, while
these were automatically decoded by simplejson using utf-8 encoding.
Add encoding option to all subprocess calls so that outputs are all
encoded strings and can be passed directly into json data.

Change-Id: Ibfd47e87994511198d8883a91978be542dac3a3e
This commit is contained in:
Takashi Kajinami
2024-04-28 21:50:24 +09:00
parent bc259c0bf0
commit 42bfac519b
17 changed files with 167 additions and 116 deletions

View File

@@ -105,7 +105,7 @@ class AmphoraInfo:
def _get_version_of_installed_package(self, name): def _get_version_of_installed_package(self, name):
cmd = self._osutils.cmd_get_version_of_installed_package(name) cmd = self._osutils.cmd_get_version_of_installed_package(name)
version = subprocess.check_output(cmd.split()) version = subprocess.check_output(cmd.split(), encoding='utf-8')
return version return version
def _count_haproxy_processes(self, lb_list): def _count_haproxy_processes(self, lb_list):

View File

@@ -121,7 +121,8 @@ class Keepalived:
if init_enable_cmd is not None: if init_enable_cmd is not None:
try: try:
subprocess.check_output(init_enable_cmd.split(), subprocess.check_output(init_enable_cmd.split(),
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT,
encoding='utf-8')
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
LOG.debug('Failed to enable octavia-keepalived service: ' LOG.debug('Failed to enable octavia-keepalived service: '
'%(err)s %(output)s', {'err': e, 'output': e.output}) '%(err)s %(output)s', {'err': e, 'output': e.output})
@@ -160,7 +161,8 @@ class Keepalived:
cmd = f"/usr/sbin/service octavia-keepalived {action}" cmd = f"/usr/sbin/service octavia-keepalived {action}"
try: try:
subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT) subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT,
encoding='utf-8')
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
LOG.debug('Failed to %s octavia-keepalived service: %s %s', LOG.debug('Failed to %s octavia-keepalived service: %s %s',
action, e, e.output) action, e, e.output)

View File

@@ -143,7 +143,8 @@ class KeepalivedLvs(lvs_listener_base.LvsListenerApiServerBase):
init_enable_cmd = f"insserv {file_path}" init_enable_cmd = f"insserv {file_path}"
try: try:
subprocess.check_output(init_enable_cmd.split(), subprocess.check_output(init_enable_cmd.split(),
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT,
encoding='utf-8')
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
LOG.debug('Failed to enable ' LOG.debug('Failed to enable '
'octavia-keepalivedlvs service: ' 'octavia-keepalivedlvs service: '
@@ -220,7 +221,8 @@ class KeepalivedLvs(lvs_listener_base.LvsListenerApiServerBase):
f"octavia-keepalivedlvs-{listener_id} {action}") f"octavia-keepalivedlvs-{listener_id} {action}")
try: try:
subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT) subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT,
encoding='utf-8')
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
LOG.debug('Failed to %s keepalivedlvs listener %s', LOG.debug('Failed to %s keepalivedlvs listener %s',
listener_id + ' : ' + action, e) listener_id + ' : ' + action, e)
@@ -281,7 +283,8 @@ class KeepalivedLvs(lvs_listener_base.LvsListenerApiServerBase):
cmd = (f"/usr/sbin/service " cmd = (f"/usr/sbin/service "
f"octavia-keepalivedlvs-{listener_id} stop") f"octavia-keepalivedlvs-{listener_id} stop")
try: try:
subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT) subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT,
encoding='utf-8')
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
LOG.error("Failed to stop keepalivedlvs service: %s", e) LOG.error("Failed to stop keepalivedlvs service: %s", e)
return webob.Response(json={ return webob.Response(json={
@@ -309,7 +312,8 @@ class KeepalivedLvs(lvs_listener_base.LvsListenerApiServerBase):
init_disable_cmd = f"insserv -r {init_path}" init_disable_cmd = f"insserv -r {init_path}"
try: try:
subprocess.check_output(init_disable_cmd.split(), subprocess.check_output(init_disable_cmd.split(),
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT,
encoding='utf-8')
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
LOG.error("Failed to disable " LOG.error("Failed to disable "
"octavia-keepalivedlvs-%(list)s service: " "octavia-keepalivedlvs-%(list)s service: "

View File

@@ -134,7 +134,8 @@ class Loadbalancer:
haproxy_ug=consts.HAPROXY_USER_GROUP_CFG) haproxy_ug=consts.HAPROXY_USER_GROUP_CFG)
try: try:
subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT) subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT,
encoding='utf-8')
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
LOG.error("Failed to verify haproxy file: %s %s", e, e.output) LOG.error("Failed to verify haproxy file: %s %s", e, e.output)
# Save the last config that failed validation for debugging # Save the last config that failed validation for debugging
@@ -213,7 +214,8 @@ class Loadbalancer:
elif init_system == consts.INIT_SYSVINIT: elif init_system == consts.INIT_SYSVINIT:
try: try:
subprocess.check_output(init_enable_cmd.split(), subprocess.check_output(init_enable_cmd.split(),
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT,
encoding='utf-8')
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
LOG.error("Failed to enable haproxy-%(lb_id)s service: " LOG.error("Failed to enable haproxy-%(lb_id)s service: "
"%(err)s %(out)s", {'lb_id': lb_id, 'err': e, "%(err)s %(out)s", {'lb_id': lb_id, 'err': e,
@@ -286,11 +288,12 @@ class Loadbalancer:
lb_id=lb_id, action=action)) lb_id=lb_id, action=action))
try: try:
subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT) subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT,
encoding='utf-8')
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
# Mitigation for # Mitigation for
# https://bugs.launchpad.net/octavia/+bug/2054666 # https://bugs.launchpad.net/octavia/+bug/2054666
if (b'is not active, cannot reload.' in e.output and if ('is not active, cannot reload.' in e.output and
action == consts.AMP_ACTION_RELOAD): action == consts.AMP_ACTION_RELOAD):
saved_exc = e saved_exc = e
@@ -312,20 +315,21 @@ class Loadbalancer:
"was reloaded, check the haproxy logs for " "was reloaded, check the haproxy logs for "
"more details.") "more details.")
break break
if b'Job is already running' not in e.output: if 'Job is already running' not in e.output:
LOG.debug( LOG.debug(
"Failed to %(action)s haproxy-%(lb_id)s service: " "Failed to %(action)s haproxy-%(lb_id)s service: "
"%(err)s %(out)s", {'action': action, 'lb_id': lb_id, "%(err)s %(out)s", {'action': action, 'lb_id': lb_id,
'err': e, 'out': e.output}) 'err': e, 'out': e.output})
return webob.Response(json={ return webob.Response(json={
'message': f"Error {action}ing haproxy", 'message': f"Error {action}ing haproxy",
'details': e.output}, status=500) 'details': e.output
}, status=500)
break break
else: else:
# no break, we reach the retry limit for reloads # no break, we reach the retry limit for reloads
return webob.Response(json={ return webob.Response(json={
'message': f"Error {action}ing haproxy", 'message': f"Error {action}ing haproxy",
'details': saved_exc.output if saved_exc else ''}, status=500) 'details': saved_exc.output}, status=500)
# If we are not in active/standby we need to send an IP # If we are not in active/standby we need to send an IP
# advertisement (GARP or NA). Keepalived handles this for # advertisement (GARP or NA). Keepalived handles this for
@@ -360,7 +364,8 @@ class Loadbalancer:
os.path.join('/proc', util.get_haproxy_pid(lb_id))): os.path.join('/proc', util.get_haproxy_pid(lb_id))):
cmd = f"/usr/sbin/service haproxy-{lb_id} stop" cmd = f"/usr/sbin/service haproxy-{lb_id} stop"
try: try:
subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT) subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT,
encoding='utf-8')
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
LOG.error("Failed to stop haproxy-%s service: %s %s", LOG.error("Failed to stop haproxy-%s service: %s %s",
lb_id, e, e.output) lb_id, e, e.output)
@@ -401,7 +406,8 @@ class Loadbalancer:
init_disable_cmd = f"insserv -r {init_path}" init_disable_cmd = f"insserv -r {init_path}"
try: try:
subprocess.check_output(init_disable_cmd.split(), subprocess.check_output(init_disable_cmd.split(),
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT,
encoding='utf-8')
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
LOG.error("Failed to disable haproxy-%(lb_id)s service: " LOG.error("Failed to disable haproxy-%(lb_id)s service: "
"%(err)s %(out)s", {'lb_id': lb_id, 'err': e, "%(err)s %(out)s", {'lb_id': lb_id, 'err': e,

View File

@@ -89,8 +89,9 @@ class BaseOS:
LOG.debug("Executing: %s", cmd) LOG.debug("Executing: %s", cmd)
try: try:
out = subprocess.check_output(cmd.split(), out = subprocess.check_output(cmd.split(),
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT,
for line in out.decode('utf-8').split('\n'): encoding='utf-8')
for line in out.split('\n'):
LOG.debug(line) LOG.debug(line)
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
LOG.error('Failed to set up %s due to error: %s %s', interface, LOG.error('Failed to set up %s due to error: %s %s', interface,

View File

@@ -269,7 +269,8 @@ def install_netns_systemd_service():
def run_systemctl_command(command, service): def run_systemctl_command(command, service):
cmd = f"systemctl {command} {service}" cmd = f"systemctl {command} {service}"
try: try:
subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT) subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT,
encoding='utf-8')
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
LOG.error("Failed to %(cmd)s %(srvc)s service: " LOG.error("Failed to %(cmd)s %(srvc)s service: "
"%(err)s %(out)s", {'cmd': command, 'srvc': service, "%(err)s %(out)s", {'cmd': command, 'srvc': service,

View File

@@ -15,6 +15,7 @@
# under the License. # under the License.
import errno import errno
import json
import os import os
import queue import queue
import stat import stat
@@ -22,7 +23,6 @@ import time
from oslo_config import cfg from oslo_config import cfg
from oslo_log import log as logging from oslo_log import log as logging
import simplejson
from octavia.amphorae.backends.agent.api_server import util from octavia.amphorae.backends.agent.api_server import util
from octavia.amphorae.backends.health_daemon import health_sender from octavia.amphorae.backends.health_daemon import health_sender
@@ -73,8 +73,8 @@ def get_counters():
global COUNTERS global COUNTERS
if COUNTERS is None: if COUNTERS is None:
try: try:
COUNTERS = simplejson.load(get_counters_file()) or {} COUNTERS = json.load(get_counters_file()) or {}
except (simplejson.JSONDecodeError, AttributeError): except (json.JSONDecodeError, AttributeError):
COUNTERS = {} COUNTERS = {}
return COUNTERS return COUNTERS
@@ -84,7 +84,7 @@ def persist_counters():
if COUNTERS is None: if COUNTERS is None:
return return
try: try:
stats = simplejson.dumps(COUNTERS) stats = json.dumps(COUNTERS)
counters_file = get_counters_file() counters_file = get_counters_file()
counters_file.truncate(0) counters_file.truncate(0)
counters_file.write(stats) counters_file.write(stats)

View File

@@ -13,11 +13,11 @@
# under the License. # under the License.
import ipaddress import ipaddress
import json
import os import os
import stat import stat
from oslo_config import cfg from oslo_config import cfg
import simplejson
from octavia.common import constants as consts from octavia.common import constants as consts
@@ -45,11 +45,11 @@ class InterfaceFile:
@classmethod @classmethod
def load(cls, fp): def load(cls, fp):
return simplejson.load(fp) return json.load(fp)
@classmethod @classmethod
def dump(cls, obj): def dump(cls, obj):
return simplejson.dumps(obj) return json.dumps(obj)
@classmethod @classmethod
def from_file(cls, filename): def from_file(cls, filename):

View File

@@ -14,6 +14,7 @@
# under the License. # under the License.
import functools import functools
import hashlib import hashlib
import json
import os import os
import ssl import ssl
import time import time
@@ -24,7 +25,6 @@ from oslo_context import context as oslo_context
from oslo_log import log as logging from oslo_log import log as logging
from oslo_utils.secretutils import md5 from oslo_utils.secretutils import md5
import requests import requests
import simplejson
from stevedore import driver as stevedore_driver from stevedore import driver as stevedore_driver
from octavia.amphorae.driver_exceptions import exceptions as driver_except from octavia.amphorae.driver_exceptions import exceptions as driver_except
@@ -723,7 +723,7 @@ class AmphoraAPIClientBase:
if 'No suitable network interface found' in json_data: if 'No suitable network interface found' in json_data:
LOG.debug("Amphora network interface not found.") LOG.debug("Amphora network interface not found.")
raise requests.ConnectionError raise requests.ConnectionError
except simplejson.JSONDecodeError: # if r.json() fails except json.JSONDecodeError: # if r.json() fails
pass # TODO(rm_work) Should we do something? pass # TODO(rm_work) Should we do something?
return r return r
except (requests.ConnectionError, requests.Timeout) as e: except (requests.ConnectionError, requests.Timeout) as e:

View File

@@ -314,7 +314,8 @@ class KeepalivedLvsTestCase(base.TestCase):
cmd = ("/usr/sbin/service octavia-keepalivedlvs-{listener_id}" cmd = ("/usr/sbin/service octavia-keepalivedlvs-{listener_id}"
" {action}".format(listener_id=self.FAKE_ID, action='start')) " {action}".format(listener_id=self.FAKE_ID, action='start'))
mock_check_output.assert_called_once_with(cmd.split(), mock_check_output.assert_called_once_with(cmd.split(),
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT,
encoding='utf-8')
self.assertEqual(202, res.status_code) self.assertEqual(202, res.status_code)
res = self.test_keepalivedlvs.manage_lvs_listener(self.FAKE_ID, res = self.test_keepalivedlvs.manage_lvs_listener(self.FAKE_ID,
@@ -347,8 +348,10 @@ class KeepalivedLvsTestCase(base.TestCase):
cmd2 = ("systemctl disable " cmd2 = ("systemctl disable "
"octavia-keepalivedlvs-{list}".format(list=self.FAKE_ID)) "octavia-keepalivedlvs-{list}".format(list=self.FAKE_ID))
calls = [ calls = [
mock.call(cmd1.split(), stderr=subprocess.STDOUT), mock.call(cmd1.split(), stderr=subprocess.STDOUT,
mock.call(cmd2.split(), stderr=subprocess.STDOUT) encoding='utf-8'),
mock.call(cmd2.split(), stderr=subprocess.STDOUT,
encoding='utf-8')
] ]
m_check_output.assert_has_calls(calls) m_check_output.assert_has_calls(calls)
self.assertEqual(200, res.status_code) self.assertEqual(200, res.status_code)

View File

@@ -38,7 +38,7 @@ import octavia.tests.unit.base as base
AMP_AGENT_CONF_PATH = '/etc/octavia/amphora-agent.conf' AMP_AGENT_CONF_PATH = '/etc/octavia/amphora-agent.conf'
RANDOM_ERROR = b'random error' RANDOM_ERROR = 'random error'
OK = dict(message='OK') OK = dict(message='OK')
FAKE_INTERFACE = 'eth33' FAKE_INTERFACE = 'eth33'
@@ -137,7 +137,7 @@ class TestServerTestCase(base.TestCase):
haproxy_ug=consts.HAPROXY_USER_GROUP_CFG, haproxy_ug=consts.HAPROXY_USER_GROUP_CFG,
peer=(octavia_utils. peer=(octavia_utils.
base64_sha1_string('amp_123').rstrip('='))).split(), base64_sha1_string('amp_123').rstrip('='))).split(),
stderr=-2) stderr=subprocess.STDOUT, encoding='utf-8')
mock_rename.assert_called_with( mock_rename.assert_called_with(
'/var/lib/octavia/123/haproxy.cfg.new', '/var/lib/octavia/123/haproxy.cfg.new',
'/var/lib/octavia/123/haproxy.cfg') '/var/lib/octavia/123/haproxy.cfg')
@@ -145,11 +145,11 @@ class TestServerTestCase(base.TestCase):
if init_system == consts.INIT_SYSTEMD: if init_system == consts.INIT_SYSTEMD:
mock_subprocess.assert_any_call( mock_subprocess.assert_any_call(
"systemctl enable haproxy-123".split(), "systemctl enable haproxy-123".split(),
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT, encoding='utf-8')
elif init_system == consts.INIT_SYSVINIT: elif init_system == consts.INIT_SYSVINIT:
mock_subprocess.assert_any_call( mock_subprocess.assert_any_call(
"insserv /etc/init.d/haproxy-123".split(), "insserv /etc/init.d/haproxy-123".split(),
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT, encoding='utf-8')
else: else:
self.assertIn(init_system, consts.VALID_INIT_SYSTEMS) self.assertIn(init_system, consts.VALID_INIT_SYSTEMS)
@@ -243,7 +243,7 @@ class TestServerTestCase(base.TestCase):
haproxy_ug=consts.HAPROXY_USER_GROUP_CFG, haproxy_ug=consts.HAPROXY_USER_GROUP_CFG,
peer=(octavia_utils. peer=(octavia_utils.
base64_sha1_string('amp_123').rstrip('='))).split(), base64_sha1_string('amp_123').rstrip('='))).split(),
stderr=-2) stderr=subprocess.STDOUT, encoding='utf-8')
mock_rename.assert_called_with( mock_rename.assert_called_with(
'/var/lib/octavia/123/haproxy.cfg.new', '/var/lib/octavia/123/haproxy.cfg.new',
'/var/lib/octavia/123/haproxy.cfg.new-failed') '/var/lib/octavia/123/haproxy.cfg.new-failed')
@@ -321,7 +321,8 @@ class TestServerTestCase(base.TestCase):
' 123 started'}, ' 123 started'},
jsonutils.loads(rv.data.decode('utf-8'))) jsonutils.loads(rv.data.decode('utf-8')))
mock_subprocess.assert_called_with( mock_subprocess.assert_called_with(
['/usr/sbin/service', 'haproxy-123', 'start'], stderr=-2) ['/usr/sbin/service', 'haproxy-123', 'start'],
stderr=subprocess.STDOUT, encoding='utf-8')
mock_exists.return_value = True mock_exists.return_value = True
mock_subprocess.side_effect = subprocess.CalledProcessError( mock_subprocess.side_effect = subprocess.CalledProcessError(
@@ -336,10 +337,11 @@ class TestServerTestCase(base.TestCase):
self.assertEqual( self.assertEqual(
{ {
'message': 'Error starting haproxy', 'message': 'Error starting haproxy',
'details': RANDOM_ERROR.decode('utf-8'), 'details': RANDOM_ERROR,
}, jsonutils.loads(rv.data.decode('utf-8'))) }, jsonutils.loads(rv.data.decode('utf-8')))
mock_subprocess.assert_called_with( mock_subprocess.assert_called_with(
['/usr/sbin/service', 'haproxy-123', 'start'], stderr=-2) ['/usr/sbin/service', 'haproxy-123', 'start'],
stderr=subprocess.STDOUT, encoding='utf-8')
def test_ubuntu_reload(self): def test_ubuntu_reload(self):
self._test_reload(consts.UBUNTU) self._test_reload(consts.UBUNTU)
@@ -377,7 +379,8 @@ class TestServerTestCase(base.TestCase):
'details': 'Listener 123 reloaded'}, 'details': 'Listener 123 reloaded'},
jsonutils.loads(rv.data.decode('utf-8'))) jsonutils.loads(rv.data.decode('utf-8')))
mock_subprocess.assert_called_with( mock_subprocess.assert_called_with(
['/usr/sbin/service', 'haproxy-123', 'reload'], stderr=-2) ['/usr/sbin/service', 'haproxy-123', 'reload'],
stderr=subprocess.STDOUT, encoding='utf-8')
# Process not running so start # Process not running so start
mock_exists.return_value = True mock_exists.return_value = True
@@ -395,7 +398,8 @@ class TestServerTestCase(base.TestCase):
' 123 started'}, ' 123 started'},
jsonutils.loads(rv.data.decode('utf-8'))) jsonutils.loads(rv.data.decode('utf-8')))
mock_subprocess.assert_called_with( mock_subprocess.assert_called_with(
['/usr/sbin/service', 'haproxy-123', 'start'], stderr=-2) ['/usr/sbin/service', 'haproxy-123', 'start'],
stderr=subprocess.STDOUT, encoding='utf-8')
def test_ubuntu_info(self): def test_ubuntu_info(self):
self._test_info(consts.UBUNTU) self._test_info(consts.UBUNTU)
@@ -604,19 +608,22 @@ class TestServerTestCase(base.TestCase):
jsonutils.loads(rv.data.decode('utf-8'))) jsonutils.loads(rv.data.decode('utf-8')))
mock_pid.assert_called_once_with('123') mock_pid.assert_called_once_with('123')
mock_check_output.assert_any_call( mock_check_output.assert_any_call(
['/usr/sbin/service', 'haproxy-123', 'stop'], stderr=-2) ['/usr/sbin/service', 'haproxy-123', 'stop'],
stderr=subprocess.STDOUT, encoding='utf-8')
if init_system == consts.INIT_SYSTEMD: if init_system == consts.INIT_SYSTEMD:
mock_check_output.assert_any_call( mock_check_output.assert_any_call(
"systemctl disable haproxy-123".split(), "systemctl disable haproxy-123".split(),
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT,
encoding='utf-8')
elif init_system == consts.INIT_UPSTART: elif init_system == consts.INIT_UPSTART:
mock_remove.assert_any_call(consts.UPSTART_DIR + mock_remove.assert_any_call(consts.UPSTART_DIR +
'/haproxy-123.conf') '/haproxy-123.conf')
elif init_system == consts.INIT_SYSVINIT: elif init_system == consts.INIT_SYSVINIT:
mock_check_output.assert_any_call( mock_check_output.assert_any_call(
"insserv -r /etc/init.d/haproxy-123".split(), "insserv -r /etc/init.d/haproxy-123".split(),
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT,
encoding='utf-8')
else: else:
self.assertIn(init_system, consts.VALID_INIT_SYSTEMS) self.assertIn(init_system, consts.VALID_INIT_SYSTEMS)
@@ -634,19 +641,22 @@ class TestServerTestCase(base.TestCase):
jsonutils.loads(rv.data.decode('utf-8'))) jsonutils.loads(rv.data.decode('utf-8')))
mock_pid.assert_called_with('123') mock_pid.assert_called_with('123')
mock_check_output.assert_any_call( mock_check_output.assert_any_call(
['/usr/sbin/service', 'haproxy-123', 'stop'], stderr=-2) ['/usr/sbin/service', 'haproxy-123', 'stop'],
stderr=subprocess.STDOUT, encoding='utf-8')
if init_system == consts.INIT_SYSTEMD: if init_system == consts.INIT_SYSTEMD:
mock_check_output.assert_any_call( mock_check_output.assert_any_call(
"systemctl disable haproxy-123".split(), "systemctl disable haproxy-123".split(),
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT,
encoding='utf-8')
elif init_system == consts.INIT_UPSTART: elif init_system == consts.INIT_UPSTART:
mock_remove.assert_any_call(consts.UPSTART_DIR + mock_remove.assert_any_call(consts.UPSTART_DIR +
'/haproxy-123.conf') '/haproxy-123.conf')
elif init_system == consts.INIT_SYSVINIT: elif init_system == consts.INIT_SYSVINIT:
mock_check_output.assert_any_call( mock_check_output.assert_any_call(
"insserv -r /etc/init.d/haproxy-123".split(), "insserv -r /etc/init.d/haproxy-123".split(),
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT,
encoding='utf-8')
else: else:
self.assertIn(init_system, consts.VALID_INIT_SYSTEMS) self.assertIn(init_system, consts.VALID_INIT_SYSTEMS)
@@ -1014,7 +1024,7 @@ class TestServerTestCase(base.TestCase):
for idx in range(test_int_num)] for idx in range(test_int_num)]
mock_isfile.return_value = True mock_isfile.return_value = True
mock_check_output.return_value = b"1\n2\n3\n" mock_check_output.return_value = "1\n2\n3\n"
test_int_num = str(test_int_num) test_int_num = str(test_int_num)
@@ -1145,7 +1155,8 @@ class TestServerTestCase(base.TestCase):
mock_check_output.assert_called_with( mock_check_output.assert_called_with(
['ip', 'netns', 'exec', consts.AMPHORA_NAMESPACE, ['ip', 'netns', 'exec', consts.AMPHORA_NAMESPACE,
'amphora-interface', 'up', 'eth' + test_int_num], stderr=-2) 'amphora-interface', 'up', 'eth' + test_int_num],
stderr=subprocess.STDOUT, encoding='utf-8')
# fixed IPs happy path # fixed IPs happy path
port_info = {'mac_address': '123', 'mtu': 1450, 'fixed_ips': [ port_info = {'mac_address': '123', 'mtu': 1450, 'fixed_ips': [
@@ -1215,7 +1226,8 @@ class TestServerTestCase(base.TestCase):
mock_check_output.assert_called_with( mock_check_output.assert_called_with(
['ip', 'netns', 'exec', consts.AMPHORA_NAMESPACE, ['ip', 'netns', 'exec', consts.AMPHORA_NAMESPACE,
'amphora-interface', 'up', 'eth' + test_int_num], stderr=-2) 'amphora-interface', 'up', 'eth' + test_int_num],
stderr=subprocess.STDOUT, encoding='utf-8')
# fixed IPs happy path IPv6 # fixed IPs happy path IPv6
port_info = {'mac_address': '123', 'mtu': 1450, 'fixed_ips': [ port_info = {'mac_address': '123', 'mtu': 1450, 'fixed_ips': [
@@ -1285,7 +1297,8 @@ class TestServerTestCase(base.TestCase):
mock_check_output.assert_called_with( mock_check_output.assert_called_with(
['ip', 'netns', 'exec', consts.AMPHORA_NAMESPACE, ['ip', 'netns', 'exec', consts.AMPHORA_NAMESPACE,
'amphora-interface', 'up', 'eth' + test_int_num], stderr=-2) 'amphora-interface', 'up', 'eth' + test_int_num],
stderr=subprocess.STDOUT, encoding='utf-8')
# fixed IPs, bogus IP # fixed IPs, bogus IP
port_info = {'mac_address': '123', 'fixed_ips': [ port_info = {'mac_address': '123', 'fixed_ips': [
@@ -1314,9 +1327,10 @@ class TestServerTestCase(base.TestCase):
# same as above but ifup fails # same as above but ifup fails
port_info = {'mac_address': '123', 'fixed_ips': [ port_info = {'mac_address': '123', 'fixed_ips': [
{'ip_address': '10.0.0.5', 'subnet_cidr': '10.0.0.0/24'}]} {'ip_address': '10.0.0.5', 'subnet_cidr': '10.0.0.0/24'}]}
mock_check_output.side_effect = [subprocess.CalledProcessError( mock_check_output.side_effect = [
7, 'test', RANDOM_ERROR), subprocess.CalledProcessError( subprocess.CalledProcessError(7, 'test', RANDOM_ERROR),
7, 'test', RANDOM_ERROR)] subprocess.CalledProcessError(7, 'test', RANDOM_ERROR)
]
m = self.useFixture(test_utils.OpenFixture(file_name)).mock_open m = self.useFixture(test_utils.OpenFixture(file_name)).mock_open
with mock.patch('os.open'), mock.patch.object(os, 'fdopen', m): with mock.patch('os.open'), mock.patch.object(os, 'fdopen', m):
@@ -1332,7 +1346,7 @@ class TestServerTestCase(base.TestCase):
data=jsonutils.dumps(port_info)) data=jsonutils.dumps(port_info))
self.assertEqual(500, rv.status_code) self.assertEqual(500, rv.status_code)
self.assertEqual( self.assertEqual(
{'details': RANDOM_ERROR.decode('utf-8'), {'details': RANDOM_ERROR,
'message': 'Error plugging network'}, 'message': 'Error plugging network'},
jsonutils.loads(rv.data.decode('utf-8'))) jsonutils.loads(rv.data.decode('utf-8')))
@@ -1477,7 +1491,8 @@ class TestServerTestCase(base.TestCase):
mock_check_output.assert_called_with( mock_check_output.assert_called_with(
['ip', 'netns', 'exec', consts.AMPHORA_NAMESPACE, ['ip', 'netns', 'exec', consts.AMPHORA_NAMESPACE,
'amphora-interface', 'up', 'eth3'], stderr=-2) 'amphora-interface', 'up', 'eth3'], stderr=subprocess.STDOUT,
encoding='utf-8')
def test_ubuntu_plug_VIP4(self): def test_ubuntu_plug_VIP4(self):
self._test_plug_VIP4(consts.UBUNTU) self._test_plug_VIP4(consts.UBUNTU)
@@ -1732,7 +1747,8 @@ class TestServerTestCase(base.TestCase):
mock_check_output.assert_called_with( mock_check_output.assert_called_with(
['ip', 'netns', 'exec', consts.AMPHORA_NAMESPACE, ['ip', 'netns', 'exec', consts.AMPHORA_NAMESPACE,
'amphora-interface', 'up', 'amphora-interface', 'up',
consts.NETNS_PRIMARY_INTERFACE], stderr=-2) consts.NETNS_PRIMARY_INTERFACE], stderr=subprocess.STDOUT,
encoding='utf-8')
# One Interface down, Happy Path IPv4 # One Interface down, Happy Path IPv4
mode = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH mode = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH
@@ -1832,12 +1848,13 @@ class TestServerTestCase(base.TestCase):
mock_check_output.assert_called_with( mock_check_output.assert_called_with(
['ip', 'netns', 'exec', consts.AMPHORA_NAMESPACE, ['ip', 'netns', 'exec', consts.AMPHORA_NAMESPACE,
'amphora-interface', 'up', 'amphora-interface', 'up',
consts.NETNS_PRIMARY_INTERFACE], stderr=-2) consts.NETNS_PRIMARY_INTERFACE], stderr=subprocess.STDOUT,
encoding='utf-8')
mock_check_output.side_effect = [ mock_check_output.side_effect = [
subprocess.CalledProcessError( subprocess.CalledProcessError(7, 'test', RANDOM_ERROR),
7, 'test', RANDOM_ERROR), subprocess.CalledProcessError( subprocess.CalledProcessError(7, 'test', RANDOM_ERROR)
7, 'test', RANDOM_ERROR)] ]
m = self.useFixture(test_utils.OpenFixture(file_name)).mock_open m = self.useFixture(test_utils.OpenFixture(file_name)).mock_open
with mock.patch('os.open'), mock.patch.object(os, 'fdopen', m): with mock.patch('os.open'), mock.patch.object(os, 'fdopen', m):
@@ -1853,7 +1870,7 @@ class TestServerTestCase(base.TestCase):
data=jsonutils.dumps(subnet_info)) data=jsonutils.dumps(subnet_info))
self.assertEqual(500, rv.status_code) self.assertEqual(500, rv.status_code)
self.assertEqual( self.assertEqual(
{'details': RANDOM_ERROR.decode('utf-8'), {'details': RANDOM_ERROR,
'message': 'Error plugging VIP'}, 'message': 'Error plugging VIP'},
jsonutils.loads(rv.data.decode('utf-8'))) jsonutils.loads(rv.data.decode('utf-8')))
@@ -2086,9 +2103,14 @@ class TestServerTestCase(base.TestCase):
self, args[0], expected_dict) self, args[0], expected_dict)
mock_check_output.assert_called_with( mock_check_output.assert_called_with(
['ip', 'netns', 'exec', consts.AMPHORA_NAMESPACE, [
'ip', 'netns', 'exec', consts.AMPHORA_NAMESPACE,
'amphora-interface', 'up', '{netns_int}'.format( 'amphora-interface', 'up', '{netns_int}'.format(
netns_int=consts.NETNS_PRIMARY_INTERFACE)], stderr=-2) netns_int=consts.NETNS_PRIMARY_INTERFACE
)
],
stderr=subprocess.STDOUT,
encoding='utf-8')
# One Interface down, Happy Path IPv6 # One Interface down, Happy Path IPv6
flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC
@@ -2181,14 +2203,14 @@ class TestServerTestCase(base.TestCase):
test_utils.assert_interface_files_equal( test_utils.assert_interface_files_equal(
self, args[0], expected_dict) self, args[0], expected_dict)
mock_check_output.assert_called_with( mock_check_output.assert_called_with([
['ip', 'netns', 'exec', consts.AMPHORA_NAMESPACE, 'ip', 'netns', 'exec', consts.AMPHORA_NAMESPACE,
'amphora-interface', 'up', '{netns_int}'.format( 'amphora-interface', 'up', consts.NETNS_PRIMARY_INTERFACE
netns_int=consts.NETNS_PRIMARY_INTERFACE)], stderr=-2) ], stderr=subprocess.STDOUT, encoding='utf-8')
mock_check_output.side_effect = [ mock_check_output.side_effect = [
subprocess.CalledProcessError( subprocess.CalledProcessError(7, 'test', RANDOM_ERROR),
7, 'test', RANDOM_ERROR), subprocess.CalledProcessError( subprocess.CalledProcessError(7, 'test', RANDOM_ERROR)
7, 'test', RANDOM_ERROR)] ]
m = self.useFixture(test_utils.OpenFixture(file_name)).mock_open m = self.useFixture(test_utils.OpenFixture(file_name)).mock_open
with mock.patch('os.open'), mock.patch.object(os, 'fdopen', m): with mock.patch('os.open'), mock.patch.object(os, 'fdopen', m):
@@ -2204,7 +2226,7 @@ class TestServerTestCase(base.TestCase):
data=jsonutils.dumps(subnet_info)) data=jsonutils.dumps(subnet_info))
self.assertEqual(500, rv.status_code) self.assertEqual(500, rv.status_code)
self.assertEqual( self.assertEqual(
{'details': RANDOM_ERROR.decode('utf-8'), {'details': RANDOM_ERROR,
'message': 'Error plugging VIP'}, 'message': 'Error plugging VIP'},
jsonutils.loads(rv.data.decode('utf-8'))) jsonutils.loads(rv.data.decode('utf-8')))
@@ -2421,10 +2443,11 @@ class TestServerTestCase(base.TestCase):
test_utils.assert_interface_files_equal( test_utils.assert_interface_files_equal(
self, args[0], expected_dict) self, args[0], expected_dict)
mock_check_output.assert_called_with( mock_check_output.assert_called_with([
['ip', 'netns', 'exec', consts.AMPHORA_NAMESPACE, 'ip', 'netns', 'exec', consts.AMPHORA_NAMESPACE,
'amphora-interface', 'up', 'amphora-interface', 'up',
consts.NETNS_PRIMARY_INTERFACE], stderr=-2) consts.NETNS_PRIMARY_INTERFACE
], stderr=subprocess.STDOUT, encoding='utf-8')
def test_ubuntu_plug_VIP6_with_additional_VIP(self): def test_ubuntu_plug_VIP6_with_additional_VIP(self):
self._test_plug_VIP6_with_additional_VIP(consts.UBUNTU) self._test_plug_VIP6_with_additional_VIP(consts.UBUNTU)
@@ -2638,10 +2661,10 @@ class TestServerTestCase(base.TestCase):
test_utils.assert_interface_files_equal( test_utils.assert_interface_files_equal(
self, args[0], expected_dict) self, args[0], expected_dict)
mock_check_output.assert_called_with( mock_check_output.assert_called_with([
['ip', 'netns', 'exec', consts.AMPHORA_NAMESPACE, 'ip', 'netns', 'exec', consts.AMPHORA_NAMESPACE,
'amphora-interface', 'up', 'amphora-interface', 'up', consts.NETNS_PRIMARY_INTERFACE
consts.NETNS_PRIMARY_INTERFACE], stderr=-2) ], stderr=subprocess.STDOUT, encoding='utf-8')
def test_ubuntu_get_interface(self): def test_ubuntu_get_interface(self):
self._test_get_interface(consts.UBUNTU) self._test_get_interface(consts.UBUNTU)

View File

@@ -35,7 +35,8 @@ class KeepalivedTestCase(base.TestCase):
cmd = ("/usr/sbin/service octavia-keepalived {action}".format( cmd = ("/usr/sbin/service octavia-keepalived {action}".format(
action='start')) action='start'))
mock_check_output.assert_called_once_with(cmd.split(), mock_check_output.assert_called_once_with(cmd.split(),
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT,
encoding='utf-8')
self.assertEqual(202, res.status_code) self.assertEqual(202, res.status_code)
res = self.test_keepalived.manager_keepalived_service('restart') res = self.test_keepalived.manager_keepalived_service('restart')

View File

@@ -86,7 +86,8 @@ class ListenerTestCase(base.TestCase):
listener_id, consts.AMP_ACTION_START) listener_id, consts.AMP_ACTION_START)
mock_check_output.assert_called_once_with(ref_command_split, mock_check_output.assert_called_once_with(ref_command_split,
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT,
encoding='utf-8')
mock_lb_exists.assert_called_once_with(listener_id) mock_lb_exists.assert_called_once_with(listener_id)
mock_vrrp_update.assert_not_called() mock_vrrp_update.assert_not_called()
self.assertEqual(202, result.status_code) self.assertEqual(202, result.status_code)
@@ -111,7 +112,8 @@ class ListenerTestCase(base.TestCase):
listener_id, consts.AMP_ACTION_RELOAD) listener_id, consts.AMP_ACTION_RELOAD)
mock_check_output.assert_called_once_with(ref_command_split, mock_check_output.assert_called_once_with(ref_command_split,
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT,
encoding='utf-8')
mock_lb_exists.assert_called_once_with(listener_id) mock_lb_exists.assert_called_once_with(listener_id)
mock_vrrp_update.assert_called_once_with(listener_id, mock_vrrp_update.assert_called_once_with(listener_id,
consts.AMP_ACTION_RELOAD) consts.AMP_ACTION_RELOAD)
@@ -134,7 +136,8 @@ class ListenerTestCase(base.TestCase):
listener_id, consts.AMP_ACTION_RELOAD) listener_id, consts.AMP_ACTION_RELOAD)
mock_check_output.assert_called_once_with(ref_command_split, mock_check_output.assert_called_once_with(ref_command_split,
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT,
encoding='utf-8')
mock_lb_exists.assert_called_once_with(listener_id) mock_lb_exists.assert_called_once_with(listener_id)
mock_vrrp_update.assert_called_once_with(listener_id, mock_vrrp_update.assert_called_once_with(listener_id,
consts.AMP_ACTION_RELOAD) consts.AMP_ACTION_RELOAD)
@@ -157,13 +160,14 @@ class ListenerTestCase(base.TestCase):
ref_command_split.append(consts.AMP_ACTION_START) ref_command_split.append(consts.AMP_ACTION_START)
mock_check_output.side_effect = subprocess.CalledProcessError( mock_check_output.side_effect = subprocess.CalledProcessError(
output=b'bogus', returncode=-2, cmd='sit') output='bogus', returncode=-2, cmd='sit')
result = self.test_loadbalancer.start_stop_lb( result = self.test_loadbalancer.start_stop_lb(
listener_id, consts.AMP_ACTION_START) listener_id, consts.AMP_ACTION_START)
mock_check_output.assert_called_once_with(ref_command_split, mock_check_output.assert_called_once_with(ref_command_split,
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT,
encoding='utf-8')
mock_lb_exists.assert_called_once_with(listener_id) mock_lb_exists.assert_called_once_with(listener_id)
mock_vrrp_update.assert_not_called() mock_vrrp_update.assert_not_called()
self.assertEqual(500, result.status_code) self.assertEqual(500, result.status_code)
@@ -181,13 +185,14 @@ class ListenerTestCase(base.TestCase):
ref_command_split.append(consts.AMP_ACTION_START) ref_command_split.append(consts.AMP_ACTION_START)
mock_check_output.side_effect = subprocess.CalledProcessError( mock_check_output.side_effect = subprocess.CalledProcessError(
output=b'Job is already running', returncode=-2, cmd='sit') output='Job is already running', returncode=-2, cmd='sit')
result = self.test_loadbalancer.start_stop_lb( result = self.test_loadbalancer.start_stop_lb(
listener_id, consts.AMP_ACTION_START) listener_id, consts.AMP_ACTION_START)
mock_check_output.assert_called_once_with(ref_command_split, mock_check_output.assert_called_once_with(ref_command_split,
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT,
encoding='utf-8')
mock_lb_exists.assert_called_once_with(listener_id) mock_lb_exists.assert_called_once_with(listener_id)
mock_vrrp_update.assert_not_called() mock_vrrp_update.assert_not_called()
self.assertEqual(202, result.status_code) self.assertEqual(202, result.status_code)
@@ -220,7 +225,7 @@ class ListenerTestCase(base.TestCase):
mock_check_output.side_effect = [ mock_check_output.side_effect = [
subprocess.CalledProcessError( subprocess.CalledProcessError(
output=b'haproxy.service is not active, cannot reload.', output='haproxy.service is not active, cannot reload.',
returncode=-2, cmd='service'), returncode=-2, cmd='service'),
None] None]
mock_check_status.return_value = 'ACTIVE' mock_check_status.return_value = 'ACTIVE'
@@ -248,13 +253,13 @@ class ListenerTestCase(base.TestCase):
mock_check_output.side_effect = [ mock_check_output.side_effect = [
subprocess.CalledProcessError( subprocess.CalledProcessError(
output=b'haproxy.service is not active, cannot reload.', output='haproxy.service is not active, cannot reload.',
returncode=-2, cmd='service'), returncode=-2, cmd='service'),
subprocess.CalledProcessError( subprocess.CalledProcessError(
output=b'haproxy.service is not active, cannot reload.', output='haproxy.service is not active, cannot reload.',
returncode=-2, cmd='service'), returncode=-2, cmd='service'),
subprocess.CalledProcessError( subprocess.CalledProcessError(
output=b'haproxy.service is not active, cannot reload.', output='haproxy.service is not active, cannot reload.',
returncode=-2, cmd='service')] returncode=-2, cmd='service')]
mock_check_status.return_value = 'ACTIVE' mock_check_status.return_value = 'ACTIVE'
mock_check_status.side_effect = None mock_check_status.side_effect = None

View File

@@ -162,7 +162,8 @@ class TestUtil(base.TestCase):
util.run_systemctl_command('test', 'world') util.run_systemctl_command('test', 'world')
mock_check_output.assert_called_once_with( mock_check_output.assert_called_once_with(
['systemctl', 'test', 'world'], stderr=subprocess.STDOUT) ['systemctl', 'test', 'world'], stderr=subprocess.STDOUT,
encoding='utf-8')
mock_check_output.side_effect = subprocess.CalledProcessError(1, mock_check_output.side_effect = subprocess.CalledProcessError(1,
'boom') 'boom')

View File

@@ -12,6 +12,7 @@
# 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 json
import os import os
import queue import queue
from unittest import mock from unittest import mock
@@ -19,7 +20,6 @@ from unittest import mock
from oslo_config import cfg from oslo_config import cfg
from oslo_config import fixture as oslo_fixture from oslo_config import fixture as oslo_fixture
from oslo_utils import uuidutils from oslo_utils import uuidutils
import simplejson
from octavia.amphorae.backends.health_daemon import health_daemon from octavia.amphorae.backends.health_daemon import health_daemon
from octavia.common import constants from octavia.common import constants
@@ -345,7 +345,7 @@ class TestHealthDaemon(base.TestCase):
with mock.patch('os.open'), mock.patch.object( with mock.patch('os.open'), mock.patch.object(
os, 'fdopen', self.mock_open) as mock_fdopen: os, 'fdopen', self.mock_open) as mock_fdopen:
mock_fdopen().read.return_value = simplejson.dumps({ mock_fdopen().read.return_value = json.dumps({
LISTENER_ID1: {'bin': 1, 'bout': 2}, LISTENER_ID1: {'bin': 1, 'bout': 2},
}) })
msg = health_daemon.build_stats_message() msg = health_daemon.build_stats_message()
@@ -353,7 +353,7 @@ class TestHealthDaemon(base.TestCase):
self.assertEqual(SAMPLE_STATS_MSG, msg) self.assertEqual(SAMPLE_STATS_MSG, msg)
mock_get_stats.assert_any_call(lb1_stats_socket) mock_get_stats.assert_any_call(lb1_stats_socket)
mock_fdopen().write.assert_called_once_with(simplejson.dumps({ mock_fdopen().write.assert_called_once_with(json.dumps({
LISTENER_ID1: { LISTENER_ID1: {
'bin': int(FRONTEND_STATS['bin']), 'bin': int(FRONTEND_STATS['bin']),
'bout': int(FRONTEND_STATS['bout']), 'bout': int(FRONTEND_STATS['bout']),
@@ -450,14 +450,14 @@ class TestHealthDaemon(base.TestCase):
with mock.patch('os.open'), mock.patch.object( with mock.patch('os.open'), mock.patch.object(
os, 'fdopen', self.mock_open) as mock_fdopen: os, 'fdopen', self.mock_open) as mock_fdopen:
mock_fdopen().read.return_value = simplejson.dumps({ mock_fdopen().read.return_value = json.dumps({
udp_listener_id1: { udp_listener_id1: {
'bin': 1, 'bout': 2, "ereq": 0, "stot": 0} 'bin': 1, 'bout': 2, "ereq": 0, "stot": 0}
}) })
msg = health_daemon.build_stats_message() msg = health_daemon.build_stats_message()
self.assertEqual(expected, msg) self.assertEqual(expected, msg)
mock_fdopen().write.assert_called_once_with(simplejson.dumps({ mock_fdopen().write.assert_called_once_with(json.dumps({
udp_listener_id1: {'bin': 5, 'bout': 10, 'ereq': 0, 'stot': 5}, udp_listener_id1: {'bin': 5, 'bout': 10, 'ereq': 0, 'stot': 5},
udp_listener_id3: {'bin': 0, 'bout': 0, 'ereq': 0, 'stot': 0}, udp_listener_id3: {'bin': 0, 'bout': 0, 'ereq': 0, 'stot': 0},
})) }))
@@ -480,7 +480,7 @@ class TestHealthDaemon(base.TestCase):
with mock.patch('os.open'), mock.patch.object( with mock.patch('os.open'), mock.patch.object(
os, 'fdopen', self.mock_open) as mock_fdopen: os, 'fdopen', self.mock_open) as mock_fdopen:
mock_fdopen().read.return_value = simplejson.dumps({ mock_fdopen().read.return_value = json.dumps({
LISTENER_ID1: {'bin': 15, 'bout': 20}, LISTENER_ID1: {'bin': 15, 'bout': 20},
}) })
msg = health_daemon.build_stats_message() msg = health_daemon.build_stats_message()
@@ -488,7 +488,7 @@ class TestHealthDaemon(base.TestCase):
self.assertEqual(SAMPLE_MSG_HAPROXY_RESTART, msg) self.assertEqual(SAMPLE_MSG_HAPROXY_RESTART, msg)
mock_get_stats.assert_any_call(lb1_stats_socket) mock_get_stats.assert_any_call(lb1_stats_socket)
mock_fdopen().write.assert_called_once_with(simplejson.dumps({ mock_fdopen().write.assert_called_once_with(json.dumps({
LISTENER_ID1: { LISTENER_ID1: {
'bin': int(FRONTEND_STATS['bin']), 'bin': int(FRONTEND_STATS['bin']),
'bout': int(FRONTEND_STATS['bout']), 'bout': int(FRONTEND_STATS['bout']),

View File

@@ -278,7 +278,7 @@ class TestInterface(base.TestCase):
iface), iface),
"-pf", "-pf",
f"/run/dhclient-{iface}.pid", f"/run/dhclient-{iface}.pid",
iface], stderr=-2) iface], stderr=subprocess.STDOUT)
@mock.patch('subprocess.check_output') @mock.patch('subprocess.check_output')
def test__dhclient_down(self, mock_check_output): def test__dhclient_down(self, mock_check_output):
@@ -295,7 +295,7 @@ class TestInterface(base.TestCase):
iface), iface),
"-pf", "-pf",
f"/run/dhclient-{iface}.pid", f"/run/dhclient-{iface}.pid",
iface], stderr=-2) iface], stderr=subprocess.STDOUT)
@mock.patch('subprocess.check_output') @mock.patch('subprocess.check_output')
def test__ipv6auto_up(self, mock_check_output): def test__ipv6auto_up(self, mock_check_output):
@@ -306,9 +306,11 @@ class TestInterface(base.TestCase):
mock_check_output.assert_has_calls([ mock_check_output.assert_has_calls([
mock.call(["/sbin/sysctl", "-w", mock.call(["/sbin/sysctl", "-w",
"net.ipv6.conf.iface2.accept_ra=2"], stderr=-2), "net.ipv6.conf.iface2.accept_ra=2"],
stderr=subprocess.STDOUT),
mock.call(["/sbin/sysctl", "-w", mock.call(["/sbin/sysctl", "-w",
"net.ipv6.conf.iface2.autoconf=1"], stderr=-2)]) "net.ipv6.conf.iface2.autoconf=1"],
stderr=subprocess.STDOUT)])
@mock.patch('subprocess.check_output') @mock.patch('subprocess.check_output')
def test__ipv6auto_down(self, mock_check_output): def test__ipv6auto_down(self, mock_check_output):
@@ -319,9 +321,11 @@ class TestInterface(base.TestCase):
mock_check_output.assert_has_calls([ mock_check_output.assert_has_calls([
mock.call(["/sbin/sysctl", "-w", mock.call(["/sbin/sysctl", "-w",
"net.ipv6.conf.iface2.accept_ra=0"], stderr=-2), "net.ipv6.conf.iface2.accept_ra=0"],
stderr=subprocess.STDOUT),
mock.call(["/sbin/sysctl", "-w", mock.call(["/sbin/sysctl", "-w",
"net.ipv6.conf.iface2.autoconf=0"], stderr=-2)]) "net.ipv6.conf.iface2.autoconf=0"],
stderr=subprocess.STDOUT)])
@mock.patch('pyroute2.IPRoute.rule') @mock.patch('pyroute2.IPRoute.rule')
@mock.patch('pyroute2.IPRoute.route') @mock.patch('pyroute2.IPRoute.route')
@@ -579,15 +583,16 @@ class TestInterface(base.TestCase):
mock_check_output.assert_has_calls([ mock_check_output.assert_has_calls([
mock.call([consts.NFT_CMD, consts.NFT_ADD, 'table', mock.call([consts.NFT_CMD, consts.NFT_ADD, 'table',
consts.NFT_FAMILY, consts.NFT_VIP_TABLE], stderr=-2), consts.NFT_FAMILY, consts.NFT_VIP_TABLE],
stderr=subprocess.STDOUT),
mock.call([consts.NFT_CMD, consts.NFT_ADD, 'chain', mock.call([consts.NFT_CMD, consts.NFT_ADD, 'chain',
consts.NFT_FAMILY, consts.NFT_VIP_TABLE, consts.NFT_FAMILY, consts.NFT_VIP_TABLE,
consts.NFT_VIP_CHAIN, '{', 'type', 'filter', 'hook', consts.NFT_VIP_CHAIN, '{', 'type', 'filter', 'hook',
'ingress', 'device', 'fake-eth1', 'priority', 'ingress', 'device', 'fake-eth1', 'priority',
consts.NFT_SRIOV_PRIORITY, ';', 'policy', 'drop', ';', consts.NFT_SRIOV_PRIORITY, ';', 'policy', 'drop', ';',
'}'], stderr=-2), '}'], stderr=subprocess.STDOUT),
mock.call([consts.NFT_CMD, '-o', '-f', consts.NFT_VIP_RULES_FILE], mock.call([consts.NFT_CMD, '-o', '-f', consts.NFT_VIP_RULES_FILE],
stderr=-2), stderr=subprocess.STDOUT),
mock.call(["post-up", "fake-eth1"]) mock.call(["post-up", "fake-eth1"])
]) ])
@@ -925,13 +930,13 @@ class TestInterface(base.TestCase):
iface.name), iface.name),
"-pf", "-pf",
f"/run/dhclient-{iface.name}.pid", f"/run/dhclient-{iface.name}.pid",
iface.name], stderr=-2), iface.name], stderr=subprocess.STDOUT),
mock.call(["/sbin/sysctl", "-w", mock.call(["/sbin/sysctl", "-w",
f"net.ipv6.conf.{iface.name}.accept_ra=2"], f"net.ipv6.conf.{iface.name}.accept_ra=2"],
stderr=-2), stderr=subprocess.STDOUT),
mock.call(["/sbin/sysctl", "-w", mock.call(["/sbin/sysctl", "-w",
f"net.ipv6.conf.{iface.name}.autoconf=1"], f"net.ipv6.conf.{iface.name}.autoconf=1"],
stderr=-2), stderr=subprocess.STDOUT),
mock.call(["post-up", iface.name]) mock.call(["post-up", iface.name])
]) ])
@@ -1342,13 +1347,13 @@ class TestInterface(base.TestCase):
iface.name), iface.name),
"-pf", "-pf",
f"/run/dhclient-{iface.name}.pid", f"/run/dhclient-{iface.name}.pid",
iface.name], stderr=-2), iface.name], stderr=subprocess.STDOUT),
mock.call(["/sbin/sysctl", "-w", mock.call(["/sbin/sysctl", "-w",
f"net.ipv6.conf.{iface.name}.accept_ra=0"], f"net.ipv6.conf.{iface.name}.accept_ra=0"],
stderr=-2), stderr=subprocess.STDOUT),
mock.call(["/sbin/sysctl", "-w", mock.call(["/sbin/sysctl", "-w",
f"net.ipv6.conf.{iface.name}.autoconf=0"], f"net.ipv6.conf.{iface.name}.autoconf=0"],
stderr=-2), stderr=subprocess.STDOUT),
mock.call(["post-down", iface.name]) mock.call(["post-down", iface.name])
]) ])

View File

@@ -46,7 +46,6 @@ tenacity>=5.0.4 # Apache-2.0
distro>=1.2.0 # Apache-2.0 distro>=1.2.0 # Apache-2.0
jsonschema>=3.2.0 # MIT jsonschema>=3.2.0 # MIT
octavia-lib>=3.3.0 # Apache-2.0 octavia-lib>=3.3.0 # Apache-2.0
simplejson>=3.13.2 # MIT
setproctitle>=1.1.10 # BSD setproctitle>=1.1.10 # BSD
python-dateutil>=2.7.0 # BSD python-dateutil>=2.7.0 # BSD