Skip to content

Commit

Permalink
pylintrc: re-enable too-many-lines, part 5 (#949)
Browse files Browse the repository at this point in the history
To get under the 1000 line limit, move host_helpers
test classes into unique files under
tests/unit/host_helpers/.
  • Loading branch information
brianphaley authored Jul 17, 2024
1 parent 187ec43 commit 9c99d2a
Show file tree
Hide file tree
Showing 12 changed files with 1,081 additions and 1,032 deletions.
Empty file.
37 changes: 37 additions & 0 deletions tests/unit/host_helpers/test_apparmor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

from hotsos.core.host_helpers import apparmor as host_apparmor

from .. import utils


class TestApparmorHelper(utils.BaseTestCase):
""" Unit tests for apparmor helper """
def test_aa_status_profiles(self):
helper = host_apparmor.ApparmorHelper()
profiles = helper.profiles
num_profiles = 2
self.assertEqual(profiles['enforce']['count'], 253)
self.assertEqual(len(profiles['enforce']['profiles']), 253)
self.assertEqual(profiles['enforce']['profiles'][-1], 'virt-aa-helper')
self.assertEqual(helper.profiles_enforce,
profiles['enforce']['profiles'])
self.assertEqual(profiles['complain']['count'], 0)
self.assertEqual(len(profiles['complain']['profiles']), 0)
self.assertEqual(helper.profiles_complain, [])
if 'kill' in profiles:
num_profiles += 1
self.assertEqual(profiles['kill']['count'], 0)
self.assertEqual(len(profiles['kill']['profiles']), 0)
self.assertEqual(helper.profiles_kill, [])
if 'unconfined' in profiles:
num_profiles += 1
self.assertEqual(profiles['unconfined']['count'], 0)
self.assertEqual(len(profiles['unconfined']['profiles']), 0)
self.assertEqual(helper.profiles_unconfined, [])
self.assertEqual(len(profiles), num_profiles)

def test_aa_profile_factory(self):
profile = getattr(host_apparmor.AAProfileFactory(),
'virt-aa-helper')
self.assertEqual(profile.name, 'virt-aa-helper')
self.assertEqual(profile.mode, 'enforce')
153 changes: 153 additions & 0 deletions tests/unit/host_helpers/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import os
import subprocess
from unittest import mock

from hotsos.core.config import HotSOSConfig
from hotsos.core.host_helpers import cli as host_cli

from .. import utils


class TestCLIHelper(utils.BaseTestCase):
"""
NOTE: remember that a data_root is configured so helpers will always
use fake_data_root if possible. If you write a test that wants to
test a scenario where no data root is set (i.e. no sosreport) you need
to unset it as part of the test.
"""

def test_journalctl(self):
HotSOSConfig.use_all_logs = False
HotSOSConfig.max_logrotate_depth = 7
self.assertEqual(host_cli.JournalctlBase().since_date,
"2022-02-09")
HotSOSConfig.use_all_logs = True
self.assertEqual(host_cli.JournalctlBase().since_date,
"2022-02-03")
HotSOSConfig.max_logrotate_depth = 1000
self.assertEqual(host_cli.JournalctlBase().since_date,
"2019-05-17")

def test_ns_ip_addr(self):
ns = "qrouter-984c22fd-64b3-4fa1-8ddd-87090f401ce5"
out = host_cli.CLIHelper().ns_ip_addr(namespace=ns)
self.assertIsInstance(out, list)
self.assertEqual(len(out), 18)

def test_udevadm_info_dev(self):
out = host_cli.CLIHelper().udevadm_info_dev(device='/dev/vdb')
self.assertEqual(out, [])

@mock.patch.object(host_cli, 'subprocess')
def test_ps(self, mock_subprocess):
path = os.path.join(HotSOSConfig.data_root, "ps")
with open(path, 'r', encoding='utf-8') as fd:
out = fd.readlines()

self.assertEqual(host_cli.CLIHelper().ps(), out)
self.assertFalse(mock_subprocess.called)

def test_get_date_local(self):
HotSOSConfig.data_root = '/'
self.assertEqual(type(host_cli.CLIHelper().date()), str)

def test_get_date(self):
self.assertEqual(host_cli.CLIHelper().date(), '1644509957')

@utils.create_data_root({'sos_commands/date/date':
'Thu Mar 25 10:55:05 2021'})
def test_get_date_no_tz(self):
self.assertEqual(host_cli.CLIHelper().date(), '1616669705')

@utils.create_data_root({'sos_commands/date/date':
'Thu Mar 25 10:55:05 -03 2021'})
def test_get_date_w_numeric_tz(self):
self.assertEqual(host_cli.CLIHelper().date(), '1616680505')

@utils.create_data_root({'sos_commands/date/date':
'Thu Mar 25 10:55:05 UTC 2021'})
def test_get_date_w_tz(self):
self.assertEqual(host_cli.CLIHelper().date(), '1616669705')

@utils.create_data_root({'sos_commands/date/date':
'Thu Mar 25 10:55:05 123UTC 2021'})
def test_get_date_w_invalid_tz(self):
with self.assertLogs(logger='hotsos', level='ERROR') as log:
self.assertEqual(host_cli.CLIHelper().date(), "")
# If invalid date, log.error() will have been called
self.assertEqual(len(log.output), 1)
self.assertIn('has invalid date string', log.output[0])

def test_ovs_ofctl_bin_w_errors(self):

def fake_run(cmd, *_args, **_kwargs):
if 'OpenFlow13' in cmd:
m = mock.MagicMock()
m.returncode = 0
m.stdout = 'testdata'.encode(encoding='utf_8', errors='strict')
m.stderr = ''
return m

raise subprocess.CalledProcessError(1, 'ofctl')

HotSOSConfig.data_root = '/'
with mock.patch.object(host_cli.subprocess, 'run') as \
mock_run:
mock_run.side_effect = fake_run

# Test errors with eventual success
helper = host_cli.CLIHelper()
self.assertEqual(helper.ovs_ofctl(command='show', args='br-int'),
['testdata'])

mock_run.side_effect = \
subprocess.CalledProcessError(1, 'ofctl')

# Ensure that if all fails the result is always iterable
helper = host_cli.CLIHelper()
self.assertEqual(helper.ovs_ofctl(command='show', args='br-int'),
[])

@mock.patch.object(host_cli.CLIHelper, 'command_catalog',
{'sleep': [host_cli.BinCmd('time sleep 2')]})
def test_cli_timeout(self):
cli = host_cli.CLIHelper()
orig_cfg = HotSOSConfig.CONFIG
try:
# ensure bin command executed
HotSOSConfig.data_root = '/'
HotSOSConfig.command_timeout = 1
out = cli.sleep()
# a returned [] implies an exception was raised and caught
self.assertEqual(out, [])
finally:
# restore
HotSOSConfig.set(**orig_cfg)

@mock.patch.object(host_cli.CLIHelper, 'command_catalog',
{'sleep': [host_cli.BinCmd('time sleep 1')]})
def test_cli_no_timeout(self):
cli = host_cli.CLIHelper()
orig_cfg = HotSOSConfig.CONFIG
try:
# ensure bin command executed
HotSOSConfig.data_root = '/'
out = cli.sleep()
self.assertEqual(len(out), 2)
finally:
# restore
HotSOSConfig.set(**orig_cfg)

def test_clitempfile(self):
with host_cli.CLIHelperFile() as cli:
self.assertEqual(os.path.basename(cli.date()), 'date')

with host_cli.CLIHelperFile() as cli:
orig_cfg = HotSOSConfig.CONFIG
try:
# ensure bin command executed
HotSOSConfig.data_root = '/'
self.assertEqual(cli.date(), cli.output_file)
finally:
# restore
HotSOSConfig.set(**orig_cfg)
47 changes: 47 additions & 0 deletions tests/unit/host_helpers/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import os

from hotsos.core.config import HotSOSConfig
from hotsos.core.host_helpers import config as host_config

from .. import utils

DUMMY_CONFIG = """
[a-section]
a-key = 1023
b-key = 10-23
c-key = 2-8,10-31
"""


class TestConfigHelper(utils.BaseTestCase):
""" Unit tests for config helper """
@utils.create_data_root({'test.conf': DUMMY_CONFIG})
def test_iniconfig_base(self):
conf = os.path.join(HotSOSConfig.data_root, 'test.conf')
cfg = host_config.IniConfigBase(conf)
self.assertTrue(cfg.exists)
self.assertEqual(cfg.get('a-key'), '1023')
self.assertEqual(cfg.get('a-key', section='a-section'), '1023')
self.assertIsNone(cfg.get('a-key', section='missing-section'))
self.assertEqual(cfg.get('a-key', expand_to_list=True), [1023])

expanded = cfg.get('b-key', expand_to_list=True)
self.assertEqual(expanded, list(range(10, 24)))
self.assertEqual(cfg.squash_int_range(expanded), '10-23')

expanded = cfg.get('c-key', expand_to_list=True)
self.assertEqual(expanded, list(range(2, 9)) + list(range(10, 32)))
self.assertEqual(cfg.squash_int_range(expanded), '2-8,10-31')

@utils.create_data_root({'test.conf': DUMMY_CONFIG})
def test_squash_int_range(self):
self.assertEqual(host_config.ConfigBase.squash_int_range([]), '')
expanded = list(range(2, 9))
self.assertEqual(host_config.ConfigBase.squash_int_range(expanded),
'2-8')
expanded = list(range(2, 9)) + list(range(10, 32))
self.assertEqual(host_config.ConfigBase.squash_int_range(expanded),
'2-8,10-31')
expanded = list(range(2, 9)) + [10] + list(range(12, 32))
self.assertEqual(host_config.ConfigBase.squash_int_range(expanded),
'2-8,10,12-31')
18 changes: 18 additions & 0 deletions tests/unit/host_helpers/test_filestat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os

from hotsos.core.config import HotSOSConfig
from hotsos.core.host_helpers import filestat as host_file

from .. import utils


class TestFileStatHelper(utils.BaseTestCase):
""" Unit tests for filesstat helper """
@utils.create_data_root({'foo': 'bar'})
def test_filestat_factory(self):
fpath = os.path.join(HotSOSConfig.data_root, 'foo')
fileobj = host_file.FileFactory().foo
self.assertEqual(fileobj.mtime, os.path.getmtime(fpath))

fileobj = host_file.FileFactory().noexist
self.assertEqual(fileobj.mtime, 0)
Loading

0 comments on commit 9c99d2a

Please sign in to comment.