"Fossies" - the Fresh Open Source Software Archive

Member "salt-3002.2/tests/unit/states/test_lvm.py" (18 Nov 2020, 10878 Bytes) of package /linux/misc/salt-3002.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_lvm.py": 3002.1_vs_3002.2.

    1 """
    2     :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
    3 """
    4 
    5 import salt.states.lvm as lvm
    6 from tests.support.mixins import LoaderModuleMockMixin
    7 from tests.support.mock import MagicMock, patch
    8 from tests.support.unit import TestCase
    9 
   10 STUB_LVDISPLAY_LV01 = {
   11     "/dev/testvg01/testlv01": {
   12         "Logical Volume Name": "/dev/testvg01/testlv01",
   13         "Volume Group Name": "testvg01",
   14         "Logical Volume Access": "3",
   15         "Logical Volume Status": "1",
   16         "Internal Logical Volume Number": "-1",
   17         "Open Logical Volumes": "0",
   18         "Logical Volume Size": "4194304",
   19         "Current Logical Extents Associated": "512",
   20         "Allocated Logical Extents": "-1",
   21         "Allocation Policy": "0",
   22         "Read Ahead Sectors": "-1",
   23         "Major Device Number": "253",
   24         "Minor Device Number": "9",
   25     }
   26 }
   27 
   28 STUB_LVDISPLAY_LV02 = {
   29     "/dev/testvg01/testlv02": {
   30         "Logical Volume Name": "/dev/testvg01/testlv02",
   31         "Volume Group Name": "testvg01",
   32         "Logical Volume Access": "3",
   33         "Logical Volume Status": "1",
   34         "Internal Logical Volume Number": "-1",
   35         "Open Logical Volumes": "0",
   36         "Logical Volume Size": "4194304",
   37         "Current Logical Extents Associated": "512",
   38         "Allocated Logical Extents": "-1",
   39         "Allocation Policy": "0",
   40         "Read Ahead Sectors": "-1",
   41         "Major Device Number": "253",
   42         "Minor Device Number": "9",
   43     }
   44 }
   45 
   46 
   47 class LvmTestCase(TestCase, LoaderModuleMockMixin):
   48     """
   49     Test cases for salt.states.lvm
   50     """
   51 
   52     def setup_loader_modules(self):
   53         return {lvm: {}}
   54 
   55     # 'pv_present' function tests: 1
   56 
   57     def test_pv_present(self):
   58         """
   59         Test to set a physical device to be used as an LVM physical volume
   60         """
   61         name = "/dev/sda5"
   62 
   63         comt = "Physical Volume {} already present".format(name)
   64 
   65         ret = {"name": name, "changes": {}, "result": True, "comment": comt}
   66 
   67         mock = MagicMock(side_effect=[True, False])
   68         with patch.dict(lvm.__salt__, {"lvm.pvdisplay": mock}):
   69             self.assertDictEqual(lvm.pv_present(name), ret)
   70 
   71             comt = "Physical Volume {} is set to be created".format(name)
   72             ret.update({"comment": comt, "result": None})
   73             with patch.dict(lvm.__opts__, {"test": True}):
   74                 self.assertDictEqual(lvm.pv_present(name), ret)
   75 
   76     # 'pv_absent' function tests: 1
   77 
   78     def test_pv_absent(self):
   79         """
   80         Test to ensure that a Physical Device is not being used by lvm
   81         """
   82         name = "/dev/sda5"
   83 
   84         comt = "Physical Volume {} does not exist".format(name)
   85 
   86         ret = {"name": name, "changes": {}, "result": True, "comment": comt}
   87 
   88         mock = MagicMock(side_effect=[False, True])
   89         with patch.dict(lvm.__salt__, {"lvm.pvdisplay": mock}):
   90             self.assertDictEqual(lvm.pv_absent(name), ret)
   91 
   92             comt = "Physical Volume {} is set to be removed".format(name)
   93             ret.update({"comment": comt, "result": None})
   94             with patch.dict(lvm.__opts__, {"test": True}):
   95                 self.assertDictEqual(lvm.pv_absent(name), ret)
   96 
   97     # 'vg_present' function tests: 1
   98 
   99     def test_vg_present(self):
  100         """
  101         Test to create an LVM volume group
  102         """
  103         name = "testvg00"
  104 
  105         comt = "Failed to create Volume Group {}".format(name)
  106 
  107         ret = {"name": name, "changes": {}, "result": False, "comment": comt}
  108 
  109         mock = MagicMock(return_value=False)
  110         with patch.dict(lvm.__salt__, {"lvm.vgdisplay": mock, "lvm.vgcreate": mock}):
  111             with patch.dict(lvm.__opts__, {"test": False}):
  112                 self.assertDictEqual(lvm.vg_present(name), ret)
  113 
  114             comt = "Volume Group {} is set to be created".format(name)
  115             ret.update({"comment": comt, "result": None})
  116             with patch.dict(lvm.__opts__, {"test": True}):
  117                 self.assertDictEqual(lvm.vg_present(name), ret)
  118 
  119     # 'vg_absent' function tests: 1
  120 
  121     def test_vg_absent(self):
  122         """
  123         Test to remove an LVM volume group
  124         """
  125         name = "testvg00"
  126 
  127         comt = "Volume Group {} already absent".format(name)
  128 
  129         ret = {"name": name, "changes": {}, "result": True, "comment": comt}
  130 
  131         mock = MagicMock(side_effect=[False, True])
  132         with patch.dict(lvm.__salt__, {"lvm.vgdisplay": mock}):
  133             self.assertDictEqual(lvm.vg_absent(name), ret)
  134 
  135             comt = "Volume Group {} is set to be removed".format(name)
  136             ret.update({"comment": comt, "result": None})
  137             with patch.dict(lvm.__opts__, {"test": True}):
  138                 self.assertDictEqual(lvm.vg_absent(name), ret)
  139 
  140     # 'lv_present' function tests: 6
  141 
  142     def test_lv_present(self):
  143         """
  144         Test to create a new logical volume
  145         """
  146         name = "testlv01"
  147         vgname = "testvg01"
  148         comt = "Logical Volume {} already present".format(name)
  149         ret = {"name": name, "changes": {}, "result": True, "comment": comt}
  150 
  151         mock = MagicMock(return_value=STUB_LVDISPLAY_LV01)
  152         with patch.dict(lvm.__salt__, {"lvm.lvdisplay": mock}):
  153             self.assertDictEqual(lvm.lv_present(name, vgname=vgname), ret)
  154 
  155         mock = MagicMock(return_value=STUB_LVDISPLAY_LV02)
  156         with patch.dict(lvm.__salt__, {"lvm.lvdisplay": mock}):
  157             comt = "Logical Volume {} is set to be created".format(name)
  158             ret.update({"comment": comt, "result": None})
  159             with patch.dict(lvm.__opts__, {"test": True}):
  160                 self.assertDictEqual(lvm.lv_present(name, vgname=vgname), ret)
  161 
  162     def test_lv_present_with_percentage_extents(self):
  163         """
  164         Test do create a new logical volume specifying extents as a percentage
  165         """
  166         name = "testlv01"
  167         vgname = "testvg01"
  168         extents = "42%FREE"
  169         comt = "Logical Volume {} already present, {} won't be resized.".format(
  170             name, extents
  171         )
  172         ret = {"name": name, "changes": {}, "result": True, "comment": comt}
  173 
  174         mock = MagicMock(return_value=STUB_LVDISPLAY_LV01)
  175         with patch.dict(lvm.__salt__, {"lvm.lvdisplay": mock}):
  176             self.assertDictEqual(
  177                 lvm.lv_present(name, vgname=vgname, extents=extents), ret
  178             )
  179 
  180         extents = "42%VG"
  181         mock = MagicMock(return_value=STUB_LVDISPLAY_LV02)
  182         with patch.dict(lvm.__salt__, {"lvm.lvdisplay": mock}):
  183             comt = "Logical Volume {} is set to be created".format(name)
  184             ret.update({"comment": comt, "result": None})
  185             with patch.dict(lvm.__opts__, {"test": True}):
  186                 self.assertDictEqual(
  187                     lvm.lv_present(name, vgname=vgname, extents=extents), ret
  188                 )
  189 
  190     def test_lv_present_with_force(self):
  191         """
  192         Test to create a new logical volume with force=True
  193         """
  194         name = "testlv01"
  195         vgname = "testvg01"
  196         comt = "Logical Volume {} already present".format(name)
  197         ret = {"name": name, "changes": {}, "result": True, "comment": comt}
  198 
  199         mock = MagicMock(return_value=STUB_LVDISPLAY_LV01)
  200         with patch.dict(lvm.__salt__, {"lvm.lvdisplay": mock}):
  201             self.assertDictEqual(lvm.lv_present(name, vgname=vgname, force=True), ret)
  202 
  203         mock = MagicMock(return_value=STUB_LVDISPLAY_LV02)
  204         with patch.dict(lvm.__salt__, {"lvm.lvdisplay": mock}):
  205             comt = "Logical Volume {} is set to be created".format(name)
  206             ret.update({"comment": comt, "result": None})
  207             with patch.dict(lvm.__opts__, {"test": True}):
  208                 self.assertDictEqual(
  209                     lvm.lv_present(name, vgname=vgname, force=True), ret
  210                 )
  211 
  212     def test_lv_present_with_same_size(self):
  213         """
  214         Test to specify the same volume size as parameter
  215         """
  216         name = "testlv01"
  217         vgname = "testvg01"
  218         comt = "Logical Volume {} already present".format(name)
  219         ret = {"name": name, "changes": {}, "result": True, "comment": comt}
  220 
  221         mock = MagicMock(return_value=STUB_LVDISPLAY_LV01)
  222         with patch.dict(lvm.__salt__, {"lvm.lvdisplay": mock}):
  223             self.assertDictEqual(lvm.lv_present(name, vgname=vgname, size="2G"), ret)
  224 
  225     def test_lv_present_with_increase(self):
  226         """
  227         Test to increase a logical volume
  228         """
  229         name = "testlv01"
  230         vgname = "testvg01"
  231         comt = "Logical Volume {} is set to be resized".format(name)
  232         ret = {"name": name, "changes": {}, "result": None, "comment": comt}
  233 
  234         mock = MagicMock(return_value=STUB_LVDISPLAY_LV01)
  235         with patch.dict(lvm.__salt__, {"lvm.lvdisplay": mock}):
  236             with patch.dict(lvm.__opts__, {"test": True}):
  237                 self.assertDictEqual(
  238                     lvm.lv_present(name, vgname=vgname, size="10G"), ret
  239                 )
  240 
  241     def test_lv_present_with_reduce_without_force(self):
  242         """
  243         Test to reduce a logical volume
  244         """
  245         name = "testlv01"
  246         vgname = "testvg01"
  247         comt = "To reduce a Logical Volume option 'force' must be True."
  248         ret = {"name": name, "changes": {}, "result": False, "comment": comt}
  249 
  250         mock = MagicMock(return_value=STUB_LVDISPLAY_LV01)
  251         with patch.dict(lvm.__salt__, {"lvm.lvdisplay": mock}):
  252             self.assertDictEqual(lvm.lv_present(name, vgname=vgname, size="1G"), ret)
  253 
  254     def test_lv_present_with_reduce_with_force(self):
  255         """
  256         Test to reduce a logical volume
  257         """
  258         name = "testlv01"
  259         vgname = "testvg01"
  260         comt = "Logical Volume {} is set to be resized".format(name)
  261         ret = {"name": name, "changes": {}, "result": None, "comment": comt}
  262 
  263         mock = MagicMock(return_value=STUB_LVDISPLAY_LV01)
  264         with patch.dict(lvm.__salt__, {"lvm.lvdisplay": mock}):
  265             with patch.dict(lvm.__opts__, {"test": True}):
  266                 self.assertDictEqual(
  267                     lvm.lv_present(name, vgname=vgname, size="1G", force=True), ret
  268                 )
  269 
  270     # 'lv_absent' function tests: 1
  271 
  272     def test_lv_absent(self):
  273         """
  274         Test to remove a given existing logical volume
  275         from a named existing volume group
  276         """
  277         name = "testlv00"
  278 
  279         comt = "Logical Volume {} already absent".format(name)
  280 
  281         ret = {"name": name, "changes": {}, "result": True, "comment": comt}
  282 
  283         mock = MagicMock(side_effect=[False, True])
  284         with patch.dict(lvm.__salt__, {"lvm.lvdisplay": mock}):
  285             self.assertDictEqual(lvm.lv_absent(name), ret)
  286 
  287             comt = "Logical Volume {} is set to be removed".format(name)
  288             ret.update({"comment": comt, "result": None})
  289             with patch.dict(lvm.__opts__, {"test": True}):
  290                 self.assertDictEqual(lvm.lv_absent(name), ret)