|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# vim: ts=4 sw=4 et ai si |
| 3 | +# |
| 4 | +# Copyright (c) 2012-2014 Intel, Inc. |
| 5 | +# License: GPLv2 |
| 6 | +# Author: Artem Bityutskiy <artem.bityutskiy@linux.intel.com> |
| 7 | +# |
| 8 | +# This program is free software; you can redistribute it and/or modify |
| 9 | +# it under the terms of the GNU General Public License, version 2, |
| 10 | +# as published by the Free Software Foundation. |
| 11 | +# |
| 12 | +# This program is distributed in the hope that it will be useful, but |
| 13 | +# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | +# General Public License for more details. |
| 16 | + |
| 17 | +""" |
| 18 | +This test verifies 'BmapHelpers' module functionality. |
| 19 | +""" |
| 20 | + |
| 21 | +import os |
| 22 | +import sys |
| 23 | +import tempfile |
| 24 | +from mock import patch, mock |
| 25 | +from backports import tempfile as btempfile |
| 26 | +from bmaptools import BmapHelpers |
| 27 | + |
| 28 | + |
| 29 | +# This is a work-around for Centos 6 |
| 30 | +try: |
| 31 | + import unittest2 as unittest # pylint: disable=F0401 |
| 32 | +except ImportError: |
| 33 | + import unittest |
| 34 | + |
| 35 | + |
| 36 | +class TestBmapHelpers(unittest.TestCase): |
| 37 | + """The test class for these unit tests.""" |
| 38 | + |
| 39 | + def test_get_file_system_type(self): |
| 40 | + """Check a file system type is returned when used with a file""" |
| 41 | + |
| 42 | + with tempfile.NamedTemporaryFile("r", prefix="testfile_", |
| 43 | + delete=True, dir=".", suffix=".img") as fobj: |
| 44 | + fstype = BmapHelpers.get_file_system_type(fobj.name) |
| 45 | + self.assertTrue(fstype) |
| 46 | + |
| 47 | + def test_get_file_system_type_no_fstype_found(self): |
| 48 | + """Check error raised when supplied file doesnt exist""" |
| 49 | + |
| 50 | + directory = os.path.dirname(__file__) |
| 51 | + fobj = os.path.join(directory, "BmapHelpers/file/does/not/exist") |
| 52 | + with self.assertRaises(BmapHelpers.Error): |
| 53 | + BmapHelpers.get_file_system_type(fobj) |
| 54 | + |
| 55 | + def test_get_file_system_type_symlink(self): |
| 56 | + """Check a file system type is returned when used with a symlink""" |
| 57 | + |
| 58 | + with btempfile.TemporaryDirectory(prefix="testdir_", dir=".") as directory: |
| 59 | + fobj = tempfile.NamedTemporaryFile("r", prefix="testfile_", delete=False, |
| 60 | + dir=directory, suffix=".img") |
| 61 | + lnk = os.path.join(directory, "test_symlink") |
| 62 | + os.symlink(fobj.name, lnk) |
| 63 | + fstype = BmapHelpers.get_file_system_type(lnk) |
| 64 | + self.assertTrue(fstype) |
| 65 | + |
| 66 | + def test_is_zfs_configuration_compatible_enabled(self): |
| 67 | + """Check compatiblilty check is true when zfs param is set correctly""" |
| 68 | + |
| 69 | + with tempfile.NamedTemporaryFile("w+", prefix="testfile_", |
| 70 | + delete=True, dir=".", suffix=".txt") as fobj: |
| 71 | + fobj.write("1") |
| 72 | + fobj.flush() |
| 73 | + mockobj = mock.patch.object(BmapHelpers, "ZFS_COMPAT_PARAM_PATH", fobj.name) |
| 74 | + with mockobj: |
| 75 | + self.assertTrue(BmapHelpers.is_zfs_configuration_compatible()) |
| 76 | + |
| 77 | + |
| 78 | + def test_is_zfs_configuration_compatible_disabled(self): |
| 79 | + """Check compatiblilty check is false when zfs param is set incorrectly""" |
| 80 | + |
| 81 | + with tempfile.NamedTemporaryFile("w+", prefix="testfile_", |
| 82 | + delete=True, dir=".", suffix=".txt") as fobj: |
| 83 | + fobj.write("0") |
| 84 | + fobj.flush() |
| 85 | + mockobj = mock.patch.object(BmapHelpers, "ZFS_COMPAT_PARAM_PATH", fobj.name) |
| 86 | + with mockobj: |
| 87 | + self.assertFalse(BmapHelpers.is_zfs_configuration_compatible()) |
| 88 | + |
| 89 | + def test_is_zfs_configuration_compatible_invalid_read_value(self): |
| 90 | + """Check error raised if any content of zfs config file invalid""" |
| 91 | + |
| 92 | + with tempfile.NamedTemporaryFile("a", prefix="testfile_", |
| 93 | + delete=True, dir=".", suffix=".txt") as fobj: |
| 94 | + mockobj = mock.patch.object(BmapHelpers, "ZFS_COMPAT_PARAM_PATH", fobj.name) |
| 95 | + with self.assertRaises(BmapHelpers.Error): |
| 96 | + with mockobj: |
| 97 | + BmapHelpers.is_zfs_configuration_compatible() |
| 98 | + |
| 99 | + @patch("builtins.open" if sys.version_info[0] >= 3 else "__builtin__.open") |
| 100 | + def test_is_zfs_configuration_compatible_unreadable_file(self, mock_open): |
| 101 | + """Check error raised if any IO errors when checking zfs config file""" |
| 102 | + |
| 103 | + mock_open.side_effect = IOError |
| 104 | + with self.assertRaises(BmapHelpers.Error): |
| 105 | + BmapHelpers.is_zfs_configuration_compatible() |
| 106 | + |
| 107 | + def test_is_zfs_configuration_compatible_notinstalled(self): |
| 108 | + """Check compatiblilty check passes when zfs not installed""" |
| 109 | + |
| 110 | + directory = os.path.dirname(__file__) |
| 111 | + filepath = os.path.join(directory, "BmapHelpers/file/does/not/exist") |
| 112 | + mockobj = mock.patch.object(BmapHelpers, "ZFS_COMPAT_PARAM_PATH", filepath) |
| 113 | + with mockobj: |
| 114 | + self.assertFalse(BmapHelpers.is_zfs_configuration_compatible()) |
| 115 | + |
| 116 | + @patch.object(BmapHelpers, "get_file_system_type", return_value="zfs") |
| 117 | + def test_is_compatible_file_system_zfs_valid(self, mock_get_fs_type): #pylint: disable=unused-argument |
| 118 | + """Check compatiblilty check passes when zfs param is set correctly""" |
| 119 | + |
| 120 | + with tempfile.NamedTemporaryFile("w+", prefix="testfile_", |
| 121 | + delete=True, dir=".", suffix=".img") as fobj: |
| 122 | + fobj.write("1") |
| 123 | + fobj.flush() |
| 124 | + mockobj = mock.patch.object(BmapHelpers, "ZFS_COMPAT_PARAM_PATH", fobj.name) |
| 125 | + with mockobj: |
| 126 | + self.assertTrue(BmapHelpers.is_compatible_file_system(fobj.name)) |
| 127 | + |
| 128 | + @patch.object(BmapHelpers, "get_file_system_type", return_value="zfs") |
| 129 | + def test_is_compatible_file_system_zfs_invalid(self, mock_get_fs_type): #pylint: disable=unused-argument |
| 130 | + """Check compatiblilty check fails when zfs param is set incorrectly""" |
| 131 | + |
| 132 | + with tempfile.NamedTemporaryFile("w+", prefix="testfile_", |
| 133 | + delete=True, dir=".", suffix=".img") as fobj: |
| 134 | + fobj.write("0") |
| 135 | + fobj.flush() |
| 136 | + mockobj = mock.patch.object(BmapHelpers, "ZFS_COMPAT_PARAM_PATH", fobj.name) |
| 137 | + with mockobj: |
| 138 | + self.assertFalse(BmapHelpers.is_compatible_file_system(fobj.name)) |
| 139 | + |
| 140 | + @patch.object(BmapHelpers, "get_file_system_type", return_value="ext4") |
| 141 | + def test_is_compatible_file_system_ext4(self, mock_get_fs_type): #pylint: disable=unused-argument |
| 142 | + """Check non-zfs file systems pass compatiblilty checks""" |
| 143 | + |
| 144 | + with tempfile.NamedTemporaryFile("w+", prefix="testfile_", |
| 145 | + delete=True, dir=".", suffix=".img") as fobj: |
| 146 | + self.assertTrue(BmapHelpers.is_compatible_file_system(fobj.name)) |
0 commit comments