def test_default_client_with_retries(self):
        self.flags(glance_num_retries=1)

        ctxt = context.RequestContext('fake', 'fake')

        info = {
            'num_calls': 0,
            'host0': 'host1',
            'port0': 9292,
            'use_ssl0': False,
            'host1': 'host2',
            'port1': 9293,
            'use_ssl1': True
        }

        # Leave the list in a known-order
        def _fake_shuffle(servers):
            pass

        def _fake_create_glance_client(context, host, port, use_ssl, version):
            attempt = info['num_calls']
            self.assertEqual(host, info['host%s' % attempt])
            self.assertEqual(port, info['port%s' % attempt])
            self.assertEqual(use_ssl, info['use_ssl%s' % attempt])
            return _create_failing_glance_client(info)

        self.stubs.Set(random, 'shuffle', _fake_shuffle)
        self.stubs.Set(glance, '_create_glance_client',
                       _fake_create_glance_client)

        client = glance.GlanceClientWrapper()
        client2 = glance.GlanceClientWrapper()
        client.call(ctxt, 1, 'get', 'meow')
        self.assertEqual(info['num_calls'], 2)

        def _fake_shuffle2(servers):
            # fake shuffle in a known manner
            servers.append(servers.pop(0))

        self.stubs.Set(random, 'shuffle', _fake_shuffle2)

        info = {
            'num_calls': 0,
            'host0': 'host2',
            'port0': 9293,
            'use_ssl0': True,
            'host1': 'host3',
            'port1': 9294,
            'use_ssl1': False
        }

        client2.call(ctxt, 1, 'get', 'meow')
        self.assertEqual(info['num_calls'], 2)
Exemple #2
0
    def test_default_client_with_retries(self, create_client_mock, sleep_mock,
                                         shuffle_mock):
        api_servers = ['host1:9292', 'https://host2:9293', 'http://host3:9294']
        client_mock = mock.MagicMock()
        images_mock = mock.MagicMock()
        images_mock.get.side_effect = [
            glanceclient.exc.ServiceUnavailable, None
        ]
        type(client_mock).images = mock.PropertyMock(return_value=images_mock)
        create_client_mock.return_value = client_mock

        self.flags(num_retries=1, group='glance')
        self.flags(api_servers=api_servers, group='glance')

        ctx = context.RequestContext('fake', 'fake')

        # And here we're testing that if num_retries is not 0, then we attempt
        # to retry the same connection action against the next client.

        client = glance.GlanceClientWrapper()
        client.call(ctx, 1, 'get', 'meow')

        create_client_mock.assert_has_calls([
            mock.call(ctx, 'host1', 9292, False, 1),
            mock.call(ctx, 'host2', 9293, True, 1),
        ])
        sleep_mock.assert_called_once_with(1)
Exemple #3
0
    def test_default_client_without_retries(self, create_client_mock,
                                            sleep_mock, shuffle_mock):
        api_servers = ['host1:9292', 'https://host2:9293', 'http://host3:9294']
        client_mock = mock.MagicMock()
        images_mock = mock.MagicMock()
        images_mock.get.side_effect = glanceclient.exc.ServiceUnavailable
        type(client_mock).images = mock.PropertyMock(return_value=images_mock)
        create_client_mock.return_value = client_mock

        shuffle_mock.return_value = api_servers
        self.flags(num_retries=0, group='glance')
        self.flags(api_servers=api_servers, group='glance')

        # Here we are testing the behaviour that calling client.call() twice
        # when there are no retries will cycle through the api_servers and not
        # sleep (which would be an indication of a retry)
        ctx = context.RequestContext('fake', 'fake')

        client = glance.GlanceClientWrapper()
        self.assertRaises(exception.GlanceConnectionFailed, client.call, ctx,
                          1, 'get', 'meow')
        self.assertFalse(sleep_mock.called)

        self.assertRaises(exception.GlanceConnectionFailed, client.call, ctx,
                          1, 'get', 'meow')
        self.assertFalse(sleep_mock.called)

        create_client_mock.assert_has_calls([
            mock.call(ctx, 'host1', 9292, False, 1),
            mock.call(ctx, 'host2', 9293, True, 1),
        ])
    def test_static_client_with_retries(self):
        self.flags(glance_num_retries=1)

        ctxt = context.RequestContext('fake', 'fake')
        fake_host = 'host4'
        fake_port = 9295
        fake_use_ssl = False

        info = {'num_calls': 0}

        def _fake_create_glance_client(context, host, port, use_ssl, version):
            self.assertEqual(host, fake_host)
            self.assertEqual(port, fake_port)
            self.assertEqual(use_ssl, fake_use_ssl)
            return _create_failing_glance_client(info)

        self.stubs.Set(glance, '_create_glance_client',
                       _fake_create_glance_client)

        client = glance.GlanceClientWrapper(context=ctxt,
                                            host=fake_host,
                                            port=fake_port,
                                            use_ssl=fake_use_ssl)
        client.call(ctxt, 1, 'get', 'meow')
        self.assertEqual(info['num_calls'], 2)
Exemple #5
0
    def _create_image_service(self, client):
        def _fake_create_glance_client(context, host, port):
            return client

        self.stubs.Set(glance, '_create_glance_client',
                       _fake_create_glance_client)

        client_wrapper = glance.GlanceClientWrapper('fake', 'fake_host', 9292)
        return glance.GlanceImageService(client=client_wrapper)
Exemple #6
0
    def test_default_client_without_retries(self):
        self.flags(glance_num_retries=0)

        ctxt = context.RequestContext('fake', 'fake')

        info = {'num_calls': 0,
                'host': 'host1',
                'port': 9292,
                'use_ssl': False}

        # Leave the list in a known-order
        def _fake_shuffle(servers):
            pass

        def _fake_create_glance_client(context, host, port, use_ssl, version):
            self.assertEqual(host, info['host'])
            self.assertEqual(port, info['port'])
            self.assertEqual(use_ssl, info['use_ssl'])
            return _create_failing_glance_client(info)

        self.stubs.Set(random, 'shuffle', _fake_shuffle)
        self.stubs.Set(glance, '_create_glance_client',
                _fake_create_glance_client)

        client = glance.GlanceClientWrapper()
        client2 = glance.GlanceClientWrapper()
        self.assertRaises(exception.GlanceConnectionFailed,
                client.call, ctxt, 1, 'get', 'meow')
        self.assertEqual(info['num_calls'], 1)

        info = {'num_calls': 0,
                'host': 'host2',
                'port': 9293,
                'use_ssl': True}

        def _fake_shuffle2(servers):
            # fake shuffle in a known manner
            servers.append(servers.pop(0))

        self.stubs.Set(random, 'shuffle', _fake_shuffle2)

        self.assertRaises(exception.GlanceConnectionFailed,
                client2.call, ctxt, 1, 'get', 'meow')
        self.assertEqual(info['num_calls'], 1)
Exemple #7
0
    def test_static_client_without_retries(self, create_client_mock,
                                           sleep_mock):
        client_mock = mock.MagicMock()
        images_mock = mock.MagicMock()
        images_mock.get.side_effect = glanceclient.exc.ServiceUnavailable
        type(client_mock).images = mock.PropertyMock(return_value=images_mock)
        create_client_mock.return_value = client_mock
        self.flags(num_retries=0, group='glance')

        ctx = context.RequestContext('fake', 'fake')
        host = 'host4'
        port = 9295
        use_ssl = False

        client = glance.GlanceClientWrapper(context=ctx, host=host, port=port,
                                            use_ssl=use_ssl)
        create_client_mock.assert_called_once_with(ctx, host, port, use_ssl, 1)
        self.assertRaises(exception.GlanceConnectionFailed,
                client.call, ctx, 1, 'get', 'meow')
        self.assertFalse(sleep_mock.called)
Exemple #8
0
    def test_static_client_with_retries(self, create_client_mock, sleep_mock):
        self.flags(num_retries=1, group='glance')
        client_mock = mock.MagicMock()
        images_mock = mock.MagicMock()
        images_mock.get.side_effect = [
            glanceclient.exc.ServiceUnavailable, None
        ]
        type(client_mock).images = mock.PropertyMock(return_value=images_mock)
        create_client_mock.return_value = client_mock

        ctx = context.RequestContext('fake', 'fake')
        host = 'host4'
        port = 9295
        use_ssl = False

        client = glance.GlanceClientWrapper(context=ctx,
                                            host=host,
                                            port=port,
                                            use_ssl=use_ssl)
        client.call(ctx, 1, 'get', 'meow')
        sleep_mock.assert_called_once_with(1)
Exemple #9
0
    def test_static_client_without_retries(self):
        self.flags(glance_num_retries=0)

        ctxt = context.RequestContext('fake', 'fake')
        fake_host = 'host4'
        fake_port = 9295

        info = {'num_calls': 0}

        def _fake_create_glance_client(context, host, port):
            self.assertEqual(host, fake_host)
            self.assertEqual(port, fake_port)
            return _create_failing_glance_client(info)

        self.stubs.Set(glance, '_create_glance_client',
                       _fake_create_glance_client)

        client = glance.GlanceClientWrapper(context=ctxt,
                                            host=fake_host,
                                            port=fake_port)
        self.assertRaises(exception.GlanceConnectionFailed, client.call, ctxt,
                          'get', 'meow')
        self.assertEqual(info['num_calls'], 1)
Exemple #10
0
    def sync_image(self, context, cascaded_url, cascading_image):
        cascaded_glance_url = cascaded_url
        _host, _port = get_host_port(cascaded_glance_url)
        _cascaded_client = glance.GlanceClientWrapper(context=context,
                                                      host=_host,
                                                      port=_port,
                                                      version=2)

        image_meta = get_adding_image_properties(cascading_image)
        cascaded_image = _cascaded_client.call(context, 2, 'create',
                                               **image_meta)
        image_id = cascading_image.id
        cascaded_id = cascaded_image.id
        candidate_path = get_candidate_path(cascading_image)
        LOG.debug("the candidate path is %s." % (candidate_path))
        # copy image
        try:
            image_loc = self._copy_data(image_id, cascaded_id, candidate_path)
        except Exception as e:
            LOG.exception(_("copy image failed, reason=%s") % e)
            raise
        else:
            if not image_loc:
                LOG.exception(_("copy image Exception, no cascaded_loc"))
        try:
            # patch loc to the cascaded image
            csd_locs = [{'url': image_loc, 'metadata': {}}]
            _cascaded_client.call(context,
                                  2,
                                  'update',
                                  cascaded_id,
                                  remove_props=None,
                                  locations=csd_locs)
        except Exception as e:
            LOG.exception(
                _("patch loc to cascaded image Exception, reason: %s" % e))
            raise

        try:
            # patch glance-loc to cascading image
            csg_locs = cascading_image.locations
            glance_loc = '%s/v2/images/%s' % (cascaded_glance_url, cascaded_id)
            csg_locs.append({
                'url': glance_loc,
                'metadata': {
                    'image_id': str(cascaded_id),
                    'action': 'upload'
                }
            })
            self._client.call(context,
                              2,
                              'update',
                              image_id,
                              remove_props=None,
                              locations=csg_locs)
        except Exception as e:
            LOG.exception(
                _("patch loc to cascading image Exception, reason: %s" % e))
            raise

        return cascaded_id
Exemple #11
0
 def __init__(self, cascading_client=None):
     self._client = cascading_client or glance.GlanceClientWrapper()