Skip to content

Commit

Permalink
tests: make suit compatible with python 3
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Styk <[email protected]>
  • Loading branch information
StykMartin committed Feb 6, 2025
1 parent 2f0165a commit 456cc79
Show file tree
Hide file tree
Showing 24 changed files with 69 additions and 84 deletions.
5 changes: 4 additions & 1 deletion IntegrationTests/src/bkr/inttest/assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
import time
import datetime

from six.moves import range


def assert_sorted(things, key=None):
"""
Asserts that the given sequence is in sorted order.
"""
if len(things) == 0: return
if key is not None:
things = map(key, things)
for n in xrange(1, len(things)):
for n in range(1, len(things)):
if things[n] < things[n - 1]:
raise AssertionError('Not in sorted order, found %r after %r' %
(things[n], things[n - 1]))
Expand Down
11 changes: 4 additions & 7 deletions IntegrationTests/src/bkr/inttest/client/test_system_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
from bkr.inttest import data_setup
from bkr.inttest.client import run_client, ClientError, ClientTestCase

try:
unicode('')
except:
unicode = str
import six


class SystemStatusTest(ClientTestCase):
Expand Down Expand Up @@ -44,7 +41,7 @@ def test_reserve_with_recipe(self):
json_out = loads(json_out)
current_reservation = json_out.get('current_reservation')
self.assertEqual(current_reservation.get('user_name'),
unicode(system.user))
six.text_type(system.user))
self.assertEqual(current_reservation.get('recipe_id'), '%s' % \
system.open_reservation.recipe.id)
self.assertEqual(json_out.get('condition'), '%s' % system.status)
Expand Down Expand Up @@ -76,7 +73,7 @@ def test_reserve_manually(self):
'--format', 'json'])
json_out = loads(json_out)
current_reservation = json_out['current_reservation']
self.assertEqual(current_reservation.get('user_name'), unicode(user))
self.assertEqual(current_reservation.get('user_name'), six.text_type(user))
self.assertEqual(json_out.get('current_loan'), None)
self.assertEqual(json_out.get('condition'), '%s' % SystemStatus.manual)

Expand All @@ -100,7 +97,7 @@ def test_unpopulated_status(self):
json_out = loads(json_out)
self.assertEqual(json_out.get('current_reservation'), None)
self.assertEqual(json_out.get('current_loan'), None)
self.assertEqual(json_out.get('condition'), unicode(system.status))
self.assertEqual(json_out.get('condition'), six.text_type(system.status))

# Human friendly output
human_out = run_client(['bkr', 'system-status', system.fqdn])
Expand Down
2 changes: 0 additions & 2 deletions IntegrationTests/src/bkr/inttest/client/test_task_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@

from turbogears.database import session
from turbogears import config
from bkr.inttest import data_setup
from bkr.inttest.client import run_client, ClientError, ClientTestCase
import pkg_resources
from os import path
from bkr.server.model import Task, OSMajor
from bkr.server.model.tasklibrary import TaskLibrary


class TaskAddTest(ClientTestCase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_errors_if_unauthorised_with_domain_info(self):
'--os-project-name=beaker'],
config=self.client_config)
self.fail('should raise a ClientError')
except ClientError, e:
except ClientError as e:
self.assertIn(
'Could not authenticate with OpenStack using your credentials',
e.stderr_output)
7 changes: 4 additions & 3 deletions IntegrationTests/src/bkr/inttest/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
path information is ignored.
"""

import os, os.path
import os
import os.path
import re
import time
import shutil
import urlparse
import mimetypes
import wsgiref.util, wsgiref.simple_server
import wsgiref.util
import wsgiref.simple_server

class Application(object):

Expand Down
12 changes: 6 additions & 6 deletions IntegrationTests/src/bkr/inttest/kickstart_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
# (at your option) any later version.

import pkg_resources
import urlparse
import re
import jinja2
import tempfile
import difflib
import six
from six.moves import urllib
from sqlalchemy.orm.exc import NoResultFound

from bkr.inttest import data_setup, get_server_base
from bkr.server.bexceptions import DatabaseLookupError
from bkr.server.model import session, Distro, DistroTree, Arch, DistroTreeRepo
Expand Down Expand Up @@ -80,10 +80,10 @@ def compare_expected(name, recipe_id, actual):
vars = {
'@RECIPEID@': str(recipe_id),
'@BEAKER@': get_server_base(),
'@REPOS@': urlparse.urljoin(get_server_base(), '/repos/'),
'@HARNESS@': urlparse.urljoin(get_server_base(), '/harness/'),
'@REPOS@': urllib.parse.urljoin(get_server_base(), '/repos/'),
'@HARNESS@': urllib.parse.urljoin(get_server_base(), '/harness/'),
}
for var, value in vars.iteritems():
for var, value in six.iteritems(vars):
expected = expected.replace(var, value)
expected = expected.rstrip('\n')
actual = actual.rstrip('\n')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -741,8 +741,6 @@ def setUp(self):
u'type': u'uimage'},
{u'path': u'images/pxeboot/uInitrd',
u'type': u'uinitrd'},],
u'arches': [
],
u'arches': [],
u'urls': [u'http://localhost:19998/Fedora-rawhide/armhfp/os/'],
u'arch': u'armhfp',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import os, os.path
import time
import pkg_resources
import gevent.hub
from turbogears.database import session
from bkr.common.helpers import makedirs_ignore
from bkr.labcontroller.config import get_conf
Expand Down
4 changes: 2 additions & 2 deletions IntegrationTests/src/bkr/inttest/mail_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"""

import threading
import smtpd
import asyncore
import smtpd # TODO: Removed in Python 3.12
import asyncore # TODO: Removed in Python 3.12
import logging

log = logging.getLogger(__name__)
Expand Down
6 changes: 4 additions & 2 deletions IntegrationTests/src/bkr/inttest/server/requests_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
# (at your option) any later version.

import json
import xmlrpclib
import requests
from six.moves import xmlrpc_client

from bkr.inttest import data_setup, get_server_base


def xmlrpc(url, xmlrpc_method, xmlrpc_args, **kwargs):
# marshal XMLRPC request
data = xmlrpclib.dumps(tuple(xmlrpc_args), methodname=xmlrpc_method, allow_none=True)
data = xmlrpc_client.dumps(tuple(xmlrpc_args), methodname=xmlrpc_method, allow_none=True)
# add Content-Type request header
headers = kwargs.pop('headers', {})
headers.update({'Content-Type': 'text/xml'})
Expand Down
22 changes: 10 additions & 12 deletions IntegrationTests/src/bkr/inttest/server/test_beakerd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@

import datetime
import os
import threading
import shutil
import logging
import pkg_resources
from mock import patch
import bkr

from bkr.server.model import TaskStatus, Job, System, User, \
Group, SystemStatus, SystemActivity, Recipe, Cpu, LabController, \
Expand All @@ -20,7 +18,6 @@
TaskResult, Command, CommandStatus, GroupMembershipType, \
RecipeVirtStatus, Arch
from bkr.server.installopts import InstallOptions
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql import not_
from turbogears import config
from turbogears.database import session, get_engine
Expand All @@ -32,9 +29,10 @@
from bkr.server.jobs import Jobs
from bkr.server import dynamic_virt
from bkr.server.model import OSMajor
from bkr.server.model.installation import RenderedKickstart
from bkr.inttest.assertions import assert_datetime_within
from unittest import SkipTest
import six


log = logging.getLogger(__name__)

Expand Down Expand Up @@ -2071,15 +2069,15 @@ def test_cleanup_openstack(self):
try:
self.virt_manager.novaclient.servers.get(recipe.resource.instance_id)
self.fail('should raise')
except Exception, e:
except Exception as e:
self.assertIn('Instance %s could not be found' % recipe.resource.instance_id,
e.message)
if self.virt_manager.is_create_floating_ip:
# the network should be deleted
try:
self.virt_manager.neutronclient.show_network(recipe.resource.network_id)
self.fail('should raise')
except Exception, e:
except Exception as e:
# neutronclient on RHEL7+ raise NetworkNotFoundClient for missing nets
if hasattr(e, 'status_code'):
self.assertEquals(e.status_code, 404)
Expand All @@ -2089,7 +2087,7 @@ def test_cleanup_openstack(self):
try:
self.virt_manager.neutronclient.show_subnet(recipe.resource.subnet_id)
self.fail('should raise')
except Exception, e:
except Exception as e:
if hasattr(e, 'status_code'):
self.assertEquals(e.status_code, 404)
else:
Expand All @@ -2098,7 +2096,7 @@ def test_cleanup_openstack(self):
try:
self.virt_manager.neutronclient.show_router(recipe.resource.router_id)
self.fail('should raise')
except Exception, e:
except Exception as e:
if hasattr(e, 'status_code'):
self.assertEquals(e.status_code, 404)
else:
Expand Down Expand Up @@ -2132,7 +2130,7 @@ def setUp(self):
for recipe in running:
recipe.recipeset.cancel()
recipe.recipeset.job.update_status()
except Exception, e:
except Exception:
session.rollback()
raise

Expand Down Expand Up @@ -2175,7 +2173,7 @@ def test_system_count_metrics(self, mock_metrics):
status=SystemStatus.removed)
session.flush()
beakerd.system_count_metrics()
for name, value in expected.iteritems():
for name, value in six.iteritems(expected):
mock_metrics.measure.assert_any_call(name, value)

def test_system_count_metrics_uses_active_access_policy(self, mock_metrics):
Expand Down Expand Up @@ -2233,7 +2231,7 @@ def test_recipe_count_metrics(self, mock_metrics):
expected['gauges.recipes_new.by_arch.%s' % arch] += 1
session.flush()
beakerd.recipe_count_metrics()
for name, value in expected.iteritems():
for name, value in six.iteritems(expected):
mock_metrics.measure.assert_any_call(name, value)
# Processing the recipes should set their virt status correctly
mock_metrics.reset_mock()
Expand All @@ -2250,7 +2248,7 @@ def test_recipe_count_metrics(self, mock_metrics):
recipe.recipeset.job.update_status()
session.flush()
beakerd.recipe_count_metrics()
for name, value in expected.iteritems():
for name, value in six.iteritems(expected):
mock_metrics.measure.assert_any_call(name, value)

def test_dirty_job_metrics(self, mock_metrics):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
RecipeTaskResult, Command, CommandStatus, LogRecipeTaskResult, DataMigration, Job,
SystemSchedulerStatus, Permission, Installation, Arch
)
import six


def has_initial_sublist(larger, prefix):
Expand Down Expand Up @@ -383,7 +384,7 @@ def check_migrated_schema(self):
if table_name == 'command_queue':
# may be left over from 22
actual_indexes = dict((name, cols) for name, cols
in actual_indexes.iteritems()
in six.iteritems(actual_indexes)
if cols != ['distro_tree_id'])
if table_name == 'virt_resource':
if 'ix_virt_resource_mac_address' in actual_indexes:
Expand Down Expand Up @@ -455,15 +456,15 @@ def find_expected_indexes_for_table(self, table):
if has_initial_sublist(table.primary_key.columns.values(),
constraint.columns.values()):
continue
name = constraint.name or constraint.columns.values()[0].name
name = constraint.name or list(constraint.columns.values())[0].name
expected_indexes[name] = cols
# Similarly, if we have defined a foreign key without an
# explicit index, InnoDB creates one implicitly.
for fk in table.foreign_keys:
if any(index_cols[0] == fk.parent.name
for index_cols in expected_indexes.values()):
continue
if table.primary_key.columns.values()[0] == fk.parent:
if list(table.primary_key.columns.values())[0] == fk.parent:
continue
expected_indexes[fk.name or fk.parent.name] = [fk.parent.name]
return expected_indexes
Expand Down
3 changes: 0 additions & 3 deletions IntegrationTests/src/bkr/inttest/server/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@

import datetime
import lxml.etree
import os.path
import pkg_resources
from turbogears import testutil
from turbogears.database import session
from bkr.server.bexceptions import BX
from bkr.inttest import data_setup, with_transaction, DatabaseTestCase, get_server_base
from bkr.server.model import TaskPackage


class TestJobsController(DatabaseTestCase):

Expand Down
4 changes: 2 additions & 2 deletions IntegrationTests/src/bkr/inttest/server/test_kickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
import sys
import tempfile
import unittest
from itertools import izip_longest

import lxml.etree
import pkg_resources
import six

from bkr.inttest import data_setup, get_server_base, Process
from bkr.inttest.kickstart_helpers import (
Expand Down Expand Up @@ -4331,5 +4331,5 @@ def test_user_defined_kickstart(self):
''' % '\n'.join(payload), self.system)
ks = recipe.installation.rendered_kickstart.kickstart
ks_lines = ks.splitlines()
for actual, expected in izip_longest(payload, ks_lines):
for actual, expected in six.moves.zip_longest(payload, ks_lines):
self.assert_(actual == expected, ks)
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

import pkg_resources
from turbogears.database import session
from bkr.server.model import TaskStatus, RecipeSet, LabController, System
from bkr.server.bexceptions import BX
from bkr.inttest import data_setup, DatabaseTestCase

class TestLabController(DatabaseTestCase):
Expand Down
2 changes: 1 addition & 1 deletion IntegrationTests/src/bkr/inttest/server/test_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def test_actions(self):
try:
bkr.server.mail.group_membership_notify(member, group, owner, 'Unchanged')
self.fail('Must fail or die')
except ValueError, e:
except ValueError as e:
self.assert_('Unknown action' in str(e))

# https://bugzilla.redhat.com/show_bug.cgi?id=1136748
Expand Down
Loading

0 comments on commit 456cc79

Please sign in to comment.