Пример #1
0
 def test_attach_volume_wait(self):
     server = dict(id='server001')
     vol = {'id': 'volume001', 'status': 'available',
            'name': '', 'attachments': []}
     volume = meta.obj_to_munch(fakes.FakeVolume(**vol))
     vol['attachments'] = [{'server_id': server['id'],
                            'device': 'device001'}]
     vol['status'] = 'attached'
     attached_volume = meta.obj_to_munch(fakes.FakeVolume(**vol))
     rattach = {'server_id': server['id'], 'device': 'device001',
                'volumeId': volume['id'], 'id': 'attachmentId'}
     self.register_uris([
         dict(method='POST',
              uri=self.get_mock_url(
                  'compute', 'public',
                  append=['servers', server['id'],
                          'os-volume_attachments']),
              json={'volumeAttachment': rattach},
              validate=dict(json={
                  'volumeAttachment': {
                      'volumeId': vol['id']}})),
         dict(method='GET',
              uri=self.get_mock_url(
                  'volumev2', 'public', append=['volumes', 'detail']),
              json={'volumes': [volume]}),
         dict(method='GET',
              uri=self.get_mock_url(
                  'volumev2', 'public', append=['volumes', 'detail']),
              json={'volumes': [attached_volume]})])
     # defaults to wait=True
     ret = self.cloud.attach_volume(server, volume)
     self.assertEqual(rattach, ret)
     self.assert_calls()
Пример #2
0
    def test_attach_volume_wait_error(self):
        server = dict(id='server001')
        vol = {'id': 'volume001', 'status': 'available',
               'name': '', 'attachments': []}
        volume = meta.obj_to_munch(fakes.FakeVolume(**vol))
        vol['status'] = 'error'
        errored_volume = meta.obj_to_munch(fakes.FakeVolume(**vol))
        rattach = {'server_id': server['id'], 'device': 'device001',
                   'volumeId': volume['id'], 'id': 'attachmentId'}
        self.register_uris([
            dict(method='POST',
                 uri=self.get_mock_url(
                     'compute', 'public',
                     append=['servers', server['id'],
                             'os-volume_attachments']),
                 json={'volumeAttachment': rattach},
                 validate=dict(json={
                     'volumeAttachment': {
                         'volumeId': vol['id']}})),
            dict(method='GET',
                 uri=self.get_mock_url(
                     'volumev2', 'public', append=['volumes', 'detail']),
                 json={'volumes': [errored_volume]})])

        with testtools.ExpectedException(
            openstack.cloud.OpenStackCloudException,
            "Error in attaching volume %s" % errored_volume['id']
        ):
            self.cloud.attach_volume(server, volume)
        self.assert_calls()
Пример #3
0
 def test_list_volumes(self):
     fake_volume = fakes.FakeVolume('volume1', 'available',
                                    'Volume 1 Display Name')
     fake_volume_dict = meta.obj_to_munch(fake_volume)
     fake_volume2 = fakes.FakeVolume('volume2', 'available',
                                     'Volume 2 Display Name')
     fake_volume2_dict = meta.obj_to_munch(fake_volume2)
     self.register_uris([
         dict(method='GET',
              uri=self.get_mock_url(
                  'volumev2', 'public', append=['volumes', 'detail']),
              json={'volumes': [fake_volume_dict]}),
         dict(method='GET',
              uri=self.get_mock_url(
                  'volumev2', 'public', append=['volumes', 'detail']),
              json={'volumes': [fake_volume_dict, fake_volume2_dict]})])
     self.assertEqual(
         [self.cloud._normalize_volume(fake_volume_dict)],
         self.cloud.list_volumes())
     # this call should hit the cache
     self.assertEqual(
         [self.cloud._normalize_volume(fake_volume_dict)],
         self.cloud.list_volumes())
     self.cloud.list_volumes.invalidate(self.cloud)
     self.assertEqual(
         [self.cloud._normalize_volume(fake_volume_dict),
          self.cloud._normalize_volume(fake_volume2_dict)],
         self.cloud.list_volumes())
     self.assert_calls()
Пример #4
0
 def test_detach_volume_wait_error(self):
     server = dict(id='server001')
     attachments = [{'server_id': 'server001', 'device': 'device001'}]
     vol = {'id': 'volume001', 'status': 'attached', 'name': '',
            'attachments': attachments}
     volume = meta.obj_to_munch(fakes.FakeVolume(**vol))
     vol['status'] = 'error'
     vol['attachments'] = []
     errored_volume = meta.obj_to_munch(fakes.FakeVolume(**vol))
     self.register_uris([
         dict(method='DELETE',
              uri=self.get_mock_url(
                  'compute', 'public',
                  append=['servers', server['id'],
                          'os-volume_attachments', volume.id])),
         dict(method='GET',
              uri=self.get_mock_url(
                  'volumev2', 'public', append=['volumes', 'detail']),
              json={'volumes': [errored_volume]})])
     with testtools.ExpectedException(
         openstack.cloud.OpenStackCloudException,
         "Error in detaching volume %s" % errored_volume['id']
     ):
         self.cloud.detach_volume(server, volume)
     self.assert_calls()
Пример #5
0
    def test_list_volumes_with_pagination_next_link_fails_once(self):
        vol1 = meta.obj_to_munch(fakes.FakeVolume('01', 'available', 'vol1'))
        vol2 = meta.obj_to_munch(fakes.FakeVolume('02', 'available', 'vol2'))
        self.register_uris([
            dict(method='GET',
                 uri=self.get_mock_url(
                     'volumev2', 'public',
                     append=['volumes', 'detail']),
                 json={
                     'volumes': [vol1],
                     'volumes_links': [
                         {'href': self.get_mock_url(
                             'volumev2', 'public',
                             append=['volumes', 'detail'],
                             qs_elements=['marker=01']),
                          'rel': 'next'}]}),
            dict(method='GET',
                 uri=self.get_mock_url(
                     'volumev2', 'public',
                     append=['volumes', 'detail'],
                     qs_elements=['marker=01']),
                 status_code=404),
            dict(method='GET',
                 uri=self.get_mock_url(
                     'volumev2', 'public',
                     append=['volumes', 'detail']),
                 json={
                     'volumes': [vol1],
                     'volumes_links': [
                         {'href': self.get_mock_url(
                             'volumev2', 'public',
                             append=['volumes', 'detail'],
                             qs_elements=['marker=01']),
                          'rel': 'next'}]}),
            dict(method='GET',
                 uri=self.get_mock_url(
                     'volumev2', 'public',
                     append=['volumes', 'detail'],
                     qs_elements=['marker=01']),
                 json={
                     'volumes': [vol2],
                     'volumes_links': [
                         {'href': self.get_mock_url(
                             'volumev2', 'public',
                             append=['volumes', 'detail'],
                             qs_elements=['marker=02']),
                          'rel': 'next'}]}),

            dict(method='GET',
                 uri=self.get_mock_url(
                     'volumev2', 'public',
                     append=['volumes', 'detail'],
                     qs_elements=['marker=02']),
                 json={'volumes': []})])
        self.assertEqual(
            [self.cloud._normalize_volume(vol1),
             self.cloud._normalize_volume(vol2)],
            self.cloud.list_volumes())
        self.assert_calls()
Пример #6
0
    def test_has_volume(self):
        mock_cloud = mock.MagicMock()

        fake_volume = fakes.FakeVolume(
            id='volume1',
            status='available',
            name='Volume 1 Display Name',
            attachments=[{'device': '/dev/sda0'}])
        fake_volume_dict = meta.obj_to_munch(fake_volume)
        mock_cloud.get_volumes.return_value = [fake_volume_dict]
        hostvars = meta.get_hostvars_from_server(
            mock_cloud, meta.obj_to_munch(standard_fake_server))
        self.assertEqual('volume1', hostvars['volumes'][0]['id'])
        self.assertEqual('/dev/sda0', hostvars['volumes'][0]['device'])
Пример #7
0
 def test_list_volumes_with_pagination_next_link_fails_all_attempts(self):
     vol1 = meta.obj_to_munch(fakes.FakeVolume('01', 'available', 'vol1'))
     uris = []
     attempts = 5
     for i in range(attempts):
         uris.extend([
             dict(method='GET',
                  uri=self.get_mock_url(
                      'volumev2', 'public',
                      append=['volumes', 'detail']),
                  json={
                      'volumes': [vol1],
                      'volumes_links': [
                          {'href': self.get_mock_url(
                              'volumev2', 'public',
                              append=['volumes', 'detail'],
                              qs_elements=['marker=01']),
                           'rel': 'next'}]}),
             dict(method='GET',
                  uri=self.get_mock_url(
                      'volumev2', 'public',
                      append=['volumes', 'detail'],
                      qs_elements=['marker=01']),
                  status_code=404)])
     self.register_uris(uris)
     # Check that found volumes are returned even if pagination didn't
     # complete because call to get next link 404'ed for all the allowed
     # attempts
     self.assertEqual(
         [self.cloud._normalize_volume(vol1)],
         self.cloud.list_volumes())
     self.assert_calls()
Пример #8
0
    def test_basic_hostvars(
            self, mock_get_server_external_ipv4,
            mock_get_server_external_ipv6):
        mock_get_server_external_ipv4.return_value = PUBLIC_V4
        mock_get_server_external_ipv6.return_value = PUBLIC_V6

        hostvars = meta.get_hostvars_from_server(
            FakeCloud(), self.cloud._normalize_server(
                meta.obj_to_munch(standard_fake_server)))
        self.assertNotIn('links', hostvars)
        self.assertEqual(PRIVATE_V4, hostvars['private_v4'])
        self.assertEqual(PUBLIC_V4, hostvars['public_v4'])
        self.assertEqual(PUBLIC_V6, hostvars['public_v6'])
        self.assertEqual(PUBLIC_V6, hostvars['interface_ip'])
        self.assertEqual('RegionOne', hostvars['region'])
        self.assertEqual('_test_cloud_', hostvars['cloud'])
        self.assertIn('location', hostvars)
        self.assertEqual('_test_cloud_', hostvars['location']['cloud'])
        self.assertEqual('RegionOne', hostvars['location']['region_name'])
        self.assertEqual('admin', hostvars['location']['project']['name'])
        self.assertEqual("test-image-name", hostvars['image']['name'])
        self.assertEqual(
            standard_fake_server['image']['id'], hostvars['image']['id'])
        self.assertNotIn('links', hostvars['image'])
        self.assertEqual(
            standard_fake_server['flavor']['id'], hostvars['flavor']['id'])
        self.assertEqual("test-flavor-name", hostvars['flavor']['name'])
        self.assertNotIn('links', hostvars['flavor'])
        # test having volumes
        # test volume exception
        self.assertEqual([], hostvars['volumes'])
Пример #9
0
    def test_create_bootable_volume(self):
        vol1 = meta.obj_to_munch(fakes.FakeVolume('01', 'available', 'vol1'))
        self.register_uris([
            dict(method='POST',
                 uri=self.get_mock_url(
                     'volumev2', 'public', append=['volumes']),
                 json={'volume': vol1},
                 validate=dict(json={
                     'volume': {
                         'size': 50,
                         'name': 'vol1',
                     }})),
            dict(method='GET',
                 uri=self.get_mock_url(
                     'volumev2', 'public',
                     append=['volumes', 'detail']),
                 json={'volumes': [vol1]}),
            dict(method='POST',
                 uri=self.get_mock_url(
                     'volumev2', 'public',
                     append=['volumes', '01', 'action']),
                 validate=dict(
                     json={'os-set_bootable': {'bootable': True}})),
        ])

        self.cloud.create_volume(50, name='vol1', bootable=True)
        self.assert_calls()
    def test_delete_volume_snapshot_with_timeout(self):
        """
        Test that a timeout while waiting for the volume snapshot to delete
        raises an exception in delete_volume_snapshot.
        """
        fake_snapshot = fakes.FakeVolumeSnapshot('1234', 'available',
                                                 'foo', 'derpysnapshot')
        fake_snapshot_dict = meta.obj_to_munch(fake_snapshot)

        self.register_uris([
            dict(method='GET',
                 uri=self.get_mock_url(
                     'volumev2', 'public',
                     append=['snapshots', 'detail']),
                 json={'snapshots': [fake_snapshot_dict]}),
            dict(method='DELETE',
                 uri=self.get_mock_url(
                     'volumev2', 'public',
                     append=['snapshots', fake_snapshot_dict['id']]))])

        self.assertRaises(
            exc.OpenStackCloudTimeout,
            self.cloud.delete_volume_snapshot, name_or_id='1234',
            wait=True, timeout=0.01)
        self.assert_calls(do_count=False)
Пример #11
0
    def test_private_interface_ip(self, mock_get_server_external_ipv4):
        mock_get_server_external_ipv4.return_value = PUBLIC_V4

        cloud = FakeCloud()
        cloud.private = True
        hostvars = meta.get_hostvars_from_server(
            cloud, meta.obj_to_munch(standard_fake_server))
        self.assertEqual(PRIVATE_V4, hostvars['interface_ip'])
Пример #12
0
    def test_image_string(self, mock_get_server_external_ipv4):
        mock_get_server_external_ipv4.return_value = PUBLIC_V4

        server = standard_fake_server
        server['image'] = 'fake-image-id'
        hostvars = meta.get_hostvars_from_server(
            FakeCloud(), meta.obj_to_munch(server))
        self.assertEqual('fake-image-id', hostvars['image']['id'])
Пример #13
0
 def test_get_volume_backup(self):
     name = 'Volume1'
     vol1 = {'name': name, 'availability_zone': 'az1'}
     vol2 = {'name': name, 'availability_zone': 'az2'}
     vol3 = {'name': 'Volume2', 'availability_zone': 'az1'}
     self.register_uris([
         dict(method='GET',
              uri=self.get_mock_url(
                  'volumev2', 'public', append=['backups', 'detail']),
              json={"backups": [vol1, vol2, vol3]})])
     result = self.cloud.get_volume_backup(
         name, {'availability_zone': 'az1'})
     result = meta.obj_to_munch(result)
     self.assertEqual(
         meta.obj_to_munch(vol1),
         result)
     self.assert_calls()
Пример #14
0
 def test_obj_to_munch_subclass(self):
     class FakeObjDict(dict):
         additional = 1
     obj = FakeObjDict(foo='bar')
     obj_dict = meta.obj_to_munch(obj)
     self.assertIn('additional', obj_dict)
     self.assertIn('foo', obj_dict)
     self.assertEqual(obj_dict['additional'], 1)
     self.assertEqual(obj_dict['foo'], 'bar')
Пример #15
0
def make_fake_port(address, node_id=None, port_id=None):
    if not node_id:
        node_id = uuid.uuid4().hex
    if not port_id:
        port_id = uuid.uuid4().hex
    return meta.obj_to_munch(FakeMachinePort(
        id=port_id,
        address=address,
        node_id=node_id))
Пример #16
0
 def test_obj_to_munch(self):
     cloud = FakeCloud()
     cloud.subcloud = FakeCloud()
     cloud_dict = meta.obj_to_munch(cloud)
     self.assertEqual(FakeCloud.name, cloud_dict['name'])
     self.assertNotIn('_unused', cloud_dict)
     self.assertNotIn('get_flavor_name', cloud_dict)
     self.assertNotIn('subcloud', cloud_dict)
     self.assertTrue(hasattr(cloud_dict, 'name'))
     self.assertEqual(cloud_dict.name, cloud_dict['name'])
    def test_create_boot_attach_volume(self):
        build_server = fakes.make_fake_server('1234', '', 'BUILD')
        active_server = fakes.make_fake_server('1234', '', 'BUILD')

        vol = {'id': 'volume001', 'status': 'available',
               'name': '', 'attachments': []}
        volume = meta.obj_to_munch(fakes.FakeVolume(**vol))

        self.register_uris([
            dict(method='GET',
                 uri=self.get_mock_url(
                     'network', 'public', append=['v2.0', 'networks.json']),
                 json={'networks': []}),
            dict(method='POST',
                 uri=self.get_mock_url(
                     'compute', 'public', append=['os-volumes_boot']),
                 json={'server': build_server},
                 validate=dict(
                     json={'server': {
                         u'flavorRef': 'flavor-id',
                         u'imageRef': 'image-id',
                         u'max_count': 1,
                         u'min_count': 1,
                         u'block_device_mapping_v2': [
                             {
                                 u'boot_index': 0,
                                 u'delete_on_termination': True,
                                 u'destination_type': u'local',
                                 u'source_type': u'image',
                                 u'uuid': u'image-id'
                             },
                             {
                                 u'boot_index': u'-1',
                                 u'delete_on_termination': False,
                                 u'destination_type': u'volume',
                                 u'source_type': u'volume',
                                 u'uuid': u'volume001'
                             }
                         ],
                         u'name': u'server-name'}})),
            dict(method='GET',
                 uri=self.get_mock_url(
                     'compute', 'public', append=['servers', '1234']),
                 json={'server': active_server}),
        ])

        self.cloud.create_server(
            name='server-name',
            image=dict(id='image-id'),
            flavor=dict(id='flavor-id'),
            boot_from_volume=False,
            volumes=[volume],
            wait=False)

        self.assert_calls()
Пример #18
0
    def test_ipv4_hostvars(
            self, mock_get_server_external_ipv4,
            mock_get_server_external_ipv6):
        mock_get_server_external_ipv4.return_value = PUBLIC_V4
        mock_get_server_external_ipv6.return_value = PUBLIC_V6

        fake_cloud = FakeCloud()
        fake_cloud.force_ipv4 = True
        hostvars = meta.get_hostvars_from_server(
            fake_cloud, meta.obj_to_munch(standard_fake_server))
        self.assertEqual(PUBLIC_V4, hostvars['interface_ip'])
Пример #19
0
 def test_detach_volume_wait(self):
     server = dict(id='server001')
     attachments = [{'server_id': 'server001', 'device': 'device001'}]
     vol = {'id': 'volume001', 'status': 'attached', 'name': '',
            'attachments': attachments}
     volume = meta.obj_to_munch(fakes.FakeVolume(**vol))
     vol['status'] = 'available'
     vol['attachments'] = []
     avail_volume = meta.obj_to_munch(fakes.FakeVolume(**vol))
     self.register_uris([
         dict(method='DELETE',
              uri=self.get_mock_url(
                  'compute', 'public',
                  append=['servers', server['id'],
                          'os-volume_attachments', volume.id])),
         dict(method='GET',
              uri=self.get_mock_url(
                  'volumev2', 'public', append=['volumes', 'detail']),
              json={'volumes': [avail_volume]})])
     self.cloud.detach_volume(server, volume)
     self.assert_calls()
Пример #20
0
 def test_get_volume_by_id(self):
     vol1 = meta.obj_to_munch(fakes.FakeVolume('01', 'available', 'vol1'))
     self.register_uris([
         dict(method='GET',
              uri=self.get_mock_url(
                  'volumev2', 'public',
                  append=['volumes', '01']),
              json={'volume': vol1}
              )
     ])
     self.assertEqual(
         self.cloud._normalize_volume(vol1),
         self.cloud.get_volume_by_id('01'))
     self.assert_calls()
Пример #21
0
    def test_unknown_volume_exception(self):
        mock_cloud = mock.MagicMock()

        class FakeException(Exception):
            pass

        def side_effect(*args):
            raise FakeException("No Volumes")
        mock_cloud.get_volumes.side_effect = side_effect
        self.assertRaises(
            FakeException,
            meta.get_hostvars_from_server,
            mock_cloud,
            meta.obj_to_munch(standard_fake_server))
Пример #22
0
 def test_list_volumes_creating_invalidates(self):
     fake_volume = fakes.FakeVolume('volume1', 'creating',
                                    'Volume 1 Display Name')
     fake_volume_dict = meta.obj_to_munch(fake_volume)
     fake_volume2 = fakes.FakeVolume('volume2', 'available',
                                     'Volume 2 Display Name')
     fake_volume2_dict = meta.obj_to_munch(fake_volume2)
     self.register_uris([
         dict(method='GET',
              uri=self.get_mock_url(
                  'volumev2', 'public', append=['volumes', 'detail']),
              json={'volumes': [fake_volume_dict]}),
         dict(method='GET',
              uri=self.get_mock_url(
                  'volumev2', 'public', append=['volumes', 'detail']),
              json={'volumes': [fake_volume_dict, fake_volume2_dict]})])
     self.assertEqual(
         [self.cloud._normalize_volume(fake_volume_dict)],
         self.cloud.list_volumes())
     self.assertEqual(
         [self.cloud._normalize_volume(fake_volume_dict),
          self.cloud._normalize_volume(fake_volume2_dict)],
         self.cloud.list_volumes())
     self.assert_calls()
Пример #23
0
 def test_get_groups_from_server(self):
     server_vars = {
         'flavor': 'test-flavor',
         'image': 'test-image',
         'az': 'test-az'
     }
     self.assertEqual([
         'test-name', 'test-region', 'test-name_test-region', 'test-group',
         'instance-test-id-0', 'meta-group_test-group', 'test-az',
         'test-region_test-az', 'test-name_test-region_test-az'
     ],
                      meta.get_groups_from_server(
                          FakeCloud(),
                          meta.obj_to_munch(standard_fake_server),
                          server_vars))
Пример #24
0
 def test_delete_volume_gone_away(self):
     vol = {'id': 'volume001', 'status': 'attached',
            'name': '', 'attachments': []}
     volume = meta.obj_to_munch(fakes.FakeVolume(**vol))
     self.register_uris([
         dict(method='GET',
              uri=self.get_mock_url(
                  'volumev2', 'public', append=['volumes', 'detail']),
              json={'volumes': [volume]}),
         dict(method='DELETE',
              uri=self.get_mock_url(
                  'volumev2', 'public', append=['volumes', volume.id]),
              status_code=404)])
     self.assertFalse(self.cloud.delete_volume(volume['id']))
     self.assert_calls()
Пример #25
0
    def test_get_security_groups(self, mock_list_server_security_groups):
        '''This test verifies that calling get_hostvars_froms_server
        ultimately calls list_server_security_groups, and that the return
        value from list_server_security_groups ends up in
        server['security_groups'].'''
        mock_list_server_security_groups.return_value = [{
            'name': 'testgroup',
            'id': '1'
        }]

        server = meta.obj_to_munch(standard_fake_server)
        hostvars = meta.get_hostvars_from_server(FakeCloud(), server)

        mock_list_server_security_groups.assert_called_once_with(server)
        self.assertEqual('testgroup', hostvars['security_groups'][0]['name'])
Пример #26
0
    def test_get_security_groups(self,
                                 mock_list_server_security_groups):
        '''This test verifies that calling get_hostvars_froms_server
        ultimately calls list_server_security_groups, and that the return
        value from list_server_security_groups ends up in
        server['security_groups'].'''
        mock_list_server_security_groups.return_value = [
            {'name': 'testgroup', 'id': '1'}]

        server = meta.obj_to_munch(standard_fake_server)
        hostvars = meta.get_hostvars_from_server(FakeCloud(), server)

        mock_list_server_security_groups.assert_called_once_with(server)
        self.assertEqual('testgroup',
                         hostvars['security_groups'][0]['name'])
Пример #27
0
 def test_delete_volume_gone_away(self):
     vol = {'id': 'volume001', 'status': 'attached',
            'name': '', 'attachments': []}
     volume = meta.obj_to_munch(fakes.FakeVolume(**vol))
     self.register_uris([
         self.get_cinder_discovery_mock_dict(),
         dict(method='GET',
              uri=self.get_mock_url(
                  'volumev2', 'public', append=['volumes', 'detail']),
              json={'volumes': [volume]}),
         dict(method='DELETE',
              uri=self.get_mock_url(
                  'volumev2', 'public', append=['volumes', volume.id]),
              status_code=404)])
     self.assertFalse(self.cloud.delete_volume(volume['id']))
     self.assert_calls()
Пример #28
0
 def test_set_volume_bootable_false(self):
     vol = {'id': 'volume001', 'status': 'attached',
            'name': '', 'attachments': []}
     volume = meta.obj_to_munch(fakes.FakeVolume(**vol))
     self.register_uris([
         dict(method='GET',
              uri=self.get_mock_url(
                  'volumev2', 'public', append=['volumes', 'detail']),
              json={'volumes': [volume]}),
         dict(method='POST',
              uri=self.get_mock_url(
                  'volumev2', 'public',
                  append=['volumes', volume.id, 'action']),
              json={'os-set_bootable': {'bootable': False}}),
     ])
     self.cloud.set_volume_bootable(volume['id'])
     self.assert_calls()
Пример #29
0
 def test_set_volume_bootable_false(self):
     vol = {'id': 'volume001', 'status': 'attached',
            'name': '', 'attachments': []}
     volume = meta.obj_to_munch(fakes.FakeVolume(**vol))
     self.register_uris([
         self.get_cinder_discovery_mock_dict(),
         dict(method='GET',
              uri=self.get_mock_url(
                  'volumev2', 'public', append=['volumes', 'detail']),
              json={'volumes': [volume]}),
         dict(method='POST',
              uri=self.get_mock_url(
                  'volumev2', 'public',
                  append=['volumes', volume.id, 'action']),
              json={'os-set_bootable': {'bootable': False}}),
     ])
     self.cloud.set_volume_bootable(volume['id'])
     self.assert_calls()
Пример #30
0
 def test_get_groups_from_server(self):
     server_vars = {'flavor': 'test-flavor',
                    'image': 'test-image',
                    'az': 'test-az'}
     self.assertEqual(
         ['test-name',
          'test-region',
          'test-name_test-region',
          'test-group',
          'instance-test-id-0',
          'meta-group_test-group',
          'test-az',
          'test-region_test-az',
          'test-name_test-region_test-az'],
         meta.get_groups_from_server(
             FakeCloud(),
             meta.obj_to_munch(standard_fake_server),
             server_vars
         )
     )
Пример #31
0
 def test_delete_volume_force(self):
     vol = {'id': 'volume001', 'status': 'attached',
            'name': '', 'attachments': []}
     volume = meta.obj_to_munch(fakes.FakeVolume(**vol))
     self.register_uris([
         dict(method='GET',
              uri=self.get_mock_url(
                  'volumev2', 'public', append=['volumes', 'detail']),
              json={'volumes': [volume]}),
         dict(method='POST',
              uri=self.get_mock_url(
                  'volumev2', 'public',
                  append=['volumes', volume.id, 'action']),
              validate=dict(
                  json={'os-force_delete': None})),
         dict(method='GET',
              uri=self.get_mock_url(
                  'volumev2', 'public', append=['volumes', 'detail']),
              json={'volumes': []})])
     self.assertTrue(self.cloud.delete_volume(volume['id'], force=True))
     self.assert_calls()
Пример #32
0
 def test_attach_volume(self):
     server = dict(id='server001')
     vol = {'id': 'volume001', 'status': 'available',
            'name': '', 'attachments': []}
     volume = meta.obj_to_munch(fakes.FakeVolume(**vol))
     rattach = {'server_id': server['id'], 'device': 'device001',
                'volumeId': volume['id'], 'id': 'attachmentId'}
     self.register_uris([
         dict(method='POST',
              uri=self.get_mock_url(
                  'compute', 'public',
                  append=['servers', server['id'],
                          'os-volume_attachments']),
              json={'volumeAttachment': rattach},
              validate=dict(json={
                  'volumeAttachment': {
                      'volumeId': vol['id']}})
              )])
     ret = self.cloud.attach_volume(server, volume, wait=False)
     self.assertEqual(rattach, ret)
     self.assert_calls()
Пример #33
0
    def test_create_volume(self):
        vol1 = meta.obj_to_munch(fakes.FakeVolume('01', 'available', 'vol1'))
        self.register_uris([
            dict(method='POST',
                 uri=self.get_mock_url('volumev2',
                                       'public',
                                       append=['volumes']),
                 json={'volume': vol1},
                 validate=dict(json={'volume': {
                     'size': 50,
                     'name': 'vol1',
                 }})),
            dict(method='GET',
                 uri=self.get_mock_url('volumev2',
                                       'public',
                                       append=['volumes', 'detail']),
                 json={'volumes': [vol1]}),
        ])

        self.cloud.create_volume(50, name='vol1')
        self.assert_calls()
Пример #34
0
 def test_delete_volume_force(self):
     vol = {'id': 'volume001', 'status': 'attached',
            'name': '', 'attachments': []}
     volume = meta.obj_to_munch(fakes.FakeVolume(**vol))
     self.register_uris([
         self.get_cinder_discovery_mock_dict(),
         dict(method='GET',
              uri=self.get_mock_url(
                  'volumev2', 'public', append=['volumes', 'detail']),
              json={'volumes': [volume]}),
         dict(method='POST',
              uri=self.get_mock_url(
                  'volumev2', 'public',
                  append=['volumes', volume.id, 'action']),
              validate=dict(
                  json={'os-force_delete': None})),
         dict(method='GET',
              uri=self.get_mock_url(
                  'volumev2', 'public', append=['volumes', 'detail']),
              json={'volumes': []})])
     self.assertTrue(self.cloud.delete_volume(volume['id'], force=True))
     self.assert_calls()
    def test_delete_volume_snapshot(self):
        """
        Test that delete_volume_snapshot without a wait returns True instance
        when the volume snapshot deletes.
        """
        fake_snapshot = fakes.FakeVolumeSnapshot('1234', 'available',
                                                 'foo', 'derpysnapshot')
        fake_snapshot_dict = meta.obj_to_munch(fake_snapshot)

        self.register_uris([
            dict(method='GET',
                 uri=self.get_mock_url(
                     'volumev2', 'public',
                     append=['snapshots', 'detail']),
                 json={'snapshots': [fake_snapshot_dict]}),
            dict(method='DELETE',
                 uri=self.get_mock_url(
                     'volumev2', 'public',
                     append=['snapshots', fake_snapshot_dict['id']]))])

        self.assertTrue(
            self.cloud.delete_volume_snapshot(name_or_id='1234', wait=False))
        self.assert_calls()
    def test_delete_volume_snapshot(self):
        """
        Test that delete_volume_snapshot without a wait returns True instance
        when the volume snapshot deletes.
        """
        fake_snapshot = fakes.FakeVolumeSnapshot('1234', 'available',
                                                 'foo', 'derpysnapshot')
        fake_snapshot_dict = meta.obj_to_munch(fake_snapshot)

        self.register_uris([
            dict(method='GET',
                 uri=self.get_mock_url(
                     'volumev2', 'public',
                     append=['snapshots', 'detail']),
                 json={'snapshots': [fake_snapshot_dict]}),
            dict(method='DELETE',
                 uri=self.get_mock_url(
                     'volumev2', 'public',
                     append=['snapshots', fake_snapshot_dict['id']]))])

        self.assertTrue(
            self.cloud.delete_volume_snapshot(name_or_id='1234', wait=False))
        self.assert_calls()
Пример #37
0
 def test_attach_volume_exception(self):
     server = dict(id='server001')
     vol = {'id': 'volume001', 'status': 'available',
            'name': '', 'attachments': []}
     volume = meta.obj_to_munch(fakes.FakeVolume(**vol))
     self.register_uris([
         dict(method='POST',
              uri=self.get_mock_url(
                  'compute', 'public',
                  append=['servers', server['id'],
                          'os-volume_attachments']),
              status_code=404,
              validate=dict(json={
                  'volumeAttachment': {
                      'volumeId': vol['id']}})
              )])
     with testtools.ExpectedException(
         openstack.cloud.OpenStackCloudURINotFound,
         "Error attaching volume %s to server %s" % (
             volume['id'], server['id'])
     ):
         self.cloud.attach_volume(server, volume, wait=False)
     self.assert_calls()
Пример #38
0
 def test_attach_volume_exception(self):
     server = dict(id='server001')
     vol = {'id': 'volume001', 'status': 'available',
            'name': '', 'attachments': []}
     volume = meta.obj_to_munch(fakes.FakeVolume(**vol))
     self.register_uris([
         dict(method='POST',
              uri=self.get_mock_url(
                  'compute', 'public',
                  append=['servers', server['id'],
                          'os-volume_attachments']),
              status_code=404,
              validate=dict(json={
                  'volumeAttachment': {
                      'volumeId': vol['id']}})
              )])
     with testtools.ExpectedException(
         openstack.cloud.OpenStackCloudURINotFound,
         "Error attaching volume %s to server %s" % (
             volume['id'], server['id'])
     ):
         self.cloud.attach_volume(server, volume, wait=False)
     self.assert_calls()
Пример #39
0
 def test_list_volumes_with_pagination_next_link_fails_all_attempts(self):
     vol1 = meta.obj_to_munch(fakes.FakeVolume('01', 'available', 'vol1'))
     uris = []
     attempts = 5
     for i in range(attempts):
         uris.extend([
             dict(method='GET',
                  uri=self.get_mock_url('volumev2',
                                        'public',
                                        append=['volumes', 'detail']),
                  json={
                      'volumes': [vol1],
                      'volumes_links': [{
                          'href':
                          self.get_mock_url('volumev2',
                                            'public',
                                            append=['volumes', 'detail'],
                                            qs_elements=['marker=01']),
                          'rel':
                          'next'
                      }]
                  }),
             dict(method='GET',
                  uri=self.get_mock_url('volumev2',
                                        'public',
                                        append=['volumes', 'detail'],
                                        qs_elements=['marker=01']),
                  status_code=404)
         ])
     self.register_uris(uris)
     # Check that found volumes are returned even if pagination didn't
     # complete because call to get next link 404'ed for all the allowed
     # attempts
     self.assertEqual([self.cloud._normalize_volume(vol1)],
                      self.cloud.list_volumes())
     self.assert_calls()
    def test_delete_volume_snapshot_with_error(self):
        """
        Test that a exception while deleting a volume snapshot will cause an
        OpenStackCloudException.
        """
        fake_snapshot = fakes.FakeVolumeSnapshot('1234', 'available',
                                                 'foo', 'derpysnapshot')
        fake_snapshot_dict = meta.obj_to_munch(fake_snapshot)
        self.register_uris([
            dict(method='GET',
                 uri=self.get_mock_url(
                     'volumev2', 'public',
                     append=['snapshots', 'detail']),
                 json={'snapshots': [fake_snapshot_dict]}),
            dict(method='DELETE',
                 uri=self.get_mock_url(
                     'volumev2', 'public',
                     append=['snapshots', fake_snapshot_dict['id']]),
                 status_code=404)])

        self.assertRaises(
            exc.OpenStackCloudException,
            self.cloud.delete_volume_snapshot, name_or_id='1234')
        self.assert_calls()
    def test_delete_volume_snapshot_with_error(self):
        """
        Test that a exception while deleting a volume snapshot will cause an
        OpenStackCloudException.
        """
        fake_snapshot = fakes.FakeVolumeSnapshot('1234', 'available',
                                                 'foo', 'derpysnapshot')
        fake_snapshot_dict = meta.obj_to_munch(fake_snapshot)
        self.register_uris([
            dict(method='GET',
                 uri=self.get_mock_url(
                     'volumev2', 'public',
                     append=['snapshots', 'detail']),
                 json={'snapshots': [fake_snapshot_dict]}),
            dict(method='DELETE',
                 uri=self.get_mock_url(
                     'volumev2', 'public',
                     append=['snapshots', fake_snapshot_dict['id']]),
                 status_code=404)])

        self.assertRaises(
            exc.OpenStackCloudException,
            self.cloud.delete_volume_snapshot, name_or_id='1234')
        self.assert_calls()
Пример #42
0
 def test_get_server_ip(self):
     srv = meta.obj_to_munch(standard_fake_server)
     self.assertEqual(PRIVATE_V4, meta.get_server_ip(srv, ext_tag='fixed'))
     self.assertEqual(PUBLIC_V4, meta.get_server_ip(srv,
                                                    ext_tag='floating'))
Пример #43
0
 def test_has_no_volume_service(self):
     fake_cloud = FakeCloud()
     fake_cloud.service_val = False
     hostvars = meta.get_hostvars_from_server(
         fake_cloud, meta.obj_to_munch(standard_fake_server))
     self.assertEqual([], hostvars['volumes'])
Пример #44
0
    def test_az(self):
        server = standard_fake_server
        server['OS-EXT-AZ:availability_zone'] = 'az1'

        hostvars = self.cloud._normalize_server(meta.obj_to_munch(server))
        self.assertEqual('az1', hostvars['az'])
Пример #45
0
    def test_create_volume_invalidates(self):
        fake_volb4 = meta.obj_to_munch(
            fakes.FakeVolume('volume1', 'available', ''))
        _id = '12345'
        fake_vol_creating = meta.obj_to_munch(
            fakes.FakeVolume(_id, 'creating', ''))
        fake_vol_avail = meta.obj_to_munch(
            fakes.FakeVolume(_id, 'available', ''))

        def now_deleting(request, context):
            fake_vol_avail['status'] = 'deleting'

        self.register_uris([
            dict(method='GET',
                 uri=self.get_mock_url('volumev2',
                                       'public',
                                       append=['volumes', 'detail']),
                 json={'volumes': [fake_volb4]}),
            dict(method='POST',
                 uri=self.get_mock_url('volumev2',
                                       'public',
                                       append=['volumes']),
                 json={'volume': fake_vol_creating}),
            dict(method='GET',
                 uri=self.get_mock_url('volumev2',
                                       'public',
                                       append=['volumes', 'detail']),
                 json={'volumes': [fake_volb4, fake_vol_creating]}),
            dict(method='GET',
                 uri=self.get_mock_url('volumev2',
                                       'public',
                                       append=['volumes', 'detail']),
                 json={'volumes': [fake_volb4, fake_vol_avail]}),
            dict(method='GET',
                 uri=self.get_mock_url('volumev2',
                                       'public',
                                       append=['volumes', 'detail']),
                 json={'volumes': [fake_volb4, fake_vol_avail]}),
            dict(method='DELETE',
                 uri=self.get_mock_url('volumev2',
                                       'public',
                                       append=['volumes', _id]),
                 json=now_deleting),
            dict(method='GET',
                 uri=self.get_mock_url('volumev2',
                                       'public',
                                       append=['volumes', 'detail']),
                 json={'volumes': [fake_volb4]})
        ])

        self.assertEqual([self.cloud._normalize_volume(fake_volb4)],
                         self.cloud.list_volumes())
        volume = dict(display_name='junk_vol',
                      size=1,
                      display_description='test junk volume')
        self.cloud.create_volume(wait=True, timeout=None, **volume)
        # If cache was not invalidated, we would not see our own volume here
        # because the first volume was available and thus would already be
        # cached.
        self.assertEqual([
            self.cloud._normalize_volume(fake_volb4),
            self.cloud._normalize_volume(fake_vol_avail)
        ], self.cloud.list_volumes())
        self.cloud.delete_volume(_id)
        # And now delete and check same thing since list is cached as all
        # available
        self.assertEqual([self.cloud._normalize_volume(fake_volb4)],
                         self.cloud.list_volumes())
        self.assert_calls()
Пример #46
0
 def _image_dict(self, fake_image):
     return self.cloud._normalize_image(meta.obj_to_munch(fake_image))
Пример #47
0
    def test_az(self):
        server = standard_fake_server
        server['OS-EXT-AZ:availability_zone'] = 'az1'

        hostvars = self.cloud._normalize_server(meta.obj_to_munch(server))
        self.assertEqual('az1', hostvars['az'])
Пример #48
0
def make_fake_machine(machine_name, machine_id=None):
    if not machine_id:
        machine_id = uuid.uuid4().hex
    return meta.obj_to_munch(FakeMachine(id=machine_id, name=machine_name))
Пример #49
0
 def test_list_volumes_with_pagination_next_link_fails_once(self):
     vol1 = meta.obj_to_munch(fakes.FakeVolume('01', 'available', 'vol1'))
     vol2 = meta.obj_to_munch(fakes.FakeVolume('02', 'available', 'vol2'))
     self.register_uris([
         dict(method='GET',
              uri=self.get_mock_url('volumev2',
                                    'public',
                                    append=['volumes', 'detail']),
              json={
                  'volumes': [vol1],
                  'volumes_links': [{
                      'href':
                      self.get_mock_url('volumev2',
                                        'public',
                                        append=['volumes', 'detail'],
                                        qs_elements=['marker=01']),
                      'rel':
                      'next'
                  }]
              }),
         dict(method='GET',
              uri=self.get_mock_url('volumev2',
                                    'public',
                                    append=['volumes', 'detail'],
                                    qs_elements=['marker=01']),
              status_code=404),
         dict(method='GET',
              uri=self.get_mock_url('volumev2',
                                    'public',
                                    append=['volumes', 'detail']),
              json={
                  'volumes': [vol1],
                  'volumes_links': [{
                      'href':
                      self.get_mock_url('volumev2',
                                        'public',
                                        append=['volumes', 'detail'],
                                        qs_elements=['marker=01']),
                      'rel':
                      'next'
                  }]
              }),
         dict(method='GET',
              uri=self.get_mock_url('volumev2',
                                    'public',
                                    append=['volumes', 'detail'],
                                    qs_elements=['marker=01']),
              json={
                  'volumes': [vol2],
                  'volumes_links': [{
                      'href':
                      self.get_mock_url('volumev2',
                                        'public',
                                        append=['volumes', 'detail'],
                                        qs_elements=['marker=02']),
                      'rel':
                      'next'
                  }]
              }),
         dict(method='GET',
              uri=self.get_mock_url('volumev2',
                                    'public',
                                    append=['volumes', 'detail'],
                                    qs_elements=['marker=02']),
              json={'volumes': []})
     ])
     self.assertEqual([
         self.cloud._normalize_volume(vol1),
         self.cloud._normalize_volume(vol2)
     ], self.cloud.list_volumes())
     self.assert_calls()
    def test_create_boot_attach_volume(self):
        build_server = fakes.make_fake_server('1234', '', 'BUILD')
        active_server = fakes.make_fake_server('1234', '', 'BUILD')

        vol = {
            'id': 'volume001',
            'status': 'available',
            'name': '',
            'attachments': []
        }
        volume = meta.obj_to_munch(fakes.FakeVolume(**vol))

        self.register_uris(
            [
                dict(method='GET',
                     uri=self.get_mock_url('network',
                                           'public',
                                           append=['v2.0', 'networks.json']),
                     json={'networks': []}),
                dict(method='POST',
                     uri=self.get_mock_url('compute',
                                           'public',
                                           append=['os-volumes_boot']),
                     json={'server': build_server},
                     validate=dict(
                         json={
                             'server': {
                                 u'flavorRef':
                                 'flavor-id',
                                 u'imageRef':
                                 'image-id',
                                 u'max_count':
                                 1,
                                 u'min_count':
                                 1,
                                 u'block_device_mapping_v2': [
                                     {
                                         u'boot_index': 0,
                                         u'delete_on_termination': True,
                                         u'destination_type': u'local',
                                         u'source_type': u'image',
                                         u'uuid': u'image-id'
                                     }, {
                                         u'boot_index': u'-1',
                                         u'delete_on_termination': False,
                                         u'destination_type': u'volume',
                                         u'source_type': u'volume',
                                         u'uuid': u'volume001'
                                     }
                                 ],
                                 u'name':
                                 u'server-name'
                             }
                         })),
                dict(method='GET',
                     uri=self.get_mock_url('compute',
                                           'public',
                                           append=['servers', '1234']),
                     json={'server': active_server}),
            ])

        self.cloud.create_server(name='server-name',
                                 image=dict(id='image-id'),
                                 flavor=dict(id='flavor-id'),
                                 boot_from_volume=False,
                                 volumes=[volume],
                                 wait=False)

        self.assert_calls()