Commit Graph

66 Commits

Author SHA1 Message Date
Abhishek Kekane 0bbaec2eeb Introduce centralized database driver for image cache
Added centralized_db driver for image cache which will use
centralized database for storing cache related information.

Related blueprint centralized-cache-db

Change-Id: Iafaaa86666176cc95f77d85a4ab77286f0042bdd
2024-02-16 16:38:06 +00:00
Pranali Deore 3790cfd4a1 Remove dead code of auth and policy layers
In Xena we have mangaed to move all policy checks to API layer,
now removing the dead code from policy and authorization layer

NOTE: Some of the code is still being used from policy layer,
hence keeping it there only at this moment.

Change-Id: Ibee749cde20687d8c243cf84ae80b4de67d8ef3d
2022-06-14 10:15:55 +00:00
Stephen Finucane 39e667a145 Remove six.moves.range usage
This is the same as the 'range' keyword in Python 3

Change-Id: If3aa008522c24e870b7bf13de32b8ed1b27cb519
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2022-01-27 14:54:05 +00:00
Stephen Finucane 67b531b143 Replace six.BytesIO, six.StringIO usage
Change-Id: I007da2ba19b4e18f7c4f64d1503a18815a3f8ce4
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
2022-01-27 14:54:05 +00:00
Cyril Roelandt 0ca2f92161 Fix typos
Change-Id: I5e7776324c01e467799b1296c35b84dc0c10cce2
2021-10-13 03:02:52 +02:00
Abhishek Kekane 9b385f7b67 [uwsgi] Add missing pefetch periodic job
Glance was not supporting uwsgi deployment when we added periodic job
to prefetch images into cache. Later in ussuri uwsgi support was added
but we missed to implement periodic job to pre-cache the image.

This patch add this support for glance + uwsgi. For WSGI, we run the
prefetcher with an external lock, which makes sure that multiple API
workers will not attempt to cache an image at the same time. In this
case, if multiple workers attempt to run at the same time, only one
will grab the lock and do the work. When completed, the other worker
will grab the lock and either find all the work completed, or complete
new work that has been queued since the first one started.

Closes-Bug: #1939307
Co-Authored-By: Dan Smith <dms@danplanet.com>
Change-Id: I2abd1e60f414fbd68ce84e0b280f8b3e4e791a82
2021-09-08 11:05:57 -07:00
Abhishek Kekane ea13046919 Check policies for Image Cache in API
This patch enforces policy checks required for caching images
in API layer.

Partially-Implements: blueprint policy-refactor
Depends-On: https://review.opendev.org/c/openstack/nova/+/688802

Change-Id: Ie17b8f5bf308b8f07915ea18ace9b49955b8f0f0
2021-08-30 07:26:51 +00:00
Ade Lee 8027d90710 Replace md5 with oslo version
md5 is not an approved algorithm in FIPS mode, and trying to
instantiate a hashlib.md5() will fail when the system is running in
FIPS mode.

md5 is allowed when in a non-security context.  There is a plan to
add a keyword parameter (usedforsecurity) to hashlib.md5() to annotate
whether or not the instance is being used in a security context.

In the case where it is not, the instantiation of md5 will be allowed.
See https://bugs.python.org/issue9216 for more details.

Some downstream python versions already support this parameter.  To
support these versions, a new encapsulation of md5() has been added to
oslo_utils.  See https://review.opendev.org/#/c/750031/

This patch is to replace the instances of hashlib.md5() with this new
encapsulation, adding an annotation indicating whether the usage is
a security context or not.

Reviewers need to pay particular attention as to whether the keyword
parameter (usedforsecurity) is set correctly.

It looks like the usage of md5() here is solely to determine a checksum
of an image.

With this patch and the dependent patch for glance_store, all the
unit and functional tests pass on a FIPS enabled system.

Depends-On: https://review.opendev.org/#/c/756157
Depends-On: https://review.opendev.org/#/c/760160
Change-Id: I3b6d78d9792d4655bf0f4989cf82aced3f27491b
2020-12-15 10:43:19 -05:00
Hervé Beraud 64e3c41167 Stop to use the __future__ module.
The __future__ module [1] was used in this context to ensure compatibility
between python 2 and python 3.

We previously dropped the support of python 2.7 [2] and now we only support
python 3 so we don't need to continue to use this module and the imports
listed below.

Imports commonly used and their related PEPs:
- `division` is related to PEP 238 [3]
- `print_function` is related to PEP 3105 [4]
- `unicode_literals` is related to PEP 3112 [5]
- `with_statement` is related to PEP 343 [6]
- `absolute_import` is related to PEP 328 [7]

[1] https://docs.python.org/3/library/__future__.html
[2] https://governance.openstack.org/tc/goals/selected/ussuri/drop-py27.html
[3] https://www.python.org/dev/peps/pep-0238
[4] https://www.python.org/dev/peps/pep-3105
[5] https://www.python.org/dev/peps/pep-3112
[6] https://www.python.org/dev/peps/pep-0343
[7] https://www.python.org/dev/peps/pep-0328

Change-Id: I82b61a2d7687ff109051815e01619eb612ea9073
2020-06-02 20:14:31 +02:00
Abhishek Kekane b55c0a31c0 Remove registry related functional and unit tests
Registry service is deprecated and due for removal since past
couple of cycles. This patch removes functional and unit tests of
registry.

NOTE:
Skipped 'test_create_with_live_time' test as it was dependent on
test_registry_client and test_registry_api modules. Something is
wrong with the test and will be corrected once entire registry code
is removed from the code base.

Change-Id: I560ab5260bed7e43d83b67a00057ac48f9e366e9
2019-12-18 09:23:37 +00:00
Lucian Petrut 8fe78170e4 Windows: fix flaky tests
Some tests compare timestamps (e.g. image created_at/updated_at
fields).

In some cases, subsequent timestamps may be identical, breaking the
assertions made by those tests.

One idea is to just add a 1ms sleep which should be a negligeable.
Mocking the functions used to retrieve timestamps may be undesireable.

For now, we'll do this only for Windows, where time.time has a lower
resolution compared to Linux (1e-7 as opposed to 1e-9).

At the same time, socket timeouts are rather inconsistent on Windows.
When a timeout is hit, recv may either throw ConnectionAbortedError
*or* return nothing. This needs to be taken into account when
expecting socket timeouts.

Change-Id: Ie5a4d8fb4c979c09eda2fdc0fad0baa1d1840c59
2019-03-25 14:23:21 +02:00
Lucian Petrut 98b7ef195c Allow glance tests to run on Windows
In order to run the unit and functional Glance tests on Windows, we
have to:

* avoid monkey patching the os module on Windows (which causes Popen
  to fail)
* update sqlite connection URL
* avoid os.fork, not available on Windows.
    * we'll use subprocess.Popen when spinning up http servers.
    * for the really simple ones defined in the test helpers, we'll just
      use threads
* do not attempt to connect to '0.0.0.0', use '127.0.0.1' instead
* some tests aren't properly skipped (xattr ones), so we're covering that
  as well
* skip log rotation test, we can't move in-use files. Log rotation can
  be performed by the log handler itself.
* expect an exception when hitting connection timeouts
* avoid installing unavailable test requirements (xattr, pysendfile)
* pin the instance creation timestamp. some tests that deal with
  markers rely on ordering, which can be flipped if the timestamps are
  identical (can happen in case of resources created one after the
  other, not sure yet if this happens really fast or the clock isn't
  accurate enough).
* add a few seconds to some timeouts (much needed when running the tests
  in VMs).

blueprint windows-support

Change-Id: Ife69f56a3f9f4d81e1e2e47fde4778efd490938f
2019-03-13 16:41:11 +02:00
Charles Short 8954959817 Introduce ''mock_object'' method to base test class
We would like to fully remove mox from the test tree. Even for tests
that don't use mox's validation, many of them are using the symbol
patching with self.stubs.Set. We can do the same thing with the
monkeypatch fixture instead.

This introduces self.stub_out to nova/test.py and an example of what a
stubs => stub_out change would look like.

The teardown function in the converted test was removed at the same
time, as those should no longer be used.

Part of the mox community goal for Rocky.

Change-Id: I8f471ff8fee600ebb4e8907bf240007b7b4fe59f
Signed-off-by: Chuck Short <chucks@redhat.com>
2018-08-09 12:40:12 -04:00
Sean McGinnis 96334ad951 Add fixture to only emit DeprecationWarning once
We have a ton of DeprecationWarning messages in our unit test runs.
Most of these are out of our control from third party libs. This
adds a WarningsFixture to limit warning output to once per test
run. In local py35 unit testing, this went from 14549 warnings to
8913.

Also including ignorning a policy 'is_admin' deprecation warning
that was added before a clear plan or replacement had been put
in place. Other projects have added this rather than fixing it
at the source since it is currently being reworked.

Based on work previously done in Nova and Cinder.

Change-Id: I4d97f74ed37b7b0e9a613ecfe33c4b26216ca768
2017-12-12 15:49:04 +00:00
Victor Stinner eeedd2030d Port image cache to Python 3
* Use bytes for image content
* On Python 3, set_xattr() encodes the text value to UTF-8
* Open files in binary mode, not in text mode
* get_caching_iter(): pass a list as the image iterator, not a string.
  On Python 3, list(b'abc') returns [97, 98, 99], whereas Python 2 returns
  ['a', 'b', 'c'].
* tox.ini: add glance.tests.unit.test_image_cache to Python 3.4

Change-Id: I638525d19c42990852cf45dd416318d9a847c303
2015-08-16 11:49:45 +02:00
Hemanth Makkapati 95d3561de8 Glance cache to not prune newly cached images
Sqlite driver doesn't set the last accessed time when an image is
first added to cache. This makes the newly cached images susceptible
to pruning first instead of older images.

Change-Id: I46973a921c7cfb42811c58383e1b7a4004e70f27
Closes-bug: #1438564
2015-04-01 23:26:12 -04:00
Erno Kuvaja a4f5bf6ab9 Unify using six.moves.range rename everywhere
Mainly to improve consistency, use range() from six.moves
renames across glance.

Behaves consistently like py2 xrange() and py3 range().

Removes unnecessary range() from glace/api/v2/images.py

Change-Id: Id21f923d05600b902f2239e25ef01716c07e74a3
2015-02-25 12:05:36 +00:00
Julien Danjou a360ed98bb Switch to mox3
As mox3 is a drop-in Python 3 replacement for mox, let's switch to it.

Change-Id: I0174398c492fdd5e53672dd0f654f220583b75d4
2015-01-26 10:59:26 +01:00
Louis Taylor 41d45dfb1c Move from oslo.utils to oslo_utils
oslo_utils has been moved out of the oslo namespace.

bp drop-namespace-packages

Change-Id: Ie818e72b31efd2a6ab182444967bdfaca9366f4a
2015-01-09 20:01:42 +00:00
Roman Vasilets cb2456ea5e Wrong order of assertEquals args(Glance)
Args of assertEquals method in glance.tests are arranged
in wrong order. In result when test fails it shows incorrect
information about observed and actual data. It's found more
than 2000 times. Right order of arguments is "expected, actual".

Change-Id: Ifea4809f5a696b608a19e36a9ed9e5eec46c8a21
Co-Authored-By: Li Yingjun <liyingjun1988@gmail.com>
Closes-Bug: 1277104
2014-11-13 17:10:09 +02:00
Julien Danjou 874e643549 Switch to oslo.utils
Change-Id: I47dc734c6d6e2ab99c25701ae3492acd5e442212
2014-10-07 14:55:57 +02:00
Julien Danjou fadbef8511 hacking: upgrade to 0.9.x serie
Change-Id: I252758fd633662de9659a402c5e3d7e3ce1fae0f
2014-10-06 14:41:03 +02:00
Flavio Percoco f6e7992a68 Adopt glance.store library in Glance
This commits removes the old `store` package from glance and adopts the
usage of the new glance.store library. The library was designed to
preserve backwards compatibility as much as possible. In fact, most of
the changes in this patch are related to function args ordering and not
function renames or workflow changes.

Some changes that are worth mentioning:

1. Glance store doesn't rely on a global config object. All config
options must be explicitly registered.

2. All store operations now accepted an optional context. This is a
fallout from the context not being required in the `Store` constructor
anymore.

3. Store drivers are behind a private package called `_drivers` and
they're not suppose to be accessed directly. Instead, functions like
`get_store_from_scheme` should be used.

4. Stores are disabled by default

5. All the store specific options are under the `glance_store` group.

DocImpact:
The old store related configuration options have been moved under the
`glance_store` section. However, the old options will go through a
deprecation path. That is, they'll still be read from the `DEFAULT`
section to give deployers enough time to update their config files.

In k-2, the deprecated options will be completely obsolete.

Closes-bug: #1291848
Implements-blueprint: create-store-package

Change-Id: Iaacc70993ad5da292b93de42bbecda73d53b19fd
2014-09-08 15:48:36 +02:00
He Yongli 01903933db Use six.StringIO/BytesIO instead of StringIO.StringIO
to keep Python 3.x compatibility, use six.StringIO/BytesIO to
replace StringIO.StringIO

StringIO works for unicode
BytesIO works for bytes

Change-Id: I93e043c633e1de9e4dedcb0a313032403b6a70fb
Closes-Bug: #1280100
2014-02-26 09:06:33 +08:00
Julia Varlamova 67c79540c7 Enable hacking H301 and disable H304, H302
Enable hacking H301: one import per line. H304 and H302 check enables too,
so we temporarily disable it since it does not pass.

Fix imports in files below.

Add # noqa to migration scripts.

Change-Id: I6a8d4dcd9c3195d2848f218aafe304b1240ab60c
2014-02-18 16:05:18 +04:00
Julia Varlamova 3fa66f63c4 Enable F841 check
Enable F841 check: local variable 'name' assigned but never used.

Make appropriate changes to files listed below.

Change-Id: I02837d4abf421dc9d85f3b01587120fd68acfa12
2014-02-13 16:54:12 +04:00
Jenkins 9ac0c31608 Merge "fix bug:range() is not same in py3.x and py2.x" 2014-01-24 10:29:37 +00:00
lizheming 56089303f0 fix bug:range() is not same in py3.x and py2.x
in python3.x, there is no method named xrange(),it
has replaced by range(),which is equal to xrange() in
python2.x. so we must fix this issue.we use six module
to fix this issue.

Change-Id: If4fd7478865e8024025af3e57e42f2a24e1e2bbc
Closes-Bug: #1268439
2014-01-21 12:46:20 +08:00
Jenkins 2c4bd69565 Merge "Change assertTrue(isinstance()) by optimal assert" 2014-01-19 05:04:52 +00:00
wanghong 3d03291df8 Change assertTrue(isinstance()) by optimal assert
Some of tests use different method of assertTrue(isinstance(A, B)) or
assertEqual(type(A), B). The correct way is to use assertIsInstance(A, B)
provided by testtools.

Change-Id: Ia8d38f73c159c7ef943a8f6cfe72b945cc493947
Closes-bug: #1268480
2014-01-15 19:01:51 +08:00
Noboru arai 8de10b0f34 Remove vim header
No need to set tabstop tons of times, this can be set in your vimrc
file instead.

More disucssion:
http://openstack.10931.n7.nabble.com/Remove-vim-modelines-td21780.html

Change-Id: I3b830a5bb831a63c188109e6fad66ba48884fff3
Partial-Bug: #1229324
2014-01-14 16:03:28 +00:00
Nassim Babaci 70d2c8c2d8 fix comments and docstrings misspelled words
Change-Id: I355b6fedf372b1273ceaebe22e8e890aa9c913c9
Closes-bug: #1267103
2014-01-08 17:03:44 +01:00
Victor Sergeyev 88bb1af1d5 Enable F40X checking
Clean up imports due to F401 and F403 checks.
F401  'module' imported but unused
F403  unable to detect undefined names with wildcard import

Change-Id: I487edb157de1a6babc7ad8a3fb65f195e476c490
2013-11-27 18:15:48 +02:00
Chang Bo Guo 30696aa243 Clean up numeric expressions with oslo constants
Replace numeric expressions with olso constants to make code
more readable. This patch won't replace single 1024 if 1024 not
in an expression like number * 1024.

Change-Id: Ic1137774f0f9a2be89a7c2706d6eb52eb906ff1f
2013-11-21 20:05:19 -08:00
Jenkins 93edd1bdc6 Merge "Changed header from LLC to Foundation based on trademark policies" 2013-11-12 13:41:45 +00:00
Dirk Mueller 6ba3ebbf8d Start using PyFlakes and Hacking
Instead of globally ignoring Pyflakes and Hacking
warnings, blacklist explitly only those that trigger
numerously. Fix the rest alongway and start to gate
on those that are now passing.

Change-Id: Ia19dc708cf0ccab2da5b46d1652f16e901499c24
2013-11-05 14:23:53 +01:00
Jenkins 3b8a8e73f6 Merge "Remove start index 0 in range()" 2013-10-10 16:00:59 +00:00
Fei Long Wang 29d4383b16 Cache cleaner wrongly deletes cache for non invalid images
Based on current implement of method delete_stalled_files,
it's just deleting all the images located in /incomplete
instead of using the param 'older_than' to check if it
should be deleted. So this fix will use 'older_than' to
confirm brfore deleting.

Fixes bug 1228256

Change-Id: Ica27915f8b9b098ecae1e38c8e678d6acf4f2f76
2013-09-25 10:51:22 +08:00
Dirk Mueller 518bbf7298 Changed header from LLC to Foundation based on trademark policies
Fixes: Bug 1214176

Change-Id: I4e394c33d810fe7d48066ff7cb6143f7eae25f13
2013-09-10 18:29:53 +02:00
Wu Wenxiang 51c5185eea Remove start index 0 in range()
Remove the useless arg ("start index" = 0) in files, since its default
value is 0, to make code cleaner.

Change-Id: I9d91ab8a8033b9a6c9b77608dea92c91a994c7fc
2013-09-06 16:20:33 +08:00
Stuart McLaren 63b64381eb Fix some logic in get_caching_iter
Mismatched indentation meant that more chunks than
expected could be yielded.

Make both filesystem ChunkedFile and S3 ChunkedFile
handle the case where the filepointer no longer exists.

Added unit tests which reproduced the problem
using both a filesystem and S3 ChunkedFile.

Fixes bug 1194929.

Change-Id: I0f70155e59f6bc754c88b5b9e96645459bfe770a
2013-08-14 10:47:01 +00:00
Brian Waldon 1397a92327 Remove unused imports
Change-Id: I3e92e2a010dc9ab333e64d18337995d12ad55bc3
2013-02-28 14:49:05 -08:00
Monty Taylor 43f8697696 Replace custom tearDown with fixtures and cleanup.
testtools.addCleanup is a more resilient way to perform cleanup activities,
as it will continue to clean things up even if there are unforseen problems.

Specifically, replace custom management of tempdirs with fixtures.TempDir
and replace tearDown methods that can be easily replaced with calls to
addCleanup in the setUp method. There are at least two temp dir creations that
did not have a corresponding cleanup in this patch, which is another reason
for using useFixture(fixtures.TempDir) instead of calls to mkdtemp.

Part of blueprint grizzly-testtools.

Change-Id: I4eb548010612bd5a8d30e8e2304fa66d3d5ffb7c
2013-01-17 16:39:40 -05:00
Paul Bourke 95e00c9247 Fix cache not handling backend failures
1) caching_iter doesn't handle backend exceptions:

caching_iter assumes any exception that occurs is the result of being
unable to cache.  Hence the IOError raised from size_checked_iter, which
indicates a problem with the backend, means the caching_iter will
continuing trying to serve non-existent data.  The exception was not
been re-raised in this case, making wsgi keep the connection open and
clients stuck forever waiting for more data.

Raising a GlanceException in size_checked_iter rather than an IOError
allows caching_iter to distinguish between a problem fetching data, and
a problem writing to the cache.

2) Checksum verification happens after cache commit rather than before:

This block was outside the context manager block which meant the
GlanceException was not caught by open_for_write and the rollback didn't
happen.  This resulted in an error been logged, but the bad image still
placed in and subsequently served from the cache.

Also:

* Fix test_gate_caching_iter_bad_checksum - the loop to consume the
  iterator in was in a subroutine that never got called.

* Move test_gate_caching_iter_(good|bad)_checksum into
  ImageCacheTestCase to excercise both the sql and xattr drivers.

* Remove invalid registry_host/registry_port params from
  TestImageCacheXattr/TestImageCacheSqlite setup which caused a failure
  when testing the file on it's own using nosetests.

Fixes bug 1045792

Change-Id: I8aedec347e7f50566c44c5b6c6db424573c5ebaf
2012-09-04 13:39:39 +01:00
Brian Waldon abcff5b5f5 Catch pruner exception when no images are cached
This handles a situation where the pruner would raise an exception when
the image_cache_max_size was set lower than the size of the last image.

Fixes bug 1039854

Change-Id: I4a6c164e9f821f1250314974f829f058fbd02863
2012-08-29 21:17:46 -07:00
RongzeZhu aea4f4416b Remove unused imports
Fixes bug #1037592

Change-Id: I3365d028ab67931517d2f94cc4b7e3752fbdb869
2012-08-17 01:29:34 +00:00
Brian Elliott 19334f0f8d Do not cache images that fail checksum verfication
On an image GET, recalculate the image checksum as the image
data is streamed to the client.  Verify that the checksum matches
the original checksum calculated when the image was added to Glance.
If checksum validation fails, purge the image from the cache.

This type of situation could occur if the backend image store
is malfunctioning.

bug 1028496

Change-Id: I9f38bac8360016bb12b5edaad87c50939a538cc0
2012-08-09 10:17:07 -05:00
Tom Hancock a30887aa3b ^c shouldn't leave incomplete images in cache
Fixes LP bug 1031842

Use a finally clause for the xattr and sqlite
image cache implementations of open_for_write()
such that if the commit() or rollback() aren't
called and the image remains in the incomplete
dir that it is moved to the invalid dir

Change-Id: Id9a0ac56a8ae9fd03af9ccc4989c9b304c95bdde
2012-08-02 16:56:33 +00:00
Josh Kearney 687604baf6 Make functional tests listen on 127.0.0.1.
Fixes bug 987399.

Change-Id: I2a393d9b01c8ae17f289941530a1d4319b174f87
2012-07-20 11:42:51 -05:00
Zhongyue Luo 878b25ea2e Reorder imports by full import path
Fixes bug #1019463

Change-Id: I3fb44cbacb09457ea34571222e0a8e8d4b8d7c87
2012-06-30 08:58:11 +08:00