Skip to content

Instantly share code, notes, and snippets.

@anteaya
Last active December 10, 2015 18:38
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save anteaya/afaf3047a28162ea3661 to your computer and use it in GitHub Desktop.
my current status of understanding regarding bug #1080515
def get_images_image id with spaces(self, **kw):
return(404, {}, {})
Following the bug report output for bug #1080515:
>>> import novaclient.v1_1.client
>>> client = novaclient.v1_1.client.Client('demo', 'secrete', 'demo', 'http://192.168.27.100:5000/v2.0')
>>> client.images.get(name='name with spaces')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: get() got an unexpected keyword argument 'name'
>>> client.images.get('invalid id')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/bcwaldon/.virtualenvwrapper/openstack-clients/lib/python2.7/site-packages/novaclient/v1_1/images.py", line 38, in get
return self._get("/images/%s" % base.getid(image), "image")
File "/Users/bcwaldon/.virtualenvwrapper/openstack-clients/lib/python2.7/site-packages/novaclient/base.py", line 142, in _get
return self.resource_class(self, body[response_key], loaded=True)
TypeError: string indices must be integers, not str
I can get a client instance of Nova to fetch an image with an image id:
>>> client.images.get('9cc8b12f-340b-4d33-83b4-052ac35439d1')
<Image: cirros-0.3.0-x86_64-uec>
But I can't figure out how to successfully use get() with an image name:
>>> client.images.get(name='name with spaces')
...
TypeError: get() got an unexpected keyword argument 'name'
>>> client.images.get('cirros-0.3.0-x86_64-uec')
...
novaclient.exceptions.NotFound: Image not found.
>>> client.images.get('cirros 0.3.0 x86_64 uec')
...
novaclient. exceptions.BadRequest: HTTPBadRequest
>>> client.images.get('cirros%200.3.0%20x86_64%20uec')
...
novaclient. exceptions.BadRequest: HTTPBadRequest
>>> import urllib
>>> image_name = urllib.quote('cirros 0.3.0 x86_64 uec')
>>> print image_name
cirros%200.3.0%20x86_64%20uec
>>> client.images.get(image_name)
...
novaclient.exceptions.BadRequest: HTTPBadRequest
It appears that asserting %20 does not result in Image Not Found, it results in Bad Request.
But if I assert %2D I get an Image Not Found error.
So I must conclude that I need to assert %2D to properly create a patch to solve this bug.
>>> client.images.get('cirros%2D0.3.0%2Dx86_64%2Duec')
...
novaclient.exceptions.NotFound: Image not found.
>>> print image_name
cirros%200.3.0%20x86_64%20uec
>>> print image_name.replace("%20", "%2D")
cirros%2D0.3.0%2Dx86_64%2Duec
>>> new_image_name = image_name.replace("%20", "%2D")
>>> client.images.get(new_image_name)
...
novaclient.exceptions.NotFound: Image not found.
I wonder if rather than invoking urllib.quote() and then replace(),
I can just go straight to replace(' ', '%2D')
name_with_spaces = ('cirros 0.3.0 x86_64 uec')
fixed_name_with_spaces = name_with_spaces.replace(' ', '%2D')
client.images.get(fixed_name_with_spaces)
...
novaclient.exceptions.NotFound: Image not found.
def test_get_image_with_hyphen_encoded_url(self):
i = cs.images.get('image id with spaces')
cs.assert_called('GET')
self.assertTrue('image%2Did%2Dwith%2Dspaces')
self.assertError('ImageNotFound')
I understand that you want the param passed to the get() method to have its
whitespace replaced with a url encoded character so that the Image Not Found error
is thrown if the image doesn't exist. At least that is the understanding I am working with.
I have to admit that I don't understand the use case though. When I look at
the get() method on the ImageManager class the method takes one param, a string,
the id of the image:
https://github.com/openstack/python-novaclient/blob/master/novaclient/v1_1/images.py#L35
I have tried to call get() with a legitimate name:`client.images.get('cirros-0.3.0-x86_64-uec')` and
I get a 404 Image Not Found error. So even using the name of an existing image
doesn't result in the image being returned.
Calling get() with the image id is the only way I have been able to succeed in having
an instance of novaclient fetch me an image, and image id's all have hyphen's in them.
I did try to change the name of an image using
`nova image-meta 9cc8b12f-340b-4d33-83b4-052ac35439d1 set name="NewName"`
but the name did not change, so I don't know what I am doing wrong there.
When I try to pass get() a dictionary (name="name") for the param,
I get an error since it doesn't know what to do
with the key "name". Get() is expecting a single string as the param.
The only way I can get get() to fetch me an image is using the image id
which has hyphens in it, so I don't know when someone would pass in an image id with spaces.
I have figured out how to get a string passed in as a param to get() to throw
an Image Not Found error, the whitespace needs to be replaced with
hyphens -> string.replace(' ', '%2D'), however when a legitimate name
with spaces has the spaces replaced with url hyphens '%2D', get() still doesn't fetch
the image, it returns Image Not Found.
I am learning a lot by working on this bug.
I welcome your guidance about how to apply my energy constructively going forward.
Thanks,
Anita.
@anteaya
Copy link
Author

anteaya commented Jan 7, 2013

The fake has a name that won't work since it has spaces in it, it results in a syntax error.

However I need to pass a param with spaces in it since a param with spaces in it is what I am testing.

I need to learn more about writing fakes properly to understand how to write a good test and have a co-responding fake with the correct syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment