Using regular expression fails unit test

Asked by Vahid Hashemian on 2014-03-06

Sorry if this is not the best forum to ask this question, but I have run into an issue with using regular expressions on a Horizon patch I'm working on:

When I try to search for instances whose names follow a regular expression the unit test for its module returns an error saying:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 9-11: ordinal not in range(128)

Here's some more information:

The code:
                instances, self._more = api.nova.server_list(
                    self.request,
                    search_opts={'name': "^" + ins_name + "$"})

The error returned:
======================================================================
ERROR: test_launch_form_instance_duplicate_case_insensitive_name_error (openstack_dashboard.dashboards.project.instances.tests.InstanceTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/django/test/utils.py", line 224, in inner
    return test_func(*args, **kwargs)
  File "/opt/stack/horizon/openstack_dashboard/test/helpers.py", line 80, in instance_stub_out
    return fn(self, *args, **kwargs)
  File "/opt/stack/horizon/openstack_dashboard/dashboards/project/instances/tests.py", line 2249, in test_launch_form_instance_duplicate_case_insensitive_name_error
    res = self.client.post(url, form_data)
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/django/test/client.py", line 463, in post
    response = super(Client, self).post(path, data=data, content_type=content_type, **extra)
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/django/test/client.py", line 297, in post
    return self.request(**r)
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/django/test/client.py", line 424, in request
    six.reraise(*exc_info)
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/opt/stack/horizon/horizon/decorators.py", line 38, in dec
    return view_func(request, *args, **kwargs)
  File "/opt/stack/horizon/horizon/decorators.py", line 54, in dec
    return view_func(request, *args, **kwargs)
  File "/opt/stack/horizon/horizon/decorators.py", line 38, in dec
    return view_func(request, *args, **kwargs)
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/django/views/generic/base.py", line 86, in dispatch
    return handler(request, *args, **kwargs)
  File "/opt/stack/horizon/horizon/workflows/views.py", line 165, in post
    context = self.get_context_data(**kwargs)
  File "/opt/stack/horizon/horizon/workflows/views.py", line 89, in get_context_data
    workflow = self.get_workflow()
  File "/opt/stack/horizon/horizon/workflows/views.py", line 79, in get_workflow
    entry_point=entry_point)
  File "/opt/stack/horizon/horizon/workflows/base.py", line 650, in __init__
    valid = step.action.is_valid()
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/django/forms/forms.py", line 126, in is_valid
    return self.is_bound and not bool(self.errors)
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/django/forms/forms.py", line 117, in _get_errors
    self.full_clean()
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/django/forms/forms.py", line 273, in full_clean
    self._clean_form()
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/django/forms/forms.py", line 299, in _clean_form
    self.cleaned_data = self.clean()
  File "/opt/stack/horizon/openstack_dashboard/dashboards/project/instances/workflows/create_instance.py", line 199, in clean
    redirect=redirect)
  File "/opt/stack/horizon/openstack_dashboard/dashboards/project/instances/workflows/create_instance.py", line 194, in clean
    search_opts={'name': "*" + ins_name + "*"})
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/mox.py", line 765, in __call__
    return mock_method(*params, **named_params)
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/mox.py", line 1002, in __call__
    expected_method = self._VerifyMethodCall()
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/mox.py", line 1060, in _VerifyMethodCall
    raise UnexpectedMethodCallError(self, expected)
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/mox.py", line 131, in __init__
    str(expected).splitlines(True))
  File "/opt/stack/horizon/.venv/local/lib/python2.7/site-packages/mox.py", line 1068, in __str__
    full_desc = "%s(%s) -> %r" % (self._name, params, self._return_value)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 9-11: ordinal not in range(128)

Note that if I change the code to:
                instances, self._more = api.nova.server_list(
                    self.request,
                    search_opts={'name': ins_name})
the test runs fine and no error is returned.

Does anyone know what's causing this and how to get around it?

Thanks.

Question information

Language:
English Edit question
Status:
Solved
For:
OpenStack Dashboard (Horizon) Edit question
Assignee:
No assignee Edit question
Solved by:
Vahid Hashemian
Solved:
2014-03-08
Last query:
2014-03-08
Last reply:
2014-03-07
Julie Pichon (jpichon) said : #1

Vahid - the 'Answers' section of Launchpad is deprecated, http://ask.openstack.org/ should be used from now on. Though for such development questions, asking on IRC (#openstack-horizon) or on the dev list is probably better.

Did you try making the string you're concatenating with into unicode to see if that makes a difference? i.e.: u"^" + ins_name + u"$"

Thank you Julie for your response. I have tried a few tricks like that but none of them worked (I get the same error).
I'll try to ask this on IRC as well as you suggested (thanks for the heads-up on the 'Answers' section).

I realized the mistake I made.
After changing the code to use regular expression I had forgot to update the test case to do the same.
Silly me!