def test_format_instance_bdm_while_attaching_volume(self): # NOTE(ft): Since instance bdm is deleted immediately by detach # operation call, no test for 'detaching' stage is required vol = self.cloud.create_volume(self.context, size=1) kwargs = { 'image_id': 'ami-1', 'instance_type': CONF.default_flavor, 'max_count': 1 } ec2_instance_id = self._run_instance(**kwargs) # NOTE(ft): Since fake attach action is very fast, we replace it to # empty function to check EC2 API results at 'attaching' stage self.stubs.Set(fake_volume.API, 'attach', lambda *args, **kwargs: None) self.cloud.attach_volume(self.context, vol['volumeId'], ec2_instance_id, '/dev/sde') resp = self.cloud.describe_instances(self.context, instance_id=['ami-1']) resp = resp['reservationSet'][0] self.assertEqual(1, len(resp['instancesSet'])) inst = resp['instancesSet'][0] self.assertThat( { 'deviceName': '/dev/sde', 'ebs': { 'deleteOnTermination': False, 'status': 'attaching', 'volumeId': vol['volumeId'] } }, matchers.IsSubDictOf(inst['blockDeviceMapping'][0]))
def test_legacy_source_image(self): for legacy, new in zip(self.legacy_mapping_source_image, self.new_mapping_source_image): if new['destination_type'] == 'volume': self.assertThat(legacy, matchers.IsSubDictOf(new.legacy())) else: self.assertRaises(exception.InvalidBDMForLegacy, new.legacy)
def test_from_api(self): for api, new in zip(self.api_mapping, self.new_mapping): new['connection_info'] = None if new['snapshot_id']: new['volume_id'] = None self.assertThat(block_device.BlockDeviceDict.from_api(api, False), matchers.IsSubDictOf(new))
def test_from_legacy_mapping(self): def _get_image_bdms(bdms): return [bdm for bdm in bdms if bdm['source_type'] == 'image'] def _get_bootable_bdms(bdms): return [bdm for bdm in bdms if bdm['boot_index'] >= 0] new_no_img = block_device.from_legacy_mapping(self.legacy_mapping) self.assertEqual(0, len(_get_image_bdms(new_no_img))) for new, expected in zip(new_no_img, self.new_mapping): self.assertThat(new, matchers.IsSubDictOf(expected)) new_with_img = block_device.from_legacy_mapping( self.legacy_mapping, 'fake_image_ref') image_bdms = _get_image_bdms(new_with_img) boot_bdms = _get_bootable_bdms(new_with_img) self.assertEqual(1, len(image_bdms)) self.assertEqual(1, len(boot_bdms)) self.assertEqual(0, image_bdms[0]['boot_index']) self.assertEqual('image', boot_bdms[0]['source_type']) new_with_img_and_root = block_device.from_legacy_mapping( self.legacy_mapping, 'fake_image_ref', 'sda1') image_bdms = _get_image_bdms(new_with_img_and_root) boot_bdms = _get_bootable_bdms(new_with_img_and_root) self.assertEqual(0, len(image_bdms)) self.assertEqual(1, len(boot_bdms)) self.assertEqual(0, boot_bdms[0]['boot_index']) self.assertEqual('volume', boot_bdms[0]['source_type']) new_no_root = block_device.from_legacy_mapping( self.legacy_mapping, 'fake_image_ref', 'sda1', no_root=True) self.assertEqual(0, len(_get_image_bdms(new_no_root))) self.assertEqual(0, len(_get_bootable_bdms(new_no_root)))
def test_volume_status_of_attaching_volume(self): """Test the volume's status in response when attaching a volume.""" vol1 = self.cloud.create_volume(self.context, size=1, name='test-ls', description='test volume ls') self.assertEqual('available', vol1['status']) kwargs = { 'image_id': 'ami-1', 'instance_type': CONF.default_flavor, 'max_count': 1 } ec2_instance_id = self._run_instance(**kwargs) # NOTE(ft): Since fake attach action is very fast, we replace it to # empty function to check EC2 API results at 'attaching' stage self.stubs.Set(fake_volume.API, 'attach', lambda *args, **kwargs: None) resp = self.cloud.attach_volume(self.context, vol1['volumeId'], ec2_instance_id, '/dev/sde') self.assertEqual('attaching', resp['status']) resp = self.cloud.describe_volumes(self.context, [vol1['volumeId']]) volume = resp['volumeSet'][0] self.assertEqual('in-use', volume['status']) self.assertThat({ 'status': 'attaching', 'volumeId': vol1['volumeId'] }, matchers.IsSubDictOf(volume['attachmentSet'][0]))
def _assertEqualBlockDeviceMapping(self, expected, result): self.assertEqual(len(expected), len(result)) for x in expected: found = False for y in result: if x['deviceName'] == y['deviceName']: self.assertThat(x, matchers.IsSubDictOf(y)) found = True break self.assertTrue(found)
def test_format_instance_bdm(self): (instances, volumes) = self._setUpBlockDeviceMapping() result = {} self.cloud._format_instance_bdm(self.context, instances[0]['uuid'], '/dev/sdb1', result) self.assertThat( {'rootDeviceType': self._expected_instance_bdm0['rootDeviceType']}, matchers.IsSubDictOf(result)) self._assertEqualBlockDeviceMapping( self._expected_block_device_mapping0, result['blockDeviceMapping']) result = {} self.cloud._format_instance_bdm(self.context, instances[1]['uuid'], '/dev/sdc1', result) self.assertThat( {'rootDeviceType': self._expected_instance_bdm1['rootDeviceType']}, matchers.IsSubDictOf(result)) self._tearDownBlockDeviceMapping(instances, volumes)
def test_describe_instances_bdm(self): """Make sure describe_instances works with root_device_name and block device mappings """ (instances, volumes) = self._setUpBlockDeviceMapping() result = self._assertInstance(instances[0]['id']) self.assertThat(self._expected_instance_bdm0, matchers.IsSubDictOf(result)) self._assertEqualBlockDeviceMapping( self._expected_block_device_mapping0, result['blockDeviceMapping']) result = self._assertInstance(instances[1]['id']) self.assertThat(self._expected_instance_bdm1, matchers.IsSubDictOf(result)) result = self._assertInstance(instances[2]['id']) self.assertThat(self._expected_instance_bdm2, matchers.IsSubDictOf(result)) self._tearDownBlockDeviceMapping(instances, volumes)
class TestIsSubDictOf(testtools.TestCase, helpers.TestMatchersInterface): matches_matcher = matchers.IsSubDictOf( OrderedDict(sorted({'foo': 'bar', 'baz': 'DONTCARE', 'cat': {'tabby': True, 'fluffy': False}}.items())) ) matches_matches = [ {'foo': 'bar', 'baz': 'noox', 'cat': {'tabby': True, 'fluffy': False}}, {'foo': 'bar', 'baz': 'quux'} ] matches_mismatches = [ {'foo': 'bop', 'baz': 'qux', 'cat': {'tabby': True, 'fluffy': False}}, {'foo': 'bar', 'baz': 'quux', 'cat': {'tabby': True, 'fluffy': True}}, {'foo': 'bar', 'cat': {'tabby': True, 'fluffy': False}, 'dog': None}, ] str_examples = [ ("IsSubDictOf({0})".format( str(OrderedDict(sorted({'foo': 'bar', 'baz': 'DONTCARE', 'cat': {'tabby': True, 'fluffy': False}}.items())))), matches_matcher), ] describe_examples = [ ("Dictionaries do not match at fluffy. d1: False d2: True", {'foo': 'bar', 'baz': 'quux', 'cat': {'tabby': True, 'fluffy': True}}, matches_matcher), ("Dictionaries do not match at foo. d1: bar d2: bop", {'foo': 'bop', 'baz': 'quux', 'cat': {'tabby': True, 'fluffy': False}}, matches_matcher), ]
def test_legacy_mapping_source_image(self): got_legacy = block_device.legacy_mapping(self.new_mapping) for legacy, expected in zip(got_legacy, self.legacy_mapping): self.assertThat(expected, matchers.IsSubDictOf(legacy))
def test_legacy(self): for legacy, new in zip(self.legacy_mapping, self.new_mapping): self.assertThat(legacy, matchers.IsSubDictOf(new.legacy()))
def test_from_legacy(self): for legacy, new in zip(self.legacy_mapping, self.new_mapping): self.assertThat(block_device.BlockDeviceDict.from_legacy(legacy), matchers.IsSubDictOf(new))