Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

import unittest 

import testlib 

import lvmlib 

import mock 

 

import os 

import lvutil 

 

 

ONE_MEGABYTE=1*1024*1024 

 

 

def with_lvm_subsystem(func): 

    @testlib.with_context 

    def decorated(self, context, *args, **kwargs): 

        lvsystem = lvmlib.LVSubsystem(context.log, context.add_executable) 

        return func(self, lvsystem, *args, **kwargs) 

 

    decorated.__name__ = func.__name__ 

    return decorated 

 

 

class TestCreate(unittest.TestCase): 

    @with_lvm_subsystem 

    def test_create_volume_size(self, lvsystem): 

        lvsystem.add_volume_group('VG_XenStorage-b3b18d06-b2ba-5b67-f098-3cdd5087a2a7') 

 

        lvutil.create('volume', 100 * ONE_MEGABYTE, 'VG_XenStorage-b3b18d06-b2ba-5b67-f098-3cdd5087a2a7') 

 

        created_lv, = lvsystem.get_logical_volumes_with_name('volume') 

 

        self.assertEquals(100, created_lv.size_mb) 

 

    @with_lvm_subsystem 

    def test_create_volume_is_in_the_right_volume_group(self, lvsystem): 

        lvsystem.add_volume_group('VG_XenStorage-b3b18d06-b2ba-5b67-f098-3cdd5087a2a7') 

 

        lvutil.create('volume', 100 * ONE_MEGABYTE, 'VG_XenStorage-b3b18d06-b2ba-5b67-f098-3cdd5087a2a7') 

 

        created_lv, = lvsystem.get_logical_volumes_with_name('volume') 

 

        self.assertEquals(100, created_lv.size_mb) 

 

        self.assertEquals('VG_XenStorage-b3b18d06-b2ba-5b67-f098-3cdd5087a2a7', created_lv.volume_group.name) 

        self.assertTrue(created_lv.active) 

        self.assertTrue(created_lv.zeroed) 

 

    @with_lvm_subsystem 

    def test_create_volume_is_active(self, lvsystem): 

        lvsystem.add_volume_group('VG_XenStorage-b3b18d06-b2ba-5b67-f098-3cdd5087a2a7') 

 

        lvutil.create('volume', 100 * ONE_MEGABYTE, 'VG_XenStorage-b3b18d06-b2ba-5b67-f098-3cdd5087a2a7') 

 

        created_lv, = lvsystem.get_logical_volumes_with_name('volume') 

 

        self.assertEquals(100, created_lv.size_mb) 

 

        self.assertTrue(created_lv.active) 

        self.assertTrue(created_lv.zeroed) 

 

    @with_lvm_subsystem 

    def test_create_volume_is_zeroed(self, lvsystem): 

        lvsystem.add_volume_group('VG_XenStorage-b3b18d06-b2ba-5b67-f098-3cdd5087a2a7') 

 

        lvutil.create('volume', 100 * ONE_MEGABYTE, 'VG_XenStorage-b3b18d06-b2ba-5b67-f098-3cdd5087a2a7') 

 

        created_lv, = lvsystem.get_logical_volumes_with_name('volume') 

 

        self.assertEquals(100, created_lv.size_mb) 

 

        self.assertTrue(created_lv.zeroed) 

 

    @with_lvm_subsystem 

    def test_create_creates_logical_volume_with_tags(self, lvsystem): 

        lvsystem.add_volume_group('VG_XenStorage-b3b18d06-b2ba-5b67-f098-3cdd5087a2a7') 

 

        lvutil.create('volume', ONE_MEGABYTE, 'VG_XenStorage-b3b18d06-b2ba-5b67-f098-3cdd5087a2a7', tag='hello') 

 

        created_lv, = lvsystem.get_logical_volumes_with_name('volume') 

        self.assertEquals('hello', created_lv.tag) 

 

    @mock.patch('util.pread', autospec=True) 

    def test_create_percentage_has_precedence_over_size(self, mock_pread): 

        lvutil.create('volume', ONE_MEGABYTE, 'VG_XenStorage-b3b18d06-b2ba-5b67-f098-3cdd5087a2a7', 

                      size_in_percentage="10%F") 

 

        self.assertEqual(1, mock_pread.call_count) 

        self.assertIn("10%F", mock_pread.call_args[0][0]) 

 

class TestRemove(unittest.TestCase): 

    @with_lvm_subsystem 

    def test_remove_removes_volume(self, lvsystem): 

        lvsystem.add_volume_group('VG_XenStorage-b3b18d06-b2ba-5b67-f098-3cdd5087a2a7') 

        lvsystem.get_volume_group('VG_XenStorage-b3b18d06-b2ba-5b67-f098-3cdd5087a2a7').add_volume('volume', 100) 

 

        lvutil.remove('VG_XenStorage-b3b18d06-b2ba-5b67-f098-3cdd5087a2a7/volume') 

 

        self.assertEquals([], lvsystem.get_logical_volumes_with_name('volume')) 

 

    @mock.patch('lvutil._lvmBugCleanup', autospec=True) 

    @mock.patch('util.pread', autospec=True) 

    def test_remove_additional_config_param(self, mock_pread, _bugCleanup): 

        lvutil.remove('VG_XenStorage-b3b18d06-b2ba-5b67-f098-3cdd5087a2a7/volume', config_param="blah") 

        mock_pread.assert_called_once_with( 

            [os.path.join(lvutil.LVM_BIN, lvutil.CMD_LVREMOVE)] 

            + "-f VG_XenStorage-b3b18d06-b2ba-5b67-f098-3cdd5087a2a7/volume --config devices{blah}".split(), 

           quiet= False)