fix invokings and unittests

This commit is contained in:
Yu Ming Zhu 2019-01-31 20:13:50 +00:00 committed by Mike McLean
parent b0c8f817fe
commit dc208d0555
4 changed files with 26 additions and 19 deletions

View file

@ -36,7 +36,6 @@ import stat
import struct
import sys
import time
import six.moves.configparser
from zlib import adler32
from six.moves import range
import six

View file

@ -9,7 +9,6 @@ from __future__ import absolute_import
import koji
from koji.plugin import callback, ignore_error, convert_datetime
from koji.context import context
import six.moves.configparser
import logging
import json
import random
@ -270,8 +269,7 @@ def send_queued_msgs(cbtype, *args, **kws):
log = logging.getLogger('koji.plugin.protonmsg')
global CONFIG
if not CONFIG:
CONFIG = koji.read_config_files(CONFIG_FILE,
six.moves.configparser.SafeConfigParser)
CONFIG = koji.read_config_files(CONFIG_FILE)
urls = CONFIG.get('broker', 'urls').split()
test_mode = False
if CONFIG.has_option('broker', 'test_mode'):

View file

@ -163,40 +163,47 @@ class ConfigFileTestCase(unittest.TestCase):
@mock_open()
@mock.patch("six.moves.configparser.ConfigParser", spec=True)
@mock.patch("six.moves.configparser.RawConfigParser", spec=True)
@mock.patch("six.moves.configparser.SafeConfigParser", spec=True)
def test_read_config_files(self, scp_clz, cp_clz, open_mock):
def test_read_config_files(self, scp_clz, rcp_clz, cp_clz, open_mock):
files = 'test1.conf'
conf = koji.read_config_files(files)
self.assertTrue(isinstance(conf,
six.moves.configparser.ConfigParser.__class__))
cp_clz.assert_called_once()
open_mock.assert_called_once_with(files, 'r')
if six.PY2:
cp_clz.return_value.readfp.assert_called_once()
self.assertTrue(isinstance(conf,
six.moves.configparser.SafeConfigParser.__class__))
scp_clz.assert_called_once()
scp_clz.return_value.readfp.assert_called_once()
else:
self.assertTrue(isinstance(conf,
six.moves.configparser.ConfigParser.__class__))
cp_clz.assert_called_once()
cp_clz.return_value.read_file.assert_called_once()
open_mock.reset_mock()
cp_clz.reset_mock()
scp_clz.reset_mock()
files = ['test1.conf', 'test2.conf']
koji.read_config_files(files)
cp_clz.assert_called_once()
open_mock.assert_has_calls([call('test1.conf', 'r'),
call('test2.conf', 'r')],
any_order=True)
if six.PY2:
self.assertEqual(cp_clz.return_value.readfp.call_count, 2)
scp_clz.assert_called_once()
self.assertEqual(scp_clz.return_value.readfp.call_count, 2)
else:
cp_clz.assert_called_once()
self.assertEqual(cp_clz.return_value.read_file.call_count, 2)
open_mock.reset_mock()
cp_clz.reset_mock()
conf = koji.read_config_files(files,
six.moves.configparser.SafeConfigParser)
scp_clz.reset_mock()
conf = koji.read_config_files(files, raw=True)
self.assertTrue(isinstance(conf,
six.moves.configparser.SafeConfigParser.__class__))
six.moves.configparser.RawConfigParser.__class__))
cp_clz.assert_not_called()
scp_clz.assert_called_once()
scp_clz.assert_not_called()
rcp_clz.assert_called_once()
class MavenUtilTestCase(unittest.TestCase):
@ -532,12 +539,13 @@ class MavenUtilTestCase(unittest.TestCase):
self.assertEqual(cm.exception.args[0], 'total ordering not possible')
def _read_conf(self, cfile):
config = six.moves.configparser.ConfigParser()
path = os.path.dirname(__file__)
with open(path + cfile, 'r') as conf_file:
if six.PY2:
config = six.moves.configparser.SafeConfigParser()
config.readfp(conf_file)
else:
config = six.moves.configparser.ConfigParser()
config.read_file(conf_file)
return config

View file

@ -9,7 +9,7 @@ except ImportError:
from mock import patch, MagicMock
from koji.context import context
from six.moves.configparser import SafeConfigParser
from six.moves.configparser import ConfigParser, SafeConfigParser
class TestProtonMsg(unittest.TestCase):
def tearDown(self):
@ -269,10 +269,11 @@ topic_prefix = koji
connect_timeout = 10
send_timeout = 60
""")
conf = SafeConfigParser()
if six.PY2:
conf = SafeConfigParser()
conf.readfp(confdata)
else:
conf = ConfigParser()
conf.read_file(confdata)
self.handler = protonmsg.TimeoutHandler('amqps://broker1.example.com:5671', [], conf)
@ -293,10 +294,11 @@ topic_prefix = koji
connect_timeout = 10
send_timeout = 60
""")
conf = SafeConfigParser()
if six.PY2:
conf = SafeConfigParser()
conf.readfp(confdata)
else:
conf = ConfigParser()
conf.read_file(confdata)
handler = protonmsg.TimeoutHandler('amqp://broker1.example.com:5672', [], conf)
event = MagicMock()