summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.openstack.org>2018-07-13 05:38:06 +0000
committerGerrit Code Review <review@openstack.org>2018-07-13 05:38:06 +0000
commitf67180d8b065b92f543b64ed3b92241a4b2fb04f (patch)
tree1ad4804688cb8d6b22b651b8678f626c92749602
parentaa50408cbc357c0ed3bda0bbb64ba675db5e42ef (diff)
parent60c0b9c0af8c056b952c782dc00ccfa84a8b3e05 (diff)
Merge "Replace os.makedirs to avoid process race"
-rw-r--r--cinder/image/image_utils.py8
-rw-r--r--cinder/tests/unit/test_image_utils.py32
-rw-r--r--cinder/volume/flows/manager/create_volume.py7
3 files changed, 18 insertions, 29 deletions
diff --git a/cinder/image/image_utils.py b/cinder/image/image_utils.py
index 6c25ff7..893f7c0 100644
--- a/cinder/image/image_utils.py
+++ b/cinder/image/image_utils.py
@@ -686,9 +686,7 @@ def coalesce_vhd(vhd_path):
686 686
687 687
688def create_temporary_file(*args, **kwargs): 688def create_temporary_file(*args, **kwargs):
689 if (CONF.image_conversion_dir and not 689 fileutils.ensure_tree(CONF.image_conversion_dir)
690 os.path.exists(CONF.image_conversion_dir)):
691 os.makedirs(CONF.image_conversion_dir)
692 690
693 fd, tmp = tempfile.mkstemp(dir=CONF.image_conversion_dir, *args, **kwargs) 691 fd, tmp = tempfile.mkstemp(dir=CONF.image_conversion_dir, *args, **kwargs)
694 os.close(fd) 692 os.close(fd)
@@ -730,9 +728,7 @@ def temporary_file(*args, **kwargs):
730 728
731 729
732def temporary_dir(): 730def temporary_dir():
733 if (CONF.image_conversion_dir and not 731 fileutils.ensure_tree(CONF.image_conversion_dir)
734 os.path.exists(CONF.image_conversion_dir)):
735 os.makedirs(CONF.image_conversion_dir)
736 732
737 return utils.tempdir(dir=CONF.image_conversion_dir) 733 return utils.tempdir(dir=CONF.image_conversion_dir)
738 734
diff --git a/cinder/tests/unit/test_image_utils.py b/cinder/tests/unit/test_image_utils.py
index 93fceaf..f47880f 100644
--- a/cinder/tests/unit/test_image_utils.py
+++ b/cinder/tests/unit/test_image_utils.py
@@ -592,24 +592,22 @@ class TestVerifyImage(test.TestCase):
592 592
593class TestTemporaryDir(test.TestCase): 593class TestTemporaryDir(test.TestCase):
594 @mock.patch('cinder.image.image_utils.CONF') 594 @mock.patch('cinder.image.image_utils.CONF')
595 @mock.patch('os.makedirs') 595 @mock.patch('oslo_utils.fileutils.ensure_tree')
596 @mock.patch('os.path.exists', return_value=True)
597 @mock.patch('cinder.image.image_utils.utils.tempdir') 596 @mock.patch('cinder.image.image_utils.utils.tempdir')
598 def test_conv_dir_exists(self, mock_tempdir, mock_exists, mock_make, 597 def test_conv_dir_exists(self, mock_tempdir, mock_make,
599 mock_conf): 598 mock_conf):
600 mock_conf.image_conversion_dir = mock.sentinel.conv_dir 599 mock_conf.image_conversion_dir = mock.sentinel.conv_dir
601 600
602 output = image_utils.temporary_dir() 601 output = image_utils.temporary_dir()
603 602
604 self.assertFalse(mock_make.called) 603 self.assertTrue(mock_make.called)
605 mock_tempdir.assert_called_once_with(dir=mock.sentinel.conv_dir) 604 mock_tempdir.assert_called_once_with(dir=mock.sentinel.conv_dir)
606 self.assertEqual(output, mock_tempdir.return_value) 605 self.assertEqual(output, mock_tempdir.return_value)
607 606
608 @mock.patch('cinder.image.image_utils.CONF') 607 @mock.patch('cinder.image.image_utils.CONF')
609 @mock.patch('os.makedirs') 608 @mock.patch('oslo_utils.fileutils.ensure_tree')
610 @mock.patch('os.path.exists', return_value=False)
611 @mock.patch('cinder.image.image_utils.utils.tempdir') 609 @mock.patch('cinder.image.image_utils.utils.tempdir')
612 def test_create_conv_dir(self, mock_tempdir, mock_exists, mock_make, 610 def test_create_conv_dir(self, mock_tempdir, mock_make,
613 mock_conf): 611 mock_conf):
614 mock_conf.image_conversion_dir = mock.sentinel.conv_dir 612 mock_conf.image_conversion_dir = mock.sentinel.conv_dir
615 613
@@ -620,16 +618,15 @@ class TestTemporaryDir(test.TestCase):
620 self.assertEqual(output, mock_tempdir.return_value) 618 self.assertEqual(output, mock_tempdir.return_value)
621 619
622 @mock.patch('cinder.image.image_utils.CONF') 620 @mock.patch('cinder.image.image_utils.CONF')
623 @mock.patch('os.makedirs') 621 @mock.patch('oslo_utils.fileutils.ensure_tree')
624 @mock.patch('os.path.exists', return_value=False)
625 @mock.patch('cinder.image.image_utils.utils.tempdir') 622 @mock.patch('cinder.image.image_utils.utils.tempdir')
626 def test_no_conv_dir(self, mock_tempdir, mock_exists, mock_make, 623 def test_no_conv_dir(self, mock_tempdir, mock_make,
627 mock_conf): 624 mock_conf):
628 mock_conf.image_conversion_dir = None 625 mock_conf.image_conversion_dir = None
629 626
630 output = image_utils.temporary_dir() 627 output = image_utils.temporary_dir()
631 628
632 self.assertFalse(mock_make.called) 629 self.assertTrue(mock_make.called)
633 mock_tempdir.assert_called_once_with(dir=None) 630 mock_tempdir.assert_called_once_with(dir=None)
634 self.assertEqual(output, mock_tempdir.return_value) 631 self.assertEqual(output, mock_tempdir.return_value)
635 632
@@ -1704,11 +1701,10 @@ class TestVhdUtils(test.TestCase):
1704class TestCreateTemporaryFile(test.TestCase): 1701class TestCreateTemporaryFile(test.TestCase):
1705 @mock.patch('cinder.image.image_utils.os.close') 1702 @mock.patch('cinder.image.image_utils.os.close')
1706 @mock.patch('cinder.image.image_utils.CONF') 1703 @mock.patch('cinder.image.image_utils.CONF')
1707 @mock.patch('cinder.image.image_utils.os.path.exists')
1708 @mock.patch('cinder.image.image_utils.os.makedirs') 1704 @mock.patch('cinder.image.image_utils.os.makedirs')
1709 @mock.patch('cinder.image.image_utils.tempfile.mkstemp') 1705 @mock.patch('cinder.image.image_utils.tempfile.mkstemp')
1710 def test_create_temporary_file_no_dir(self, mock_mkstemp, mock_dirs, 1706 def test_create_temporary_file_no_dir(self, mock_mkstemp, mock_dirs,
1711 mock_path, mock_conf, mock_close): 1707 mock_conf, mock_close):
1712 mock_conf.image_conversion_dir = None 1708 mock_conf.image_conversion_dir = None
1713 fd = mock.sentinel.file_descriptor 1709 fd = mock.sentinel.file_descriptor
1714 path = mock.sentinel.absolute_pathname 1710 path = mock.sentinel.absolute_pathname
@@ -1722,11 +1718,10 @@ class TestCreateTemporaryFile(test.TestCase):
1722 1718
1723 @mock.patch('cinder.image.image_utils.os.close') 1719 @mock.patch('cinder.image.image_utils.os.close')
1724 @mock.patch('cinder.image.image_utils.CONF') 1720 @mock.patch('cinder.image.image_utils.CONF')
1725 @mock.patch('cinder.image.image_utils.os.path.exists', return_value=True)
1726 @mock.patch('cinder.image.image_utils.os.makedirs') 1721 @mock.patch('cinder.image.image_utils.os.makedirs')
1727 @mock.patch('cinder.image.image_utils.tempfile.mkstemp') 1722 @mock.patch('cinder.image.image_utils.tempfile.mkstemp')
1728 def test_create_temporary_file_with_dir(self, mock_mkstemp, mock_dirs, 1723 def test_create_temporary_file_with_dir(self, mock_mkstemp, mock_dirs,
1729 mock_path, mock_conf, mock_close): 1724 mock_conf, mock_close):
1730 conv_dir = mock.sentinel.image_conversion_dir 1725 conv_dir = mock.sentinel.image_conversion_dir
1731 mock_conf.image_conversion_dir = conv_dir 1726 mock_conf.image_conversion_dir = conv_dir
1732 fd = mock.sentinel.file_descriptor 1727 fd = mock.sentinel.file_descriptor
@@ -1736,17 +1731,16 @@ class TestCreateTemporaryFile(test.TestCase):
1736 output = image_utils.create_temporary_file() 1731 output = image_utils.create_temporary_file()
1737 1732
1738 self.assertEqual(path, output) 1733 self.assertEqual(path, output)
1739 self.assertFalse(mock_dirs.called) 1734 self.assertTrue(mock_dirs.called)
1740 mock_mkstemp.assert_called_once_with(dir=conv_dir) 1735 mock_mkstemp.assert_called_once_with(dir=conv_dir)
1741 mock_close.assert_called_once_with(fd) 1736 mock_close.assert_called_once_with(fd)
1742 1737
1743 @mock.patch('cinder.image.image_utils.os.close') 1738 @mock.patch('cinder.image.image_utils.os.close')
1744 @mock.patch('cinder.image.image_utils.CONF') 1739 @mock.patch('cinder.image.image_utils.CONF')
1745 @mock.patch('cinder.image.image_utils.os.path.exists', return_value=False) 1740 @mock.patch('cinder.image.image_utils.fileutils.ensure_tree')
1746 @mock.patch('cinder.image.image_utils.os.makedirs')
1747 @mock.patch('cinder.image.image_utils.tempfile.mkstemp') 1741 @mock.patch('cinder.image.image_utils.tempfile.mkstemp')
1748 def test_create_temporary_file_and_dir(self, mock_mkstemp, mock_dirs, 1742 def test_create_temporary_file_and_dir(self, mock_mkstemp, mock_dirs,
1749 mock_path, mock_conf, mock_close): 1743 mock_conf, mock_close):
1750 conv_dir = mock.sentinel.image_conversion_dir 1744 conv_dir = mock.sentinel.image_conversion_dir
1751 mock_conf.image_conversion_dir = conv_dir 1745 mock_conf.image_conversion_dir = conv_dir
1752 fd = mock.sentinel.file_descriptor 1746 fd = mock.sentinel.file_descriptor
diff --git a/cinder/volume/flows/manager/create_volume.py b/cinder/volume/flows/manager/create_volume.py
index e9e6bd1..8ecf2d7 100644
--- a/cinder/volume/flows/manager/create_volume.py
+++ b/cinder/volume/flows/manager/create_volume.py
@@ -10,13 +10,13 @@
10# License for the specific language governing permissions and limitations 10# License for the specific language governing permissions and limitations
11# under the License. 11# under the License.
12 12
13import os
14import traceback 13import traceback
15 14
16from oslo_concurrency import processutils 15from oslo_concurrency import processutils
17from oslo_config import cfg 16from oslo_config import cfg
18from oslo_log import log as logging 17from oslo_log import log as logging
19from oslo_utils import excutils 18from oslo_utils import excutils
19from oslo_utils import fileutils
20from oslo_utils import timeutils 20from oslo_utils import timeutils
21import taskflow.engines 21import taskflow.engines
22from taskflow.patterns import linear_flow 22from taskflow.patterns import linear_flow
@@ -762,9 +762,8 @@ class CreateVolumeFromSpecTask(flow_utils.CinderTask):
762 # NOTE(mnaser): This check *only* happens if the backend is not able 762 # NOTE(mnaser): This check *only* happens if the backend is not able
763 # to clone volumes and we have to resort to downloading 763 # to clone volumes and we have to resort to downloading
764 # the image from Glance and uploading it. 764 # the image from Glance and uploading it.
765 if (CONF.image_conversion_dir and not 765 if CONF.image_conversion_dir:
766 os.path.exists(CONF.image_conversion_dir)): 766 fileutils.ensure_tree(CONF.image_conversion_dir)
767 os.makedirs(CONF.image_conversion_dir)
768 try: 767 try:
769 image_utils.check_available_space( 768 image_utils.check_available_space(
770 CONF.image_conversion_dir, 769 CONF.image_conversion_dir,