Merge "Fix problem where image data is not read from a pipe."

This commit is contained in:
Jenkins
2013-06-18 12:47:31 +00:00
committed by Gerrit Code Review
2 changed files with 33 additions and 20 deletions

View File

@@ -122,12 +122,12 @@ def _set_data_field(fields, args):
# (3) no image data provided: # (3) no image data provided:
# glance ... # glance ...
try: try:
stat_result = os.fstat(0) os.fstat(0)
except OSError: except OSError:
# (1) stdin is not valid (closed...) # (1) stdin is not valid (closed...)
fields['data'] = None fields['data'] = None
return return
if not sys.stdin.isatty() and stat_result.st_size != 0: if not sys.stdin.isatty():
# (2) image data is provided through standard input # (2) image data is provided through standard input
if msvcrt: if msvcrt:
msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
@@ -295,7 +295,8 @@ def do_image_update(gc, args):
UPDATE_PARAMS = glanceclient.v1.images.UPDATE_PARAMS UPDATE_PARAMS = glanceclient.v1.images.UPDATE_PARAMS
fields = dict(filter(lambda x: x[0] in UPDATE_PARAMS, fields.items())) fields = dict(filter(lambda x: x[0] in UPDATE_PARAMS, fields.items()))
_set_data_field(fields, args) if image.status == 'queued':
_set_data_field(fields, args)
image = gc.images.update(image, purge_props=args.purge_props, **fields) image = gc.images.update(image, purge_props=args.purge_props, **fields)
_image_show(image, args.human_readable) _image_show(image, args.human_readable)

View File

@@ -18,6 +18,7 @@
import argparse import argparse
import json import json
import os import os
import subprocess
import tempfile import tempfile
import testtools import testtools
@@ -385,23 +386,8 @@ class ShellStdinHandlingTests(testtools.TestCase):
or self.collected_args[1]['data'] is None or self.collected_args[1]['data'] is None
) )
def test_image_update_empty_stdin(self): def test_image_update_data_is_read_from_file(self):
"""Supply glanceclient with an open but empty stdin, and perform an """Ensure that data is read from a file."""
image update to an active image. Glanceclient should not attempt to
read stdin.
"""
self._do_update()
self.assertTrue(
'data' not in self.collected_args[1]
or self.collected_args[1]['data'] is None
)
def test_image_update_data_is_read(self):
"""Ensure that data is read from stdin when image is in queued
status and data is available.
"""
try: try:
@@ -417,6 +403,8 @@ class ShellStdinHandlingTests(testtools.TestCase):
self.assertTrue('data' in self.collected_args[1]) self.assertTrue('data' in self.collected_args[1])
self.assertIsInstance(self.collected_args[1]['data'], file) self.assertIsInstance(self.collected_args[1]['data'], file)
self.assertEqual(self.collected_args[1]['data'].read(),
'Some Data')
finally: finally:
try: try:
@@ -424,3 +412,27 @@ class ShellStdinHandlingTests(testtools.TestCase):
os.remove(f.name) os.remove(f.name)
except: except:
pass pass
def test_image_update_data_is_read_from_pipe(self):
"""Ensure that data is read from a pipe."""
try:
# NOTE(hughsaunders): Setup a pipe, duplicate it to stdin
# ensure it is read.
process = subprocess.Popen(['/bin/echo', 'Some Data'],
stdout=subprocess.PIPE)
os.dup2(process.stdout.fileno(), 0)
self._do_update('44d2c7e1-de4e-4612-8aa2-ba26610c444f')
self.assertTrue('data' in self.collected_args[1])
self.assertIsInstance(self.collected_args[1]['data'], file)
self.assertEqual(self.collected_args[1]['data'].read(),
'Some Data\n')
finally:
try:
process.stdout.close()
except:
pass