Skip to content

Commit

Permalink
Merge pull request ceph#1806 from ceph/fix-pytest
Browse files Browse the repository at this point in the history
tests: Fix warnings, deprecations, and broken tests
  • Loading branch information
zmc authored Jan 25, 2023
2 parents 8e1bba1 + 7f855e0 commit 065d679
Show file tree
Hide file tree
Showing 61 changed files with 219 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from unittest.mock import patch

class TestCheckReImageFailureMarkDown(object):
def setup(self):
def setup_method(self):
self.the_function = supervisor.check_for_reimage_failures_and_mark_down

def create_n_out_of_10_reimage_failed_jobs(self, n):
Expand Down
2 changes: 1 addition & 1 deletion teuthology/ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def get_jobs(archive_dir):
dir_contents = os.listdir(archive_dir)

def is_job_dir(parent, subdir):
if (os.path.isdir(os.path.join(parent, subdir)) and re.match('\d+$',
if (os.path.isdir(os.path.join(parent, subdir)) and re.match(r'\d+$',
subdir)):
return True
return False
Expand Down
4 changes: 2 additions & 2 deletions teuthology/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def host_shortname(hostname):

def canonicalize_hostname(hostname, user='ubuntu'):
hostname_expr = hostname_expr_templ.format(
lab_domain=config.lab_domain.replace('.', '\.'))
lab_domain=config.lab_domain.replace('.', r'\.'))
match = re.match(hostname_expr, hostname)
if _is_ipv4(hostname) or _is_ipv6(hostname):
return "%s@%s" % (user, hostname)
Expand Down Expand Up @@ -82,7 +82,7 @@ def canonicalize_hostname(hostname, user='ubuntu'):
def decanonicalize_hostname(hostname):
lab_domain = ''
if config.lab_domain:
lab_domain='\.' + config.lab_domain.replace('.', '\.')
lab_domain=r'\.' + config.lab_domain.replace('.', r'\.')
hostname_expr = hostname_expr_templ.format(lab_domain=lab_domain)
match = re.match(hostname_expr, hostname)
if match:
Expand Down
4 changes: 2 additions & 2 deletions teuthology/nuke/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def remove_osd_tmpfs(ctx):
log.info('Unmount any osd tmpfs dirs...')
ctx.cluster.run(
args=[
'egrep', 'tmpfs\s+/mnt', '/etc/mtab', run.Raw('|'),
'egrep', r'tmpfs\s+/mnt', '/etc/mtab', run.Raw('|'),
'awk', '{print $2}', run.Raw('|'),
'xargs', '-r',
'sudo', 'umount', run.Raw(';'),
Expand Down Expand Up @@ -229,7 +229,7 @@ def remove_yum_timedhosts(ctx):
if remote.os.package_type != 'rpm':
continue
remote.run(
args="sudo find /var/cache/yum -name 'timedhosts' -exec rm {} \;",
args=r"sudo find /var/cache/yum -name 'timedhosts' -exec rm {} \;",
check_status=False, timeout=180
)

Expand Down
4 changes: 2 additions & 2 deletions teuthology/openstack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def get_addresses(self):
with safe_while(sleep=2, tries=30,
action="get ip " + self['id']) as proceed:
while proceed():
found = re.match('.*\d+', self['addresses'])
found = re.match(r'.*\d+', self['addresses'])
if found:
return self['addresses']
self.set_info()
Expand Down Expand Up @@ -165,7 +165,7 @@ def get_ip(self, network):
self.private_ip = self.get_ip_neutron()
except Exception as e:
log.debug("ignoring get_ip_neutron exception " + str(e))
self.private_ip = re.findall(network + '=([\d.]+)',
self.private_ip = re.findall(network + r'=([\d.]+)',
self.get_addresses())[0]
return self.private_ip

Expand Down
8 changes: 4 additions & 4 deletions teuthology/openstack/test/openstack-integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def get_teuthology_log(self):

class TestSuite(Integration):

def setup(self):
def setup_method(self):
self.d = tempfile.mkdtemp()
self.setup_worker()
logging.info("TestSuite: done worker")
Expand Down Expand Up @@ -152,7 +152,7 @@ def test_suite_nuke(self):

class TestSchedule(Integration):

def setup(self):
def setup_method(self):
self.d = tempfile.mkdtemp()
self.setup_worker()

Expand Down Expand Up @@ -218,7 +218,7 @@ def test_schedule_resources_hint(self):

class TestLock(Integration):

def setup(self):
def setup_method(self):
self.options = ['--verbose',
'--machine-type', 'openstack' ]

Expand Down Expand Up @@ -259,7 +259,7 @@ def test_list(self, capsys):

class TestNuke(Integration):

def setup(self):
def setup_method(self):
self.options = ['--verbose',
'--machine-type', 'openstack']

Expand Down
2 changes: 1 addition & 1 deletion teuthology/openstack/test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class TestOpenStack(object):

def setup(self):
def setup_method(self):
self.openstack_config = config['openstack']

def test_config_clone(self):
Expand Down
12 changes: 6 additions & 6 deletions teuthology/openstack/test/test_openstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@

class TestOpenStackBase(object):

def setup(self):
def setup_method(self):
OpenStack.token = None
OpenStack.token_expires = None
self.environ = {}
for k in os.environ.keys():
if k.startswith('OS_'):
self.environ[k] = os.environ[k]

def teardown(self):
def teardown_method(self):
OpenStack.token = None
OpenStack.token_expires = None
for k in os.environ.keys():
Expand Down Expand Up @@ -1598,8 +1598,8 @@ def setup_class(self):
else:
self.can_create_floating_ips = False

def setup(self):
super(TestTeuthologyOpenStack, self).setup()
def setup_method(self):
super(TestTeuthologyOpenStack, self).setup_method()
self.key_filename = tempfile.mktemp()
self.key_name = 'teuthology-test'
self.name = 'teuthology-test'
Expand All @@ -1614,8 +1614,8 @@ def setup(self):
'--name', self.name,
'--verbose']

def teardown(self):
super(TestTeuthologyOpenStack, self).teardown()
def teardown_method(self):
super(TestTeuthologyOpenStack, self).teardown_method()
self.clobber()
os.unlink(self.key_filename)

Expand Down
2 changes: 1 addition & 1 deletion teuthology/orchestra/daemon/systemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def parse_line(line):
self.status_cmd + " | grep 'Main.*code=exited'",
)
line = out.strip().split('\n')[-1]
exit_code = int(re.match('.*status=(\d+).*', line).groups()[0])
exit_code = int(re.match(r'.*status=(\d+).*', line).groups()[0])
if exit_code:
self.remote.run(
args=self.output_cmd
Expand Down
2 changes: 1 addition & 1 deletion teuthology/orchestra/test/integration/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


class TestIntegration():
def setup(self):
def setup_method(self):
try:
host = os.environ['ORCHESTRA_TEST_HOST']
except KeyError:
Expand Down
6 changes: 3 additions & 3 deletions teuthology/orchestra/test/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ def test_run_all(self):
(r2, ['baz']),
],
)
assert r1.run.called_once_with(args=['test'])
assert r2.run.called_once_with(args=['test'])
got = c.run(args=['test'])
r1.run.assert_called_once_with(args=['test'], wait=True)
r2.run.assert_called_once_with(args=['test'], wait=True)
assert len(got) == 2
assert got, [ret1 == ret2]
# check identity not equality
Expand Down Expand Up @@ -203,7 +203,7 @@ def func(r):

class TestWriteFile(object):
""" Tests for cluster.write_file """
def setup(self):
def setup_method(self):
self.r1 = remote.Remote('r1', ssh=Mock())
self.c = cluster.Cluster(
remotes=[
Expand Down
4 changes: 2 additions & 2 deletions teuthology/orchestra/test/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@


class TestConnection(object):
def setup(self):
def setup_method(self):
self.start_patchers()

def teardown(self):
def teardown_method(self):
self.stop_patchers()

def start_patchers(self):
Expand Down
2 changes: 1 addition & 1 deletion teuthology/orchestra/test/test_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class TestPhysicalConsole(TestConsole):
ipmi_cmd_templ = 'ipmitool -H {h}.{d} -I lanplus -U {u} -P {p} {c}'
conserver_cmd_templ = 'console -M {m} -p {p} {mode} {h}'

def setup(self):
def setup_method(self):
self.hostname = 'host'
teuth_config.ipmi_domain = 'ipmi_domain'
teuth_config.ipmi_user = 'ipmi_user'
Expand Down
24 changes: 11 additions & 13 deletions teuthology/orchestra/test/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

class TestRemote(object):

def setup(self):
def setup_method(self):
self.start_patchers()

def teardown(self):
def teardown_method(self):
self.stop_patchers()

def start_patchers(self):
Expand Down Expand Up @@ -60,8 +60,9 @@ def test_run(self):
rem = remote.Remote(name='[email protected]', ssh=self.m_ssh)
rem._runner = m_run
result = rem.run(args=args)
assert m_transport.getpeername.called_once_with()
assert m_run.called_once_with(args=args)
m_transport.getpeername.assert_called_once_with()
m_run_call_kwargs = m_run.call_args_list[0][1]
assert m_run_call_kwargs['args'] == args
assert result is proc
assert result.remote is rem

Expand Down Expand Up @@ -101,7 +102,7 @@ def test_arch(self):
stdout.seek(0)
proc = RemoteProcess(
client=self.m_ssh,
args='fakey',
args=args,
)
proc._stdout_buf = Mock()
proc._stdout_buf.channel = Mock()
Expand All @@ -111,15 +112,12 @@ def test_arch(self):
m_run.return_value = proc
r = remote.Remote(name='[email protected]', ssh=self.m_ssh)
r._runner = m_run
assert m_transport.getpeername.called_once_with()
assert proc._stdout_buf.channel.recv_exit_status.called_once_with()
assert m_run.called_once_with(
client=self.m_ssh,
args=args,
stdout=BytesIO(),
name=r.shortname,
)
assert r.arch == 'test_arch'
assert len(m_run.call_args_list) == 1
m_run_call_kwargs = m_run.call_args_list[0][1]
assert m_run_call_kwargs['client'] == self.m_ssh
assert m_run_call_kwargs['name'] == r.shortname
assert m_run_call_kwargs['args'] == ' '.join(args)

def test_host_key(self):
m_key = MagicMock()
Expand Down
4 changes: 2 additions & 2 deletions teuthology/orchestra/test/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ def set_buffer_contents(buf, contents):


class TestRun(object):
def setup(self):
def setup_method(self):
self.start_patchers()

def teardown(self):
def teardown_method(self):
self.stop_patchers()

def start_patchers(self):
Expand Down
4 changes: 2 additions & 2 deletions teuthology/provision/cloud/test/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@


class TestBase(object):
def setup(self):
def setup_method(self):
config.load()
config.libcloud = dummy_config
cloud.supported_drivers['dummy'] = dummy_drivers

def teardown(self):
def teardown_method(self):
del cloud.supported_drivers['dummy']


Expand Down
4 changes: 2 additions & 2 deletions teuthology/provision/cloud/test/test_cloud_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ class DummyProvider(cloud.base.Provider):


class TestInit(object):
def setup(self):
def setup_method(self):
config.load()
config.libcloud = dummy_config
cloud.supported_drivers['dummy'] = dummy_drivers

def teardown(self):
def teardown_method(self):
del cloud.supported_drivers['dummy']

def test_get_types(self):
Expand Down
4 changes: 2 additions & 2 deletions teuthology/provision/cloud/test/test_cloud_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def get_datetime_string(offset_hours=0):
class TestAuthToken(object):
klass = util.AuthToken

def setup(self):
def setup_method(self):
default_expires = get_datetime_string(0)
self.test_data = dict(
value='token_value',
Expand All @@ -103,7 +103,7 @@ def setup(self):
for name, patcher in self.patchers.items():
self.mocks[name] = patcher.start()

def teardown(self):
def teardown_method(self):
for patcher in self.patchers.values():
patcher.stop()

Expand Down
8 changes: 4 additions & 4 deletions teuthology/provision/cloud/test/test_openstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def get_fake_obj(mock_args=None, attributes=None):


class TestOpenStackBase(object):
def setup(self):
def setup_method(self):
config.load(dict(libcloud=deepcopy(test_config)))
self.start_patchers()

Expand Down Expand Up @@ -159,7 +159,7 @@ def start_patchers(self):
self.mocks[name] = patcher.start()
self.mocks['m_get_endpoint'].return_value = 'endpoint'

def teardown(self):
def teardown_method(self):
for patcher in self.patchers.values():
patcher.stop()

Expand Down Expand Up @@ -671,7 +671,7 @@ def test_create(self, networks, security_groups):
obj.provider.driver.create_volume.side_effect = Exception
with patch.object(obj, '_destroy_volumes'):
assert obj.create() is False
assert obj._destroy_volumes.called_once_with()
obj._destroy_volumes.assert_called_once_with()

def test_update_dns(self):
config.nsupdate_url = 'nsupdate_url'
Expand Down Expand Up @@ -702,7 +702,7 @@ def test_destroy(self, nodes):
assert result is True
else:
for node in nodes:
assert node.destroy.called_once_with()
node.destroy.assert_called_once_with()

_volume_matrix = (
'count, size, should_succeed',
Expand Down
2 changes: 1 addition & 1 deletion teuthology/provision/openstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def ip2name(prefix, ip):
"""
return the instance name suffixed with the IP address.
"""
digits = map(int, re.findall('(\d+)\.(\d+)\.(\d+)\.(\d+)', ip)[0])
digits = map(int, re.findall(r'(\d+)\.(\d+)\.(\d+)\.(\d+)', ip)[0])
return prefix + "%03d%03d%03d%03d" % tuple(digits)

def create(self, num, os_type, os_version, arch, resources_hint):
Expand Down
2 changes: 1 addition & 1 deletion teuthology/provision/test/test_downburst.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


class TestDownburst(object):
def setup(self):
def setup_method(self):
self.ctx = Mock()
self.ctx.os_type = 'rhel'
self.ctx.os_version = '7.0'
Expand Down
Loading

0 comments on commit 065d679

Please sign in to comment.