예제 #1
0
 def test_remove_expired_items_from_cache(self):
     expired_items = {
         uuidutils.generate_uuid():
         image_service.TempUrlCacheElement('fake-url-1',
                                           int(time.time()) - 10),
         uuidutils.generate_uuid():
         image_service.TempUrlCacheElement(
             'fake-url-2',
             int(time.time()) + 90  # Agent won't be able to start in time
         )
     }
     valid_items = {
         uuidutils.generate_uuid():
         image_service.TempUrlCacheElement('fake-url-3',
                                           int(time.time()) + 1000),
         uuidutils.generate_uuid():
         image_service.TempUrlCacheElement('fake-url-4',
                                           int(time.time()) + 2000)
     }
     self.glance_service._cache.update(expired_items)
     self.glance_service._cache.update(valid_items)
     self.glance_service._remove_expired_items_from_cache()
     for uuid in valid_items:
         self.assertEqual(valid_items[uuid],
                          self.glance_service._cache[uuid])
     for uuid in expired_items:
         self.assertNotIn(uuid, self.glance_service._cache)
예제 #2
0
    def test_do_not_return_expired_tempurls(self, tempurl_mock):
        fake_image = {'id': uuidutils.generate_uuid()}
        old_exp_time = int(time.time()) + 99
        path = ('/v1/AUTH_a422b2-91f3-2f46-74b7-d7c9e8958f5d30'
                '/glance'
                '/%s' % fake_image['id'])
        query = '?temp_url_sig=hmacsig&temp_url_expires=%s'
        self.glance_service._cache[fake_image['id']] = (
            image_service.TempUrlCacheElement(
                url=(CONF.glance.swift_endpoint_url + path +
                     query % old_exp_time),
                url_expires_at=old_exp_time))

        new_exp_time = int(time.time()) + 1200
        tempurl_mock.return_value = (path + query % new_exp_time)

        self.glance_service._validate_temp_url_config = mock.Mock()

        fresh_temp_url = self.glance_service.swift_temp_url(
            image_info=fake_image)

        self.assertEqual(
            CONF.glance.swift_endpoint_url + tempurl_mock.return_value,
            fresh_temp_url)
        tempurl_mock.assert_called_with(
            path=path,
            seconds=CONF.glance.swift_temp_url_duration,
            key=CONF.glance.swift_temp_url_key,
            method='GET')
        self.assertEqual((fresh_temp_url, new_exp_time),
                         self.glance_service._cache[fake_image['id']])
예제 #3
0
    def test_return_cached_tempurl(self, tempurl_mock):
        fake_image = {'id': uuidutils.generate_uuid()}

        exp_time = int(time.time()) + 1200
        temp_url = CONF.glance.swift_endpoint_url + (
            '/v1/AUTH_a422b2-91f3-2f46-74b7-d7c9e8958f5d30'
            '/glance'
            '/%(uuid)s'
            '?temp_url_sig=hmacsig&temp_url_expires=%(exp_time)s' % {
                'uuid': fake_image['id'],
                'exp_time': exp_time
            })
        self.glance_service._cache[fake_image['id']] = (
            image_service.TempUrlCacheElement(url=temp_url,
                                              url_expires_at=exp_time))

        cleanup_mock = mock.Mock()
        self.glance_service._remove_expired_items_from_cache = cleanup_mock
        self.glance_service._validate_temp_url_config = mock.Mock()

        self.assertEqual(
            temp_url,
            self.glance_service.swift_temp_url(image_info=fake_image))

        cleanup_mock.assert_called_once_with()
        self.assertFalse(tempurl_mock.called)