Skip to content

Commit

Permalink
ut
Browse files Browse the repository at this point in the history
  • Loading branch information
liangxin1300 committed Oct 9, 2021
1 parent 6694b89 commit 8333a4a
Showing 1 changed file with 130 additions and 1 deletion.
131 changes: 130 additions & 1 deletion test/unittests/test_report_core.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,87 @@
import os
import pytest
import sys
import unittest
from unittest import mock
from inspect import isfunction

from crmsh import utils as crmutils
from crmsh.config import report
from crmsh.config import report, path
from crmsh.report import collect, core, const, utils


class TestContext(unittest.TestCase):
"""
Unitary tests for crmsh.report.core.Context
"""

@classmethod
def setUpClass(cls):
"""
Global setUp.
"""

def setUp(self):
"""
Test setUp.
"""
self.context_inst = core.Context()

def tearDown(self):
"""
Test tearDown.
"""

@classmethod
def tearDownClass(cls):
"""
Global tearDown.
"""

@mock.patch('json.dumps')
def test_str(self, mock_dumps):
mock_dumps.return_value = "context str"
assert str(core.context) == mock_dumps.return_value
mock_dumps.assert_called_once_with(core.context.__dict__)

@mock.patch('json.dumps')
def test_dumps(self, mock_dumps):
mock_dumps.return_value = "context str"
assert core.context.dumps() == mock_dumps.return_value
mock_dumps.assert_called_once_with(core.context.__dict__, indent=2)

@mock.patch('crmsh.report.core.sanitize.load_sanitize_rule')
@mock.patch('crmsh.report.core.Context.get_cores_dir')
@mock.patch('crmsh.report.core.Context.get_ha_varlib')
@mock.patch('crmsh.report.core.Context.get_dir_from_crm_config_path')
@mock.patch('crmsh.report.utils.now')
@mock.patch('crmsh.report.utils.parse_to_timestamp')
def test_load_values(self, mock_parse, mock_now, mock_getdir, mock_get_havarlib, mock_get_coredir, mock_load_sanitize):
mock_parse.side_effect = [123, 456]
mock_now.return_value = "now date"
self.context_inst.load_values()

@mock.patch('os.path.isdir')
def test_get_dir_from_crm_config_path_exception(self, mock_isdir):
mock_isdir.return_value = False
with pytest.raises(utils.CRMReportError) as err:
self.context_inst.get_dir_from_crm_config_path()
assert str(err.value) == "Cannot find ocf_root directory from crmsh.config.path"
mock_isdir.assert_called_once_with(path.ocf_root)

@mock.patch('os.path.isdir')
def test_get_dir_from_crm_config_path(self, mock_isdir):
mock_isdir.side_effect = [True, True, True]
self.context_inst.get_dir_from_crm_config_path()
assert self.context_inst.ocf_root == path.ocf_root
assert self.context_inst.pe_state_dir == path.pe_state_dir
assert self.context_inst.crm_config == path.crm_config

@mock.patch('os.path.exists')
def test_get_ha_varlib_exists_exception(self, mock_exists):
mock_exists


@mock.patch('crmsh.report.core.getmembers')
def test_generate_collect_functions(mock_getmembers):
mock_getmembers.return_value = [("test1", None), ("collect_func1", None), ("collect_func2", None)]
Expand Down Expand Up @@ -697,3 +770,59 @@ def test_dump_logset_good_multifiles(mock_arch_logs, mock_print, mock_log, mock_
])
mock_str2file.assert_called_once_with("data1\ndata2\ndata3", mock_work_path.return_value)
mock_debug.assert_called_once_with('Dump logset "%s" into %s', ["file1", "file2", "file3"], mock_dest_path.return_value)


@mock.patch('os.path.isdir')
def test_validate_dest_exist_dir_exception(mock_isdir):
core.context.dest = "report"
core.context.no_compress = True
mock_isdir.return_value = True
core.context.rm_exist_dest = False
with pytest.raises(utils.CRMReportError) as err:
core.validate_dest()
assert str(err.value) == 'Destination directory "report" exists, please cleanup or use -Z option'


@mock.patch('os.path.dirname')
@mock.patch('os.path.isdir')
def test_validate_dest_not_dir_exception(mock_isdir, mock_dirname):
core.context.dest = "dir/report"
mock_isdir.side_effect = [False, False]
mock_dirname.return_value = "dir"
with pytest.raises(utils.CRMReportError) as err:
core.validate_dest()
assert str(err.value) == "\"dir\" isn't a directory"


@mock.patch('crmsh.report.core.crmutils.is_filename_sane')
@mock.patch('os.path.basename')
@mock.patch('os.path.dirname')
@mock.patch('os.path.isdir')
def test_validate_dest_invalid_name_exception(mock_isdir, mock_dirname, mock_basename, mock_sane_name):
core.context.dest = "dir/report"
mock_isdir.side_effect = [False, True]
mock_basename.return_value = "report"
mock_dirname.return_value = "dir"
mock_sane_name.return_value = False
with pytest.raises(utils.CRMReportError) as err:
core.validate_dest()
assert str(err.value) == "\"report\" is invalid file name"


@mock.patch('crmsh.report.core.crmutils.is_filename_sane')
@mock.patch('os.path.basename')
@mock.patch('os.path.dirname')
@mock.patch('shutil.rmtree')
@mock.patch('os.path.isdir')
def test_validate_dest(mock_isdir, mock_rmtree, mock_dirname, mock_basename, mock_sane_name):
core.context.dest = "dir/report"
core.context.no_compress = True
core.context.rm_exist_dest = True
mock_isdir.side_effect = [True, True]
mock_basename.return_value = "report"
mock_dirname.return_value = "dir"
mock_sane_name.return_value = True
core.validate_dest()
assert core.context.dest == "report"
assert core.context.dest_dir == "dir"
mock_rmtree.assert_called_once_with("dir/report")

0 comments on commit 8333a4a

Please sign in to comment.