Esempio n. 1
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(
            shade.OpenStackCloudException,
            "Error in attaching volume %s" % errored_volume['id']
        ):
            self.cloud.attach_volume(server, volume)
        self.assert_calls()
Esempio n. 2
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()
Esempio n. 3
0
    def test_create_volume_snapshot_with_error(self):
        """
        Test that a error status while waiting for the volume snapshot to
        create raises an exception in create_volume_snapshot.
        """
        snapshot_id = '5678'
        volume_id = '1234'
        build_snapshot = fakes.FakeVolumeSnapshot(snapshot_id, 'creating',
                                                  'bar', 'derpysnapshot')
        build_snapshot_dict = meta.obj_to_munch(build_snapshot)
        error_snapshot = fakes.FakeVolumeSnapshot(snapshot_id, 'error',
                                                  'blah', 'derpysnapshot')
        error_snapshot_dict = meta.obj_to_munch(error_snapshot)

        self.register_uris([
            dict(method='POST',
                 uri=self.get_mock_url(
                     'volumev2', 'public', append=['snapshots']),
                 json={'snapshot': build_snapshot_dict},
                 validate=dict(json={
                     'snapshot': {'force': False, 'volume_id': '1234'}})),
            dict(method='GET',
                 uri=self.get_mock_url('volumev2', 'public',
                                       append=['snapshots', snapshot_id]),
                 json={'snapshot': build_snapshot_dict}),
            dict(method='GET',
                 uri=self.get_mock_url('volumev2', 'public',
                                       append=['snapshots', snapshot_id]),
                 json={'snapshot': error_snapshot_dict})])

        self.assertRaises(
            exc.OpenStackCloudException,
            self.cloud.create_volume_snapshot, volume_id=volume_id,
            wait=True, timeout=5)
        self.assert_calls()
Esempio n. 4
0
    def test_create_volume_snapshot_wait(self):
        """
        Test that create_volume_snapshot with a wait returns the volume
        snapshot when its status changes to "available".
        """
        snapshot_id = '5678'
        volume_id = '1234'
        build_snapshot = fakes.FakeVolumeSnapshot(snapshot_id, 'creating',
                                                  'foo', 'derpysnapshot')
        build_snapshot_dict = meta.obj_to_munch(build_snapshot)
        fake_snapshot = fakes.FakeVolumeSnapshot(snapshot_id, 'available',
                                                 'foo', 'derpysnapshot')
        fake_snapshot_dict = meta.obj_to_munch(fake_snapshot)

        self.register_uris([
            dict(method='POST',
                 uri=self.get_mock_url(
                     'volumev2', 'public', append=['snapshots']),
                 json={'snapshot': build_snapshot_dict},
                 validate=dict(json={
                     'snapshot': {'force': False, 'volume_id': '1234'}})),
            dict(method='GET',
                 uri=self.get_mock_url('volumev2', 'public',
                                       append=['snapshots', snapshot_id]),
                 json={'snapshot': build_snapshot_dict}),
            dict(method='GET',
                 uri=self.get_mock_url('volumev2', 'public',
                                       append=['snapshots', snapshot_id]),
                 json={'snapshot': fake_snapshot_dict})])

        self.assertEqual(
            self.cloud._normalize_volume(fake_snapshot_dict),
            self.cloud.create_volume_snapshot(volume_id=volume_id, wait=True)
        )
        self.assert_calls()
Esempio n. 5
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(
         shade.OpenStackCloudException,
         "Error in detaching volume %s" % errored_volume['id']
     ):
         self.cloud.detach_volume(server, volume)
     self.assert_calls()
Esempio n. 6
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()
Esempio n. 7
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()
Esempio n. 8
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'])
Esempio n. 9
0
    def test_get_server_private_ip(self):
        self.register_uris([
            dict(method='GET',
                 uri='https://network.example.com/v2.0/networks.json',
                 json={
                     'networks': [{
                         'id': 'test-net-id',
                         'name': 'test-net-name'
                     }]
                 }),
            dict(method='GET',
                 uri='https://network.example.com/v2.0/subnets.json',
                 json={'subnets': SUBNETS_WITH_NAT})
        ])

        srv = meta.obj_to_munch(
            fakes.FakeServer(id='test-id',
                             name='test-name',
                             status='ACTIVE',
                             addresses={
                                 'private': [{
                                     'OS-EXT-IPS:type': 'fixed',
                                     'addr': PRIVATE_V4,
                                     'version': 4
                                 }],
                                 'public': [{
                                     'OS-EXT-IPS:type': 'floating',
                                     'addr': PUBLIC_V4,
                                     'version': 4
                                 }]
                             }))

        self.assertEqual(PRIVATE_V4,
                         meta.get_server_private_ip(srv, self.cloud))
        self.assert_calls()
Esempio n. 10
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'])
Esempio n. 11
0
    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)
Esempio n. 12
0
    def test_get_server_external_none_ipv4_neutron(self):
        # Testing Clouds with Neutron
        self.register_uris([
            dict(method='GET',
                 uri='https://network.example.com/v2.0/networks.json',
                 json={
                     'networks': [{
                         'id': 'test-net-id',
                         'name': 'test-net',
                         'router:external': False,
                     }]
                 }),
            dict(method='GET',
                 uri='https://network.example.com/v2.0/subnets.json',
                 json={'subnets': SUBNETS_WITH_NAT})
        ])

        srv = meta.obj_to_munch(
            fakes.FakeServer(
                id='test-id',
                name='test-name',
                status='ACTIVE',
                addresses={'test-net': [{
                    'addr': PUBLIC_V4,
                    'version': 4
                }]},
            ))
        ip = meta.get_server_external_ipv4(cloud=self.cloud, server=srv)

        self.assertEqual(None, ip)
        self.assert_calls()
Esempio n. 13
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()
Esempio n. 14
0
 def test_rebuild_server_wait(self, mock_nova):
     """
     Test that rebuild_server with a wait returns the server instance when
     its status changes to "ACTIVE".
     """
     rebuild_server = fakes.FakeServer('1234', '', 'REBUILD')
     active_server = fakes.FakeServer('1234', '', 'ACTIVE')
     fake_floating_ip = fakes.FakeFloatingIP('1234', 'ippool',
                                             '1.1.1.1', '2.2.2.2',
                                             '5678')
     mock_nova.servers.rebuild.return_value = rebuild_server
     mock_nova.servers.list.return_value = [active_server]
     self.register_uris([
         dict(method='GET',
              uri=self.get_mock_url(
                  'network', 'public', append=['v2.0', 'ports.json'],
                  qs_elements=['device_id=1234']),
              json={'ports': []}),
         dict(method='GET',
              uri=self.get_mock_url(
                  'network', 'public', append=['v2.0', 'networks.json']),
              json={'networks': []}),
         dict(method='GET',
              uri=self.get_mock_url(
                  'network', 'public', append=['v2.0', 'floatingips.json']),
              json={'floatingips': [fake_floating_ip]})
     ])
     self.cloud.name = 'cloud-name'
     self.assertEqual(
         self.cloud._normalize_server(
             meta.obj_to_munch(active_server)),
         self.cloud.rebuild_server("1234", "b", wait=True))
     # TODO(slaweq): change do_count to True when all nova mocks will be
     # replaced with request_mocks also
     self.assert_calls(do_count=False)
Esempio n. 15
0
    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)
Esempio n. 16
0
    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()
Esempio n. 17
0
    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()
Esempio n. 18
0
    def test_get_server_cloud_osic_split(self, mock_get_flavor_name,
                                         mock_get_image_name,
                                         mock_get_volumes):
        self.cloud._floating_ip_source = None
        self.cloud.force_ipv4 = False
        self.cloud._local_ipv6 = True
        self.cloud._external_ipv4_names = ['GATEWAY_NET']
        self.cloud._external_ipv6_names = ['GATEWAY_NET_V6']
        self.cloud._internal_ipv4_names = ['GATEWAY_NET_V6']
        self.cloud._internal_ipv6_names = []
        mock_get_image_name.return_value = 'cirros-0.3.4-x86_64-uec'
        mock_get_flavor_name.return_value = 'm1.tiny'
        mock_get_volumes.return_value = []

        self.register_uris([
            dict(method='GET',
                 uri='https://network.example.com/v2.0/networks.json',
                 json={'networks': OSIC_NETWORKS}),
            dict(method='GET',
                 uri='https://network.example.com/v2.0/subnets.json',
                 json={'subnets': OSIC_SUBNETS}),
            dict(method='GET',
                 uri='{endpoint}/servers/test-id/os-security-groups'.format(
                     endpoint=fakes.COMPUTE_ENDPOINT),
                 json={'security_groups': []})
        ])

        srv = self.cloud.get_openstack_vars(
            meta.obj_to_munch(
                fakes.FakeServer(
                    id='test-id',
                    name='test-name',
                    status='ACTIVE',
                    flavor={u'id': u'1'},
                    image={
                        'name': u'cirros-0.3.4-x86_64-uec',
                        u'id': u'f93d000b-7c29-4489-b375-3641a1758fe1'
                    },
                    addresses={
                        'private': [{
                            'addr': "10.223.160.141",
                            'version': 4
                        }],
                        'public': [{
                            'addr': "104.130.246.91",
                            'version': 4
                        }, {
                            'addr': "2001:4800:7819:103:be76:4eff:fe05:8525",
                            'version': 6
                        }]
                    })))

        self.assertEqual("10.223.160.141", srv['private_v4'])
        self.assertEqual("104.130.246.91", srv['public_v4'])
        self.assertEqual("2001:4800:7819:103:be76:4eff:fe05:8525",
                         srv['public_v6'])
        self.assertEqual("2001:4800:7819:103:be76:4eff:fe05:8525",
                         srv['interface_ip'])
        self.assert_calls()
Esempio n. 19
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()
Esempio n. 20
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'])
Esempio n. 21
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'])
Esempio n. 22
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')
Esempio n. 23
0
    def test_get_server_external_ipv4_neutron_accessIPv6(self):
        srv = meta.obj_to_munch(
            fakes.FakeServer(id='test-id',
                             name='test-name',
                             status='ACTIVE',
                             accessIPv6=PUBLIC_V6))
        ip = meta.get_server_external_ipv6(server=srv)

        self.assertEqual(PUBLIC_V6, ip)
Esempio n. 24
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'])
Esempio n. 25
0
    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()
Esempio n. 26
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'])
Esempio n. 27
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()
Esempio n. 28
0
    def wait(self, raw=False):
        super(RequestTask, self).wait()

        if raw:
            # Do NOT convert the result.
            return self._result

        if _is_listlike(self._result):
            return meta.obj_list_to_munch(
                self._result, request_id=self._request_id)
        elif _is_objlike(self._result):
            return meta.obj_to_munch(self._result, request_id=self._request_id)
        return self._result
Esempio n. 29
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))
Esempio n. 30
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'])
Esempio n. 31
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()
Esempio n. 32
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
         )
     )
Esempio n. 33
0
    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()
Esempio n. 34
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(
         shade.OpenStackCloudURINotFound,
         "Error attaching volume %s to server %s" % (
             volume['id'], server['id'])
     ):
         self.cloud.attach_volume(server, volume, wait=False)
     self.assert_calls()
Esempio n. 35
0
    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()
Esempio n. 36
0
 def _image_dict(self, fake_image):
     return self.cloud._normalize_image(meta.obj_to_munch(fake_image))
Esempio n. 37
0
File: fakes.py Progetto: dbckz/shade
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))
Esempio n. 38
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()