2222from osc_lib .cli import format_columns
2323from osc_lib import exceptions
2424
25- from openstackclient .image .v2 import image
25+ from openstackclient .image .v2 import image as _image
2626from openstackclient .tests .unit .identity .v3 import fakes as identity_fakes
2727from openstackclient .tests .unit .image .v2 import fakes as image_fakes
2828from openstackclient .tests .unit .volume .v3 import fakes as volume_fakes
@@ -73,19 +73,19 @@ def setUp(self):
7373 self .client .update_image .return_value = self .new_image
7474
7575 (self .expected_columns , self .expected_data ) = zip (
76- * sorted (image ._format_image (self .new_image ).items ()))
76+ * sorted (_image ._format_image (self .new_image ).items ()))
7777
7878 # Get the command object to test
79- self .cmd = image .CreateImage (self .app , None )
79+ self .cmd = _image .CreateImage (self .app , None )
8080
8181 @mock .patch ("sys.stdin" , side_effect = [None ])
8282 def test_image_reserve_no_options (self , raw_input ):
8383 arglist = [
8484 self .new_image .name
8585 ]
8686 verifylist = [
87- ('container_format' , image .DEFAULT_CONTAINER_FORMAT ),
88- ('disk_format' , image .DEFAULT_DISK_FORMAT ),
87+ ('container_format' , _image .DEFAULT_CONTAINER_FORMAT ),
88+ ('disk_format' , _image .DEFAULT_DISK_FORMAT ),
8989 ('name' , self .new_image .name ),
9090 ]
9191 parsed_args = self .check_parser (self .cmd , arglist , verifylist )
@@ -99,8 +99,8 @@ def test_image_reserve_no_options(self, raw_input):
9999 self .client .create_image .assert_called_with (
100100 name = self .new_image .name ,
101101 allow_duplicates = True ,
102- container_format = image .DEFAULT_CONTAINER_FORMAT ,
103- disk_format = image .DEFAULT_DISK_FORMAT ,
102+ container_format = _image .DEFAULT_CONTAINER_FORMAT ,
103+ disk_format = _image .DEFAULT_DISK_FORMAT ,
104104 )
105105
106106 self .assertEqual (self .expected_columns , columns )
@@ -224,8 +224,8 @@ def test_image_create_file(self):
224224 self .client .create_image .assert_called_with (
225225 name = self .new_image .name ,
226226 allow_duplicates = True ,
227- container_format = image .DEFAULT_CONTAINER_FORMAT ,
228- disk_format = image .DEFAULT_DISK_FORMAT ,
227+ container_format = _image .DEFAULT_CONTAINER_FORMAT ,
228+ disk_format = _image .DEFAULT_DISK_FORMAT ,
229229 is_protected = self .new_image .is_protected ,
230230 visibility = self .new_image .visibility ,
231231 Alpha = '1' ,
@@ -245,7 +245,7 @@ def test_image_create_file(self):
245245 def test_image_create__progress_ignore_with_stdin (
246246 self , mock_get_data_from_stdin ,
247247 ):
248- fake_stdin = io .StringIO ( ' fake-image- data' )
248+ fake_stdin = io .BytesIO ( b'some fake data' )
249249 mock_get_data_from_stdin .return_value = fake_stdin
250250
251251 arglist = [
@@ -263,8 +263,8 @@ def test_image_create__progress_ignore_with_stdin(
263263 self .client .create_image .assert_called_with (
264264 name = self .new_image .name ,
265265 allow_duplicates = True ,
266- container_format = image .DEFAULT_CONTAINER_FORMAT ,
267- disk_format = image .DEFAULT_DISK_FORMAT ,
266+ container_format = _image .DEFAULT_CONTAINER_FORMAT ,
267+ disk_format = _image .DEFAULT_DISK_FORMAT ,
268268 data = fake_stdin ,
269269 validate_checksum = False ,
270270 )
@@ -305,8 +305,8 @@ def test_image_create_import(self, raw_input):
305305 self .client .create_image .assert_called_with (
306306 name = self .new_image .name ,
307307 allow_duplicates = True ,
308- container_format = image .DEFAULT_CONTAINER_FORMAT ,
309- disk_format = image .DEFAULT_DISK_FORMAT ,
308+ container_format = _image .DEFAULT_CONTAINER_FORMAT ,
309+ disk_format = _image .DEFAULT_DISK_FORMAT ,
310310 use_import = True
311311 )
312312
@@ -445,7 +445,7 @@ def setUp(self):
445445 self .project_mock .get .return_value = self .project
446446 self .domain_mock .get .return_value = self .domain
447447 # Get the command object to test
448- self .cmd = image .AddProjectToImage (self .app , None )
448+ self .cmd = _image .AddProjectToImage (self .app , None )
449449
450450 def test_add_project_to_image_no_option (self ):
451451 arglist = [
@@ -504,7 +504,7 @@ def setUp(self):
504504 self .client .delete_image .return_value = None
505505
506506 # Get the command object to test
507- self .cmd = image .DeleteImage (self .app , None )
507+ self .cmd = _image .DeleteImage (self .app , None )
508508
509509 def test_image_delete_no_options (self ):
510510 images = self .setup_images_mock (count = 1 )
@@ -595,7 +595,7 @@ def setUp(self):
595595 self .client .images .side_effect = [[self ._image ], []]
596596
597597 # Get the command object to test
598- self .cmd = image .ListImage (self .app , None )
598+ self .cmd = _image .ListImage (self .app , None )
599599
600600 def test_image_list_no_options (self ):
601601 arglist = []
@@ -993,7 +993,7 @@ def setUp(self):
993993 self .client .find_image .return_value = self ._image
994994 self .client .members .return_value = [self .member ]
995995
996- self .cmd = image .ListImageProjects (self .app , None )
996+ self .cmd = _image .ListImageProjects (self .app , None )
997997
998998 def test_image_member_list (self ):
999999 arglist = [
@@ -1028,7 +1028,7 @@ def setUp(self):
10281028 self .domain_mock .get .return_value = self .domain
10291029 self .client .remove_member .return_value = None
10301030 # Get the command object to test
1031- self .cmd = image .RemoveProjectImage (self .app , None )
1031+ self .cmd = _image .RemoveProjectImage (self .app , None )
10321032
10331033 def test_remove_project_image_no_options (self ):
10341034 arglist = [
@@ -1095,7 +1095,7 @@ def setUp(self):
10951095 )
10961096
10971097 # Get the command object to test
1098- self .cmd = image .SetImage (self .app , None )
1098+ self .cmd = _image .SetImage (self .app , None )
10991099
11001100 def test_image_set_no_options (self ):
11011101 arglist = [
@@ -1624,7 +1624,7 @@ def setUp(self):
16241624 self .client .find_image = mock .Mock (return_value = self ._data )
16251625
16261626 # Get the command object to test
1627- self .cmd = image .ShowImage (self .app , None )
1627+ self .cmd = _image .ShowImage (self .app , None )
16281628
16291629 def test_image_show (self ):
16301630 arglist = [
@@ -1689,7 +1689,7 @@ def setUp(self):
16891689 self .client .update_image .return_value = self .image
16901690
16911691 # Get the command object to test
1692- self .cmd = image .UnsetImage (self .app , None )
1692+ self .cmd = _image .UnsetImage (self .app , None )
16931693
16941694 def test_image_unset_no_options (self ):
16951695 arglist = [
@@ -1769,6 +1769,60 @@ def test_image_unset_mixed_option(self):
17691769 self .assertIsNone (result )
17701770
17711771
1772+ class TestImageStage (TestImage ):
1773+
1774+ image = image_fakes .create_one_image ({})
1775+
1776+ def setUp (self ):
1777+ super ().setUp ()
1778+
1779+ self .client .find_image .return_value = self .image
1780+
1781+ self .cmd = _image .StageImage (self .app , None )
1782+
1783+ def test_stage_image__from_file (self ):
1784+ imagefile = tempfile .NamedTemporaryFile (delete = False )
1785+ imagefile .write (b'\0 ' )
1786+ imagefile .close ()
1787+
1788+ arglist = [
1789+ '--file' , imagefile .name ,
1790+ self .image .name ,
1791+ ]
1792+ verifylist = [
1793+ ('filename' , imagefile .name ),
1794+ ('image' , self .image .name ),
1795+ ]
1796+ parsed_args = self .check_parser (self .cmd , arglist , verifylist )
1797+
1798+ self .cmd .take_action (parsed_args )
1799+
1800+ self .client .stage_image .assert_called_once_with (
1801+ self .image ,
1802+ filename = imagefile .name ,
1803+ )
1804+
1805+ @mock .patch ('openstackclient.image.v2.image.get_data_from_stdin' )
1806+ def test_stage_image__from_stdin (self , mock_get_data_from_stdin ):
1807+ fake_stdin = io .BytesIO (b"some initial binary data: \x00 \x01 " )
1808+ mock_get_data_from_stdin .return_value = fake_stdin
1809+
1810+ arglist = [
1811+ self .image .name ,
1812+ ]
1813+ verifylist = [
1814+ ('image' , self .image .name ),
1815+ ]
1816+ parsed_args = self .check_parser (self .cmd , arglist , verifylist )
1817+
1818+ self .cmd .take_action (parsed_args )
1819+
1820+ self .client .stage_image .assert_called_once_with (
1821+ self .image ,
1822+ data = fake_stdin ,
1823+ )
1824+
1825+
17721826class TestImageSave (TestImage ):
17731827
17741828 image = image_fakes .create_one_image ({})
@@ -1780,7 +1834,7 @@ def setUp(self):
17801834 self .client .download_image .return_value = self .image
17811835
17821836 # Get the command object to test
1783- self .cmd = image .SaveImage (self .app , None )
1837+ self .cmd = _image .SaveImage (self .app , None )
17841838
17851839 def test_save_data (self ):
17861840
@@ -1810,7 +1864,7 @@ def test_get_data_from_stdin(self):
18101864 stdin .isatty .return_value = False
18111865 stdin .buffer = fd
18121866
1813- test_fd = image .get_data_from_stdin ()
1867+ test_fd = _image .get_data_from_stdin ()
18141868
18151869 # Ensure data written to temp file is correct
18161870 self .assertEqual (fd , test_fd )
@@ -1822,6 +1876,6 @@ def test_get_data_from_stdin__interactive(self):
18221876 # There is stdin, but interactive
18231877 stdin .return_value = fd
18241878
1825- test_fd = image .get_data_from_stdin ()
1879+ test_fd = _image .get_data_from_stdin ()
18261880
18271881 self .assertIsNone (test_fd )
0 commit comments