|
25 | 25 | import mock |
26 | 26 | from oslo_concurrency import lockutils |
27 | 27 | from oslo_config import fixture as config_fixture |
| 28 | +from oslo_service import loopingcall |
28 | 29 | from oslo_utils import imageutils |
29 | 30 | from oslo_utils import units |
30 | 31 | from oslo_utils import uuidutils |
@@ -1768,6 +1769,230 @@ def test_cleanup_direct_snapshot_destroy_volume(self): |
1768 | 1769 | mock_destroy.assert_called_once_with(image.rbd_name, |
1769 | 1770 | pool=image.driver.pool) |
1770 | 1771 |
|
| 1772 | + @mock.patch('nova.virt.libvirt.imagebackend.IMAGE_API') |
| 1773 | + def test_copy_to_store(self, mock_imgapi): |
| 1774 | + # Test copy_to_store() happy path where we ask for the image |
| 1775 | + # to be copied, it goes into progress and then completes. |
| 1776 | + self.flags(images_rbd_glance_copy_poll_interval=0, |
| 1777 | + group='libvirt') |
| 1778 | + self.flags(images_rbd_glance_store_name='store', |
| 1779 | + group='libvirt') |
| 1780 | + image = self.image_class(self.INSTANCE, self.NAME) |
| 1781 | + mock_imgapi.get.side_effect = [ |
| 1782 | + # Simulate a race between starting the copy and the first poll |
| 1783 | + {'stores': []}, |
| 1784 | + # Second poll shows it in progress |
| 1785 | + {'os_glance_importing_to_stores': ['store'], |
| 1786 | + 'stores': []}, |
| 1787 | + # Third poll shows it has also been copied to a non-local store |
| 1788 | + {'os_glance_importing_to_stores': ['store'], |
| 1789 | + 'stores': ['other']}, |
| 1790 | + # Should-be-last poll shows it complete |
| 1791 | + {'os_glance_importing_to_stores': [], |
| 1792 | + 'stores': ['other', 'store']}, |
| 1793 | + ] |
| 1794 | + image.copy_to_store(self.CONTEXT, {'id': 'foo'}) |
| 1795 | + mock_imgapi.copy_image_to_store.assert_called_once_with( |
| 1796 | + self.CONTEXT, 'foo', 'store') |
| 1797 | + self.assertEqual(4, mock_imgapi.get.call_count) |
| 1798 | + |
| 1799 | + @mock.patch('nova.virt.libvirt.imagebackend.IMAGE_API') |
| 1800 | + def test_copy_to_store_race_with_existing(self, mock_imgapi): |
| 1801 | + # Test copy_to_store() where we race to ask Glance to do the |
| 1802 | + # copy with another node. One of us will get a BadRequest, which |
| 1803 | + # should not cause us to fail. If our desired store is now |
| 1804 | + # in progress, continue to wait like we would have if we had |
| 1805 | + # won the race. |
| 1806 | + self.flags(images_rbd_glance_copy_poll_interval=0, |
| 1807 | + group='libvirt') |
| 1808 | + self.flags(images_rbd_glance_store_name='store', |
| 1809 | + group='libvirt') |
| 1810 | + image = self.image_class(self.INSTANCE, self.NAME) |
| 1811 | + |
| 1812 | + mock_imgapi.copy_image_to_store.side_effect = ( |
| 1813 | + exception.ImageBadRequest(image_id='foo', |
| 1814 | + response='already in progress')) |
| 1815 | + # Make the first poll indicate that the image has already |
| 1816 | + # been copied |
| 1817 | + mock_imgapi.get.return_value = {'stores': ['store', 'other']} |
| 1818 | + |
| 1819 | + # Despite the (expected) exception from the copy, we should |
| 1820 | + # not raise here if the subsequent poll works. |
| 1821 | + image.copy_to_store(self.CONTEXT, {'id': 'foo'}) |
| 1822 | + |
| 1823 | + mock_imgapi.get.assert_called_once_with(self.CONTEXT, |
| 1824 | + 'foo', |
| 1825 | + include_locations=True) |
| 1826 | + mock_imgapi.copy_image_to_store.assert_called_once_with( |
| 1827 | + self.CONTEXT, 'foo', 'store') |
| 1828 | + |
| 1829 | + @mock.patch('nova.virt.libvirt.imagebackend.IMAGE_API') |
| 1830 | + def test_copy_to_store_import_impossible(self, mock_imgapi): |
| 1831 | + # Test copy_to_store() where Glance tells us that the image |
| 1832 | + # is not copy-able for some reason (like it is not active yet |
| 1833 | + # or some other workflow reason). |
| 1834 | + image = self.image_class(self.INSTANCE, self.NAME) |
| 1835 | + mock_imgapi.copy_image_to_store.side_effect = ( |
| 1836 | + exception.ImageImportImpossible(image_id='foo', |
| 1837 | + reason='because tests')) |
| 1838 | + self.assertRaises(exception.ImageUnacceptable, |
| 1839 | + image.copy_to_store, |
| 1840 | + self.CONTEXT, {'id': 'foo'}) |
| 1841 | + |
| 1842 | + @mock.patch('nova.virt.libvirt.imagebackend.IMAGE_API') |
| 1843 | + def test_copy_to_store_import_failed_other_reason(self, mock_imgapi): |
| 1844 | + # Test copy_to_store() where some unexpected failure gets raised. |
| 1845 | + # We should bubble that up so it gets all the way back to the caller |
| 1846 | + # of the clone() itself, which can handle it independent of one of |
| 1847 | + # the image-specific exceptions. |
| 1848 | + image = self.image_class(self.INSTANCE, self.NAME) |
| 1849 | + mock_imgapi.copy_image_to_store.side_effect = test.TestingException |
| 1850 | + # Make sure any other exception makes it through, as those are already |
| 1851 | + # expected failures by the callers of the imagebackend code. |
| 1852 | + self.assertRaises(test.TestingException, |
| 1853 | + image.copy_to_store, |
| 1854 | + self.CONTEXT, {'id': 'foo'}) |
| 1855 | + |
| 1856 | + @mock.patch('nova.virt.libvirt.imagebackend.IMAGE_API') |
| 1857 | + def test_copy_to_store_import_failed_in_progress(self, mock_imgapi): |
| 1858 | + # Test copy_to_store() in the situation where we ask for the copy, |
| 1859 | + # things start to look good (in progress) and later get reported |
| 1860 | + # as failed. |
| 1861 | + self.flags(images_rbd_glance_copy_poll_interval=0, |
| 1862 | + group='libvirt') |
| 1863 | + self.flags(images_rbd_glance_store_name='store', |
| 1864 | + group='libvirt') |
| 1865 | + image = self.image_class(self.INSTANCE, self.NAME) |
| 1866 | + mock_imgapi.get.side_effect = [ |
| 1867 | + # First poll shows it in progress |
| 1868 | + {'os_glance_importing_to_stores': ['store'], |
| 1869 | + 'stores': []}, |
| 1870 | + # Second poll shows it failed |
| 1871 | + {'os_glance_failed_import': ['store'], |
| 1872 | + 'stores': []}, |
| 1873 | + ] |
| 1874 | + exc = self.assertRaises(exception.ImageUnacceptable, |
| 1875 | + image.copy_to_store, |
| 1876 | + self.CONTEXT, {'id': 'foo'}) |
| 1877 | + self.assertIn('unsuccessful because', str(exc)) |
| 1878 | + |
| 1879 | + @mock.patch.object(loopingcall.FixedIntervalWithTimeoutLoopingCall, |
| 1880 | + 'start') |
| 1881 | + @mock.patch('nova.virt.libvirt.imagebackend.IMAGE_API') |
| 1882 | + def test_copy_to_store_import_failed_timeout(self, mock_imgapi, |
| 1883 | + mock_timer_start): |
| 1884 | + # Test copy_to_store() simulating the case where we timeout waiting |
| 1885 | + # for Glance to do the copy. |
| 1886 | + self.flags(images_rbd_glance_store_name='store', |
| 1887 | + group='libvirt') |
| 1888 | + image = self.image_class(self.INSTANCE, self.NAME) |
| 1889 | + mock_timer_start.side_effect = loopingcall.LoopingCallTimeOut() |
| 1890 | + exc = self.assertRaises(exception.ImageUnacceptable, |
| 1891 | + image.copy_to_store, |
| 1892 | + self.CONTEXT, {'id': 'foo'}) |
| 1893 | + self.assertIn('timed out', str(exc)) |
| 1894 | + mock_imgapi.copy_image_to_store.assert_called_once_with( |
| 1895 | + self.CONTEXT, 'foo', 'store') |
| 1896 | + |
| 1897 | + @mock.patch('nova.virt.libvirt.storage.rbd_utils.RBDDriver') |
| 1898 | + @mock.patch('nova.virt.libvirt.imagebackend.IMAGE_API') |
| 1899 | + def test_clone_copy_to_store(self, mock_imgapi, mock_driver_): |
| 1900 | + # Call image.clone() in a way that will cause it to fall through |
| 1901 | + # the locations check to the copy-to-store behavior, and assert |
| 1902 | + # that after the copy, we recurse (without becoming infinite) and |
| 1903 | + # do the check again. |
| 1904 | + self.flags(images_rbd_glance_store_name='store', group='libvirt') |
| 1905 | + fake_image = { |
| 1906 | + 'id': 'foo', |
| 1907 | + 'disk_format': 'raw', |
| 1908 | + 'locations': ['fake'], |
| 1909 | + } |
| 1910 | + mock_imgapi.get.return_value = fake_image |
| 1911 | + mock_driver = mock_driver_.return_value |
| 1912 | + mock_driver.is_cloneable.side_effect = [False, True] |
| 1913 | + image = self.image_class(self.INSTANCE, self.NAME) |
| 1914 | + with mock.patch.object(image, 'copy_to_store') as mock_copy: |
| 1915 | + image.clone(self.CONTEXT, 'foo') |
| 1916 | + mock_copy.assert_called_once_with(self.CONTEXT, fake_image) |
| 1917 | + mock_driver.is_cloneable.assert_has_calls([ |
| 1918 | + # First call is the initial check |
| 1919 | + mock.call('fake', fake_image), |
| 1920 | + # Second call with the same location must be because we |
| 1921 | + # recursed after the copy-to-store operation |
| 1922 | + mock.call('fake', fake_image)]) |
| 1923 | + |
| 1924 | + @mock.patch('nova.virt.libvirt.storage.rbd_utils.RBDDriver') |
| 1925 | + @mock.patch('nova.virt.libvirt.imagebackend.IMAGE_API') |
| 1926 | + def test_clone_copy_to_store_failed(self, mock_imgapi, mock_driver_): |
| 1927 | + # Call image.clone() in a way that will cause it to fall through |
| 1928 | + # the locations check to the copy-to-store behavior, but simulate |
| 1929 | + # some situation where we didn't actually copy the image and the |
| 1930 | + # recursed check does not succeed. Assert that we do not copy again, |
| 1931 | + # nor recurse again, and raise the expected error. |
| 1932 | + self.flags(images_rbd_glance_store_name='store', group='libvirt') |
| 1933 | + fake_image = { |
| 1934 | + 'id': 'foo', |
| 1935 | + 'disk_format': 'raw', |
| 1936 | + 'locations': ['fake'], |
| 1937 | + } |
| 1938 | + mock_imgapi.get.return_value = fake_image |
| 1939 | + mock_driver = mock_driver_.return_value |
| 1940 | + mock_driver.is_cloneable.side_effect = [False, False] |
| 1941 | + image = self.image_class(self.INSTANCE, self.NAME) |
| 1942 | + with mock.patch.object(image, 'copy_to_store') as mock_copy: |
| 1943 | + self.assertRaises(exception.ImageUnacceptable, |
| 1944 | + image.clone, self.CONTEXT, 'foo') |
| 1945 | + mock_copy.assert_called_once_with(self.CONTEXT, fake_image) |
| 1946 | + mock_driver.is_cloneable.assert_has_calls([ |
| 1947 | + # First call is the initial check |
| 1948 | + mock.call('fake', fake_image), |
| 1949 | + # Second call with the same location must be because we |
| 1950 | + # recursed after the copy-to-store operation |
| 1951 | + mock.call('fake', fake_image)]) |
| 1952 | + |
| 1953 | + @mock.patch('nova.virt.libvirt.storage.rbd_utils.RBDDriver') |
| 1954 | + @mock.patch('nova.virt.libvirt.imagebackend.IMAGE_API') |
| 1955 | + def test_clone_without_needed_copy(self, mock_imgapi, mock_driver_): |
| 1956 | + # Call image.clone() in a way that will cause it to pass the locations |
| 1957 | + # check the first time. Assert that we do not call copy-to-store |
| 1958 | + # nor recurse. |
| 1959 | + self.flags(images_rbd_glance_store_name='store', group='libvirt') |
| 1960 | + fake_image = { |
| 1961 | + 'id': 'foo', |
| 1962 | + 'disk_format': 'raw', |
| 1963 | + 'locations': ['fake'], |
| 1964 | + } |
| 1965 | + mock_imgapi.get.return_value = fake_image |
| 1966 | + mock_driver = mock_driver_.return_value |
| 1967 | + mock_driver.is_cloneable.return_value = True |
| 1968 | + image = self.image_class(self.INSTANCE, self.NAME) |
| 1969 | + with mock.patch.object(image, 'copy_to_store') as mock_copy: |
| 1970 | + image.clone(self.CONTEXT, 'foo') |
| 1971 | + mock_copy.assert_not_called() |
| 1972 | + mock_driver.is_cloneable.assert_called_once_with('fake', fake_image) |
| 1973 | + |
| 1974 | + @mock.patch('nova.virt.libvirt.storage.rbd_utils.RBDDriver') |
| 1975 | + @mock.patch('nova.virt.libvirt.imagebackend.IMAGE_API') |
| 1976 | + def test_clone_copy_not_configured(self, mock_imgapi, mock_driver_): |
| 1977 | + # Call image.clone() in a way that will cause it to fail the locations |
| 1978 | + # check the first time. Assert that if the store name is not configured |
| 1979 | + # we do not try to copy-to-store and just raise the original exception |
| 1980 | + # indicating that the image is not reachable. |
| 1981 | + fake_image = { |
| 1982 | + 'id': 'foo', |
| 1983 | + 'disk_format': 'raw', |
| 1984 | + 'locations': ['fake'], |
| 1985 | + } |
| 1986 | + mock_imgapi.get.return_value = fake_image |
| 1987 | + mock_driver = mock_driver_.return_value |
| 1988 | + mock_driver.is_cloneable.return_value = False |
| 1989 | + image = self.image_class(self.INSTANCE, self.NAME) |
| 1990 | + with mock.patch.object(image, 'copy_to_store') as mock_copy: |
| 1991 | + self.assertRaises(exception.ImageUnacceptable, |
| 1992 | + image.clone, self.CONTEXT, 'foo') |
| 1993 | + mock_copy.assert_not_called() |
| 1994 | + mock_driver.is_cloneable.assert_called_once_with('fake', fake_image) |
| 1995 | + |
1771 | 1996 |
|
1772 | 1997 | class PloopTestCase(_ImageTestCase, test.NoDBTestCase): |
1773 | 1998 | SIZE = 1024 |
|
0 commit comments