Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions tests/bexio/unit-test/run
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,7 @@ class TestEveryWrapperTargetsItsEndpoint(BexioTestBase):
self.fetch_json.return_value = (True, [])
func(API_TOKEN)
self.assertTrue(
self.sent_url().startswith(
f'{bexio.BEXIO_API_BASE_URL}{path}?'
),
self.sent_url().startswith(f'{bexio.BEXIO_API_BASE_URL}{path}?'),
self.sent_url(),
)

Expand All @@ -479,9 +477,7 @@ class TestEveryWrapperTargetsItsEndpoint(BexioTestBase):
with self.subTest(func=func.__name__):
self.fetch_json.reset_mock()
self.fetch_json.return_value = (False, 'HTTP 401: invalid token')
self.assertEqual(
func(API_TOKEN), (False, 'HTTP 401: invalid token')
)
self.assertEqual(func(API_TOKEN), (False, 'HTTP 401: invalid token'))

def test_every_creator_posts_its_payload(self):
for func, path in self.CREATORS:
Expand All @@ -490,9 +486,7 @@ class TestEveryWrapperTargetsItsEndpoint(BexioTestBase):
self.fetch_json.return_value = (True, {'id': 42})
payload = {'name': 'linuxfabrik'}
func(API_TOKEN, data=payload)
self.assertEqual(
self.sent_url(), f'{bexio.BEXIO_API_BASE_URL}{path}'
)
self.assertEqual(self.sent_url(), f'{bexio.BEXIO_API_BASE_URL}{path}')
self.assertEqual(self.sent_kwargs()['data'], payload)
self.assertIsNone(self.sent_kwargs()['method'])

Expand All @@ -502,13 +496,13 @@ class TestEveryWrapperTargetsItsEndpoint(BexioTestBase):
self.fetch_json.reset_mock()
self.fetch_json.return_value = (True, {'id': 7})
func(API_TOKEN, 7, {'name': 'linuxfabrik'})
self.assertEqual(
self.sent_url(), f'{bexio.BEXIO_API_BASE_URL}{path}/7'
)
self.assertEqual(self.sent_url(), f'{bexio.BEXIO_API_BASE_URL}{path}/7')

def test_every_wrapper_is_in_the_table(self):
# a wrapper added later has to be added here too, or this fails
covered = {func.__name__ for func, _ in self.READERS + self.CREATORS + self.EDITORS}
covered = {
func.__name__ for func, _ in self.READERS + self.CREATORS + self.EDITORS
}
covered |= {'call_api', 'get_all', 'delete_contact_relation'}
public = {
name
Expand Down
5 changes: 4 additions & 1 deletion tests/db_mysql/unit-test/run
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ class TestLod2Dict(unittest.TestCase):
class TestCheckPrivileges(unittest.TestCase):
"""The grant parsing, driven through a mocked select(), no server needed."""

GRANTS: typing.ClassVar = [{'Grants for u@h': "GRANT SELECT, PROCESS ON *.* TO `u`@`h`"}]
GRANTS: typing.ClassVar = [
{'Grants for u@h': 'GRANT SELECT, PROCESS ON *.* TO `u`@`h`'}
]

def _with_grants(self, grants):
return mock.patch.object(db_mysql, 'select', return_value=(True, grants))
Expand Down Expand Up @@ -209,6 +211,7 @@ class TestCheckPrivileges(unittest.TestCase):
self.assertFalse(success)
self.assertEqual(message, 'gone away')


class TestGetFlavor(unittest.TestCase):
def test_reports_the_flavor_of_the_server_info(self):
with mock.patch.object(
Expand Down
13 changes: 7 additions & 6 deletions tests/huawei_pacific/unit-test/run
Original file line number Diff line number Diff line change
Expand Up @@ -731,9 +731,12 @@ class TestGetClusterNodes(unittest.TestCase):
return {'data': nodes, 'result': {'code': 0}}

def _nodes(self, nodes):
with mock.patch.object(
huawei_pacific, 'get_data', return_value=self._envelope(nodes)
), mock.patch.object(huawei_pacific, '_assert_all_nodes_listed'):
with (
mock.patch.object(
huawei_pacific, 'get_data', return_value=self._envelope(nodes)
),
mock.patch.object(huawei_pacific, '_assert_all_nodes_listed'),
):
return huawei_pacific.get_cluster_nodes(Args())

def test_keeps_a_node_in_the_cluster_and_one_that_says_nothing(self):
Expand Down Expand Up @@ -805,9 +808,7 @@ class TestPureHelpers(unittest.TestCase):
self.assertEqual(huawei_pacific.get_password_status(99), 'Unknown')

def test_warranty_status(self):
self.assertEqual(
huawei_pacific.get_warranty_status(0), 'non-storage node (0)'
)
self.assertEqual(huawei_pacific.get_warranty_status(0), 'non-storage node (0)')
self.assertEqual(
huawei_pacific.get_warranty_status(1), 'normal, more than six months (1)'
)
Expand Down
4 changes: 1 addition & 3 deletions tests/infomaniak/unit-test/run
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,7 @@ class TestGetEvents(InfomaniakTestCase):
def test_proxy_forwarded(self):
m = self.patch_fetch(ok(EVENTS_SUCCESS))
infomaniak.get_events('TOK', proxy='http://proxy.example.com:3128')
self.assertEqual(
m.call_args.kwargs['proxy'], 'http://proxy.example.com:3128'
)
self.assertEqual(m.call_args.kwargs['proxy'], 'http://proxy.example.com:3128')

def test_proxy_defaults_to_none(self):
m = self.patch_fetch(ok(EVENTS_SUCCESS))
Expand Down
4 changes: 3 additions & 1 deletion tests/keycloak/unit-test/run
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,9 @@ class TestGetServerInfoSection(unittest.TestCase):

def test_returns_the_section(self):
self.assertEqual(
keycloak.get_server_info_section({'memoryInfo': {'total': 1}}, 'memoryInfo'),
keycloak.get_server_info_section(
{'memoryInfo': {'total': 1}}, 'memoryInfo'
),
(True, {'total': 1}),
)

Expand Down
24 changes: 15 additions & 9 deletions tests/kvm/unit-test/run
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class TestParseDomstats(unittest.TestCase):
self.assertIn('db 01', kvm.parse_domstats(DOMSTATS))

def test_digits_become_int_everything_else_stays_text(self):
stats = kvm.parse_domstats(DOMSTATS + " block.0.name=vda\n")
stats = kvm.parse_domstats(DOMSTATS + ' block.0.name=vda\n')
self.assertEqual(stats['db 01']['balloon.current'], 1048576)
self.assertEqual(stats['db 01']['block.0.name'], 'vda')

Expand Down Expand Up @@ -148,8 +148,10 @@ class TestGroupByStore(unittest.TestCase):

class TestVirsh(unittest.TestCase):
def test_connects_read_only_to_the_uri(self):
with mock.patch.object(kvm.shell, 'which', return_value='/usr/bin/virsh'), \
_virsh(stdout='ok') as exec_mock:
with (
mock.patch.object(kvm.shell, 'which', return_value='/usr/bin/virsh'),
_virsh(stdout='ok') as exec_mock,
):
success, stdout = kvm.virsh(['list', '--all'], uri='qemu:///system')
self.assertTrue(success)
self.assertEqual(stdout, 'ok')
Expand All @@ -164,17 +166,21 @@ class TestVirsh(unittest.TestCase):
self.assertIn('libvirt-client', message)

def test_a_non_zero_return_code_is_explained(self):
with mock.patch.object(kvm.shell, 'which', return_value='/usr/bin/virsh'), \
_virsh(stderr='error: failed to connect to the hypervisor', retc=1):
with (
mock.patch.object(kvm.shell, 'which', return_value='/usr/bin/virsh'),
_virsh(stderr='error: failed to connect to the hypervisor', retc=1),
):
success, message = kvm.virsh(['list'])
self.assertFalse(success)
self.assertNotEqual(message, '')

def test_a_failed_call_is_passed_on(self):
with mock.patch.object(kvm.shell, 'which', return_value='/usr/bin/virsh'), \
mock.patch.object(
kvm.shell, 'shell_exec', return_value=(False, 'timed out')
):
with (
mock.patch.object(kvm.shell, 'which', return_value='/usr/bin/virsh'),
mock.patch.object(
kvm.shell, 'shell_exec', return_value=(False, 'timed out')
),
):
self.assertEqual(kvm.virsh(['list']), (False, 'timed out'))


Expand Down
4 changes: 1 addition & 3 deletions tests/librenms/unit-test/run
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,7 @@ class TestGetDataRequest(LibrenmsTestBase):
PROXY = 'http://proxy.example.com:3128'

librenms.get_data(A(), uri='/x')
self.assertEqual(
self.sent_kwargs()['proxy'], 'http://proxy.example.com:3128'
)
self.assertEqual(self.sent_kwargs()['proxy'], 'http://proxy.example.com:3128')

def test_proxy_defaults_to_none(self):
librenms.get_data(Args(), uri='/x')
Expand Down
46 changes: 30 additions & 16 deletions tests/net/unit-test/run
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,9 @@ class TestFetch(unittest.TestCase):
self.assertEqual(sock.sent, [b'EHLO x\r\n'])

def test_dialog_reports_a_connection_closed_before_the_pattern(self):
success, message = self._fetch(FakeSocket([b'220 ready\n']),
dialog=[(None, '250')])
success, message = self._fetch(
FakeSocket([b'220 ready\n']), dialog=[(None, '250')]
)
self.assertFalse(success)
self.assertIn('closed before pattern', message)

Expand Down Expand Up @@ -411,12 +412,14 @@ class TestGetNetinfo(unittest.TestCase):
self.assertEqual(net.get_netinfo(), [])

def test_reports_the_address_of_the_default_route(self):
with mock.patch.object(net, 'HAVE_PSUTIL', True), \
mock.patch.object(net, '_default_route_ip', return_value='192.0.2.10'), \
mock.patch.object(
net, '_iface_for_ip', return_value=('eth0', '255.255.255.0')
), \
mock.patch.object(net, '_default_gateway', return_value='192.0.2.1'):
with (
mock.patch.object(net, 'HAVE_PSUTIL', True),
mock.patch.object(net, '_default_route_ip', return_value='192.0.2.10'),
mock.patch.object(
net, '_iface_for_ip', return_value=('eth0', '255.255.255.0')
),
mock.patch.object(net, '_default_gateway', return_value='192.0.2.1'),
):
netinfo = net.get_netinfo()
self.assertEqual(netinfo['address'], '192.0.2.10')
self.assertEqual(netinfo['mask'], '255.255.255.0')
Expand All @@ -426,8 +429,10 @@ class TestGetNetinfo(unittest.TestCase):
self.assertIsNone(netinfo['public_address'])

def test_without_a_default_route_it_returns_an_empty_list(self):
with mock.patch.object(net, 'HAVE_PSUTIL', True), \
mock.patch.object(net, '_default_route_ip', return_value=None):
with (
mock.patch.object(net, 'HAVE_PSUTIL', True),
mock.patch.object(net, '_default_route_ip', return_value=None),
):
self.assertEqual(net.get_netinfo(), [])


Expand All @@ -441,7 +446,9 @@ class TestGetSubnetHosts(unittest.TestCase):
return have, module

def test_lists_the_hosts_of_the_interface_subnet(self):
addr = mock.Mock(family=net.AF_INET, address='192.0.2.1', netmask='255.255.255.0')
addr = mock.Mock(
family=net.AF_INET, address='192.0.2.1', netmask='255.255.255.0'
)
have, module = self._interfaces({'eth0': [addr]})
with have, module:
success, hosts = net.get_subnet_hosts('eth0')
Expand All @@ -451,21 +458,27 @@ class TestGetSubnetHosts(unittest.TestCase):

def test_without_an_interface_it_asks_get_netinfo(self):
netinfo = {'address': '192.0.2.1', 'mask_cidr': 24}
with mock.patch.object(net, 'HAVE_PSUTIL', True), \
mock.patch.object(net, 'get_netinfo', return_value=netinfo):
with (
mock.patch.object(net, 'HAVE_PSUTIL', True),
mock.patch.object(net, 'get_netinfo', return_value=netinfo),
):
success, hosts = net.get_subnet_hosts()
self.assertTrue(success, hosts)
self.assertEqual(len(hosts), 254)

def test_without_a_default_route_it_says_so(self):
with mock.patch.object(net, 'HAVE_PSUTIL', True), \
mock.patch.object(net, 'get_netinfo', return_value=[]):
with (
mock.patch.object(net, 'HAVE_PSUTIL', True),
mock.patch.object(net, 'get_netinfo', return_value=[]),
):
success, message = net.get_subnet_hosts()
self.assertFalse(success)
self.assertIn('default interface', message)

def test_max_hosts_is_honoured(self):
addr = mock.Mock(family=net.AF_INET, address='192.0.2.1', netmask='255.255.255.0')
addr = mock.Mock(
family=net.AF_INET, address='192.0.2.1', netmask='255.255.255.0'
)
have, module = self._interfaces({'eth0': [addr]})
with have, module:
success, message = net.get_subnet_hosts('eth0', max_hosts=10)
Expand All @@ -485,5 +498,6 @@ class TestGetSubnetHosts(unittest.TestCase):
self.assertFalse(success)
self.assertIn('psutil', message)


if __name__ == '__main__':
unittest.main()
4 changes: 1 addition & 3 deletions tests/nodebb/unit-test/run
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,7 @@ class TestGetDataRequestShape(NodebbTestCase):
def test_proxy_forwarded(self):
m = self.patch_fetch(ok(POSTS_SUCCESS))
nodebb.get_data(Args(proxy='http://proxy.example.com:3128'), uri='/api')
self.assertEqual(
m.call_args.kwargs['proxy'], 'http://proxy.example.com:3128'
)
self.assertEqual(m.call_args.kwargs['proxy'], 'http://proxy.example.com:3128')

def test_proxy_defaults_to_none(self):
m = self.patch_fetch(ok(POSTS_SUCCESS))
Expand Down
14 changes: 8 additions & 6 deletions tests/psutil/unit-test/run
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,16 @@ class TestGetProcessAccounts(unittest.TestCase):
links = links or {}
with (
mock.patch.object(
lib_psutil.psutil, 'process_iter',
lib_psutil.psutil,
'process_iter',
return_value=self._procs() if procs is None else list(procs),
),
mock.patch.object(
lib_psutil.user, 'own_mount_namespace', return_value=namespace
),
mock.patch.object(
lib_psutil.os, 'readlink',
lib_psutil.os,
'readlink',
side_effect=lambda path: links.get(path, namespace),
),
):
Expand All @@ -248,8 +250,7 @@ class TestGetProcessAccounts(unittest.TestCase):

def test_a_generator_is_accepted(self):
# The names are consumed more than once, so an iterator has to survive.
self.assertEqual(self._run(n for n in ('httpd',)),
['root', 'apache', 'apache'])
self.assertEqual(self._run(n for n in ('httpd',)), ['root', 'apache', 'apache'])

def test_a_foreign_mount_namespace_is_left_out(self):
# The same program in a container shows up in the host's process list
Expand All @@ -262,8 +263,9 @@ class TestGetProcessAccounts(unittest.TestCase):

def test_without_a_namespace_nothing_is_filtered(self):
# Not Linux: the kernel publishes none, so every process counts.
self.assertEqual(self._run(('httpd',), namespace=None),
['root', 'apache', 'apache'])
self.assertEqual(
self._run(('httpd',), namespace=None), ['root', 'apache', 'apache']
)

def test_a_process_that_vanished_is_skipped(self):
procs = [_Proc('httpd', 10, 'root'), _Vanished('httpd', 11, 'apache')]
Expand Down
18 changes: 12 additions & 6 deletions tests/redfish/unit-test/run
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ class TestGetAuthHeader(unittest.TestCase):
# get_chassis
# ---------------------------------------------------------------------------


class TestSessionLifecycle(unittest.TestCase):
"""The session a login creates must not outlive its usefulness on the controller."""

Expand Down Expand Up @@ -725,7 +726,9 @@ class TestAuthRenewal(unittest.TestCase):

@mock.patch.object(redfish, 'cache')
@mock.patch.object(redfish, 'url')
def test_renewal_replaces_the_token_in_the_caller_s_header(self, url_mock, cache_mock):
def test_renewal_replaces_the_token_in_the_caller_s_header(
self, url_mock, cache_mock
):
# The header dict is the one the caller passes to every fetch, so updating it in
# place is what makes the requests after the retry carry the new token too.
cache_mock.get.return_value = False
Expand Down Expand Up @@ -2314,7 +2317,8 @@ class TestRecordAndReplay(unittest.TestCase):
def test_the_service_root_is_found_with_and_without_a_trailing_slash(self):
redfish.replay('### GET /redfish/v1\n{"Vendor": "Contoso"}')
self.assertEqual(
redfish.fetch_resource('https://bmc/redfish/v1/'), (True, {'Vendor': 'Contoso'})
redfish.fetch_resource('https://bmc/redfish/v1/'),
(True, {'Vendor': 'Contoso'}),
)

def test_the_expand_suffix_is_derived_from_a_replayed_root(self):
Expand Down Expand Up @@ -2375,7 +2379,11 @@ class TestRecordAndReplay(unittest.TestCase):
def test_sensitive_fields_are_redacted(self, url_mock):
url_mock.fetch_json.return_value = (
True,
{'Id': '1', 'Password': 'linuxfabrik', 'Oem': {'Contoso': {'Token': 'x1y2'}}},
{
'Id': '1',
'Password': 'linuxfabrik',
'Oem': {'Contoso': {'Token': 'x1y2'}},
},
)
redfish.fetch_resource('https://bmc/redfish/v1/AccountService/Accounts/1')
text = redfish.format_responses()
Expand Down Expand Up @@ -2609,9 +2617,7 @@ class TestGetUpdateserviceFirmwareInventory(unittest.TestCase):

def test_output_key_set_is_exact(self):
expected = set(redfish.FIRMWARE_KEYS) | set(redfish.FIRMWARE_NESTED_KEYS)
self.assertEqual(
set(redfish.get_updateservice_firmwareinventory({})), expected
)
self.assertEqual(set(redfish.get_updateservice_firmwareinventory({})), expected)


# ---------------------------------------------------------------------------
Expand Down
Loading
Loading