Ejemplo n.º 1
0
    def test_list_block_devices(self, mock_ureport, mock_breport, mock_ereport,
                                mock_isdisk, mock_get_devs):
        # should run blockdev --report command
        # in order to get a list of block devices
        # should call report methods to get device info
        # should call is_disk method to filter out
        # those block devices which are not disks
        mock_get_devs.return_value = ['/dev/fake', '/dev/fake1', '/dev/sr0']

        def isdisk_side_effect(arg, uspec=None, bspec=None):
            if arg == '/dev/fake':
                return True
            elif arg in ('/dev/fake1', '/dev/sr0'):
                return False
        mock_isdisk.side_effect = isdisk_side_effect
        mock_ureport.return_value = {'key0': 'value0'}
        mock_breport.return_value = {'key1': 'value1'}
        mock_ereport.return_value = {'key2': 'value2'}

        expected = [{
            'device': '/dev/fake',
            'uspec': {'key0': 'value0'},
            'bspec': {'key1': 'value1'},
            'espec': {'key2': 'value2'}
        }]
        self.assertEqual(hu.list_block_devices(), expected)
        self.assertEqual(mock_ureport.call_args_list, [mock.call('/dev/fake'),
                         mock.call('/dev/fake1'), mock.call('/dev/sr0')])
        self.assertEqual(mock_breport.call_args_list, [mock.call('/dev/fake'),
                         mock.call('/dev/fake1'), mock.call('/dev/sr0')])
        self.assertEqual(mock_ereport.call_args_list, [mock.call('/dev/fake'),
                         mock.call('/dev/fake1'), mock.call('/dev/sr0')])
Ejemplo n.º 2
0
 def test_list_block_devices_skip_block_device(self, mock_ureport,
                                               mock_breport, mock_ereport,
                                               mock_isdisk, mock_get_devs):
     mock_get_devs.return_value = ['/dev/disk',
                                   '/dev/wrong_disk_skipped',
                                   '/dev/wrong_disk_skipped_too']
     mock_isdisk.return_value = True
     mock_ureport.return_value = {}
     mock_ereport.side_effect = [
         {'removable': '1'},
         errors.ProcessExecutionError,
         KeyError,
     ]
     mock_breport.return_value = {'key1': 'value1'}
     expected = [{
         'device': '/dev/disk',
         'uspec': {},
         'bspec': {'key1': 'value1'},
         'espec': {'removable': '1'},
     }]
     self.assertEqual(hu.list_block_devices(), expected)
     expected_calls = [mock.call('/dev/disk'),
                       mock.call('/dev/wrong_disk_skipped'),
                       mock.call('/dev/wrong_disk_skipped_too')]
     self.assertEqual(expected_calls, mock_ureport.call_args_list)
     mock_breport.assert_called_once_with('/dev/disk')
     self.assertEqual(expected_calls, mock_ereport.call_args_list)
Ejemplo n.º 3
0
def mdcreate(mdname, level, devices, metadata='default'):
    mds = mddisplay()

    # check if md device already exists
    if filter(lambda x: x['name'] == mdname, mds):
        raise errors.MDAlreadyExistsError(
            'Error while creating md: md %s already exists' % mdname)

    # check if level argument is valid
    supported_levels = ('0', '1', 'raid0', 'raid1', 'stripe', 'mirror')
    if level not in supported_levels:
        raise errors.MDWrongSpecError(
            'Error while creating md device: '
            'level must be one of: %s' % ', '.join(supported_levels))

    # check if all necessary devices exist
    if not set(devices).issubset(
            set([bd['device'] for bd in hu.list_block_devices(disks=False)])):
        raise errors.MDNotFoundError(
            'Error while creating md: at least one of devices is not found')

    # check if devices are not parts of some md array
    if set(devices) & \
            set(itertools.chain(*[md.get('devices', []) for md in mds])):
        raise errors.MDDeviceDuplicationError(
            'Error while creating md: at least one of devices is '
            'already in belongs to some md')

    # FIXME: mdadm will ask user to continue creating if any device appears to
    #       be a part of raid array. Superblock zeroing helps to avoid that.
    map(mdclean, devices)
    utils.execute('mdadm', '--create', '--force', mdname, '-e', metadata,
                  '--level=%s' % level,
                  '--raid-devices=%s' % len(devices), *devices,
                  check_exit_code=[0])
Ejemplo n.º 4
0
 def test_list_block_devices_removable_vendors(self, mock_ureport,
                                               mock_breport, mock_ereport,
                                               mock_isdisk, mock_get_devs):
     mock_get_devs.return_value = ['/dev/no_vendor_id',
                                   '/dev/wrong_vendor_id',
                                   '/dev/right_vendor_id']
     mock_isdisk.return_value = True
     mock_ureport.side_effect = [
         {},
         {'ID_VENDOR': 'Cisco'},
         {'ID_VENDOR': 'IBM'},
     ]
     mock_ereport.return_value = {'removable': '1'}
     mock_breport.return_value = {'key1': 'value1'}
     expected = [{
         'device': '/dev/right_vendor_id',
         'uspec': {'ID_VENDOR': 'IBM'},
         'bspec': {'key1': 'value1'},
         'espec': {'removable': '1'}
     }]
     self.assertEqual(hu.list_block_devices(), expected)
     self.assertEqual(
         mock_ureport.call_args_list,
         [mock.call('/dev/no_vendor_id'),
          mock.call('/dev/wrong_vendor_id'),
          mock.call('/dev/right_vendor_id')])
     mock_breport.assert_called_once_with('/dev/right_vendor_id')
     self.assertEqual(
         mock_ereport.call_args_list,
         [mock.call('/dev/no_vendor_id'),
          mock.call('/dev/wrong_vendor_id'),
          mock.call('/dev/right_vendor_id')])
Ejemplo n.º 5
0
 def test_list_block_devices_skip_block_device(self, mock_ureport,
                                               mock_breport, mock_ereport,
                                               mock_isdisk, mock_get_devs):
     mock_get_devs.return_value = ['/dev/disk',
                                   '/dev/wrong_disk_skipped',
                                   '/dev/wrong_disk_skipped_too']
     mock_isdisk.return_value = True
     mock_ureport.return_value = {}
     mock_ereport.side_effect = [
         {'removable': '1'},
         errors.ProcessExecutionError,
         KeyError,
     ]
     mock_breport.return_value = {'key1': 'value1'}
     expected = [{
         'device': '/dev/disk',
         'uspec': {},
         'bspec': {'key1': 'value1'},
         'espec': {'removable': '1'},
     }]
     self.assertEqual(hu.list_block_devices(), expected)
     expected_calls = [mock.call('/dev/disk'),
                       mock.call('/dev/wrong_disk_skipped'),
                       mock.call('/dev/wrong_disk_skipped_too')]
     self.assertEqual(expected_calls, mock_ureport.call_args_list)
     mock_breport.assert_called_once_with('/dev/disk')
     self.assertEqual(expected_calls, mock_ereport.call_args_list)
Ejemplo n.º 6
0
    def test_list_block_devices(self, mock_ureport, mock_breport, mock_ereport,
                                mock_isdisk, mock_get_devs):
        # should run blockdev --report command
        # in order to get a list of block devices
        # should call report methods to get device info
        # should call is_disk method to filter out
        # those block devices which are not disks
        mock_get_devs.return_value = ['/dev/fake', '/dev/fake1', '/dev/sr0']

        def isdisk_side_effect(arg, uspec=None, bspec=None):
            if arg == '/dev/fake':
                return True
            elif arg in ('/dev/fake1', '/dev/sr0'):
                return False
        mock_isdisk.side_effect = isdisk_side_effect
        mock_ureport.return_value = {'key0': 'value0'}
        mock_breport.return_value = {'key1': 'value1'}
        mock_ereport.return_value = {'key2': 'value2'}

        expected = [{
            'device': '/dev/fake',
            'uspec': {'key0': 'value0'},
            'bspec': {'key1': 'value1'},
            'espec': {'key2': 'value2'}
        }]
        self.assertEqual(hu.list_block_devices(), expected)
        self.assertEqual(mock_ureport.call_args_list, [mock.call('/dev/fake'),
                         mock.call('/dev/fake1'), mock.call('/dev/sr0')])
        self.assertEqual(mock_breport.call_args_list, [mock.call('/dev/fake'),
                         mock.call('/dev/fake1'), mock.call('/dev/sr0')])
        self.assertEqual(mock_ereport.call_args_list, [mock.call('/dev/fake'),
                         mock.call('/dev/fake1'), mock.call('/dev/sr0')])
Ejemplo n.º 7
0
    def hu_disks(self):
        """Actual disks which are available on this node

        It is a list of dicts which are formatted other way than
        ks_spaces disks. To match both of those formats use
        _match_device method.
        """
        if not getattr(self, '_hu_disks', None):
            self._hu_disks = hu.list_block_devices(disks=True)
        return self._hu_disks
Ejemplo n.º 8
0
def mdcreate(mdname, level, devices, metadata='default'):
    mds = mddisplay()

    # check if md device already exists
    if next((x for x in mds if x['name'] == mdname), False):
        raise errors.MDAlreadyExistsError(
            'Error while creating md: md %s already exists' % mdname)

    # check if level argument is valid
    supported_levels = ('0', '1', 'raid0', 'raid1', 'stripe', 'mirror')
    if level not in supported_levels:
        raise errors.MDWrongSpecError('Error while creating md device: '
                                      'level must be one of: %s' %
                                      ', '.join(supported_levels))

    # check if all necessary devices exist
    if not set(devices).issubset(
            set([bd['device'] for bd in hu.list_block_devices(disks=False)])):
        raise errors.MDNotFoundError(
            'Error while creating md: at least one of devices is not found')

    # check if devices are not parts of some md array
    if set(devices) & set(
            itertools.chain.from_iterable(md.get('devices', [])
                                          for md in mds)):
        raise errors.MDDeviceDuplicationError(
            'Error while creating md: at least one of devices is '
            'already in belongs to some md')

    # FIXME: mdadm will ask user to continue creating if any device appears to
    #       be a part of raid array. Superblock zeroing helps to avoid that.
    for device in devices:
        mdclean(device)

    utils.execute('mdadm',
                  '--create',
                  '--force',
                  mdname,
                  '-e',
                  metadata,
                  '--level=%s' % level,
                  '--raid-devices=%s' % len(devices),
                  *devices,
                  check_exit_code=[0])
Ejemplo n.º 9
0
def mdcreate(mdname, level, device, *args):
    mds = mddisplay()

    # check if md device already exists
    if filter(lambda x: x['name'] == mdname, mds):
        raise errors.MDAlreadyExistsError(
            'Error while creating md: md %s already exists' % mdname)

    # check if level argument is valid
    supported_levels = ('0', '1', 'raid0', 'raid1', 'stripe', 'mirror')
    if level not in supported_levels:
        raise errors.MDWrongSpecError('Error while creating md device: '
                                      'level must be one of: %s' %
                                      ', '.join(supported_levels))

    devices = [device] + list(args)

    # check if all necessary devices exist
    if not set(devices).issubset(
            set([bd['device'] for bd in hu.list_block_devices(disks=False)])):
        raise errors.MDNotFoundError(
            'Error while creating md: at least one of devices is not found')

    # check if devices are not parts of some md array
    if set(devices) & \
            set(reduce(lambda x, y: x + y,
                       [md.get('devices', []) for md in mds], [])):
        raise errors.MDDeviceDuplicationError(
            'Error while creating md: at least one of devices is '
            'already in belongs to some md')

    # FIXME: mdadm will ask user to continue creating if any device appears to
    #       be a part of raid array. Superblock zeroing helps to avoid that.
    map(mdclean, devices)
    utils.execute('mdadm',
                  '--create',
                  '--force',
                  mdname,
                  '-e0.90',
                  '--level=%s' % level,
                  '--raid-devices=%s' % len(devices),
                  *devices,
                  check_exit_code=[0])
Ejemplo n.º 10
0
 def test_list_block_devices_removable_vendors(self, mock_ureport,
                                               mock_breport, mock_ereport,
                                               mock_isdisk, mock_get_devs):
     mock_get_devs.return_value = [
         '/dev/no_vendor_id', '/dev/wrong_vendor_id', '/dev/right_vendor_id'
     ]
     mock_isdisk.return_value = True
     mock_ureport.side_effect = [
         {},
         {
             'ID_VENDOR': 'Cisco'
         },
         {
             'ID_VENDOR': 'IBM'
         },
     ]
     mock_ereport.return_value = {'removable': '1'}
     mock_breport.return_value = {'key1': 'value1'}
     expected = [{
         'device': '/dev/right_vendor_id',
         'uspec': {
             'ID_VENDOR': 'IBM'
         },
         'bspec': {
             'key1': 'value1'
         },
         'espec': {
             'removable': '1'
         }
     }]
     self.assertEqual(hu.list_block_devices(), expected)
     self.assertEqual(mock_ureport.call_args_list, [
         mock.call('/dev/no_vendor_id'),
         mock.call('/dev/wrong_vendor_id'),
         mock.call('/dev/right_vendor_id')
     ])
     mock_breport.assert_called_once_with('/dev/right_vendor_id')
     self.assertEqual(mock_ereport.call_args_list, [
         mock.call('/dev/no_vendor_id'),
         mock.call('/dev/wrong_vendor_id'),
         mock.call('/dev/right_vendor_id')
     ])