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 call so that outputs are all encoded strings and can be passed directly into json data. Change-Id: Ibfd47e87994511198d8883a91978be542dac3a3e
This commit is contained in:
parent
5898d54b13
commit
aa89db0675
|
@ -105,7 +105,7 @@ class AmphoraInfo:
|
|||
def _get_version_of_installed_package(self, 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
|
||||
|
||||
def _count_haproxy_processes(self, lb_list):
|
||||
|
|
|
@ -120,13 +120,15 @@ class Keepalived:
|
|||
if init_system != consts.INIT_UPSTART:
|
||||
try:
|
||||
subprocess.check_output(init_enable_cmd.split(),
|
||||
stderr=subprocess.STDOUT)
|
||||
stderr=subprocess.STDOUT,
|
||||
encoding='utf-8')
|
||||
except subprocess.CalledProcessError as e:
|
||||
LOG.debug('Failed to enable octavia-keepalived service: '
|
||||
'%(err)s %(output)s', {'err': e, 'output': e.output})
|
||||
return webob.Response(json={
|
||||
'message': "Error enabling octavia-keepalived service",
|
||||
'details': e.output}, status=500)
|
||||
'details': e.output
|
||||
}, status=500)
|
||||
|
||||
res = webob.Response(json={'message': 'OK'}, status=200)
|
||||
res.headers['ETag'] = stream.get_md5()
|
||||
|
@ -159,13 +161,15 @@ class Keepalived:
|
|||
cmd = f"/usr/sbin/service octavia-keepalived {action}"
|
||||
|
||||
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:
|
||||
LOG.debug('Failed to %s octavia-keepalived service: %s %s',
|
||||
action, e, e.output)
|
||||
return webob.Response(json={
|
||||
'message': f"Failed to {action} octavia-keepalived service",
|
||||
'details': e.output}, status=500)
|
||||
'details': e.output
|
||||
}, status=500)
|
||||
|
||||
return webob.Response(
|
||||
json={'message': 'OK',
|
||||
|
|
|
@ -143,7 +143,8 @@ class KeepalivedLvs(lvs_listener_base.LvsListenerApiServerBase):
|
|||
init_enable_cmd = f"insserv {file_path}"
|
||||
try:
|
||||
subprocess.check_output(init_enable_cmd.split(),
|
||||
stderr=subprocess.STDOUT)
|
||||
stderr=subprocess.STDOUT,
|
||||
encoding='utf-8')
|
||||
except subprocess.CalledProcessError as e:
|
||||
LOG.debug('Failed to enable '
|
||||
'octavia-keepalivedlvs service: '
|
||||
|
@ -151,7 +152,8 @@ class KeepalivedLvs(lvs_listener_base.LvsListenerApiServerBase):
|
|||
return webob.Response(json={
|
||||
'message': ("Error enabling "
|
||||
"octavia-keepalivedlvs service"),
|
||||
'details': e.output}, status=500)
|
||||
'details': e.output
|
||||
}, status=500)
|
||||
|
||||
if NEED_CHECK:
|
||||
# inject the check script for keepalived process
|
||||
|
@ -220,14 +222,16 @@ class KeepalivedLvs(lvs_listener_base.LvsListenerApiServerBase):
|
|||
f"octavia-keepalivedlvs-{listener_id} {action}")
|
||||
|
||||
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:
|
||||
LOG.debug('Failed to %s keepalivedlvs listener %s',
|
||||
listener_id + ' : ' + action, e)
|
||||
return webob.Response(json={
|
||||
'message': (f"Failed to {action} keepalivedlvs listener "
|
||||
f"{listener_id}"),
|
||||
'details': e.output}, status=500)
|
||||
'details': e.output
|
||||
}, status=500)
|
||||
|
||||
return webob.Response(
|
||||
json={'message': 'OK',
|
||||
|
@ -281,12 +285,14 @@ class KeepalivedLvs(lvs_listener_base.LvsListenerApiServerBase):
|
|||
cmd = (f"/usr/sbin/service "
|
||||
f"octavia-keepalivedlvs-{listener_id} stop")
|
||||
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:
|
||||
LOG.error("Failed to stop keepalivedlvs service: %s", e)
|
||||
return webob.Response(json={
|
||||
'message': "Error stopping keepalivedlvs",
|
||||
'details': e.output}, status=500)
|
||||
'details': e.output
|
||||
}, status=500)
|
||||
|
||||
# Since the lvs check script based on the keepalived pid file for
|
||||
# checking whether it is alived. So here, we had stop the keepalived
|
||||
|
@ -312,7 +318,8 @@ class KeepalivedLvs(lvs_listener_base.LvsListenerApiServerBase):
|
|||
if init_system == consts.INIT_SYSVINIT:
|
||||
try:
|
||||
subprocess.check_output(init_disable_cmd.split(),
|
||||
stderr=subprocess.STDOUT)
|
||||
stderr=subprocess.STDOUT,
|
||||
encoding='utf-8')
|
||||
except subprocess.CalledProcessError as e:
|
||||
LOG.error("Failed to disable "
|
||||
"octavia-keepalivedlvs-%(list)s service: "
|
||||
|
@ -321,7 +328,8 @@ class KeepalivedLvs(lvs_listener_base.LvsListenerApiServerBase):
|
|||
'message': (
|
||||
"Error disabling octavia-keepalivedlvs-"
|
||||
"{} service".format(listener_id)),
|
||||
'details': e.output}, status=500)
|
||||
'details': e.output
|
||||
}, status=500)
|
||||
|
||||
# delete init script ,config file and log file for that listener
|
||||
if os.path.exists(init_path):
|
||||
|
|
|
@ -134,14 +134,16 @@ class Loadbalancer:
|
|||
haproxy_ug=consts.HAPROXY_USER_GROUP_CFG)
|
||||
|
||||
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:
|
||||
LOG.error("Failed to verify haproxy file: %s %s", e, e.output)
|
||||
# Save the last config that failed validation for debugging
|
||||
os.rename(name, ''.join([name, '-failed']))
|
||||
return webob.Response(
|
||||
json={'message': "Invalid request", 'details': e.output},
|
||||
status=400)
|
||||
return webob.Response(json={
|
||||
'message': "Invalid request",
|
||||
'details': e.output
|
||||
}, status=400)
|
||||
|
||||
# file ok - move it
|
||||
os.rename(name, util.config_path(lb_id))
|
||||
|
@ -213,14 +215,17 @@ class Loadbalancer:
|
|||
elif init_system == consts.INIT_SYSVINIT:
|
||||
try:
|
||||
subprocess.check_output(init_enable_cmd.split(),
|
||||
stderr=subprocess.STDOUT)
|
||||
stderr=subprocess.STDOUT,
|
||||
encoding='utf-8')
|
||||
except subprocess.CalledProcessError as e:
|
||||
LOG.error("Failed to enable haproxy-%(lb_id)s service: "
|
||||
"%(err)s %(out)s", {'lb_id': lb_id, 'err': e,
|
||||
'out': e.output})
|
||||
return webob.Response(json={
|
||||
'message': "Error enabling haproxy-{} service".format(
|
||||
lb_id), 'details': e.output}, status=500)
|
||||
lb_id),
|
||||
'details': e.output
|
||||
}, status=500)
|
||||
|
||||
res = webob.Response(json={'message': 'OK'}, status=202)
|
||||
res.headers['ETag'] = stream.get_md5()
|
||||
|
@ -286,11 +291,12 @@ class Loadbalancer:
|
|||
lb_id=lb_id, action=action))
|
||||
|
||||
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:
|
||||
# Mitigation for
|
||||
# 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):
|
||||
|
||||
saved_exc = e
|
||||
|
@ -312,20 +318,22 @@ class Loadbalancer:
|
|||
"was reloaded, check the haproxy logs for "
|
||||
"more details.")
|
||||
break
|
||||
if b'Job is already running' not in e.output:
|
||||
if 'Job is already running' not in e.output:
|
||||
LOG.debug(
|
||||
"Failed to %(action)s haproxy-%(lb_id)s service: "
|
||||
"%(err)s %(out)s", {'action': action, 'lb_id': lb_id,
|
||||
'err': e, 'out': e.output})
|
||||
return webob.Response(json={
|
||||
'message': f"Error {action}ing haproxy",
|
||||
'details': e.output}, status=500)
|
||||
'details': e.output
|
||||
}, status=500)
|
||||
break
|
||||
else:
|
||||
# no break, we reach the retry limit for reloads
|
||||
return webob.Response(json={
|
||||
'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
|
||||
# advertisement (GARP or NA). Keepalived handles this for
|
||||
|
@ -360,13 +368,15 @@ class Loadbalancer:
|
|||
os.path.join('/proc', util.get_haproxy_pid(lb_id))):
|
||||
cmd = f"/usr/sbin/service haproxy-{lb_id} stop"
|
||||
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:
|
||||
LOG.error("Failed to stop haproxy-%s service: %s %s",
|
||||
lb_id, e, e.output)
|
||||
return webob.Response(json={
|
||||
'message': "Error stopping haproxy",
|
||||
'details': e.output}, status=500)
|
||||
'details': e.output
|
||||
}, status=500)
|
||||
|
||||
# parse config and delete stats socket
|
||||
try:
|
||||
|
@ -404,14 +414,17 @@ class Loadbalancer:
|
|||
if init_system == consts.INIT_SYSVINIT:
|
||||
try:
|
||||
subprocess.check_output(init_disable_cmd.split(),
|
||||
stderr=subprocess.STDOUT)
|
||||
stderr=subprocess.STDOUT,
|
||||
encoding='utf-8')
|
||||
except subprocess.CalledProcessError as e:
|
||||
LOG.error("Failed to disable haproxy-%(lb_id)s service: "
|
||||
"%(err)s %(out)s", {'lb_id': lb_id, 'err': e,
|
||||
'out': e.output})
|
||||
return webob.Response(json={
|
||||
'message': "Error disabling haproxy-{} service".format(
|
||||
lb_id), 'details': e.output}, status=500)
|
||||
lb_id),
|
||||
'details': e.output
|
||||
}, status=500)
|
||||
|
||||
# delete the directory + init script for that listener
|
||||
shutil.rmtree(util.haproxy_dir(lb_id))
|
||||
|
|
|
@ -89,8 +89,9 @@ class BaseOS:
|
|||
LOG.debug("Executing: %s", cmd)
|
||||
try:
|
||||
out = subprocess.check_output(cmd.split(),
|
||||
stderr=subprocess.STDOUT)
|
||||
for line in out.decode('utf-8').split('\n'):
|
||||
stderr=subprocess.STDOUT,
|
||||
encoding='utf-8')
|
||||
for line in out.split('\n'):
|
||||
LOG.debug(line)
|
||||
except subprocess.CalledProcessError as e:
|
||||
LOG.error('Failed to set up %s due to error: %s %s', interface,
|
||||
|
@ -98,7 +99,9 @@ class BaseOS:
|
|||
raise exceptions.HTTPException(
|
||||
response=webob.Response(json={
|
||||
'message': f'Error plugging {name}',
|
||||
'details': e.output}, status=500))
|
||||
'details': e.output
|
||||
}, status=500)
|
||||
)
|
||||
|
||||
|
||||
class Ubuntu(BaseOS):
|
||||
|
|
|
@ -269,7 +269,8 @@ def install_netns_systemd_service():
|
|||
def run_systemctl_command(command, service):
|
||||
cmd = f"systemctl {command} {service}"
|
||||
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:
|
||||
LOG.error("Failed to %(cmd)s %(srvc)s service: "
|
||||
"%(err)s %(out)s", {'cmd': command, 'srvc': service,
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
# under the License.
|
||||
|
||||
import errno
|
||||
import json
|
||||
import os
|
||||
import queue
|
||||
import stat
|
||||
|
@ -22,7 +23,6 @@ import time
|
|||
|
||||
from oslo_config import cfg
|
||||
from oslo_log import log as logging
|
||||
import simplejson
|
||||
|
||||
from octavia.amphorae.backends.agent.api_server import util
|
||||
from octavia.amphorae.backends.health_daemon import health_sender
|
||||
|
@ -73,8 +73,8 @@ def get_counters():
|
|||
global COUNTERS
|
||||
if COUNTERS is None:
|
||||
try:
|
||||
COUNTERS = simplejson.load(get_counters_file()) or {}
|
||||
except (simplejson.JSONDecodeError, AttributeError):
|
||||
COUNTERS = json.load(get_counters_file()) or {}
|
||||
except (json.JSONDecodeError, AttributeError):
|
||||
COUNTERS = {}
|
||||
return COUNTERS
|
||||
|
||||
|
@ -84,7 +84,7 @@ def persist_counters():
|
|||
if COUNTERS is None:
|
||||
return
|
||||
try:
|
||||
stats = simplejson.dumps(COUNTERS)
|
||||
stats = json.dumps(COUNTERS)
|
||||
counters_file = get_counters_file()
|
||||
counters_file.truncate(0)
|
||||
counters_file.write(stats)
|
||||
|
|
|
@ -13,11 +13,11 @@
|
|||
# under the License.
|
||||
|
||||
import ipaddress
|
||||
import json
|
||||
import os
|
||||
import stat
|
||||
|
||||
from oslo_config import cfg
|
||||
import simplejson
|
||||
|
||||
from octavia.common import constants as consts
|
||||
|
||||
|
@ -45,11 +45,11 @@ class InterfaceFile:
|
|||
|
||||
@classmethod
|
||||
def load(cls, fp):
|
||||
return simplejson.load(fp)
|
||||
return json.load(fp)
|
||||
|
||||
@classmethod
|
||||
def dump(cls, obj):
|
||||
return simplejson.dumps(obj)
|
||||
return json.dumps(obj)
|
||||
|
||||
@classmethod
|
||||
def from_file(cls, filename):
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
# under the License.
|
||||
import functools
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import ssl
|
||||
import time
|
||||
|
@ -24,7 +25,6 @@ from oslo_context import context as oslo_context
|
|||
from oslo_log import log as logging
|
||||
from oslo_utils.secretutils import md5
|
||||
import requests
|
||||
import simplejson
|
||||
from stevedore import driver as stevedore_driver
|
||||
|
||||
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:
|
||||
LOG.debug("Amphora network interface not found.")
|
||||
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?
|
||||
return r
|
||||
except (requests.ConnectionError, requests.Timeout) as e:
|
||||
|
|
|
@ -314,7 +314,8 @@ class KeepalivedLvsTestCase(base.TestCase):
|
|||
cmd = ("/usr/sbin/service octavia-keepalivedlvs-{listener_id}"
|
||||
" {action}".format(listener_id=self.FAKE_ID, action='start'))
|
||||
mock_check_output.assert_called_once_with(cmd.split(),
|
||||
stderr=subprocess.STDOUT)
|
||||
stderr=subprocess.STDOUT,
|
||||
encoding='utf-8')
|
||||
self.assertEqual(202, res.status_code)
|
||||
|
||||
res = self.test_keepalivedlvs.manage_lvs_listener(self.FAKE_ID,
|
||||
|
@ -347,8 +348,10 @@ class KeepalivedLvsTestCase(base.TestCase):
|
|||
cmd2 = ("systemctl disable "
|
||||
"octavia-keepalivedlvs-{list}".format(list=self.FAKE_ID))
|
||||
calls = [
|
||||
mock.call(cmd1.split(), stderr=subprocess.STDOUT),
|
||||
mock.call(cmd2.split(), stderr=subprocess.STDOUT)
|
||||
mock.call(cmd1.split(), stderr=subprocess.STDOUT,
|
||||
encoding='utf-8'),
|
||||
mock.call(cmd2.split(), stderr=subprocess.STDOUT,
|
||||
encoding='utf-8')
|
||||
]
|
||||
m_check_output.assert_has_calls(calls)
|
||||
self.assertEqual(200, res.status_code)
|
||||
|
|
|
@ -137,7 +137,7 @@ class TestServerTestCase(base.TestCase):
|
|||
haproxy_ug=consts.HAPROXY_USER_GROUP_CFG,
|
||||
peer=(octavia_utils.
|
||||
base64_sha1_string('amp_123').rstrip('='))).split(),
|
||||
stderr=-2)
|
||||
stderr=-2, encoding='utf-8')
|
||||
mock_rename.assert_called_with(
|
||||
'/var/lib/octavia/123/haproxy.cfg.new',
|
||||
'/var/lib/octavia/123/haproxy.cfg')
|
||||
|
@ -145,11 +145,11 @@ class TestServerTestCase(base.TestCase):
|
|||
if init_system == consts.INIT_SYSTEMD:
|
||||
mock_subprocess.assert_any_call(
|
||||
"systemctl enable haproxy-123".split(),
|
||||
stderr=subprocess.STDOUT)
|
||||
stderr=subprocess.STDOUT, encoding='utf-8')
|
||||
elif init_system == consts.INIT_SYSVINIT:
|
||||
mock_subprocess.assert_any_call(
|
||||
"insserv /etc/init.d/haproxy-123".split(),
|
||||
stderr=subprocess.STDOUT)
|
||||
stderr=subprocess.STDOUT, encoding='utf-8')
|
||||
else:
|
||||
self.assertIn(init_system, consts.VALID_INIT_SYSTEMS)
|
||||
|
||||
|
@ -243,7 +243,7 @@ class TestServerTestCase(base.TestCase):
|
|||
haproxy_ug=consts.HAPROXY_USER_GROUP_CFG,
|
||||
peer=(octavia_utils.
|
||||
base64_sha1_string('amp_123').rstrip('='))).split(),
|
||||
stderr=-2)
|
||||
stderr=-2, encoding='utf-8')
|
||||
mock_rename.assert_called_with(
|
||||
'/var/lib/octavia/123/haproxy.cfg.new',
|
||||
'/var/lib/octavia/123/haproxy.cfg.new-failed')
|
||||
|
@ -321,7 +321,8 @@ class TestServerTestCase(base.TestCase):
|
|||
' 123 started'},
|
||||
jsonutils.loads(rv.data.decode('utf-8')))
|
||||
mock_subprocess.assert_called_with(
|
||||
['/usr/sbin/service', 'haproxy-123', 'start'], stderr=-2)
|
||||
['/usr/sbin/service', 'haproxy-123', 'start'],
|
||||
stderr=-2, encoding='utf-8')
|
||||
|
||||
mock_exists.return_value = True
|
||||
mock_subprocess.side_effect = subprocess.CalledProcessError(
|
||||
|
@ -339,7 +340,8 @@ class TestServerTestCase(base.TestCase):
|
|||
'details': RANDOM_ERROR.decode('utf-8'),
|
||||
}, jsonutils.loads(rv.data.decode('utf-8')))
|
||||
mock_subprocess.assert_called_with(
|
||||
['/usr/sbin/service', 'haproxy-123', 'start'], stderr=-2)
|
||||
['/usr/sbin/service', 'haproxy-123', 'start'],
|
||||
stderr=-2, encoding='utf-8')
|
||||
|
||||
def test_ubuntu_reload(self):
|
||||
self._test_reload(consts.UBUNTU)
|
||||
|
@ -377,7 +379,8 @@ class TestServerTestCase(base.TestCase):
|
|||
'details': 'Listener 123 reloaded'},
|
||||
jsonutils.loads(rv.data.decode('utf-8')))
|
||||
mock_subprocess.assert_called_with(
|
||||
['/usr/sbin/service', 'haproxy-123', 'reload'], stderr=-2)
|
||||
['/usr/sbin/service', 'haproxy-123', 'reload'],
|
||||
stderr=-2, encoding='utf-8')
|
||||
|
||||
# Process not running so start
|
||||
mock_exists.return_value = True
|
||||
|
@ -395,7 +398,8 @@ class TestServerTestCase(base.TestCase):
|
|||
' 123 started'},
|
||||
jsonutils.loads(rv.data.decode('utf-8')))
|
||||
mock_subprocess.assert_called_with(
|
||||
['/usr/sbin/service', 'haproxy-123', 'start'], stderr=-2)
|
||||
['/usr/sbin/service', 'haproxy-123', 'start'],
|
||||
stderr=-2, encoding='utf-8')
|
||||
|
||||
def test_ubuntu_info(self):
|
||||
self._test_info(consts.UBUNTU)
|
||||
|
|
|
@ -35,7 +35,8 @@ class KeepalivedTestCase(base.TestCase):
|
|||
cmd = ("/usr/sbin/service octavia-keepalived {action}".format(
|
||||
action='start'))
|
||||
mock_check_output.assert_called_once_with(cmd.split(),
|
||||
stderr=subprocess.STDOUT)
|
||||
stderr=subprocess.STDOUT,
|
||||
encoding='utf-8')
|
||||
self.assertEqual(202, res.status_code)
|
||||
|
||||
res = self.test_keepalived.manager_keepalived_service('restart')
|
||||
|
|
|
@ -86,7 +86,8 @@ class ListenerTestCase(base.TestCase):
|
|||
listener_id, consts.AMP_ACTION_START)
|
||||
|
||||
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_vrrp_update.assert_not_called()
|
||||
self.assertEqual(202, result.status_code)
|
||||
|
@ -111,7 +112,8 @@ class ListenerTestCase(base.TestCase):
|
|||
listener_id, consts.AMP_ACTION_RELOAD)
|
||||
|
||||
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_vrrp_update.assert_called_once_with(listener_id,
|
||||
consts.AMP_ACTION_RELOAD)
|
||||
|
@ -134,7 +136,8 @@ class ListenerTestCase(base.TestCase):
|
|||
listener_id, consts.AMP_ACTION_RELOAD)
|
||||
|
||||
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_vrrp_update.assert_called_once_with(listener_id,
|
||||
consts.AMP_ACTION_RELOAD)
|
||||
|
@ -157,13 +160,14 @@ class ListenerTestCase(base.TestCase):
|
|||
ref_command_split.append(consts.AMP_ACTION_START)
|
||||
|
||||
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(
|
||||
listener_id, consts.AMP_ACTION_START)
|
||||
|
||||
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_vrrp_update.assert_not_called()
|
||||
self.assertEqual(500, result.status_code)
|
||||
|
@ -181,13 +185,14 @@ class ListenerTestCase(base.TestCase):
|
|||
ref_command_split.append(consts.AMP_ACTION_START)
|
||||
|
||||
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(
|
||||
listener_id, consts.AMP_ACTION_START)
|
||||
|
||||
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_vrrp_update.assert_not_called()
|
||||
self.assertEqual(202, result.status_code)
|
||||
|
@ -220,7 +225,7 @@ class ListenerTestCase(base.TestCase):
|
|||
|
||||
mock_check_output.side_effect = [
|
||||
subprocess.CalledProcessError(
|
||||
output=b'haproxy.service is not active, cannot reload.',
|
||||
output='haproxy.service is not active, cannot reload.',
|
||||
returncode=-2, cmd='service'),
|
||||
None]
|
||||
mock_check_status.return_value = 'ACTIVE'
|
||||
|
@ -248,13 +253,13 @@ class ListenerTestCase(base.TestCase):
|
|||
|
||||
mock_check_output.side_effect = [
|
||||
subprocess.CalledProcessError(
|
||||
output=b'haproxy.service is not active, cannot reload.',
|
||||
output='haproxy.service is not active, cannot reload.',
|
||||
returncode=-2, cmd='service'),
|
||||
subprocess.CalledProcessError(
|
||||
output=b'haproxy.service is not active, cannot reload.',
|
||||
output='haproxy.service is not active, cannot reload.',
|
||||
returncode=-2, cmd='service'),
|
||||
subprocess.CalledProcessError(
|
||||
output=b'haproxy.service is not active, cannot reload.',
|
||||
output='haproxy.service is not active, cannot reload.',
|
||||
returncode=-2, cmd='service')]
|
||||
mock_check_status.return_value = 'ACTIVE'
|
||||
mock_check_status.side_effect = None
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#
|
||||
import json
|
||||
import os
|
||||
import queue
|
||||
from unittest import mock
|
||||
|
@ -19,7 +20,6 @@ from unittest import mock
|
|||
from oslo_config import cfg
|
||||
from oslo_config import fixture as oslo_fixture
|
||||
from oslo_utils import uuidutils
|
||||
import simplejson
|
||||
|
||||
from octavia.amphorae.backends.health_daemon import health_daemon
|
||||
from octavia.common import constants
|
||||
|
@ -345,7 +345,7 @@ class TestHealthDaemon(base.TestCase):
|
|||
|
||||
with mock.patch('os.open'), mock.patch.object(
|
||||
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},
|
||||
})
|
||||
msg = health_daemon.build_stats_message()
|
||||
|
@ -353,7 +353,7 @@ class TestHealthDaemon(base.TestCase):
|
|||
self.assertEqual(SAMPLE_STATS_MSG, msg)
|
||||
|
||||
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: {
|
||||
'bin': int(FRONTEND_STATS['bin']),
|
||||
'bout': int(FRONTEND_STATS['bout']),
|
||||
|
@ -450,14 +450,14 @@ class TestHealthDaemon(base.TestCase):
|
|||
|
||||
with mock.patch('os.open'), mock.patch.object(
|
||||
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: {
|
||||
'bin': 1, 'bout': 2, "ereq": 0, "stot": 0}
|
||||
})
|
||||
msg = health_daemon.build_stats_message()
|
||||
|
||||
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_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(
|
||||
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},
|
||||
})
|
||||
msg = health_daemon.build_stats_message()
|
||||
|
@ -488,7 +488,7 @@ class TestHealthDaemon(base.TestCase):
|
|||
self.assertEqual(SAMPLE_MSG_HAPROXY_RESTART, msg)
|
||||
|
||||
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: {
|
||||
'bin': int(FRONTEND_STATS['bin']),
|
||||
'bout': int(FRONTEND_STATS['bout']),
|
||||
|
|
|
@ -46,7 +46,6 @@ tenacity>=5.0.4 # Apache-2.0
|
|||
distro>=1.2.0 # Apache-2.0
|
||||
jsonschema>=3.2.0 # MIT
|
||||
octavia-lib>=3.3.0 # Apache-2.0
|
||||
simplejson>=3.13.2 # MIT
|
||||
setproctitle>=1.1.10 # BSD
|
||||
python-dateutil>=2.7.0 # BSD
|
||||
|
||||
|
|
Loading…
Reference in New Issue