OpenSCAP /
oscap-anaconda-addon
| 1 | # |
||
| 2 | # Copyright (C) 2013 Red Hat, Inc. |
||
| 3 | # |
||
| 4 | # This copyrighted material is made available to anyone wishing to use, |
||
| 5 | # modify, copy, or redistribute it subject to the terms and conditions of |
||
| 6 | # the GNU General Public License v.2, or (at your option) any later version. |
||
| 7 | # This program is distributed in the hope that it will be useful, but WITHOUT |
||
| 8 | # ANY WARRANTY expressed or implied, including the implied warranties of |
||
| 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
||
| 10 | # Public License for more details. You should have received a copy of the |
||
| 11 | # GNU General Public License along with this program; if not, write to the |
||
| 12 | # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
||
| 13 | # 02110-1301, USA. Any Red Hat trademarks that are incorporated in the |
||
| 14 | # source code or documentation are not subject to the GNU General Public |
||
| 15 | # License and may only be used or replicated with the express permission of |
||
| 16 | # Red Hat, Inc. |
||
| 17 | # |
||
| 18 | # Red Hat Author(s): Vratislav Podzimek <[email protected]> |
||
| 19 | # |
||
| 20 | |||
| 21 | |||
| 22 | from unittest import mock |
||
| 23 | import os |
||
| 24 | from collections import namedtuple |
||
| 25 | |||
| 26 | import pytest |
||
| 27 | |||
| 28 | from org_fedora_oscap import utils |
||
| 29 | |||
| 30 | |||
| 31 | @pytest.fixture() |
||
| 32 | def mock_os(): |
||
| 33 | mock_os = mock.Mock() |
||
| 34 | mock_os.makedirs = mock.Mock() |
||
| 35 | mock_os.path = mock.Mock() |
||
| 36 | mock_os.path.isdir = mock.Mock() |
||
| 37 | return mock_os |
||
| 38 | |||
| 39 | |||
| 40 | def mock_utils_os(mock_os, monkeypatch): |
||
| 41 | utils_module_symbols = utils.__dict__ |
||
| 42 | |||
| 43 | monkeypatch.setitem(utils_module_symbols, "os", mock_os) |
||
| 44 | |||
| 45 | |||
| 46 | def test_existing_dir(mock_os, monkeypatch): |
||
| 47 | mock_utils_os(mock_os, monkeypatch) |
||
| 48 | mock_os.path.isdir.return_value = True |
||
| 49 | |||
| 50 | utils.ensure_dir_exists("/tmp/test_dir") |
||
| 51 | |||
| 52 | mock_os.path.isdir.assert_called_with("/tmp/test_dir") |
||
| 53 | assert not mock_os.makedirs.called |
||
| 54 | |||
| 55 | |||
| 56 | def test_nonexisting_dir(mock_os, monkeypatch): |
||
| 57 | mock_utils_os(mock_os, monkeypatch) |
||
| 58 | mock_os.path.isdir.return_value = False |
||
| 59 | |||
| 60 | utils.ensure_dir_exists("/tmp/test_dir") |
||
| 61 | |||
| 62 | mock_os.path.isdir.assert_called_with("/tmp/test_dir") |
||
| 63 | mock_os.makedirs.assert_called_with("/tmp/test_dir") |
||
| 64 | |||
| 65 | |||
| 66 | def test_no_dir(mock_os, monkeypatch): |
||
| 67 | mock_utils_os(mock_os, monkeypatch) |
||
| 68 | # shouldn't raise an exception |
||
| 69 | utils.ensure_dir_exists("") |
||
| 70 | |||
| 71 | |||
| 72 | def test_relative_relative(): |
||
| 73 | assert utils.join_paths("foo", "blah") == "foo/blah" |
||
| 74 | |||
| 75 | |||
| 76 | def test_relative_absolute(): |
||
| 77 | assert utils.join_paths("foo", "/blah") == "foo/blah" |
||
| 78 | |||
| 79 | |||
| 80 | def test_absolute_relative(): |
||
| 81 | assert utils.join_paths("/foo", "blah") == "/foo/blah" |
||
| 82 | |||
| 83 | |||
| 84 | def test_absolute_absolute(): |
||
| 85 | assert utils.join_paths("/foo", "/blah") == "/foo/blah" |
||
| 86 | |||
| 87 | |||
| 88 | def test_dict(): |
||
| 89 | dct = {"a": 1, "b": 2} |
||
| 90 | |||
| 91 | mapped_dct = utils.keep_type_map(str.upper, dct) |
||
| 92 | assert list(mapped_dct.keys()) == ["A", "B"] |
||
| 93 | assert isinstance(mapped_dct, dict) |
||
| 94 | |||
| 95 | |||
| 96 | def test_list(): |
||
| 97 | lst = [1, 2, 4, 5] |
||
| 98 | |||
| 99 | mapped_lst = utils.keep_type_map(lambda x: x ** 2, lst) |
||
| 100 | assert mapped_lst == [1, 4, 16, 25] |
||
| 101 | assert isinstance(mapped_lst, list) |
||
| 102 | |||
| 103 | |||
| 104 | def test_tuple(): |
||
| 105 | tpl = (1, 2, 4, 5) |
||
| 106 | |||
| 107 | mapped_tpl = utils.keep_type_map(lambda x: x ** 2, tpl) |
||
| 108 | assert mapped_tpl == (1, 4, 16, 25) |
||
| 109 | assert isinstance(mapped_tpl, tuple) |
||
| 110 | |||
| 111 | |||
| 112 | def test_namedtuple(): |
||
| 113 | NT = namedtuple("TestingNT", ["a", "b"]) |
||
| 114 | ntpl = NT(2, 4) |
||
| 115 | |||
| 116 | mapped_tpl = utils.keep_type_map(lambda x: x ** 2, ntpl) |
||
| 117 | assert mapped_tpl == NT(4, 16) |
||
| 118 | assert isinstance(mapped_tpl, tuple) |
||
| 119 | assert isinstance(mapped_tpl, NT) |
||
| 120 | |||
| 121 | |||
| 122 | def test_set(): |
||
| 123 | st = {1, 2, 4, 5} |
||
| 124 | |||
| 125 | mapped_st = utils.keep_type_map(lambda x: x ** 2, st) |
||
| 126 | assert mapped_st == {1, 4, 16, 25} |
||
| 127 | assert isinstance(mapped_st, set) |
||
| 128 | |||
| 129 | |||
| 130 | def test_str(): |
||
| 131 | stri = "abcd" |
||
| 132 | |||
| 133 | mapped_stri = utils.keep_type_map(lambda c: chr((ord(c) + 2) % 256), stri) |
||
| 134 | assert mapped_stri == "cdef" |
||
| 135 | assert isinstance(mapped_stri, str) |
||
| 136 | |||
| 137 | |||
| 138 | def test_gen(): |
||
| 139 | generator = (el for el in (1, 2, 4, 5)) |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 140 | |||
| 141 | mapped_generator = utils.keep_type_map(lambda x: x ** 2, generator) |
||
| 142 | assert tuple(mapped_generator) == (1, 4, 16, 25) |
||
| 143 | |||
| 144 | # any better test for this? |
||
| 145 | assert "__next__" in dir(mapped_generator) |
||
| 146 | |||
| 147 | |||
| 148 | def test_hash(): |
||
| 149 | file_hash = '87fcda7d9e7a22412e95779e2f8e70f929106c7b27a94f5f8510553ebf4624a6' |
||
| 150 | hash_obj = utils.get_hashing_algorithm(file_hash) |
||
| 151 | assert hash_obj.name == "sha256" |
||
| 152 | |||
| 153 | filepath = os.path.join(os.path.dirname(__file__), 'data', 'file') |
||
| 154 | computed_hash = utils.get_file_fingerprint(filepath, hash_obj) |
||
| 155 | |||
| 156 | assert file_hash == computed_hash |
||
| 157 |